(file) Return to sock.c CVS log (file) (dir) Up to [RizwankCVS] / wine1 / wine

Diff for /wine1/wine/sock.c between version 1.2 and 1.3

version 1.2, 2005/03/15 22:05:53 version 1.3, 2005/03/16 14:11:26
Line 62 
Line 62 
     struct sockaddr_in     peer;     struct sockaddr_in     peer;
     char                  *buf;     char                  *buf;
     int                    nread;     int                    nread;
       int                    nsent;
 } sock_info; } sock_info;
  
 /* Test parameters for both server & client */ /* Test parameters for both server & client */
Line 123 
Line 124 
 typedef struct async_param_t typedef struct async_param_t
 { {
   server_params            *svc_params;   server_params            *svc_params;
   int                      numClients;    server_memory            *mem;
   char                     *fileBuf;    HWND                     hwnd;
 } async_param_t; } async_param_t;
  
 /**************** Static variables ***************/ /**************** Static variables ***************/
Line 263 
Line 264 
         mem->sock[i].s = INVALID_SOCKET;         mem->sock[i].s = INVALID_SOCKET;
         mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );         mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
         mem->sock[i].nread = 0;         mem->sock[i].nread = 0;
           mem->sock[i].nsent = 0;
     }     }
  
     if ( gen->sock_type == SOCK_STREAM )     if ( gen->sock_type == SOCK_STREAM )
