Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,11 @@ int main () {
}
// Handle packet data
handlePacket(client_fd, length - sizeVarInt(packet_id), packet_id, state);
if (recv_count == 0 || (recv_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK)) {
#ifdef _WIN32
if (recv_count == 0 || (recv_count == -1 && WSAGetLastError() != WSAEWOULDBLOCK)) {
#else
if (recv_count == 0 || (recv_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK)) {
#endif
disconnectClient(&clients[client_index], 4);
continue;
}
Expand Down
31 changes: 25 additions & 6 deletions src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) {
if (require_first) {
ssize_t r = recv(client_fd, p, 1, MSG_PEEK);
if (r <= 0) {
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
#ifdef _WIN32
if (r < 0 && (WSAGetLastError() == WSAEWOULDBLOCK)) {
#else
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
#endif
return 0; // no first byte available yet
}
return -1; // error or connection closed
Expand All @@ -64,7 +68,11 @@ ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) {
while (total < n) {
ssize_t r = recv(client_fd, p + total, n - total, 0);
if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK) {
#else
if (errno == EAGAIN || errno == EWOULDBLOCK) {
#endif
// handle network timeout
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
disconnectClient(&client_fd, -1);
Expand Down Expand Up @@ -111,7 +119,11 @@ ssize_t send_all (int client_fd, const void *buf, ssize_t len) {
continue;
}
if (n == 0) { // connection was closed, treat this as an error
errno = ECONNRESET;
#ifdef _WIN32
errno = WSAECONNRESET;
#else
errno = ECONNRESET;
#endif
return -1;
}
// not yet ready to transmit, try again
Expand Down Expand Up @@ -277,8 +289,15 @@ uint64_t splitmix64 (uint64_t state) {
// the start of the program*, and NOT wall clock time. To ensure
// compatibility, this should only be used to measure time intervals.
int64_t get_program_time () {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
#ifdef _WIN32
LARGE_INTEGER frequency, counter;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&counter);
return (int64_t)(counter.QuadPart * 1000000LL / frequency.QuadPart);
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
#endif
}
#endif