| 88 | } |
| 89 | |
| 90 | ReadResult read(SOCKET socket, void *data, u32 size) |
| 91 | { |
| 92 | CE_ENSURE(socket != INVALID_SOCKET); |
| 93 | |
| 94 | ReadResult rr; |
| 95 | rr.error = ReadResult::SUCCESS; |
| 96 | rr.bytes_read = 0; |
| 97 | |
| 98 | u32 to_read = size; |
| 99 | |
| 100 | while (to_read > 0) { |
| 101 | int bytes_read = ::recv(socket |
| 102 | , (char *)data + rr.bytes_read |
| 103 | , to_read |
| 104 | , 0 |
| 105 | ); |
| 106 | |
| 107 | if (bytes_read == SOCKET_ERROR) { |
| 108 | if (last_error() == WSAEWOULDBLOCK) |
| 109 | rr.error = ReadResult::WOULDBLOCK; |
| 110 | else if (last_error() == WSAETIMEDOUT) |
| 111 | rr.error = ReadResult::TIMEOUT; |
| 112 | else |
| 113 | rr.error = ReadResult::UNKNOWN; |
| 114 | return rr; |
| 115 | } else if (bytes_read == 0) { |
| 116 | rr.error = ReadResult::REMOTE_CLOSED; |
| 117 | return rr; |
| 118 | } |
| 119 | |
| 120 | to_read -= bytes_read; |
| 121 | rr.bytes_read += bytes_read; |
| 122 | } |
| 123 | |
| 124 | return rr; |
| 125 | } |
| 126 | |
| 127 | WriteResult write(SOCKET socket, const void *data, u32 size) |
| 128 | { |
no test coverage detected