| 167 | } |
| 168 | |
| 169 | size_t Socket::read(uint8_t *buffer, size_t maxBytes, timeval *timeout) const |
| 170 | { |
| 171 | if (!Select(timeout)) { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | const auto msvcMaxBytes = static_cast<int>( |
| 176 | std::min<size_t>(std::numeric_limits<int>::max(), maxBytes)); |
| 177 | const int bytesRead = recv(m_Socket, reinterpret_cast<char *>(buffer), |
| 178 | msvcMaxBytes, 0); |
| 179 | if (bytesRead > 0) { |
| 180 | return bytesRead; |
| 181 | } |
| 182 | const auto lastError = WSAGetLastError(); |
| 183 | if ((0 == bytesRead) || (lastError == CONNECTION_CLOSED) || |
| 184 | (lastError == CONNECTION_ABORTED)) { |
| 185 | throw std::runtime_error("connection closed by remote"); |
| 186 | } else { |
| 187 | LOG_ERROR("read frame failed with error: " |
| 188 | << std::dec << std::strerror(lastError)); |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | Frame &Socket::read(Frame &frame, timeval *timeout) const |
| 194 | { |