-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
client.c
226 lines (207 loc) · 4.93 KB
/
client.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifdef _WIN32
#include <windows.h>
#define close closesocket
#define sleep Sleep
#else
#include <netdb.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "client.h"
#include "tinycthread.h"
#define QUEUE_SIZE 65536
#define BUFFER_SIZE 4096
static int client_enabled = 0;
static int running = 0;
static int sd = 0;
static char recv_buffer[QUEUE_SIZE] = {0};
static thrd_t recv_thread;
static mtx_t mutex;
void client_enable() {
client_enabled = 1;
}
void client_disable() {
client_enabled = 0;
}
int get_client_enabled() {
return client_enabled;
}
int client_sendall(int sd, char *data, int length) {
if (!client_enabled) {
return 0;
}
int count = 0;
while (count < length) {
int n = send(sd, data + count, length, 0);
if (n == -1) {
return -1;
}
count += n;
length -= n;
}
return 0;
}
void client_send(char *data) {
if (!client_enabled) {
return;
}
if (client_sendall(sd, data, strlen(data)) == -1) {
perror("client_sendall");
exit(1);
}
}
void client_version(int version) {
if (!client_enabled) {
return;
}
char buffer[1024];
snprintf(buffer, 1024, "V,%d\n", version);
client_send(buffer);
}
void client_position(float x, float y, float z, float rx, float ry) {
if (!client_enabled) {
return;
}
static float px, py, pz, prx, pry = 0;
float distance =
(px - x) * (px - x) +
(py - y) * (py - y) +
(pz - z) * (pz - z) +
(prx - rx) * (prx - rx) +
(pry - ry) * (pry - ry);
if (distance < 0.0001) {
return;
}
px = x; py = y; pz = z; prx = rx; pry = ry;
char buffer[1024];
snprintf(buffer, 1024, "P,%.2f,%.2f,%.2f,%.2f,%.2f\n", x, y, z, rx, ry);
client_send(buffer);
}
void client_chunk(int p, int q, int key) {
if (!client_enabled) {
return;
}
char buffer[1024];
snprintf(buffer, 1024, "C,%d,%d,%d\n", p, q, key);
client_send(buffer);
}
void client_block(int x, int y, int z, int w) {
if (!client_enabled) {
return;
}
char buffer[1024];
snprintf(buffer, 1024, "B,%d,%d,%d,%d\n", x, y, z, w);
client_send(buffer);
}
void client_sign(int x, int y, int z, int face, const char *text) {
if (!client_enabled) {
return;
}
char buffer[1024];
snprintf(buffer, 1024, "S,%d,%d,%d,%d,%s\n", x, y, z, face, text);
client_send(buffer);
}
void client_talk(char *text) {
if (!client_enabled) {
return;
}
if (strlen(text) == 0) {
return;
}
char buffer[1024];
snprintf(buffer, 1024, "T,%s\n", text);
client_send(buffer);
}
int client_recv(char *data, int length) {
if (!client_enabled) {
return 0;
}
int result = 0;
mtx_lock(&mutex);
char *p = strstr(recv_buffer, "\n");
if (p) {
*p = '\0';
strncpy(data, recv_buffer, length);
data[length - 1] = '\0';
memmove(recv_buffer, p + 1, strlen(p + 1) + 1);
result = 1;
}
mtx_unlock(&mutex);
return result;
}
int recv_worker(void *arg) {
while (1) {
char data[BUFFER_SIZE] = {0};
if (recv(sd, data, BUFFER_SIZE - 1, 0) <= 0) {
if (running) {
perror("recv");
exit(1);
}
else {
break;
}
}
while (1) {
int done = 0;
mtx_lock(&mutex);
if (strlen(recv_buffer) + strlen(data) < QUEUE_SIZE) {
strcat(recv_buffer, data);
done = 1;
}
mtx_unlock(&mutex);
if (done) {
break;
}
sleep(0);
}
}
return 0;
}
void client_connect(char *hostname, int port) {
if (!client_enabled) {
return;
}
struct hostent *host;
struct sockaddr_in address;
if ((host = gethostbyname(hostname)) == 0) {
perror("gethostbyname");
exit(1);
}
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = ((struct in_addr *)(host->h_addr_list[0]))->s_addr;
address.sin_port = htons(port);
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (connect(sd, (struct sockaddr *)&address, sizeof(address)) == -1) {
perror("connect");
exit(1);
}
}
void client_start() {
if (!client_enabled) {
return;
}
running = 1;
mtx_init(&mutex, mtx_plain);
if (thrd_create(&recv_thread, recv_worker, NULL) != thrd_success) {
perror("thrd_create");
exit(1);
}
}
void client_stop() {
if (!client_enabled) {
return;
}
running = 0;
close(sd);
// if (thrd_join(recv_thread, NULL) != thrd_success) {
// perror("thrd_join");
// exit(1);
// }
// mtx_destroy(&mutex);
}