improved network performance

Mon, 31 Mar 2014 14:08:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 14:08:00 +0200
changeset 22
41bbfd4d17a3
parent 21
2e5846019b4f
child 23
824c9522ce66

improved network performance

src/game.c file | annotate | diff | comparison | revisions
src/network.c file | annotate | diff | comparison | revisions
src/network.h file | annotate | diff | comparison | revisions
src/server.c file | annotate | diff | comparison | revisions
--- a/src/game.c	Mon Mar 31 14:00:58 2014 +0200
+++ b/src/game.c	Mon Mar 31 14:08:00 2014 +0200
@@ -159,8 +159,7 @@
             int eval_result = eval_move(board, mycolor, movestr, &move);
             switch (eval_result) {
             case VALID_MOVE_SYNTAX:
-                net_send_code(opponent, NETCODE_MOVE);
-                net_send_data(opponent, &move, sizeof(Move));
+                net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
                 code = net_recieve_code(opponent);
                 move.check = code == NETCODE_CHECK;
                 move.checkmate = code == NETCODE_CHECKMATE;
--- a/src/network.c	Mon Mar 31 14:00:58 2014 +0200
+++ b/src/network.c	Mon Mar 31 14:08:00 2014 +0200
@@ -113,19 +113,21 @@
     return EXIT_SUCCESS;
 }
 
-void net_send_code(int socket, uint32_t code) {
-    code = htonl(code);
-    send(socket, &code, sizeof(uint32_t), 0);
+void net_send_code(int socket, uint8_t code) {
+    send(socket, &code, sizeof(uint8_t), 0);
 }
 
-void net_send_data(int socket, void *data, size_t len) {
-    send(socket, data, len, 0);
+void net_send_data(int socket, uint8_t code, void *data, size_t len) {
+    uint8_t pkg[len+1];
+    pkg[0] = code;
+    memcpy(pkg+1, data, len);
+    send(socket, pkg, len+1, 0);
 }
 
-uint32_t net_recieve_code(int socket) {
-    uint32_t code;
-    recv(socket, &code, sizeof(uint32_t), 0);
-    return ntohl(code);
+uint8_t net_recieve_code(int socket) {
+    uint8_t code;
+    recv(socket, &code, sizeof(uint8_t), 0);
+    return code;
 }
 
 void net_recieve_data(int socket, void *data, size_t len) {
--- a/src/network.h	Mon Mar 31 14:00:58 2014 +0200
+++ b/src/network.h	Mon Mar 31 14:08:00 2014 +0200
@@ -46,7 +46,7 @@
 #define NETCODE_CHECK 0x23
 #define NETCODE_CHECKMATE 0x24
     
-#define NETCODE_VERSION 5
+#define NETCODE_VERSION 6
 
 typedef struct {
     int fd; /* -1, if we are the client */
@@ -67,9 +67,9 @@
 int net_destroy(Server *server);
 int net_connect(Server *server);
 
-void net_send_code(int socket, uint32_t code);
-void net_send_data(int socket, void *data, size_t len);
-uint32_t net_recieve_code(int socket);
+void net_send_code(int socket, uint8_t code);
+void net_send_data(int socket, uint8_t code, void *data, size_t len);
+uint8_t net_recieve_code(int socket);
 void net_recieve_data(int socket, void *data, size_t len);
 
 
--- a/src/server.c	Mon Mar 31 14:00:58 2014 +0200
+++ b/src/server.c	Mon Mar 31 14:08:00 2014 +0200
@@ -76,8 +76,8 @@
     }
 
     int fd = server.client->fd;
-    net_send_code(fd, NETCODE_GAMEINFO);
-    net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo));
+    net_send_data(fd, NETCODE_GAMEINFO,
+        &(settings->gameinfo), sizeof(Gameinfo));
     printw("\rClient connected - awaiting challenge acceptance...");
     refresh();
     int code = net_recieve_code(fd);

mercurial