From fac5e3de0929c3abb73da552efbda6d641136588 Mon Sep 17 00:00:00 2001 From: Mario Kilies Date: Sun, 16 Jan 2011 07:57:28 +0100 Subject: Simplified net_recv(). Refactored 'hand' from array definition to hand_t struct definition. --- src/game.c | 9 +++++---- src/hand.c | 6 ++++-- src/hand.h | 7 +++++-- src/main.c | 12 +++++++----- src/net.c | 34 ++++++++++++++++++---------------- src/net.h | 6 +++--- src/net_client.c | 8 +++++--- src/net_server.c | 4 ++-- src/ui.c | 25 +++++++++++++------------ src/ui.h | 4 ++-- 10 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/game.c b/src/game.c index 2583fb1..96a987c 100644 --- a/src/game.c +++ b/src/game.c @@ -1,5 +1,6 @@ #include "game.h" #include "card.h" +#include "hand.h" #include #include #include @@ -59,14 +60,14 @@ void start_game(const bool servermode, const char *addr, const uint16_t port) const uint32_t score = 10; // Example data set for hand cards window - hand h = {22, 0, 12, 85, 27, 69, 78, 0, 77, 0}; - hand_sort(h); + hand_t h = { {22, 0, 12, 85, 27, 69, 78, 0, 77, 0} }; + hand_sort(&h); // Display all windows ui_display_wnd_table_cards(ts, false, 0); ui_display_wnd_stack_points(ts, false, 0); ui_display_wnd_current_state(pnoc, num_players, 2, score); - ui_display_wnd_hand_cards(h, false, 0); + ui_display_wnd_hand_cards(&h, false, 0); // main game loop while(running) @@ -80,7 +81,7 @@ void start_game(const bool servermode, const char *addr, const uint16_t port) case STATE_WAIT_CARDS: // wait on client until host has dealt cards break; case STATE_SELECTCARD: // player has to select own card, if done, set state to STATE_WAIT_OPPONENTCARDS - ui_choose_card(h); + ui_choose_card(&h); running = false; break; case STATE_WAIT_OPPONENTCARDS: // wait until all opponents have selected their open card. Then check if we have the lowest open card. If so, set state to STATE_SELECTSTACK, otherwise to STATE_WAIT_OPPONENTSTACK diff --git a/src/hand.c b/src/hand.c index 8c2a392..3f8c8ff 100644 --- a/src/hand.c +++ b/src/hand.c @@ -1,5 +1,6 @@ #include "hand.h" #include +#include #include "card.h" static int hand_comparator(const void *a, const void *b) @@ -10,7 +11,8 @@ static int hand_comparator(const void *a, const void *b) return c1 - c2; } -void hand_sort(hand h) +void hand_sort(hand_t *h) { - qsort(h, MAX_HAND_CARDS, sizeof(card), hand_comparator); + assert(h != NULL); + qsort(h->cards, MAX_HAND_CARDS, sizeof(card), hand_comparator); } diff --git a/src/hand.h b/src/hand.h index 83c3160..f13eaf8 100644 --- a/src/hand.h +++ b/src/hand.h @@ -5,8 +5,11 @@ #define MAX_HAND_CARDS 10 -typedef card hand[MAX_HAND_CARDS]; +typedef struct +{ + card cards[MAX_HAND_CARDS]; +} hand_t; -void hand_sort(hand h); +void hand_sort(hand_t *h); #endif // OXEN_HAND_H diff --git a/src/main.c b/src/main.c index 6e68d35..9f0bb65 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include "ui.h" #include "game.h" #include "net.h" @@ -22,9 +24,9 @@ int main(int argc, char **argv) int ssock; int* csocks; player_list players; - int opponents = 3; + int opponents = 1; const char* nickname = "deki"; - const hand testhand = { 12, 23, 35, 42, 55, 57, 70, 81, 103, 0 }; + const hand_t testhand = { { 12, 23, 35, 42, 55, 57, 70, 81, 103, 0 } }; servermode = true; port = argv[1]; @@ -51,7 +53,7 @@ int main(int argc, char **argv) // send test hand for(int i=0; icards[i]); printf("\n"); // cleanup diff --git a/src/net.c b/src/net.c index d7222e1..ed7e69f 100644 --- a/src/net.c +++ b/src/net.c @@ -5,40 +5,42 @@ #include #include "net.h" -void* net_recv(int sock, msg_type_t wanted) +void* net_recv(int sock, msg_type_t type) { - void* result = NULL; - uint8_t peekbuf[2], type, payload_len, *packet, *payload; - ssize_t len = recv(sock, peekbuf, 2, MSG_PEEK); // just peek into packet to determine type + msg_t m; + void *result; + ssize_t len = recv(sock, &m.hdr, sizeof(msg_header_t), MSG_PEEK); // just peek into packet to determine message header assert(len != -1); - type = peekbuf[NET_MSG_OFFSET_TYPE]; - payload_len = peekbuf[NET_MSG_OFFSET_PAYLOAD_LENGTH]; - - if(type != wanted) + if(m.hdr.type != type) { - printf("client_recv: received type %d instead of %d", type, wanted); + printf("net_recv: received message type %d instead of %d", m.hdr.type, type); return NULL; } - packet = malloc(payload_len+NET_MSG_OFFSET_PAYLOAD); - recv(sock, packet, payload_len+NET_MSG_OFFSET_PAYLOAD, 0); - payload = &packet[NET_MSG_OFFSET_PAYLOAD]; + m.payload = malloc(m.hdr.payload_length); // Allocate space for message payload + recv(sock, &m.hdr, sizeof(msg_header_t), 0); // Remove message header from socket + recv(sock, m.payload, m.hdr.payload_length, 0);// And then receive the payload switch(type) { case msg_type_hello: - result = server_recv_hello(payload, payload_len); + result = server_recv_hello(m.payload, m.hdr.payload_length); break; case msg_type_start_game: - result = client_recv_player_list(payload, payload_len); + result = client_recv_player_list(m.payload, m.hdr.payload_length); break; case msg_type_deal_cards: - result = client_recv_deal_cards(payload, payload_len); + result = client_recv_deal_cards(m.payload, m.hdr.payload_length); + break; + default: + printf("net_recv: Unknown message type %d received!\n", type); + exit(EXIT_FAILURE); break; } - free(packet); + + free(m.payload); return result; } diff --git a/src/net.h b/src/net.h index 8679d77..1ef87ff 100644 --- a/src/net.h +++ b/src/net.h @@ -29,7 +29,7 @@ typedef struct typedef struct { msg_header_t hdr; - uint8_t payload[]; + uint8_t *payload; } msg_t; // generic receive function @@ -39,13 +39,13 @@ void* net_recv(int sock, msg_type_t wanted); int server_start(const char* port); int* server_get_players(int serversock, const uint8_t count); void server_start_game(int* clients, const uint8_t clientcount, const player_list* players); -void server_deal_cards(int sock, const hand h); +void server_deal_cards(int sock, const hand_t *h); char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len); // Client side functions int client_connect_server(const char* host, const char* port); void client_hello(int sock, const char* username); player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len); -hand* client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len); +hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len); #endif // OXEN_NET_H diff --git a/src/net_client.c b/src/net_client.c index 9134cf7..a225529 100644 --- a/src/net_client.c +++ b/src/net_client.c @@ -110,14 +110,16 @@ player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_ return players; } -hand* client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len) +hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len) { - hand* h = malloc(sizeof(hand)); + hand_t *h = malloc(sizeof(hand_t)); assert(payload_len == MAX_HAND_CARDS); // deal_cards packet have fixed size for(int i=0; icards[i] = payload[i]; + } return h; } diff --git a/src/net_server.c b/src/net_server.c index da9d33d..1b60eec 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -180,14 +180,14 @@ char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len) * @param[in] sock Socket to use * @param[in] h Hand to send */ -void server_deal_cards(int sock, const hand h) +void server_deal_cards(int sock, const hand_t *h) { uint8_t buf[2+MAX_HAND_CARDS]; buf[NET_MSG_OFFSET_TYPE] = msg_type_deal_cards; buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = MAX_HAND_CARDS; for(int i=0; icards[i]; send(sock, buf, 2+MAX_HAND_CARDS, 0); } diff --git a/src/ui.c b/src/ui.c index ee27567..a42ecf4 100644 --- a/src/ui.c +++ b/src/ui.c @@ -241,7 +241,7 @@ void ui_display_wnd_current_state(const pnoc_t pnoc[], const uint8_t num_players * @param[in] highlight If true, a card will be highlighted * @param[in] highlighted_card The card to highlight. Only used, if highlight is true */ -void ui_display_wnd_hand_cards(const hand h, const bool highlight, const uint8_t highlighted_card) +void ui_display_wnd_hand_cards(const hand_t *h, const bool highlight, const uint8_t highlighted_card) { uint8_t num_zero_cards = 0; @@ -249,7 +249,7 @@ void ui_display_wnd_hand_cards(const hand h, const bool highlight, const uint8_t mvwprintw(w_hand_cards, 0, 0, "Hand Cards:"); wattroff(w_hand_cards, A_BOLD); - // Clear old cards from screen first + // Clear old cards from screen first by drawing emtpy card placeholders over them for (uint8_t i = 0; i < MAX_HAND_CARDS; i++) { if (i - num_zero_cards < 5) // Start with the first row of cards @@ -261,7 +261,7 @@ void ui_display_wnd_hand_cards(const hand h, const bool highlight, const uint8_t for (uint8_t i = 0; i < MAX_HAND_CARDS; i++) { // Cound all 0 cards and don't draw them - if (0 == h[i]) + if (0 == h->cards[i]) { num_zero_cards++; continue; @@ -270,22 +270,22 @@ void ui_display_wnd_hand_cards(const hand h, const bool highlight, const uint8_t if (highlight && i == highlighted_card) { if (i - num_zero_cards < 5) // Start with the first row of cards - draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, true, h[i]); + draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, true, h->cards[i]); else // And then draw the second row - draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, true, h[i]); + draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, true, h->cards[i]); continue; } if (i - num_zero_cards < 5) // Start with the first row of cards - draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, false, h[i]); + draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, false, h->cards[i]); else // And then draw the second row - draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, false, h[i]); + draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, false, h->cards[i]); } wrefresh(w_hand_cards); } -uint8_t ui_choose_card(hand h) +uint8_t ui_choose_card(hand_t *h) { int key; uint8_t chosen_card_idx = 0; @@ -294,10 +294,10 @@ uint8_t ui_choose_card(hand h) hand_sort(h); // Select card with lowest index as default - if (0 == h[chosen_card_idx]) + if (0 == h->cards[chosen_card_idx]) { i = (chosen_card_idx + 1) % MAX_HAND_CARDS; - while(0 == h[i]) + while(0 == h->cards[i]) i = (i + 1) % MAX_HAND_CARDS; chosen_card_idx = i; } @@ -311,8 +311,9 @@ uint8_t ui_choose_card(hand h) case KEY_VI_LEFT: // Fall through case KEY_LEFT: + // Add MAX_HAND_CARDS to prevent calculating the modulus of negative values i = (chosen_card_idx - 1 + MAX_HAND_CARDS) % MAX_HAND_CARDS; - while(0 == h[i]) + while(0 == h->cards[i]) i = (i - 1 + MAX_HAND_CARDS) % MAX_HAND_CARDS; chosen_card_idx = i; break; @@ -321,7 +322,7 @@ uint8_t ui_choose_card(hand h) // Fall through case KEY_RIGHT: i = (chosen_card_idx + 1) % MAX_HAND_CARDS; - while(0 == h[i]) + while(0 == h->cards[i]) i = (i + 1) % MAX_HAND_CARDS; chosen_card_idx = i; break; diff --git a/src/ui.h b/src/ui.h index 05f82ff..8c001bb 100644 --- a/src/ui.h +++ b/src/ui.h @@ -11,8 +11,8 @@ void ui_display_wnd_table_cards(const tablestacks ts, const bool highlight, const uint8_t highlighted_stack); void ui_display_wnd_stack_points(const tablestacks ts, const bool highlight, const uint8_t highlighted_points); void ui_display_wnd_current_state(const pnoc_t pnoc[], const uint8_t num_players, const uint8_t active_player, const uint32_t score); -void ui_display_wnd_hand_cards(const hand h, const bool highlight, const uint8_t highlighted_card); -uint8_t ui_choose_card(hand h); +void ui_display_wnd_hand_cards(const hand_t *h, const bool highlight, const uint8_t highlighted_card); +uint8_t ui_choose_card(hand_t *h); uint8_t ui_choose_stack(const tablestacks ts); void ui_init(void); void ui_fini(void); -- cgit v1.2.3