home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / nonblock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.0 KB  |  249 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "nspr.h"
  20. #include "prio.h"
  21. #include "prerror.h"
  22. #include "prlog.h"
  23. #include "prprf.h"
  24. #include "prnetdb.h"
  25. #include "plerror.h"
  26. #ifndef XP_MAC
  27. #include "obsolete/probslet.h"
  28. #else
  29. #include "probslet.h"
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35.  
  36. #define NUMBER_ROUNDS 5
  37.  
  38. #if defined(WIN16)
  39. /*
  40. ** Make win16 unit_time interval 300 milliseconds, others get 100
  41. */
  42. #define UNIT_TIME  200       /* unit time in milliseconds */
  43. #else
  44. #define UNIT_TIME  100       /* unit time in milliseconds */
  45. #endif
  46. #define CHUNK_SIZE 10
  47. #undef USE_PR_SELECT         /* If defined, we use PR_Select.
  48.                               * If not defined, use PR_Poll instead. */
  49.  
  50. #if defined(USE_PR_SELECT)
  51. #include "pprio.h"
  52. #endif
  53.  
  54. #ifdef XP_MAC
  55. int fprintf(FILE *stream, const char *fmt, ...)
  56. {
  57. PR_LogPrint(fmt);
  58. return 0;
  59. }
  60. #define printf PR_LogPrint
  61. extern void SetupMacPrintfLog(char *logFile);
  62. #endif
  63.  
  64. static void PR_CALLBACK
  65. clientThreadFunc(void *arg)
  66. {
  67.     PRUintn port = (PRUintn)arg;
  68.     PRFileDesc *sock;
  69.     PRNetAddr addr;
  70.     char buf[CHUNK_SIZE];
  71.     int i;
  72.     PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME);
  73.     PRIntn optval = 1;
  74.     PRStatus retVal;
  75.     PRInt32 nBytes;
  76.  
  77.     addr.inet.family = AF_INET;
  78.     addr.inet.port = PR_htons((PRUint16)port);
  79.     addr.inet.ip = PR_htonl(INADDR_LOOPBACK);
  80.     PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.ip);
  81.  
  82.     /* time 1 */
  83.     PR_Sleep(unitTime);
  84.     sock = PR_NewTCPSocket();
  85.     PR_SetSockOpt(sock, PR_SockOpt_Nonblocking, &optval, sizeof(optval));
  86.     retVal = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
  87.     if (retVal == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) {
  88. #if !defined(USE_PR_SELECT)
  89.     PRPollDesc pd;
  90.     PRInt32 n;
  91.     fprintf(stderr, "connect: EWOULDBLOCK, good\n");
  92.     pd.fd = sock;
  93.     pd.in_flags = PR_POLL_WRITE;
  94.     n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
  95.     PR_ASSERT(n == 1);
  96.         PR_ASSERT(pd.out_flags == PR_POLL_WRITE);
  97. #else
  98.         PR_fd_set writeSet;
  99.         PRInt32 n;
  100.         fprintf(stderr, "connect: EWOULDBLOCK, good\n");
  101.         PR_FD_ZERO(&writeSet);
  102.         PR_FD_SET(sock, &writeSet);
  103.         n = PR_Select(0, NULL, &writeSet, NULL, PR_INTERVAL_NO_TIMEOUT);
  104.         PR_ASSERT(n == 1);
  105.         PR_ASSERT(PR_FD_ISSET(sock, &writeSet));
  106. #endif
  107.     }
  108.     printf("client connected\n");
  109.     fflush(stdout);
  110.  
  111.     /* time 4, 7, 11, etc. */
  112.     for (i = 0; i < NUMBER_ROUNDS; i++) {
  113.         PR_Sleep(3 * unitTime);
  114.         nBytes = PR_Write(sock, buf, sizeof(buf));
  115.         if (nBytes == -1) {
  116.         if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  117.         fprintf(stderr, "write: EWOULDBLOCK\n");
  118.         exit(1);
  119.             } else {
  120.         fprintf(stderr, "write: failed\n");
  121.             }
  122.     }
  123.     printf("client sent %d bytes\n", nBytes);
  124.     fflush(stdout);
  125.     }
  126.  
  127.     PR_Close(sock);
  128. }
  129.  
  130. static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
  131. {
  132.     PRFileDesc *listenSock, *sock;
  133.     PRUint16 listenPort;
  134.     PRNetAddr addr;
  135.     char buf[CHUNK_SIZE];
  136.     PRThread *clientThread;
  137.     PRInt32 retVal;
  138.     PRIntn optval = 1;
  139.     PRIntn i;
  140.     PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME);
  141.  
  142. #ifdef XP_MAC
  143.     SetupMacPrintfLog("nonblock.log");
  144. #endif
  145.  
  146.     /* Create a listening socket */
  147.     if ((listenSock = PR_NewTCPSocket()) == NULL) {
  148.     fprintf(stderr, "Can't create a new TCP socket\n");
  149.     exit(1);
  150.     }
  151.     addr.inet.family = AF_INET;
  152.     addr.inet.ip = PR_htonl(INADDR_ANY);
  153.     addr.inet.port = PR_htons(0);
  154.     if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
  155.     fprintf(stderr, "Can't bind socket\n");
  156.     exit(1);
  157.     }
  158.     if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
  159.     fprintf(stderr, "PR_GetSockName failed\n");
  160.     exit(1);
  161.     }
  162.     listenPort = PR_ntohs(addr.inet.port);
  163.     if (PR_Listen(listenSock, 5) == PR_FAILURE) {
  164.     fprintf(stderr, "Can't listen on a socket\n");
  165.     exit(1);
  166.     }
  167.  
  168.     PR_snprintf(buf, sizeof(buf),
  169.         "The server thread is listening on port %hu\n\n",
  170.         listenPort);
  171.     printf("%s", buf);
  172.  
  173.     clientThread = PR_CreateThread(PR_USER_THREAD,
  174.         clientThreadFunc, (void *) listenPort,
  175.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  176.         PR_UNJOINABLE_THREAD, 0);
  177.     if (clientThread == NULL) {
  178.     fprintf(stderr, "can't create thread\n");
  179.     exit(1);
  180.     }
  181.  
  182.     printf("client thread created.\n");
  183.  
  184.     PR_SetSockOpt(listenSock, PR_SockOpt_Nonblocking, &optval,
  185.     sizeof(PRIntn));
  186.     /* time 0 */
  187.     sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
  188.     if (sock != NULL || PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  189.         PL_PrintError("First Accept\n");
  190.         fprintf(stderr, "First PR_Accept() xxx\n" );
  191.             exit(1);
  192.     }
  193.     printf("accept: EWOULDBLOCK, good\n");
  194.     fflush(stdout);
  195.     /* time 2 */
  196.     PR_Sleep(2 * unitTime);
  197.     sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
  198.     if (sock == NULL) {
  199.         PL_PrintError("Second Accept\n");
  200.         fprintf(stderr, "Second PR_Accept() failed: (%d, %d)\n",
  201.                 PR_GetError(), PR_GetOSError());
  202.             exit(1);
  203.     }
  204.     printf("accept: succeeded, good\n");
  205.     fflush(stdout);
  206.     PR_Close(listenSock);
  207.  
  208.     PR_SetSockOpt(sock, PR_SockOpt_Nonblocking, &optval, sizeof(PRIntn));
  209.  
  210.     /* time 3, 5, 6, 8, etc. */
  211.     for (i = 0; i < NUMBER_ROUNDS; i++) {
  212.     PR_Sleep(unitTime);
  213.     retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
  214.     if (retVal != -1 || PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  215.         PL_PrintError("First Receive:\n");
  216.         fprintf(stderr, "First PR_Recv: retVal: %ld, Error: %ld\n",
  217.             retVal, PR_GetError());
  218.         exit(1);
  219.         }
  220.     printf("read: EWOULDBLOCK, good\n");
  221.     fflush(stdout);
  222.     PR_Sleep(2 * unitTime);
  223.     retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
  224.     if (retVal != CHUNK_SIZE) {
  225.         PL_PrintError("Second Receive:\n");
  226.         fprintf(stderr, "Second PR_Recv: retVal: %ld, Error: %ld\n", 
  227.             retVal, PR_GetError());
  228.         exit(1);
  229.         }
  230.     printf("read: %d bytes, good\n", retVal);
  231.     fflush(stdout);
  232.     }
  233.     PR_Close(sock);
  234.  
  235.     printf("All tests finished\n");
  236.     printf("PASS\n");
  237.     return 0;
  238. }
  239.  
  240. PRIntn main(PRIntn argc, char *argv[])
  241. {
  242.     PRIntn rv;
  243.     
  244.     PR_STDIO_INIT();
  245.     rv = PR_Initialize(RealMain, argc, argv, 0);
  246.     return rv;
  247. }  /* main */
  248.  
  249.