Skip to content

Commit

Permalink
add: client id is now provided by client instead of the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Samoth69 committed Dec 3, 2021
1 parent 2086a34 commit 856b33a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ void new_client(int client_id)
printf("new_client: %d (id: %d)\n", counter, client_id);

// envoie d'un message
net_client_betray();
net_client_betray(124241322);

net_server_send_screen_choice(0);
net_server_send_screen_waiting(0);
net_server_send_screen_score(0, true, 42);
net_server_send_screen_choice(432);
net_server_send_screen_waiting(432);
net_server_send_screen_score(432, true, 42);
}

void client_disconnecting(int client_id)
Expand Down Expand Up @@ -69,7 +69,7 @@ int main()

char *addrServer = "0.0.0.0";
net_server_init(addrServer, 7799);
net_client_init(addrServer, 7799);
net_client_init(addrServer, 7799, 432);

net_server_wait();

Expand Down
5 changes: 4 additions & 1 deletion src/net_prisoner_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ void *_net_client_threadProcess(void *ptr)
*/
void net_client_init(char *addrServer, int port, int client_id)
{
net_client_id = client_id;

struct sockaddr_in serverAddr;
pthread_t thread;

Expand Down Expand Up @@ -165,9 +167,10 @@ void net_client_init(char *addrServer, int port, int client_id)
// init the client id
_net_common_netpacket packet;
packet.msg_type = INIT_CLIENT_ID;
packet.client_id = client_id;
packet.client_id = net_client_id;
write(net_client_sockfd, &packet, sizeof(packet));
_net_common_dbg("the client sent his id : %d\n", net_client_id);


// reading pthread creation
pthread_create(&thread, 0, _net_client_threadProcess, &net_client_sockfd);
Expand Down
2 changes: 1 addition & 1 deletion src/net_prisoner_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ void _net_common_init()
sem_init(&_lock_log_dbg, PTHREAD_PROCESS_SHARED, 1);
if (THREAD_SAFETY)
{
_net_common_dbg("Thread safety enabled");
_net_common_dbg("Thread safety enabled\n");
}
}
13 changes: 8 additions & 5 deletions src/net_prisoner_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool _net_server_exit = false;
/**
* @brief counter to keep track of client id
*/
int _net_server_client_id_counter = 0;
//int _net_server_client_id_counter = 0;

/**
* @brief Use to protect lib user function from multiple executions
Expand Down Expand Up @@ -202,8 +202,6 @@ void _net_server_connection_add(_net_server_connection_t *connection)
perror("Too much simultaneous connections");
exit(-5);
}
_net_server_call_new_client(_net_server_client_id_counter);
_net_server_client_id_counter++;
}

/**
Expand Down Expand Up @@ -347,11 +345,11 @@ void *_net_server_thread_process(void *ptr)

if (len != sizeof(packet))
{
_net_common_dbg("WARN: Invalid packet received, ignoring\n");
_net_common_dbg("WARN: Invalid packet received from client %d, ignoring\n", connection->client_id);
continue;
}

_net_common_dbg("Received from client #%d length %d\n", connection->client_id, len);
//_net_common_dbg("Received from client #%d length %d\n", connection->client_id, len);
memcpy(&packet, &buffer_in, len);

switch (packet.msg_type)
Expand All @@ -378,6 +376,11 @@ void *_net_server_thread_process(void *ptr)
case SCREEN_SCORE:
_net_common_dbg("ERROR: received SCREEN_SCORE from client %d\n", connection->client_id);
break;
case INIT_CLIENT_ID:
connection->client_id = packet.client_id;
_net_common_dbg("Received INIT_CLIENT_ID from client %d\n", connection->client_id);
_net_server_call_new_client(connection->client_id);
break;

default:
_net_common_dbg("Unknown message type, do you have the latest version of the lib ?\n");
Expand Down

0 comments on commit 856b33a

Please sign in to comment.