#include #include #include #include #include #include #include #include #include "net.h" #include "player.h" /** * Client side function; Send hello to server * @param[in] sock Socket to use * @param[in] username Username of player */ void client_hello(int sock, const char* username) { uint8_t* buf; 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[NET_MSG_OFFSET_TYPE] = msg_type_hello; buf[NET_MSG_OFFSET_PAYLOAD_LENGTH] = namelen; memcpy(buf+NET_MSG_OFFSET_PAYLOAD, username, namelen); send(sock, buf, namelen+2, 0); free(buf); } /** * Client side function; connects to specified host:port * @param[in] host Hostname of server * @param[in] port Port of server * @return Socket with open connection to server */ int client_connect_server(const char* host, const char* port) { int status; int sock; struct addrinfo hints, *result, *tmp; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; // TCP socket status = getaddrinfo(host, port, &hints, &result); if(status != 0) { printf("getaddrinfo: %s\n", gai_strerror(status)); exit(EXIT_FAILURE); } // connect to first result in linked addrinfo list for(tmp = result; tmp != NULL; tmp = tmp->ai_next) { // create socket sock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); if(sock == -1) continue; // connect! if(connect(sock, tmp->ai_addr, tmp->ai_addrlen) != -1) break; // Success! close(sock); } if(tmp == NULL) { printf("failed to connect\n"); exit(EXIT_FAILURE); } freeaddrinfo(result); return sock; } player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len) { player_list* players; uint32_t pos = 0; players = create_playerlist(); if(players == NULL) { printf("client_recv_player_list: Out of memory\n"); exit(EXIT_FAILURE); } players->count = payload[pos++]; // read usernames from buffer for(int i=0; icount; i++) { uint8_t namelen = payload[pos++]; players->names[i] = malloc(namelen+1); memcpy(players->names[i], payload+pos, namelen); players->names[i][namelen] = '\0'; pos += namelen; } return players; } hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len) { 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; }