michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #define DEFAULT_PORT 12273 michael@0: #define GET "GET / HTTP/1.0\n\n" michael@0: static PRFileDesc *std_out, *err_out; michael@0: static PRIntervalTime write_dally, accept_timeout; michael@0: michael@0: static PRStatus PrintAddress(const PRNetAddr* address) michael@0: { michael@0: char buffer[100]; michael@0: PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer)); michael@0: if (PR_FAILURE == rv) PL_FPrintError(err_out, "PR_NetAddrToString"); michael@0: else PR_fprintf( michael@0: std_out, "Accepted connection from (0x%p)%s:%d\n", michael@0: address, buffer, address->inet.port); michael@0: return rv; michael@0: } /* PrintAddress */ michael@0: michael@0: static void ConnectingThread(void *arg) michael@0: { michael@0: PRInt32 nbytes; michael@0: #ifdef SYMBIAN michael@0: char buf[256]; michael@0: #else michael@0: char buf[1024]; michael@0: #endif michael@0: PRFileDesc *sock; michael@0: PRNetAddr peer_addr, *addr; michael@0: michael@0: addr = (PRNetAddr*)arg; michael@0: michael@0: sock = PR_NewTCPSocket(); michael@0: if (sock == NULL) michael@0: { michael@0: PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: michael@0: if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) michael@0: { michael@0: PL_FPrintError(err_out, "PR_Connect (client) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE) michael@0: { michael@0: PL_FPrintError(err_out, "PR_GetPeerName (client) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: michael@0: /* michael@0: ** Then wait between the connection coming up and sending the expected michael@0: ** data. At some point in time, the server should fail due to a timeou michael@0: ** on the AcceptRead() operation, which according to the document is michael@0: ** only due to the read() portion. michael@0: */ michael@0: PR_Sleep(write_dally); michael@0: michael@0: nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT); michael@0: if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed"); michael@0: michael@0: nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); michael@0: if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed"); michael@0: else michael@0: { michael@0: PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes); michael@0: buf[sizeof(buf) - 1] = '\0'; michael@0: PR_fprintf(std_out, "%s\n", buf); michael@0: } michael@0: michael@0: if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH)) michael@0: PL_FPrintError(err_out, "PR_Shutdown (client) failed"); michael@0: michael@0: if (PR_FAILURE == PR_Close(sock)) michael@0: PL_FPrintError(err_out, "PR_Close (client) failed"); michael@0: michael@0: return; michael@0: } /* ConnectingThread */ michael@0: michael@0: #define BUF_SIZE 117 michael@0: static void AcceptingThread(void *arg) michael@0: { michael@0: PRStatus rv; michael@0: PRInt32 bytes; michael@0: PRSize buf_size = BUF_SIZE; michael@0: PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32]; michael@0: PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg; michael@0: PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket(); michael@0: PRSocketOptionData sock_opt; michael@0: michael@0: if (NULL == listen_sock) michael@0: { michael@0: PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: sock_opt.option = PR_SockOpt_Reuseaddr; michael@0: sock_opt.value.reuse_addr = PR_TRUE; michael@0: rv = PR_SetSocketOption(listen_sock, &sock_opt); michael@0: if (PR_FAILURE == rv) michael@0: { michael@0: PL_FPrintError(err_out, "PR_SetSocketOption (server) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: rv = PR_Bind(listen_sock, listen_addr); michael@0: if (PR_FAILURE == rv) michael@0: { michael@0: PL_FPrintError(err_out, "PR_Bind (server) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: rv = PR_Listen(listen_sock, 10); michael@0: if (PR_FAILURE == rv) michael@0: { michael@0: PL_FPrintError(err_out, "PR_Listen (server) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: bytes = PR_AcceptRead( michael@0: listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout); michael@0: michael@0: if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed"); michael@0: else michael@0: { michael@0: PrintAddress(accept_addr); michael@0: PR_fprintf( michael@0: std_out, "(Server) read [0x%p..0x%p) %s\n", michael@0: buf, &buf[BUF_SIZE], buf); michael@0: bytes = PR_Write(accept_sock, buf, bytes); michael@0: rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH); michael@0: if (PR_FAILURE == rv) michael@0: PL_FPrintError(err_out, "PR_Shutdown (server) failed"); michael@0: } michael@0: michael@0: if (-1 != bytes) michael@0: { michael@0: rv = PR_Close(accept_sock); michael@0: if (PR_FAILURE == rv) michael@0: PL_FPrintError(err_out, "PR_Close (server) failed"); michael@0: } michael@0: michael@0: rv = PR_Close(listen_sock); michael@0: if (PR_FAILURE == rv) michael@0: PL_FPrintError(err_out, "PR_Close (server) failed"); michael@0: } /* AcceptingThread */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRHostEnt he; michael@0: PRStatus status; michael@0: PRIntn next_index; michael@0: PRUint16 port_number; michael@0: char netdb_buf[PR_NETDB_BUF_SIZE]; michael@0: PRNetAddr client_addr, server_addr; michael@0: PRThread *client_thread, *server_thread; michael@0: PRIntervalTime delta = PR_MillisecondsToInterval(500); michael@0: michael@0: err_out = PR_STDERR; michael@0: std_out = PR_STDOUT; michael@0: accept_timeout = PR_SecondsToInterval(2); michael@0: michael@0: if (argc != 2 && argc != 3) port_number = DEFAULT_PORT; michael@0: else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]); michael@0: michael@0: status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr); michael@0: if (PR_SUCCESS != status) michael@0: { michael@0: PL_FPrintError(err_out, "PR_InitializeNetAddr failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: if (argc < 3) michael@0: { michael@0: status = PR_InitializeNetAddr( michael@0: PR_IpAddrLoopback, port_number, &client_addr); michael@0: if (PR_SUCCESS != status) michael@0: { michael@0: PL_FPrintError(err_out, "PR_InitializeNetAddr failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: status = PR_GetHostByName( michael@0: argv[1], netdb_buf, sizeof(netdb_buf), &he); michael@0: if (status == PR_FAILURE) michael@0: { michael@0: PL_FPrintError(err_out, "PR_GetHostByName failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr); michael@0: if (next_index == -1) michael@0: { michael@0: PL_FPrintError(err_out, "PR_EnumerateHostEnt failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: michael@0: for ( michael@0: write_dally = 0; michael@0: write_dally < accept_timeout + (2 * delta); michael@0: write_dally += delta) michael@0: { michael@0: PR_fprintf( michael@0: std_out, "Testing w/ write_dally = %d msec\n", michael@0: PR_IntervalToMilliseconds(write_dally)); michael@0: server_thread = PR_CreateThread( michael@0: PR_USER_THREAD, AcceptingThread, &server_addr, michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (server_thread == NULL) michael@0: { michael@0: PL_FPrintError(err_out, "PR_CreateThread (server) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: michael@0: PR_Sleep(delta); /* let the server pot thicken */ michael@0: michael@0: client_thread = PR_CreateThread( michael@0: PR_USER_THREAD, ConnectingThread, &client_addr, michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (client_thread == NULL) michael@0: { michael@0: PL_FPrintError(err_out, "PR_CreateThread (client) failed"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: michael@0: if (PR_JoinThread(client_thread) == PR_FAILURE) michael@0: PL_FPrintError(err_out, "PR_JoinThread (client) failed"); michael@0: michael@0: if (PR_JoinThread(server_thread) == PR_FAILURE) michael@0: PL_FPrintError(err_out, "PR_JoinThread (server) failed"); michael@0: } michael@0: michael@0: return 0; michael@0: }