nsprpub/pr/tests/acceptreademu.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/acceptreademu.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,274 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * This test is the same as acceptread.c except that it uses the
    1.11 + * emulated acceptread method instead of the regular acceptread.
    1.12 + */
    1.13 +
    1.14 +#include <prio.h>
    1.15 +#include <prprf.h>
    1.16 +#include <prinit.h>
    1.17 +#include <prnetdb.h>
    1.18 +#include <prinrval.h>
    1.19 +#include <prthread.h>
    1.20 +#include <pprio.h>
    1.21 +
    1.22 +#include <plerror.h>
    1.23 +
    1.24 +#include <stdlib.h>
    1.25 +
    1.26 +#define DEFAULT_PORT 12273
    1.27 +#define GET "GET / HTTP/1.0\n\n"
    1.28 +static PRFileDesc *std_out, *err_out;
    1.29 +static PRIntervalTime write_dally, accept_timeout;
    1.30 +static PRDescIdentity emu_layer_ident;
    1.31 +static PRIOMethods emu_layer_methods;
    1.32 +
    1.33 +/* the acceptread method in emu_layer_methods */
    1.34 +static PRInt32 PR_CALLBACK emu_AcceptRead(PRFileDesc *sd, PRFileDesc **nd,
    1.35 +    PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout)
    1.36 +{
    1.37 +    return PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout);
    1.38 +}
    1.39 +
    1.40 +static PRStatus PrintAddress(const PRNetAddr* address)
    1.41 +{
    1.42 +    char buffer[100];
    1.43 +    PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
    1.44 +    if (PR_FAILURE == rv) PL_FPrintError(err_out, "PR_NetAddrToString");
    1.45 +    else PR_fprintf(
    1.46 +        std_out, "Accepted connection from (0x%p)%s:%d\n",
    1.47 +        address, buffer, address->inet.port);
    1.48 +    return rv;
    1.49 +}  /* PrintAddress */
    1.50 +
    1.51 +static void ConnectingThread(void *arg)
    1.52 +{
    1.53 +    PRInt32 nbytes;
    1.54 +#ifdef SYMBIAN
    1.55 +    char buf[256];
    1.56 +#else
    1.57 +    char buf[1024];
    1.58 +#endif
    1.59 +    PRFileDesc *sock;
    1.60 +    PRNetAddr peer_addr, *addr;
    1.61 +
    1.62 +    addr = (PRNetAddr*)arg;
    1.63 +
    1.64 +    sock = PR_NewTCPSocket();
    1.65 +    if (sock == NULL)
    1.66 +    {
    1.67 +        PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
    1.68 +        PR_ProcessExit(1);
    1.69 +    }
    1.70 +
    1.71 +    if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
    1.72 +    {
    1.73 +        PL_FPrintError(err_out, "PR_Connect (client) failed");
    1.74 +        PR_ProcessExit(1);
    1.75 +    }
    1.76 +    if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
    1.77 +    {
    1.78 +        PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
    1.79 +        PR_ProcessExit(1);
    1.80 +    }
    1.81 +
    1.82 +    /*
    1.83 +    ** Then wait between the connection coming up and sending the expected
    1.84 +    ** data. At some point in time, the server should fail due to a timeou
    1.85 +    ** on the AcceptRead() operation, which according to the document is
    1.86 +    ** only due to the read() portion.
    1.87 +    */
    1.88 +    PR_Sleep(write_dally);
    1.89 +
    1.90 +    nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
    1.91 +    if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");
    1.92 +
    1.93 +    nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
    1.94 +    if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
    1.95 +    else
    1.96 +    {
    1.97 +        PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
    1.98 +        buf[sizeof(buf) - 1] = '\0';
    1.99 +        PR_fprintf(std_out, "%s\n", buf);
   1.100 +    }
   1.101 +
   1.102 +    if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
   1.103 +        PL_FPrintError(err_out, "PR_Shutdown (client) failed");
   1.104 +
   1.105 +    if (PR_FAILURE == PR_Close(sock))
   1.106 +        PL_FPrintError(err_out, "PR_Close (client) failed");
   1.107 +
   1.108 +    return;
   1.109 +}  /* ConnectingThread */
   1.110 +
   1.111 +#define BUF_SIZE 117
   1.112 +static void AcceptingThread(void *arg)
   1.113 +{
   1.114 +    PRStatus rv;
   1.115 +    PRInt32 bytes;
   1.116 +    PRSize buf_size = BUF_SIZE;
   1.117 +    PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
   1.118 +    PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
   1.119 +    PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
   1.120 +    PRFileDesc *layer;
   1.121 +    PRSocketOptionData sock_opt;
   1.122 +
   1.123 +    if (NULL == listen_sock)
   1.124 +    {
   1.125 +        PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
   1.126 +        PR_ProcessExit(1);        
   1.127 +    }
   1.128 +    layer = PR_CreateIOLayerStub(emu_layer_ident, &emu_layer_methods);
   1.129 +    if (NULL == layer)
   1.130 +    {
   1.131 +        PL_FPrintError(err_out, "PR_CreateIOLayerStub (server) failed");
   1.132 +        PR_ProcessExit(1);        
   1.133 +    }
   1.134 +    if (PR_PushIOLayer(listen_sock, PR_TOP_IO_LAYER, layer) == PR_FAILURE)
   1.135 +    {
   1.136 +        PL_FPrintError(err_out, "PR_PushIOLayer (server) failed");
   1.137 +        PR_ProcessExit(1);        
   1.138 +    }
   1.139 +    sock_opt.option = PR_SockOpt_Reuseaddr;
   1.140 +    sock_opt.value.reuse_addr = PR_TRUE;
   1.141 +    rv = PR_SetSocketOption(listen_sock, &sock_opt);
   1.142 +    if (PR_FAILURE == rv)
   1.143 +    {
   1.144 +        PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
   1.145 +        PR_ProcessExit(1);        
   1.146 +    }
   1.147 +    rv = PR_Bind(listen_sock, listen_addr);
   1.148 +    if (PR_FAILURE == rv)
   1.149 +    {
   1.150 +        PL_FPrintError(err_out, "PR_Bind (server) failed");
   1.151 +        PR_ProcessExit(1);        
   1.152 +    }
   1.153 +    rv = PR_Listen(listen_sock, 10);
   1.154 +    if (PR_FAILURE == rv)
   1.155 +    {
   1.156 +        PL_FPrintError(err_out, "PR_Listen (server) failed");
   1.157 +        PR_ProcessExit(1);        
   1.158 +    }
   1.159 +    bytes = PR_AcceptRead(
   1.160 +        listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);
   1.161 +
   1.162 +    if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
   1.163 +    else
   1.164 +    {
   1.165 +        PrintAddress(accept_addr);
   1.166 +        PR_fprintf(
   1.167 +            std_out, "(Server) read [0x%p..0x%p) %s\n",
   1.168 +            buf, &buf[BUF_SIZE], buf);
   1.169 +        bytes = PR_Write(accept_sock, buf, bytes);
   1.170 +        rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
   1.171 +        if (PR_FAILURE == rv)
   1.172 +            PL_FPrintError(err_out, "PR_Shutdown (server) failed");
   1.173 +    }
   1.174 +
   1.175 +    if (-1 != bytes)
   1.176 +    {
   1.177 +        rv = PR_Close(accept_sock);
   1.178 +        if (PR_FAILURE == rv)
   1.179 +            PL_FPrintError(err_out, "PR_Close (server) failed");
   1.180 +    }
   1.181 +
   1.182 +    rv = PR_Close(listen_sock);
   1.183 +    if (PR_FAILURE == rv)
   1.184 +        PL_FPrintError(err_out, "PR_Close (server) failed");
   1.185 +}  /* AcceptingThread */
   1.186 +
   1.187 +int main(int argc, char **argv)
   1.188 +{
   1.189 +    PRHostEnt he;
   1.190 +    PRStatus status;
   1.191 +    PRIntn next_index;
   1.192 +    PRUint16 port_number;
   1.193 +    char netdb_buf[PR_NETDB_BUF_SIZE];
   1.194 +    PRNetAddr client_addr, server_addr;
   1.195 +    PRThread *client_thread, *server_thread;
   1.196 +    PRIntervalTime delta = PR_MillisecondsToInterval(500);
   1.197 +
   1.198 +    err_out = PR_STDERR;
   1.199 +    std_out = PR_STDOUT;
   1.200 +    accept_timeout = PR_SecondsToInterval(2);
   1.201 +    emu_layer_ident = PR_GetUniqueIdentity("Emulated AcceptRead");
   1.202 +    emu_layer_methods = *PR_GetDefaultIOMethods();
   1.203 +    emu_layer_methods.acceptread = emu_AcceptRead;
   1.204 +
   1.205 +    if (argc != 2 && argc != 3) port_number = DEFAULT_PORT;
   1.206 +    else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
   1.207 +
   1.208 +    status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
   1.209 +    if (PR_SUCCESS != status)
   1.210 +    {
   1.211 +        PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
   1.212 +        PR_ProcessExit(1);
   1.213 +    }
   1.214 +    if (argc < 3)
   1.215 +    {
   1.216 +        status = PR_InitializeNetAddr(
   1.217 +            PR_IpAddrLoopback, port_number, &client_addr);
   1.218 +        if (PR_SUCCESS != status)
   1.219 +        {
   1.220 +            PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
   1.221 +            PR_ProcessExit(1);
   1.222 +        }
   1.223 +    }
   1.224 +    else
   1.225 +    {
   1.226 +        status = PR_GetHostByName(
   1.227 +            argv[1], netdb_buf, sizeof(netdb_buf), &he);
   1.228 +        if (status == PR_FAILURE)
   1.229 +        {
   1.230 +            PL_FPrintError(err_out, "PR_GetHostByName failed");
   1.231 +            PR_ProcessExit(1);
   1.232 +        }
   1.233 +        next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
   1.234 +        if (next_index == -1)
   1.235 +        {
   1.236 +            PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
   1.237 +            PR_ProcessExit(1);
   1.238 +        }
   1.239 +    }
   1.240 +
   1.241 +    for (
   1.242 +        write_dally = 0;
   1.243 +        write_dally < accept_timeout + (2 * delta);
   1.244 +        write_dally += delta)
   1.245 +    {
   1.246 +        PR_fprintf(
   1.247 +            std_out, "Testing w/ write_dally = %d msec\n",
   1.248 +            PR_IntervalToMilliseconds(write_dally));
   1.249 +        server_thread = PR_CreateThread(
   1.250 +            PR_USER_THREAD, AcceptingThread, &server_addr,
   1.251 +            PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
   1.252 +        if (server_thread == NULL)
   1.253 +        {
   1.254 +            PL_FPrintError(err_out, "PR_CreateThread (server) failed");
   1.255 +            PR_ProcessExit(1);
   1.256 +        }
   1.257 +
   1.258 +        PR_Sleep(delta);  /* let the server pot thicken */
   1.259 +
   1.260 +        client_thread = PR_CreateThread(
   1.261 +            PR_USER_THREAD, ConnectingThread, &client_addr,
   1.262 +            PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
   1.263 +        if (client_thread == NULL)
   1.264 +        {
   1.265 +            PL_FPrintError(err_out, "PR_CreateThread (client) failed");
   1.266 +            PR_ProcessExit(1);
   1.267 +        }
   1.268 +
   1.269 +        if (PR_JoinThread(client_thread) == PR_FAILURE)
   1.270 +            PL_FPrintError(err_out, "PR_JoinThread (client) failed");
   1.271 +
   1.272 +        if (PR_JoinThread(server_thread) == PR_FAILURE)
   1.273 +            PL_FPrintError(err_out, "PR_JoinThread (server) failed");
   1.274 +    }
   1.275 +
   1.276 +    return 0;
   1.277 +}

mercurial