Line 284 
Line 286 
     ExitThread ( GetCurrentThreadId () );     ExitThread ( GetCurrentThreadId () );
 } }
  
   /* This function takes in server_memory struct of the running server
      and looks for the first existence of sockToFind socket in sock[] array. */
   static sock_info* GetClientSockInfo( server_memory* servMem, SOCKET sockToFind )
   {
   
     // Looping through all the possible clients
     int clientNum;
     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
   
       // When the match is found, return the pointer to that sock_info struct
       if( servMem->sock[clientNum].s == sockToFind ){
   
         return &(servMem->sock[clientNum]);
   
       }
   
     }
   
     // Did not find a match.
     return NULL;
   
   }
   
   /* This function takes in server_memory structure of the running
      server and finds the first socket that is available */
   static sock_info* GetNextOpenSock( server_memory* servMem )
   {
   
     // Loop through sock array of servMem
     int clientNum;
     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
   
       // when an open slot is found, return
       if( servMem->sock[clientNum].s == INVALID_SOCKET )
         return &(servMem->sock[clientNum]);
   
     }
   
     // When there is no more open socket, return NULL
     return NULL;
   
   }
   
   /* this function returns TRUE if all sockets in servMem are INVALID_SOCKET */
   static BOOL noOpenSock( server_memory* servMem )
   {
   
     // Loop through sock array of servMem
     int clientNum;
     for( clientNum = 0; clientNum < MAX_CLIENTS; clientNum++ ){
   
       // if any socket != INVALID_SOCKET, there is an open socket
       if( servMem->sock[clientNum].s != INVALID_SOCKET )
         return FALSE;
   
     }
   
     // no open sockets
     return TRUE;
   
   }
   
   /* This function takes in pointer to sock_info structure of a client
      and attempts to send all the data that hasn't been sent back yet.  */
   static void try_send_all_buf( sock_info* sockInfo, int servBufLen )
   {
     int n_sent = 0, sendLen, err;
     int id = GetCurrentThreadId();
   
     // send any data that hasn't been sent yet
     while( sockInfo->nsent < sockInfo->nread ){
   
       // find out the buffer length to send
       sendLen = min( sockInfo->nread - sockInfo->nsent, servBufLen );
   
       // send the data
       n_sent = send( sockInfo->s, sockInfo->buf + sockInfo->nsent, sendLen, 0 );
   
       // If there was an error, break
       if( n_sent == SOCKET_ERROR )
         break;
   
       // update the number bytes sent
       sockInfo->nsent += n_sent;
   
     }
   
     // check the error
     if( n_sent == SOCKET_ERROR && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
       ok( 0, "async_server (%x): send error: %d\n", id, err );
   
   }
   
 /**************** Client utilitiy functions ***************/ /**************** Client utilitiy functions ***************/
  
 static void client_start ( client_params *par ) static void client_start ( client_params *par )
Line 386 
Line 481 
 */ */
 static LRESULT CALLBACK async_server_proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) static LRESULT CALLBACK async_server_proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
 { {
   SOCKET socket = (SOCKET) wParam;  
   server_memory* mem = TlsGetValue( tls );    server_memory* mem = async_params.mem;
     SOCKET clientSock = (SOCKET) wParam;
     test_params* gen = (async_params.svc_params)->general;
     char* inetAddr = (char*)gen->inet_addr;
   
     sock_info* sockInfo;
   struct sockaddr_in peer;   struct sockaddr_in peer;
   char *file_buf = async_params.fileBuf;    int addrLen = sizeof( peer );
   char *inetAddr = (char*)((async_params.svc_params)->general)->inet_addr;    int eventCode, err, id = GetCurrentThreadId();
   int n_recvd, n_sent, addrLen, id = GetCurrentThreadId();    int servBufLen = (async_params.svc_params)->buflen;
   int eventCode, packet_len = (async_params.svc_params)->buflen;;    int n_recvd = 0, n_expected;
     char *p;
  
   switch( msg ){    switch( msg )
       {
  
     case ASYNC_EVENT:     case ASYNC_EVENT:
  
       eventCode = WSAGETSELECTEVENT( lParam );       eventCode = WSAGETSELECTEVENT( lParam );
       switch( eventCode ){        switch( eventCode )
           {
  
         case FD_ACCEPT:         case FD_ACCEPT:
  
           // accept connection  
           trace( "async_server (%x): accept event\n", id );           trace( "async_server (%x): accept event\n", id );
           addrLen = sizeof ( peer );  
           socket = accept( mem->s, (struct sockaddr*) &peer, &addrLen );  
           wsa_ok( socket, INVALID_SOCKET !=, "async_server (%lx): accept failed: %d\n" );  
   
           if( socket == INVALID_SOCKET ){  
  
             trace( "async_server (%x): exiting\n", id );            // Find the next available socket
             server_stop();            sockInfo = GetNextOpenSock( mem );
             ok( sockInfo != NULL, "async_server (%x): no more open socket\n", id );
  
           }            // accept connection
             sockInfo->s = accept( mem->s, (struct sockaddr*) &peer, &addrLen );
             wsa_ok( sockInfo->s, INVALID_SOCKET !=, "async_server (%lx): accept failed: %d\n" );
  
           ok( peer.sin_addr.s_addr == inet_addr ( inetAddr ),            // check the address retrieved from accept() is valid
               "async_server (%x): strange peer address\n", id );            ok( peer.sin_addr.s_addr == inet_addr( inetAddr ), "async_server (%x): strange peer address\n", id );
           break;  
  
         case FD_READ:            // make this newly accepted socket asynchronous
             err = WSAAsyncSelect( sockInfo->s, async_params.hwnd, ASYNC_EVENT, FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE );
             wsa_ok( err, SOCKET_ERROR !=, "async_server (%lx): WSAAsyncSelect failed: %d\n" );
  
           // server has data waiting to be read            break;
           n_recvd = recv( socket, file_buf, packet_len, 0 );  
           wsa_ok( n_recvd, SOCKET_ERROR !=, "recv (%lx): error %d:\n" );  
  
           if( n_recvd == SOCKET_ERROR ){          case FD_WRITE:
  
             trace( "async_server (%x) exiting\n", id );            // get the sock_info* of this client socket
             server_stop();            sockInfo = GetClientSockInfo( mem, clientSock );
             ok( sockInfo != NULL, "async_server (%x): socket to write to is not open\n", id );
  
           }            // try sending any remaining data
             try_send_all_buf( sockInfo, servBufLen );
             break;
  
           // Send the packet back          case FD_READ:
           n_sent = send( socket, file_buf, n_recvd, 0 );  
           ok( n_sent == n_recvd,  
                "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_recvd);  
           if( n_sent == SOCKET_ERROR ){  
  
             trace( "async_server (%x) exiting\n", id );            // get the sock_info* of this client socket
             server_stop();            sockInfo = GetClientSockInfo( mem, clientSock );
             ok( sockInfo != NULL, "async_server (%x): socket to read from is not open\n", id );
   
             // retrieve the data
             n_recvd = recv( sockInfo->s, sockInfo->buf + sockInfo->nread, servBufLen, 0 );
             if( n_recvd == SOCKET_ERROR && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
               ok( 0, "async_server (%x): recv error: %d\n", id, err );
   
             // record number of bytes read so far
             if( n_recvd != SOCKET_ERROR )
               sockInfo->nread += n_recvd;
  
           }            // send the remaining data
             try_send_all_buf( sockInfo, servBufLen );
           break;           break;
  
         case FD_CLOSE:         case FD_CLOSE:
  
           // Close the client socket and then close the server            // Client closed connection - close the connection with this server.
           // when all clients are done.  
           trace( "async_server (%x): close event\n", id );           trace( "async_server (%x): close event\n", id );
           wsa_ok( closesocket ( socket ),  0 ==, "async_server (%lx): closesocket error: %d\n" );            sockInfo = GetClientSockInfo( mem, clientSock );
             ok( sockInfo != NULL, "async_server (%x): socket to close is not open\n", id );
   
             // Check the data it received
             n_expected = gen->n_chunks * gen->chunk_size;
             ok( sockInfo->nread == n_expected,
                 "async_server (%x): received less data than expected: %d of %d\n", id, sockInfo->nread, n_expected );
   
             p = test_buffer( sockInfo->buf, gen->chunk_size, gen->n_chunks );
             ok( p == NULL, "async_server (%x): test pattern error: %d\n", id, p - sockInfo->buf);
   
             // clean up
             wsa_ok( closesocket( sockInfo->s ), 0 ==, "async_server (%lx): closesocket error: %d\n" );
             sockInfo->s = INVALID_SOCKET;
  
           if( --async_params.numClients == 0 ){            // If all sockets are closed, kill the server
             if( noOpenSock( mem ) ){
  
             trace( "async_server (%x) exiting\n", id );             trace( "async_server (%x) exiting\n", id );
             server_stop();             server_stop();
Line 469 
Line 591 
  */  */
 static void WINAPI async_server( server_params *par ) static void WINAPI async_server( server_params *par )
 { {
   int id = GetCurrentThreadId(), err;    int id = GetCurrentThreadId();
   test_params* gen = par->general;  
   
   // set up async_params structure  
   memset( &async_params, 0, sizeof( async_params ) );  
   async_params.svc_params = par;  
   async_params.numClients = gen->n_clients;  
   async_params.fileBuf = (LPVOID) LocalAlloc( LPTR, gen->n_chunks * gen->chunk_size );  
  
   trace( "async_server (%x) starting\n", id );   trace( "async_server (%x) starting\n", id );
   set_so_opentype( FALSE ); /* non-overlapped */   set_so_opentype( FALSE ); /* non-overlapped */
   server_start( par );   server_start( par );
   server_memory *mem = TlsGetValue( tls );  
  
   wsa_ok( listen ( mem->s, SOMAXCONN ), 0 ==, "async_server (%lx): listen failed: %d\n");    // set up async_params structure
     memset( &async_params, 0, sizeof( async_params ) );
     async_params.svc_params = par;
     async_params.mem = TlsGetValue( tls );
   
     // Listen to socket
     wsa_ok( listen ( (async_params.mem)->s, SOMAXCONN ), 0 ==, "async_server (%lx): listen failed: %d\n");
  
   // Get the current instance   // Get the current instance
   HINSTANCE instance = GetModuleHandle( NULL );   HINSTANCE instance = GetModuleHandle( NULL );
   if( instance == NULL ){    ok( instance != NULL, "async_server (%x):GetModuleHandle error %ld\n", id, GetLastError() );
   
     trace( "async_server (%x) exiting\n", id );  
     server_stop();  
   
   }  
  
   // Create a hiddent window to handle async events   // Create a hiddent window to handle async events
   WNDCLASS windowClass;   WNDCLASS windowClass;
Line 507 
Line 622 
   windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);   windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   windowClass.lpszMenuName = NULL;   windowClass.lpszMenuName = NULL;
   ATOM classAtom = RegisterClass( &windowClass );   ATOM classAtom = RegisterClass( &windowClass );
   if( classAtom == 0 ){    ok( classAtom != 0, "async_server (%x): RegisterClass error %ld\n", id, GetLastError() );
   
     trace( "async_server (%x) exiting\n", id );  
     server_stop();  
   
   }  
  
   HWND hwnd = CreateWindow( "Hidden_Winsock_Window",   HWND hwnd = CreateWindow( "Hidden_Winsock_Window",
                             "Winsock Window",                             "Winsock Window",
Line 525 
Line 635 
                             NULL,                             NULL,
                             instance,                             instance,
                             NULL );                             NULL );
     ok( hwnd != NULL, "async_server (%x): CreateWindowd error %ld\n", id, GetLastError() );
  
   if( hwnd == NULL ){    /* save hwnd */
     async_params.hwnd = hwnd;
     trace ( "async_server (%x) exiting\n", id );  
     server_stop();  
   
   }  
  
   /* make the socket asynchronous */   /* make the socket asynchronous */
   err = WSAAsyncSelect( mem->s, hwnd, ASYNC_EVENT, FD_ACCEPT | FD_READ | FD_CLOSE );    int err;
     err = WSAAsyncSelect( (async_params.mem)->s, hwnd, ASYNC_EVENT, FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE );
   wsa_ok( err, SOCKET_ERROR !=, "async_server (%lx): WSAAsyncSelect failed: %d\n" );   wsa_ok( err, SOCKET_ERROR !=, "async_server (%lx): WSAAsyncSelect failed: %d\n" );
   if( err == SOCKET_ERROR ){  
   
     trace( "async_server (%x) exiting\n", id );  
     server_stop();  
   
   }  
  
   trace( "async_server (%x) ready\n", id );   trace( "async_server (%x) ready\n", id );
   SetEvent( server_ready ); /* notify clients */   SetEvent( server_ready ); /* notify clients */
Line 551 
Line 653 
   MSG msg;   MSG msg;
   while( ( retVal = GetMessage( &msg, NULL, 0, 0 ) ) != 0 ){   while( ( retVal = GetMessage( &msg, NULL, 0, 0 ) ) != 0 ){
  
       ok( retVal != -1, "async_server (%x): GetMessage error %ld\n", id, GetLastError() );
     if( retVal == -1 ){     if( retVal == -1 ){
  
       // error: exit        // exit so that the server won't block
       trace( "async_server (%x) exiting\n", id );        trace( "async_server (%x): exiting\n", id );
       server_stop();       server_stop();
  
     }     }


Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

Rizwan Kassim
Powered by
ViewCVS 0.9.2