Skip to content

Commit

Permalink
Now client is multi-threading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Oliviero committed Feb 25, 2021
1 parent 44674c4 commit f183dc0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
44 changes: 24 additions & 20 deletions src/client.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#ifdef __linux__
#include <sys/socket.h>
#include <arpa/inet.h>
Expand All @@ -17,9 +18,9 @@ extern int winsP1;
extern int winsP2;
extern int winner;
#ifdef __linux__
int sock; //-6108638100957931777 1.16.5 x:-16 y:73 z:76
int sock;
#elif _WIN32
unsigned int sock;
unsigned int sock;
#endif
extern int ready;
extern char user1[10];
Expand Down Expand Up @@ -48,24 +49,27 @@ int client_connect() {
return 0;
}

void client_comm() {
// read game data
recv(sock, (char *) &is_game_over, 4, 0);
recv(sock, (char *) &turn, 4, 0);
recv(sock, (char *) &winsP1, 4, 0);
recv(sock, (char *) &winsP2, 4, 0);
recv(sock, (char *) &winner, 4, 0);
for (int i = 0; i < 9; i++) {
recv(sock, (char *) &game_grid[i], 4, 0);
}
void* client_comm() {
while (1) {
// read game data
recv(sock, (char *) &is_game_over, 4, 0);
recv(sock, (char *) &turn, 4, 0);
recv(sock, (char *) &winsP1, 4, 0);
recv(sock, (char *) &winsP2, 4, 0);
recv(sock, (char *) &winner, 4, 0);
for (int i = 0; i < 9; i++) {
recv(sock, (char *) &game_grid[i], 4, 0);
}

// write events
send(sock, (char *) &click_position, 4, 0);
if (click_position != -1 || is_game_over == 1) {
click_position = -1;
}
send(sock, (char *) &ready, 4, 0);
if (ready == 1 && is_game_over == 0) {
ready = 0;
// write events
send(sock, (char *) &click_position, 4, 0);
if (click_position != -1 || is_game_over == 1) {
click_position = -1;
}
send(sock, (char *) &ready, 4, 0);
if (ready == 1 && is_game_over == 0) {
ready = 0;
}
}
pthread_exit(NULL);
}
2 changes: 1 addition & 1 deletion src/include/client.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
int client_connect();
void client_comm();
void* client_comm();
19 changes: 12 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "include/client.h"
#include "include/gameplay.h"
Expand Down Expand Up @@ -34,19 +35,24 @@ void log_level(){}
char user1[10];
char user2[10];

void* window_main();

int main() {
join_window();
if (client_connect() > 0) return 1;
pthread_t tid[2];
pthread_create(&tid[0], 0, client_comm, NULL);
pthread_create(&tid[1], 0, window_main, NULL);
for (int i = 0; i <= 1; i++) pthread_join(tid[i], NULL);
return 0;
}

void* window_main() {
SetTraceLogCallback(log_level);
InitWindow(SCR_WIDTH, SCR_HEIGHT, PROGRAM_NAME);
SetTargetFPS(GetMonitorRefreshRate(0));
/*pthread_t tid[];
if(pthread_create(&tid[0], NULL, clientThread, NULL) != 0) printf("Failed to create thread\n");
if(pthread_create(&tid[1], NULL, test, NULL) != 0) printf("Failed to create thread\n");*/
initHitBox();
// ---MAIN GAME LOOP---
while (!WindowShouldClose()) {
client_comm();
if (turn) {
cross(game);
} else {
Expand All @@ -61,11 +67,10 @@ int main() {
if (is_game_over == 1) {
endGame(winner);
}
DrawFPS(10, 10);
matchInfo();
EndDrawing();
}
CloseWindow();
close(sock);
return 0;
pthread_exit(NULL);
}
2 changes: 1 addition & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(void) {
send(clientfd[1], (char *) &user2, 10, 0);

// creating and joining threads
pthread_t tid[3];
pthread_t tid[2];
for (int i = 0; i <= 1; i++) {
int *arg = malloc(sizeof(*arg));
*arg = i;
Expand Down

0 comments on commit f183dc0

Please sign in to comment.