diff options
| author | Mario Kilies <MarioKilies@GMX.net> | 2011-01-15 00:30:56 +0100 |
|---|---|---|
| committer | Mario Kilies <MarioKilies@GMX.net> | 2011-01-15 00:30:56 +0100 |
| commit | 22a3b4d27084a93197f82c88a8dc954b49672bd9 (patch) | |
| tree | 0a73531484a3a9d8bb6f94c31753abc85c9ec3af | |
| parent | a68b72e60128834e41a09e2aeb3c2faea59384b2 (diff) | |
| parent | e486953991884606f60d747b0cfb846f576c6bc4 (diff) | |
Merge branch 'master' of ssh://git@wg.reiner-h.de:22003/~git/oxen
| -rw-r--r-- | src/net.c | 32 | ||||
| -rw-r--r-- | src/net.h | 5 |
2 files changed, 28 insertions, 9 deletions
@@ -84,7 +84,11 @@ int* server_get_players(int serversock, const uint8_t count) assert(count < MAX_PLAYERS && count > 0); clientsocks = malloc(count*sizeof(int)); - assert(clientsocks != NULL); + if(clientsocks == NULL) + { + printf("server_get_players: Out of memory\n"); + exit(EXIT_FAILURE); + } // accept connections for(i=0; i<count; i++) @@ -117,8 +121,13 @@ void client_hello(int sock, const char* username) uint8_t namelen = strlen(username); buf = malloc(namelen+2); // type + len + username + if(buf == NULL) + { + printf("client_hello: Out of memory\n"); + exit(EXIT_FAILURE); + } - buf[0] = CLIENT_HELLO; + buf[0] = msg_type_hello; buf[1] = namelen; memcpy(buf+2, username, namelen); @@ -187,7 +196,12 @@ void server_start_game(int* clients, const uint8_t clientcount, const char* user buflen += strlen(usernames[i]); buf = malloc(buflen); - buf[pos++] = SERVER_START_GAME; + if(buf == NULL) + { + printf("server_start_game: Out of memory\n"); + exit(EXIT_FAILURE); + } + buf[pos++] = msg_type_start_game; buf[pos++] = clientcount; // copy usernames with length to buffer for(int i=0; i<usercount; i++) @@ -217,11 +231,17 @@ char* server_recv_hello(int sock) recv(sock, buf, 13, 0); - assert(buf[0] == CLIENT_HELLO); + assert(buf[0] == msg_type_hello); namelen = buf[1]; name = malloc(namelen+1); - strncpy(name, buf+2, namelen); + if(name == NULL) + { + printf("sender_recv_hello: Out of memory\n"); + exit(EXIT_FAILURE); + } + + memcpy(name, buf+2, namelen); name[namelen] = '\0'; return name; @@ -247,7 +267,7 @@ void* server_recv(int sock, uint8_t wanted) switch(type) { - case CLIENT_HELLO: + case msg_type_hello: result = server_recv_hello(sock); break; } @@ -3,13 +3,12 @@ #define MAX_PLAYERS 10 -enum packet_type { CLIENT_HELLO, SERVER_START_GAME }; - typedef enum { // Specify message type identifier here - msg_type_hello = 0x0 + msg_type_hello = 0x0, + msg_type_start_game = 0x1 } msg_type_t; typedef struct |
