Rev 136 | Rev 154 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 136 | Rev 153 | ||
---|---|---|---|
Line 798... | Line 798... | ||
798 | va_end (argptr); |
798 | va_end (argptr); |
799 | fclose (fp); // flush buffers and close it |
799 | fclose (fp); // flush buffers and close it |
800 | } |
800 | } |
801 | 801 | ||
802 | return; // finished |
802 | return; // finished |
- | 803 | } |
|
- | 804 | ||
- | 805 | ||
- | 806 | int RecvWithTimeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags) |
|
- | 807 | { |
|
- | 808 | // variant of recv() that honors a specific timeout in seconds |
|
- | 809 | ||
- | 810 | unsigned long nonblocking_mode; |
|
- | 811 | unsigned long msec_start; |
|
- | 812 | float timediff; |
|
- | 813 | int total_size; |
|
- | 814 | int recv_size; |
|
- | 815 | ||
- | 816 | // make socket non blocking |
|
- | 817 | nonblocking_mode = 1; |
|
- | 818 | ioctlsocket (socket_id, FIONBIO, &nonblocking_mode); |
|
- | 819 | ||
- | 820 | // loop endlessly, noting the time at which we start |
|
- | 821 | msec_start = GetTickCount (); |
|
- | 822 | total_size = 0; |
|
- | 823 | for (;;) |
|
- | 824 | { |
|
- | 825 | // see how much time elapsed since the last time we received data |
|
- | 826 | timediff = (GetTickCount () - msec_start) / 1000.0f; |
|
- | 827 | if (timediff > timeout_in_seconds) |
|
- | 828 | break; // if we've waited long enough, give up |
|
- | 829 | ||
- | 830 | // see if we have something to read from the socket |
|
- | 831 | recv_size = recv (socket_id, &outbuf[total_size], outbuf_size - total_size, flags); |
|
- | 832 | if (recv_size == 0) |
|
- | 833 | break; // on TCP disconnection, give up too |
|
- | 834 | else if (recv_size < 0) |
|
- | 835 | { |
|
- | 836 | Sleep (100); // if nothing was received then we want to wait a little before trying again, 0.1 seconds |
|
- | 837 | continue; |
|
- | 838 | } |
|
- | 839 | ||
- | 840 | total_size += recv_size; // increase the received bytes count |
|
- | 841 | outbuf[total_size] = 0; // terminate outbuf ourselves |
|
- | 842 | if (total_size == outbuf_size) |
|
- | 843 | break; // if the output buffer is full, give up |
|
- | 844 | msec_start = GetTickCount (); // and remember when we last received data (i.e. now) |
|
- | 845 | } |
|
- | 846 | ||
- | 847 | return (total_size); // and return the number of bytes received |
|
803 | } |
848 | } |
804 | 849 | ||
805 | 850 | ||
806 | const wchar_t *GetLastNetworkError (void) |
851 | const wchar_t *GetLastNetworkError (void) |
807 | { |
852 | { |