nsprpub/pr/tests/acceptread.c

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

mercurial