Skip to content

Commit

Permalink
Handle send/recv errors/timeouts in MT's internal sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel committed Jul 16, 2024
1 parent 38ae6cd commit 8362deb
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/client/craftium.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ inline int syncServerInit() {
perror("[SyncServer] setsockopt failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout)) < 0) {
perror("[SyncServer] setsockopt failed");

int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
perror("[SyncServer] setsockopt SO_REUSEADDR failed");
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -101,7 +103,7 @@ inline int syncClientInit() {
struct sockaddr_in serv_addr;
if ((sync_client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("[SycClient] Socket creation error");
return -1;
exit(EXIT_FAILURE);
}

serv_addr.sin_family = AF_INET;
Expand All @@ -112,16 +114,24 @@ inline int syncClientInit() {
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
<= 0) {
perror("[SycClient] Invalid address");
return -1;
exit(EXIT_FAILURE);
}

struct timeval timeout;
timeout.tv_sec = 10; // timeout time in seconds
timeout.tv_usec = 0;
if (setsockopt(sync_client_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout)) < 0) {
perror("[SyncClient] setsockopt failed");
exit(EXIT_FAILURE);
}

// Connect to the server @ sync_port
if ((status
= connect(sync_client_fd, (struct sockaddr*)&serv_addr,
sizeof(serv_addr)))
< 0) {
perror("[SycClient] Connection Failed");
return -1;
perror("[SycClient] Connection failed");
exit(EXIT_FAILURE);
}

// printf("=> Sync Client connected @ %d\n", sync_port);
Expand All @@ -133,15 +143,21 @@ inline void syncServerStep() {
syncServerInit();

char msg[2];
read(sync_conn_fd, msg, 2);
if (read(sync_conn_fd, msg, 2) <= 0) {
perror("[syncServerStep] Step failed");
exit(EXIT_FAILURE);
}
}

inline void syncClientStep() {
if (sync_client_fd == -1)
syncClientInit();

/* Send a dummy message of two bytes */
send(sync_client_fd, "-", 2, 0);
if (send(sync_client_fd, "-", 2, 0) <= 0) {
perror("[syncClientStep] Step failed");
exit(EXIT_FAILURE);
}
}

/*
Expand Down

0 comments on commit 8362deb

Please sign in to comment.