home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / writev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.3 KB  |  216 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.  
  21. #include "plgetopt.h"
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #ifdef XP_MAC
  27. #include "prlog.h"
  28. #define printf PR_LogPrint
  29. #endif
  30.  
  31.  
  32. #ifndef IOV_MAX
  33. #define IOV_MAX 16
  34. #endif
  35.  
  36. #define BASE_PORT 9867
  37.  
  38. int PR_CALLBACK Writev(int argc, char **argv)
  39. {
  40.  
  41.     PRStatus rv;
  42.     PRNetAddr serverAddr;
  43.     PRFileDesc *clientSock, *debug = NULL;
  44.  
  45.     char *buffer = NULL;
  46.     PRIOVec *iov = NULL;
  47.     PRBool passed = PR_TRUE;
  48.     PRIntervalTime timein, elapsed, timeout;
  49.     PRIntervalTime tmo_min = 0x7fffffff, tmo_max = 0, tmo_elapsed = 0;
  50.     PRInt32 tmo_counted = 0, iov_index, loop, bytes, number_fragments;
  51.     PRInt32 message_length = 100, fragment_length = 100, messages = 100;
  52.     struct Descriptor { PRInt32 length; PRUint32 checksum; } descriptor;
  53.  
  54.     /*
  55.      * USAGE
  56.      * -h       dns name of host serving the connection (default = self)
  57.      * -m       number of messages to send              (default = 100)
  58.      * -s       size of each message                    (default = 100)
  59.      * -f       size of each message fragment           (default = 100)
  60.      */
  61.  
  62.     PLOptStatus os;
  63.     PLOptState *opt = PL_CreateOptState(argc, argv, "dh:m:s:f:");
  64.  
  65.     PR_STDIO_INIT();
  66.     rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddr);
  67.     PR_ASSERT(PR_SUCCESS == rv);
  68.  
  69.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  70.     {
  71.         if (PL_OPT_BAD == os) continue;
  72.         switch (opt->option)
  73.         {
  74.         case 'h':  /* the remote host */
  75.             {
  76.                 PRIntn es = 0;
  77.                 PRHostEnt host;
  78.                 char buffer[1024];
  79.                 (void)PR_GetHostByName(opt->value, buffer, sizeof(buffer), &host);
  80.                 es = PR_EnumerateHostEnt(es, &host, BASE_PORT, &serverAddr);
  81.                 PR_ASSERT(es > 0);
  82.             }
  83.             break;
  84.         case 'd':  /* debug mode */
  85.             debug = PR_GetSpecialFD(PR_StandardError);
  86.             break;
  87.         case 'm':  /* number of messages to send */
  88.             messages = atoi(opt->value);
  89.             break;
  90.         case 's':  /* total size of each message */
  91.             message_length = atoi(opt->value);
  92.             break;
  93.         case 'f':  /* size of each message fragment */
  94.             fragment_length = atoi(opt->value);
  95.             break;
  96.         default:
  97.             break;
  98.         }
  99.     }
  100.     PL_DestroyOptState(opt);
  101.  
  102.     buffer = (char*)malloc(message_length);
  103.  
  104.     number_fragments = (message_length + fragment_length - 1) / fragment_length + 1;
  105.     while (IOV_MAX < number_fragments)
  106.     {
  107.         fragment_length = message_length / (IOV_MAX - 2);
  108.         number_fragments = (message_length + fragment_length - 1) /
  109.             fragment_length + 1;
  110.         if (NULL != debug) PR_fprintf(debug, 
  111.             "Too many fragments - reset fragment length to %ld\n", fragment_length);
  112.     }
  113.     iov = (PRIOVec*)malloc(number_fragments * sizeof(PRIOVec));
  114.  
  115.     iov[0].iov_base = (char*)&descriptor;
  116.     iov[0].iov_len = sizeof(descriptor);
  117.     for (iov_index = 1; iov_index < number_fragments; ++iov_index)
  118.     {
  119.         iov[iov_index].iov_base = buffer + (iov_index - 1) * fragment_length;
  120.         iov[iov_index].iov_len = fragment_length;
  121.     }
  122.  
  123.     for (bytes = 0; bytes < message_length; ++bytes)
  124.         buffer[bytes] = (char)bytes;
  125.  
  126.     timeout = PR_SecondsToInterval(1);
  127.  
  128.     for (loop = 0; loop < messages; ++loop)
  129.     {
  130.         if (NULL != debug)
  131.             PR_fprintf(debug, "[%d]socket ... ", loop);
  132.         clientSock = PR_NewTCPSocket();
  133.         if (clientSock)
  134.         {
  135.             timein = PR_IntervalNow();
  136.             if (NULL != debug)
  137.                 PR_fprintf(debug, "connecting ... ");
  138.             rv = PR_Connect(clientSock, &serverAddr, timeout);
  139.             if (PR_SUCCESS == rv)
  140.             {
  141.                 descriptor.checksum = 0;
  142.                 descriptor.length = (loop < (messages - 1)) ? message_length : 0;
  143.                 if (0 == descriptor.length) number_fragments = 1;
  144.                 else
  145.                     for (iov_index = 0; iov_index < descriptor.length; ++iov_index)
  146.                     {
  147.                         PRUint32 overflow = descriptor.checksum & 0x80000000;
  148.                         descriptor.checksum = (descriptor.checksum << 1);
  149.                         if (0x00000000 != overflow) descriptor.checksum += 1;
  150.                         descriptor.checksum += buffer[iov_index];
  151.                     }
  152.                 if (NULL != debug) PR_fprintf(
  153.                     debug, "sending %d bytes ... ", descriptor.length);
  154.  
  155.                 /* then, at the last moment ... */
  156.                 descriptor.length = PR_ntohl(descriptor.length);
  157.                 descriptor.checksum = PR_ntohl(descriptor.checksum);
  158.  
  159.                 bytes = PR_Writev(clientSock, iov, number_fragments, timeout);
  160.                 if (NULL != debug)
  161.                     PR_fprintf(debug, "closing ... ");
  162.                 rv = PR_Shutdown(clientSock, PR_SHUTDOWN_BOTH);
  163.                 rv = PR_Close(clientSock);
  164.                 if (NULL != debug) PR_fprintf(
  165.                     debug, "%s\n", ((PR_SUCCESS == rv) ? "good" : "bad"));
  166.                 elapsed = PR_IntervalNow() - timein;
  167.                 if (elapsed < tmo_min) tmo_min = elapsed;
  168.                 else if (elapsed > tmo_max) tmo_max = elapsed;
  169.                 tmo_elapsed += elapsed;
  170.                 tmo_counted += 1;
  171.             }
  172.             else
  173.             {
  174.                 if (NULL != debug) PR_fprintf(
  175.                     debug, "failed - retrying (%d, %d)\n",
  176.                     PR_GetError(), PR_GetOSError());
  177.                 PR_Close(clientSock);
  178.             }
  179.         }
  180.         else if (NULL != debug)
  181.         {
  182.             PR_fprintf(debug, "unable to create client socket\n");
  183.             passed = PR_FALSE;
  184.         }
  185.     }
  186.     if (NULL != debug) {
  187.         if (0 == tmo_counted) {
  188.             PR_fprintf(debug, "No connection made\n");
  189.         } else {
  190.         PR_fprintf(
  191.             debug, "\nTimings: %d [%d] %d (microseconds)\n",
  192.             PR_IntervalToMicroseconds(tmo_min),
  193.             PR_IntervalToMicroseconds(tmo_elapsed / tmo_counted),
  194.             PR_IntervalToMicroseconds(tmo_max));
  195.     }
  196.     }
  197.  
  198.     PR_DELETE(buffer);
  199.     PR_DELETE(iov);
  200.  
  201.     PR_fprintf(
  202.         PR_GetSpecialFD(PR_StandardError),
  203.         "%s\n", (passed) ? "PASSED" : "FAILED");
  204.     return (passed) ? 0 : 1;
  205. }
  206.  
  207. int main(int argc, char **argv)
  208. {
  209.     return (PR_VersionCheck(PR_VERSION)) ?
  210.         PR_Initialize(Writev, argc, argv, 4) : -1;
  211. }  /* main */
  212.  
  213. /* writev.c */
  214.  
  215.  
  216.