int MySocket::Recv(int s, void *buf, int bytes, int seconds, int useconds, int flags) // Receive a block of data from a specified socket and do not return // until all the bytes have been read or the timeout value has been // exceeded. Returns the total number of bytes received or -1 if an // error occurs. { bytes_read = 0; // Reset the byte counter int num_read = 0; // Actual number of bytes read int num_req = (int)bytes; // Number of bytes requested char *p = (char *)buf; // Pointer to the buffer while(bytes_read < bytes) { // Loop until the buffer is full if(!ReadSelect(s, seconds, useconds)) { socket_error = MySOCKET_REQUEST_TIMEOUT; return -1; // Exceeded the timeout value } if((num_read = recv(s, p, num_req-bytes_read, flags)) > 0) { bytes_read += num_read; // Increment the byte counter p += num_read; // Move the buffer pointer for the next read } if(num_read < 0) { socket_error = MySOCKET_RECEIVE_ERROR; return -1; // An error occurred during the read } } return bytes_read; }
int MySocket::RecvFrom(int s, sockaddr_in *sa, void *buf, int bytes, int seconds, int useconds, int flags) // Receive a block of data from a remote datagram socket // and do not return until all the bytes have been read // or the timeout value has been exceeded. Returns the total // number of bytes received or -1 if an error occurs. { // Length of client address int addr_size = (int)sizeof(sockaddr_in); bytes_read = 0; // Reset the byte counter int num_read = 0; // Actual number of bytes read int num_req = (int)bytes; // Number of bytes requested char *p = (char *)buf; // Pointer to the buffer while(bytes_read < bytes) { // Loop until the buffer is full if(!ReadSelect(s, seconds, useconds)) { socket_error = MySOCKET_REQUEST_TIMEOUT; return -1; // Exceeded the timeout value } if((num_read = recvfrom(s, p, num_req-bytes_read, flags, (struct sockaddr *)sa, &addr_size)) > 0) { bytes_read += num_read; // Increment the byte counter p += num_read; // Move the buffer pointer for the next read } if(num_read < 0) { socket_error = MySOCKET_RECEIVE_ERROR; return -1; // An error occurred during the read } } return bytes_read; }
FdArr_t CFdSelector_F::ReadSelect(U32* pdwMilliSeconds) { struct timeval time; time.tv_sec = *pdwMilliSeconds / 1000; time.tv_usec = ( *pdwMilliSeconds % 1000 ) * 1000; FdArr_t fdArrReturn = ReadSelect( &time ); *pdwMilliSeconds = (time.tv_sec * 1000) + (time.tv_usec / 1000); return fdArrReturn; }
int gxsSMTPClient::WaitForReply() // Returns false if a reply time is longer then the timeout values. { return ReadSelect(gxsocket, time_out_sec, time_out_usec); }