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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#ifndef __WIN32__
# include <sys/ioctl.h>
# include <sys/socket.h>
# include <sys/select.h>
#else
# include <io.h>
# include <winsock2.h>
#endif
#include "main.h"
#include "list.h"
#include "tux.h"
#include "proto.h"
#include "client.h"
#include "analyze.h"
#include "world.h"
#include "buffer.h"
#include "udp.h"
static sock_udp_t *sock_server_udp;
static list_t *listRecvMsg;
static buffer_t *clientRecvBuffer;
static buffer_t *clientSendBuffer;
static my_time_t lastPing;
static my_time_t lastPingServerAlive;
#ifdef SUPPORT_TRAFFIC
static my_time_t lastTraffic;
static int traffic_down;
static int traffic_up;
#endif
typedef struct proto_cmd_client_struct {
char *name;
int len;
void (*fce_proto) (char *msg);
} proto_cmd_client_t;
static proto_cmd_client_t proto_cmd_list[] = {
{.name = "error",.len = 5,.fce_proto = proto_recv_error_client},
{.name = "init",.len = 4,.fce_proto = proto_recv_init_client},
{.name = "event",.len = 5,.fce_proto = proto_recv_event_client},
{.name = "newtux",.len = 6,.fce_proto = proto_recv_newtux_client},
{.name = "del",.len = 3,.fce_proto = proto_recv_del_client},
{.name = "additem",.len = 7,.fce_proto = proto_recv_additem_client},
{.name = "shot",.len = 4,.fce_proto = proto_recv_shot_client},
{.name = "kill",.len = 4,.fce_proto = proto_recv_kill_client},
{.name = "module",.len = 6,.fce_proto = proto_recv_module_client},
{.name = "chat",.len = 4,.fce_proto = proto_recv_chat_client},
{.name = "ping",.len = 4,.fce_proto = proto_recv_ping_client},
{.name = "end",.len = 3,.fce_proto = proto_recv_end_client},
{.name = "",.len = 0,.fce_proto = NULL},
};
static proto_cmd_client_t *findCmdProto(char *msg)
{
int len;
int i;
len = strlen(msg);
for (i = 0; proto_cmd_list[i].len != 0; i++) {
proto_cmd_client_t *thisCmd;
thisCmd = &proto_cmd_list[i];
if (len >= thisCmd->len
&& strncmp(msg, thisCmd->name, thisCmd->len) == 0) {
return thisCmd;
}
}
return NULL;
}
static int initUdpClient(char *ip, int port)
{
sock_server_udp = connectUdpSocket(ip, port);
if (sock_server_udp == NULL) {
return -1;
}
DEBUG_MSG(_("Connected to: %s on port: %d via UDP\n"), ip, port);
return 0;
}
int initClient(char *ip, int port)
{
char name[STR_NAME_SIZE];
int ret;
listRecvMsg = newList();
lastPing = getMyTime();
lastPingServerAlive = getMyTime();
#ifdef SUPPORT_TRAFFIC
lastTraffic = getMyTime();
traffic_down = 0;
traffic_up = 0;
#endif
clientRecvBuffer = newBuffer(CLIENT_BUFFER_LIMIT);
clientSendBuffer = newBuffer(CLIENT_BUFFER_LIMIT);
ret = initUdpClient(ip, port);
if (ret < 0) {
return -1;
}
getSettingNameRight(name);
proto_send_hello_client(name);
return 0;
}
static void errorWithServer()
{
fprintf(stderr, _("Server did not respond!\n"));
setMsgToAnalyze(_("Server is not running or being blocked. I was unable to connect."));
setWorldEnd();
}
void sendServer(char *msg)
{
int ret;
assert(msg != NULL);
ret = -1;
#ifndef PUBLIC_SERVER
if (isParamFlag("--send")) {
printf(_("Sending: \"%s\""), msg);
}
#endif
#ifdef SUPPORT_TRAFFIC
traffic_up += strlen(msg);
#endif
if (sock_server_udp != NULL) {
ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg));
}
if (ret < 0) {
errorWithServer();
return;
}
}
static int eventServerSelect()
{
char buffer[STR_PROTO_SIZE];
int ret;
memset(buffer, 0, STR_PROTO_SIZE);
ret = -1;
if (sock_server_udp != NULL) {
ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE - 1);
}
if (ret < 0) {
errorWithServer();
return ret;
}
#ifdef SUPPORT_TRAFFIC
traffic_down += ret;
#endif
addList(listRecvMsg, strdup(buffer));
#if 0
if (addBuffer(clientRecvBuffer, buffer, ret) < 0) {
errorWithServer();
return ret;
}
while (getBufferLine(clientRecvBuffer, buffer, STR_PROTO_SIZE) >= 0) {
if (strlen(buffer) > 0) {
addList(listRecvMsg, strdup(buffer));
}
}
#endif
return ret;
}
static void eventClientWorkRecvList()
{
proto_cmd_client_t *protoCmd;
char *line;
int i;
/* obsluha udalosti od servera */
assert(listRecvMsg != NULL);
for (i = 0; i < listRecvMsg->count; i++) {
line = (char *) listRecvMsg->list[i];
protoCmd = findCmdProto(line);
#ifndef PUBLIC_SERVER
if (isParamFlag("--recv")) {
printf(_("Recieved: \"%s\""), line);
}
#endif
if (protoCmd != NULL) {
protoCmd->fce_proto(line);
lastPingServerAlive = getMyTime();
}
}
destroyListItem(listRecvMsg, free);
listRecvMsg = newList();
}
static void eventPingServer()
{
my_time_t currentTime;
currentTime = getMyTime();
if (currentTime - lastPing > CLIENT_TIMEOUT) {
proto_send_ping_client();
lastPing = getMyTime();
}
}
static bool_t isServerAlive()
{
my_time_t currentTime;
currentTime = getMyTime();
if (currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE) {
return FALSE;
}
return TRUE;
}
static void selectClientSocket()
{
fd_set readfds;
struct timeval tv;
int max_fd;
int sock;
bool_t isNext;
max_fd = 0;
sock = 0;
if (sock_server_udp != NULL) {
if (isServerAlive() == FALSE) {
errorWithServer();
return;
}
}
do {
isNext = FALSE;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
if (sock_server_udp != NULL) {
sock = sock_server_udp->sock;
}
FD_SET(sock, &readfds);
max_fd = sock;
select(max_fd + 1, &readfds, (fd_set *) NULL, (fd_set *) NULL, &tv);
if (FD_ISSET(sock, &readfds)) {
if (eventServerSelect() > 0) {
isNext = TRUE;
}
}
} while (isNext == TRUE);
}
#ifdef SUPPORT_TRAFFIC
static void eventTraffic()
{
my_time_t currentTime;
currentTime = getMyTime();
if (currentTime - lastTraffic > 5000) {
lastTraffic = currentTime;
DEBUG_MSG(_("down: %d\n") _("up : %d\n"), traffic_down, traffic_up);
traffic_down = 0;
traffic_up = 0;
}
}
#endif
void eventClient()
{
#ifdef SUPPORT_TRAFFIC
eventTraffic();
#endif
eventPingServer();
selectClientSocket();
eventClientWorkRecvList();
}
static void quitUdpClient()
{
assert(sock_server_udp != NULL);
closeUdpSocket(sock_server_udp);
DEBUG_MSG(_("Closing UDP connection.\n"));
}
void quitClient()
{
proto_send_end_client();
assert(listRecvMsg != NULL);
destroyListItem(listRecvMsg, free);
destroyBuffer(clientRecvBuffer);
destroyBuffer(clientSendBuffer);
if (sock_server_udp != NULL) {
quitUdpClient();
}
}
|