summaryrefslogtreecommitdiff
path: root/src/client/client.c
blob: 2ed5e0aa83b4330071807245df516c6955a313ef (plain)
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

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>

#include "base/main.h"
#include "base/list.h"
#include "base/tux.h"
#include "base/proto.h"

#include "client/client.h"
#include "client/language.h"

#include "screen/screen_analyze.h"
#include "screen/screen_world.h"

static int protocolType;

#ifdef SUPPORT_NET_UNIX_UDP
#include "net_unix/udp.h"
static sock_udp_t *sock_server_udp;
#endif

#ifdef SUPPORT_NET_SDL_UDP
#include "net_sdl/sdl_udp.h"
static sock_sdl_udp_t *sock_server_sdl_udp;
#endif

static list_t *clientBuffer;
static my_time_t lastPing;
static my_time_t lastPingServerAlive;

static void initClient()
{
	char name[STR_NAME_SIZE];

	clientBuffer = newList();
	lastPing = getMyTime();
	lastPingServerAlive = getMyTime();

	getSettingNameRight(name);
	proto_send_hello_client(name);
}

#ifdef SUPPORT_NET_UNIX_UDP

int initUdpClient(char *ip, int port, int proto)
{
	sock_server_udp = connectUdpSocket(ip, port, proto);

	if( sock_server_udp == NULL )
	{
		return -1;
	}

	printf("connect UDP %s %d\n", ip, port);

	protocolType = NET_PROTOCOL_TYPE_UDP;
	initClient();

	return 0;
}

#endif

#ifdef SUPPORT_NET_SDL_UDP

int initSdlUdpClient(char *ip, int port)
{
	sock_server_sdl_udp = connectSdlUdpSocket(ip, port);

	if( sock_server_sdl_udp == NULL )
	{
		return -1;
	}

	printf("connect UDP %s %d\n", ip, port);

	protocolType = NET_PROTOCOL_TYPE_UDP;
	initClient();

	return 0;
}

#endif

void sendServer(char *msg)
{
	int ret;

	assert( msg != NULL );

#ifndef PUBLIC_SERVER
		if( isParamFlag("--send") )
		{
			printf("send -> %s", msg);
		}
#endif

#ifdef SUPPORT_NET_UNIX_UDP
	ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg));
#endif

#ifdef SUPPORT_NET_SDL_UDP
	ret = writeSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, msg, strlen(msg));
#endif
}

static int eventServerSelect()
{
	char buffer[STR_PROTO_SIZE];
	int ret;

	memset(buffer,0 ,STR_PROTO_SIZE);

#ifdef SUPPORT_NET_UNIX_UDP
	ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1);
#endif

#ifdef SUPPORT_NET_SDL_UDP
	ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_PROTO_SIZE-1);

	if( ret < 0 )
	{
		return ret;
	}
#endif

	addList(clientBuffer, strdup(buffer) );
/*
	if( addBuffer(clientBuffer, buffer, ret) != 0 )
	{
		fprintf(stderr, "chyba, nemozem zapisovat do mojho buffera !\n");
		setWorldEnd();
		return ret;
	}
*/
	return ret;
}

void eventServerBuffer()
{
	char *line;
	int i;

	/* obsluha udalosti od servera */
	
	assert( clientBuffer != NULL );

	for( i = 0 ; i < clientBuffer->count ; i++ )
	{
		line = (char *)clientBuffer->list[i];

#ifndef PUBLIC_SERVER
		if( isParamFlag("--recv") )
		{
			printf("recv -> %s", line);
		}
#endif
		if( strncmp(line, "error", 5) == 0 )proto_recv_error_client(line);
		if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line);
		if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line);
		if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line);
		if( strncmp(line, "del", 3) == 0 )proto_recv_del_client(line);
		if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line);
		if( strncmp(line, "shot", 4) == 0 )proto_recv_shot_client(line);
		if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line);
		if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_client(line);
		if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line);

		lastPingServerAlive = getMyTime();
	}

	destroyListItem(clientBuffer, free);
	clientBuffer = newList();
}

void eventPingServer()
{
	my_time_t currentTime;

 	currentTime = getMyTime();

	if( currentTime - lastPing > CLIENT_TIMEOUT )
	{
		proto_send_ping_client();
		lastPing = getMyTime();
	}
}

bool_t isServerAlive()
{
	my_time_t currentTime;

 	currentTime = getMyTime();

	if( currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE )
	{
		return FALSE;
	}

	return TRUE;
}

#ifdef SUPPORT_NET_UNIX_UDP

void selectClientUdpSocket()
{
	fd_set readfds;
	struct timeval tv;
	int max_fd;
	bool_t isNext;

	if( isServerAlive() == FALSE )
	{
		fprintf(stderr, "server neodpoveda !\n");
		setMsgToAnalyze(getMyText("ERROR_SERVER_DONT_ALIVE"));
		setWorldEnd();
		return;
	}

	do{
		isNext = FALSE;

		tv.tv_sec = 0;
		tv.tv_usec = 0;
		
		FD_ZERO(&readfds);
		FD_SET(sock_server_udp->sock, &readfds);
		max_fd = sock_server_udp->sock;
	
		select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
	
		if( FD_ISSET(sock_server_udp->sock, &readfds) )
		{
			eventServerSelect();
			isNext = TRUE;
		}

	}while( isNext == TRUE );

}

#endif

#ifdef SUPPORT_NET_SDL_UDP

void selectClientSdlUdpSocket()
{
	int ret;

	if( isServerAlive() == FALSE )
	{
		setMsgToAnalyze(getMyText("ERROR_SERVER_DONT_ALIVE"));
		setWorldEnd();
		return;
	}

	do{
		ret = eventServerSelect();
	}while( ret > 0);
}

#endif

static void quitClient()
{
	proto_send_end_client();
	assert( clientBuffer != NULL );
	destroyListItem(clientBuffer, free);
}

#ifdef SUPPORT_NET_UNIX_UDP

void quitUdpClient()
{
	quitClient();

	assert( sock_server_udp != NULL );
	closeUdpSocket(sock_server_udp);

	printf("quit UDP conenct\n");
}

#endif

#ifdef SUPPORT_NET_SDL_UDP

void quitSdlUdpClient()
{
	quitClient();

	assert( sock_server_sdl_udp != NULL );
	closeSdlUdpSocket(sock_server_sdl_udp);

	printf("quit UDP conenct\n");
}

#endif