Bugzilla bug #34920: test PR_EmulateSendFile.
authorwtc%netscape.com
Fri, 26 May 2000 22:08:29 +0000
changeset 1391 e837d84a18f4089ada30a6ac0c8d63d3efd07f2b
parent 1390 6983ad01956d794f63398a551891b24eb75f1413
child 1392 a0491d9796f0bae22ab7ce8c91572c2c9bfce883
push idunknown
push userunknown
push dateunknown
bugs34920
Bugzilla bug #34920: test PR_EmulateSendFile.
pr/tests/socket.c
--- a/pr/tests/socket.c
+++ b/pr/tests/socket.c
@@ -127,16 +127,20 @@ static PRInt32 num_tcp_connections_per_c
 static PRInt32 tcp_mesg_size = TCP_MESG_SIZE;
 static PRInt32 num_tcp_mesgs_per_connection = NUM_TCP_MESGS_PER_CONNECTION;
 static PRInt32 num_udp_datagrams_per_client = NUM_UDP_DATAGRAMS_PER_CLIENT;
 static PRInt32 udp_datagram_size = UDP_DGRAM_SIZE;
 
 static PRInt32 thread_count;
 PRUint16 server_domain = PR_AF_INET, client_domain = PR_AF_INET;
 
+/* an I/O layer that uses the emulated senfile method */
+static PRDescIdentity emuSendFileIdentity;
+static PRIOMethods emuSendFileMethods;
+
 int failed_already=0;
 typedef struct buffer {
     char    data[BUF_DATA_SIZE];
 } buffer;
 
 PRNetAddr tcp_server_addr, udp_server_addr;
 
 typedef struct Serve_Client_Param {
@@ -155,16 +159,41 @@ typedef struct Server_Param {
 typedef struct Client_Param {
     PRNetAddr server_addr;
     PRMonitor *exit_mon;    /* monitor to signal on exit */
     PRInt32 *exit_counter;    /* counter to decrement, before exit */
     PRInt32    datalen;
     PRInt32    udp_connect;    /* if set clients connect udp sockets */
 } Client_Param;
 
+/* the sendfile method in emuSendFileMethods */
+static PRInt32 PR_CALLBACK
+emu_SendFile(PRFileDesc *sd, PRSendFileData *sfd,
+    PRTransmitFileFlags flags, PRIntervalTime timeout)
+{
+    return PR_EmulateSendFile(sd, sfd, flags, timeout);
+}
+
+/* the transmitfile method in emuSendFileMethods */
+static PRInt32 PR_CALLBACK
+emu_TransmitFile(PRFileDesc *sd, PRFileDesc *fd, const void *headers,
+    PRInt32 hlen, PRTransmitFileFlags flags, PRIntervalTime timeout)
+{
+    PRSendFileData sfd;
+
+    sfd.fd = fd;
+    sfd.file_offset = 0;
+    sfd.file_nbytes = 0;
+    sfd.header = headers;
+    sfd.hlen = hlen;
+    sfd.trailer = NULL;
+    sfd.tlen = 0;
+    return emu_SendFile(sd, &sfd, flags, timeout);
+}
+
 /*
  * readn
  *    read data from sockfd into buf
  */
 static PRInt32
 readn(PRFileDesc *sockfd, char *buf, int len)
 {
     int rem;
@@ -1757,16 +1786,34 @@ TransmitFile_Server(void *arg)
 
         if ((newsockfd = PR_Accept(sockfd, addrp,
             PR_INTERVAL_NO_TIMEOUT)) == NULL) {
             fprintf(stderr,
                 "prsocket_test: ERROR - PR_Accept failed\n");
             failed_already=1;
             goto exit;
         }
+        /* test both regular and emulated PR_SendFile */
+        if (i%2) {
+            PRFileDesc *layer = PR_CreateIOLayerStub(
+                emuSendFileIdentity, &emuSendFileMethods);
+            if (layer == NULL) {
+                fprintf(stderr,
+                    "prsocket_test: ERROR - PR_CreateIOLayerStub failed\n");
+                failed_already=1;
+                goto exit;
+            }
+            if (PR_PushIOLayer(newsockfd, PR_TOP_IO_LAYER, layer)
+                    == PR_FAILURE) {
+                fprintf(stderr,
+                    "prsocket_test: ERROR - PR_PushIOLayer failed\n");
+                failed_already=1;
+                goto exit;
+            }
+        }
         scp = PR_NEW(Serve_Client_Param);
         if (scp == NULL) {
             fprintf(stderr,"prsocket_test: PR_NEW failed\n");
             failed_already=1;
             goto exit;
         }
 
         /*
@@ -2156,16 +2203,22 @@ main(int argc, char **argv)
 
     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
     PR_STDIO_INIT();
 
 #ifdef XP_MAC
     SetupMacPrintfLog("socket.log");
 #endif
     PR_SetConcurrency(4);
+
+    emuSendFileIdentity = PR_GetUniqueIdentity("Emulated SendFile");
+    emuSendFileMethods = *PR_GetDefaultIOMethods();
+    emuSendFileMethods.transmitfile = emu_TransmitFile;
+    emuSendFileMethods.sendfile = emu_SendFile;
+
     /*
      * run client-server test with TCP, Ipv4-Ipv4
      */
 	printf("TCP Client/Server Test - IPv4/Ipv4\n");
     if (TCP_Socket_Client_Server_Test() < 0) {
         printf("TCP_Socket_Client_Server_Test failed\n");
         goto done;
     } else