Skip to content

Commit 8a96e94

Browse files
committed
added sending/receiving gameplay packets
1 parent 234ad21 commit 8a96e94

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, connect_tuple, debug = True):
1717
self.server_conn = None
1818
self.debug = debug
1919

20-
def read_resposne(self):
20+
def read_response(self):
2121
resp_len = self.server_conn.recv(4)
2222
if len(resp_len) != 4:
2323
client_error_wrapper("unable to read resposne length")
@@ -39,7 +39,7 @@ def connect(self):
3939

4040
def get_version(self):
4141
self.server_conn.sendall(TicTacToe.GET_VERSION_PKT)
42-
return self.read_resposne()
42+
return self.read_response()
4343

4444
def play(connect_tuple):
4545
game = TicTacToe(connect_tuple)

helper.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ bool get_str_from_client(int client_fd, char* str) {
5252
return true;
5353
}
5454

55+
int get_int_from_client(int client_fd) {
56+
int number;
57+
int bytes_read = recv(client_fd, &number, sizeof(int), 0);
58+
if (bytes_read == -1) {
59+
perror("recv from get_int_from_client in len");
60+
}
61+
if (bytes_read == 0) {
62+
printf("client disconnected in number in get_int_from_client\n");
63+
return -1;
64+
}
65+
if (bytes_read != sizeof(int)) {
66+
printf("client didn't send exactly 4 bytes for int length in get_int_from_client\n");
67+
return -1;
68+
}
69+
70+
number = ntohl(number);
71+
log_verbose("will read number %d\n", number);
72+
73+
return number;
74+
}
75+
5576
void uses_assumed_sizes() {
5677
if (sizeof(int) != 4) {
5778
printf("oh no! expected sizeof(int) to be size 4\n");

helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
bool handle_get_version(int client_fd, char* client_str);
1010
void respond_str_to_client(int fd, char* str);
1111

12+
int get_int_from_client(int fd);
1213
bool get_str_from_client(int fd, char* str);
1314

1415
// C heleprs

tictactoe.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,52 @@
1616
enum Player board[__BOARD_SIZE__][__BOARD_SIZE__] = { //(0, 0) top left, (2, 2) bottom right
1717
{E, E, E},
1818
{E, E, E},
19-
{E, E, E}
19+
{E, E, E}
2020
};
2121

2222
bool handle_place(int client_fd, char* client_str) {
23-
enum Player player = (int)(*client_str); //first four bytes of packet are the player
24-
int x_coord = (int)( *( client_str + sizeof(int)) );
25-
int y_coord = (int)( *( client_str + sizeof(int) * 2) );
23+
enum Player player; //first four bytes of packet are the player
24+
while ( (player = get_int_from_client(client_fd)) == -1) { }
25+
int x_coord; //next four bytes are the x
26+
while ( (x_coord = get_int_from_client(client_fd)) == -1) { }
27+
int y_coord; //last 4 are the y
28+
while ( (y_coord = get_int_from_client(client_fd)) == -1) { }
29+
2630
board[x_coord][y_coord] = player;
2731

28-
return 0;
32+
return true;
2933
}
3034

3135
//should we send the whole board as a (9 * 4) byte string instead? modify client/how we send this
32-
bool handle_read_board(int client_fd, char* client_str) {
33-
for (int x = 0; x < __BOARD_SIZE__; x++) {
34-
for (int y = 0; y < __BOARD_SIZE__; y++) {
35-
//send the info at each location to client
36-
}
37-
}
38-
39-
return 0;
40-
}
41-
42-
//commented function sends the board as a 36-byte (9 ints) char*
4336
// bool handle_read_board(int client_fd, char* client_str) {
44-
// char board_pkt[9];
45-
// int i = 0;
4637
// for (int x = 0; x < __BOARD_SIZE__; x++) {
4738
// for (int y = 0; y < __BOARD_SIZE__; y++) {
48-
// board_pkt[i] = board[x][y];
49-
// i++;
39+
// //send the info at each location to client
5040
// }
5141
// }
5242

53-
// //send the board_pkt string to client
54-
5543
// return 0;
5644
// }
5745

46+
// sends the board as a 36-byte (9 ints) char*
47+
bool handle_read_board(int client_fd, char* client_str) {
48+
char board_pkt[9];
49+
int i = 0;
50+
for (int x = 0; x < __BOARD_SIZE__; x++) {
51+
for (int y = 0; y < __BOARD_SIZE__; y++) {
52+
board_pkt[i] = board[x][y];
53+
i++;
54+
}
55+
}
56+
57+
respond_str_to_client(client_fd, board_pkt);
58+
59+
return true;
60+
}
61+
5862

5963

60-
bool handle_get_winner(int client_fd) {
64+
bool handle_get_winner(int client_fd, char* client_str) {
6165
//implement a more effecient check if needed
6266

6367
enum Player winner = E;
@@ -78,9 +82,9 @@ bool handle_get_winner(int client_fd) {
7882
if ((board[2][0] != E) && (board[2][0] == board[1][1]) && (board[0][0] == board[0][2]))
7983
winner = board[2][0];
8084

81-
//send winner to db stub code
82-
add_winner(client_fd, winner);
85+
//send winner to client
86+
respond_str_to_client(client_fd, (char*)winner);
8387

84-
return 0;//what to return for status?
88+
return true;//what to return for status?
8589

8690
}

tictactoe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ bool handle_place(int client_fd, char* client_str);
1212

1313
bool handle_read_board(int client_fd, char* client_str);
1414

15-
bool handle_get_winner(int client_fd);
15+
bool handle_get_winner(int client_fd, char* client_str);
1616

1717
#endif

0 commit comments

Comments
 (0)