-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.c
142 lines (121 loc) · 4 KB
/
helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdbool.h>
#include "helper.h"
#include "globals.h"
#include "tictactoe.h"
bool handle_get_version(int client_fd, char* client_str) {
char version_str[0x100] = {0};
sprintf(version_str, "Stanford Zero TicTacToe vesion %s", TICTACTOE_VERSION_STR);
respond_str_to_client(client_fd, version_str);
//reset the board here in case a previous game was cut off before winning
reset_board();
return true;
}
void respond_buff_to_client(int fd, char* buff, int buff_len) {
int len_htonl = htonl(buff_len);
int err = send(fd, &len_htonl, sizeof(len_htonl), 0);
if (err < 0) {
perror("send len in respond_buff_to_client");
}
err = send(fd, buff, buff_len, 0);
if (err < 0) {
perror("send buff in respond_buff_to_client");
}
}
void respond_str_to_client(int fd, char* str) {
int len_htonl = htonl(strlen(str));
int err = send(fd, &len_htonl, sizeof(len_htonl), 0);
if (err < 0) {
perror("send len in respond_str_to_client");
}
err = send(fd, str, strlen(str), 0);
if (err < 0) {
perror("send str in respond_str_to_client");
}
}
bool get_str_from_client(int client_fd, char* str) {
int str_len;
int bytes_read = recv(client_fd, &str_len, sizeof(str_len), 0);
if (bytes_read == -1) {
perror("recv from get_str_from_client in len");
}
if (bytes_read == 0) {
printf("client disconnected in str len in get_str_from_client\n");
return false;
}
if (bytes_read != sizeof(str_len)) {
printf("client didn't send enough bytes for string length in get_str_from_client\n");
return false;
}
str_len = ntohl(str_len);
log_verbose("will read str len %d\n", str_len);
recv(client_fd, str, str_len, 0);
if (bytes_read == -1) {
perror("recv from get_str_from_client in data");
}
return true;
}
int get_int_from_client(int client_fd) {
int number;
int bytes_read = recv(client_fd, &number, sizeof(int), 0);
if (bytes_read == -1) {
perror("recv from get_int_from_client in len");
}
if (bytes_read == 0) {
printf("client disconnected in number in get_int_from_client\n");
return -1;
}
if (bytes_read != sizeof(int)) {
printf("client didn't send exactly 4 bytes for int length in get_int_from_client\n");
return -1;
}
number = ntohl(number);
log_verbose("will read number %d\n", number);
return number;
}
bool get_buffer_from_client(int client_fd, char* output, int output_size) {
int client_send_len;
int bytes_read = recv(client_fd, &client_send_len, sizeof(client_send_len), 0);
if (bytes_read == -1) {
perror("recv from get_str_from_client in len");
}
if (bytes_read == 0) {
printf("client disconnected in str len in get_str_from_client\n");
return false;
}
if (bytes_read != sizeof(client_send_len)) {
printf("client didn't send enough bytes for string length in get_str_from_client\n");
return false;
}
client_send_len = ntohl(client_send_len);
if (client_send_len > output_size) {
printf("can't read %d bytes :( (expecting less than %d)\n", client_send_len, output_size);
return false;
}
bytes_read = recv(client_fd, output, client_send_len, 0);
if (bytes_read == -1) {
perror("recv in get_buffer_from_client in data");
}
if (bytes_read == 0) {
printf("client disconnected in get_buffer_from_client\n");
return false;
}
if (bytes_read != client_send_len) {
printf("client didn't send enough bytes in get_buffer_from_client\n");
return false;
}
return true;
}
void uses_assumed_sizes() {
if (sizeof(int) != 4) {
printf("oh no! expected sizeof(int) to be size 4\n");
exit(1);
}
}
void stub_exit(char * str) {
printf("%s\n", str);
exit(1);
}