Skip to content

Commit c669beb

Browse files
committed
making the packets line up
1 parent 46b8919 commit c669beb

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

client.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
names = ['Mav', 'Adam', 'Teddy', 'Ari', 'Donovan', 'Diana',
77
'Eduardo', 'Emanuel', 'Nourya', 'Ron', 'Tobias', 'Vrushank']
88

9+
handlers = {
10+
"handle_get_version":0,
11+
"handle_get_currently_logged_in_uname":1,
12+
"handle_login":2,
13+
"handle_login_admin":3,
14+
"handle_logout":4,
15+
"handle_create_user":5,
16+
"handle_admin_run_cmd":6,
17+
"handle_place":7,
18+
"handle_read_board":8,
19+
"handle_get_winner":9,
20+
"handle_add_winner":10,
21+
"handle_set_intro":11,
22+
"handle_set_outro":12,
23+
"handle_report_winners":13,
24+
}
25+
926
def client_error_wrapper(error_msg):
1027
print("*"*len(str(error_msg)))
1128
print(error_msg)
@@ -24,9 +41,12 @@ def __init__(self, connect_tuple, debug = True):
2441

2542
def read_response(self):
2643
resp_len = self.server_conn.recv(4)
27-
if len(resp_len) != 4:
44+
if len(resp_len) < 4:
45+
client_error_wrapper("unable to read resposne length")
46+
raise Exception("need 4 bytes in length resposne, you have " + str(len(resp_len)))
47+
elif len(resp_len) > 4:
2848
client_error_wrapper("unable to read resposne length")
29-
raise Exception("need at least 4 bytes in resposne")
49+
raise Exception("need 4 bytes in length resposne, you have " + str(len(resp_len)))
3050
resp_len = int.from_bytes(resp_len, NET_ORDER)
3151
if self.debug:
3252
print("getting msg resposne len:", resp_len)
@@ -55,11 +75,13 @@ def place(self, turn):
5575
turn_enum = 1
5676
if turn == 'O':
5777
turn_enum = 2
58-
packet = turn.encode("utf-8") + user_coords[0].encode("utf-8") + user_coords[1].encode("utf-8")
78+
packet = int.to_bytes(turn_enum, 4, NET_ORDER) + user_coords[0].encode("utf-8") + user_coords[1].encode("utf-8")
79+
self.server_conn.sendall(int.to_bytes(handlers["handle_get_winner"], 4, NET_ORDER))
5980
self.server_conn.sendall(packet) #send which player, and coordinates
6081

6182
#expects a response that is a 9 byte string representing the board
6283
def read_board(self):
84+
self.server_conn.sendall(int.to_bytes(handlers["handle_get_winner"], 4, NET_ORDER))
6385
response = self.read_response()
6486

6587
#print the board
@@ -72,6 +94,7 @@ def read_board(self):
7294

7395
#expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
7496
def get_winner(self):
97+
self.server_conn.sendall(int.to_bytes(handlers["handle_get_winner"], 4, NET_ORDER))
7598
response = self.read_response()
7699
return int.from_bytes(response, NET_ORDER)
77100

@@ -85,6 +108,7 @@ def play(connect_tuple):
85108
print("WELCOME TO ZERO TICTACTOE. 100% VULNERABILITY-FREE CODE GUARANTEED")
86109
game.read_board()
87110

111+
time.sleep(1)
88112
#gameplay
89113
turn = 'X'
90114
while game.get_winner == 0:

game_dispatcher.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#include "game_dispatcher.h"
1313

1414
// includes for dispatchers
15+
#include "tictactoe.h"
1516
#include "account_login.h"
1617
#include "high_score.h"
1718

1819
typedef bool (*pkt_handler)(int client_fd, char* client_str);
1920

20-
2121
pkt_handler handlers[] = {
2222
/* general */
2323
handle_get_version,
@@ -30,21 +30,27 @@ pkt_handler handlers[] = {
3030
handle_create_user,
3131
handle_admin_run_cmd,
3232

33+
/* gameplay stuff */
34+
handle_place,
35+
handle_read_board,
36+
handle_get_winner,
37+
3338
/* high scores */
3439
handle_add_winner,
3540
handle_set_intro,
3641
handle_set_outro,
37-
handle_report_winners,
42+
handle_report_winners
3843
};
3944

4045
void handle_client(int client_fd, char* client_str) {
4146
int pkt_type;
4247
int bytes_read;
4348

44-
printf("%s connected\n", client_str);
49+
printf("%s connected in handle_client\n", client_str);
4550

4651
while (1) {
4752
// read packet type
53+
printf("reading packet type\n");
4854
bytes_read = recv(client_fd, &pkt_type, sizeof(pkt_type), 0);
4955

5056
if (bytes_read == -1) {

server 2

35.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)