File: server.cc

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (444 lines) | stat: -rw-r--r-- 10,544 bytes parent folder | download
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
 **	Server.cc - Server functions for Exult (NOT ExultStudio).
 **
 **	Written: 5/2/2001 - JSF
 **/

/*
Copyright (C) 2000-2001 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>	// All the ifdefs aren't useful if we don't do this
#endif

/*
 *	For the time being, we'll only inflict this on X users.
 */
#ifdef USE_EXULTSTUDIO

#include <unistd.h>
#include <fcntl.h>

#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#include <cstdio>

#ifdef _AIX
#include <strings.h>
#endif

#if HAVE_NETDB_H
#include <netdb.h>
#endif

#ifndef WIN32
#include <sys/un.h>
#endif

#include "server.h"
#include "servemsg.h"
#include "utils.h"
#include "egg.h"
#include "actors.h"
#include "gamewin.h"
#include "gamemap.h"
#include "chunkter.h"
#include "cheat.h"

#ifdef WIN32
#include "servewin32.h"
#include "cheat.h"
#endif

using std::cout;
using std::cerr;
using std::endl;
/*
 *	Sockets, etc.
 */
extern int xfd;				// X-windows fd.
int listen_socket = -1;			// Listen here for map-editor.
int client_socket = -1;			// Socket to the map-editor.
int highest_fd = -1;			// Largest fd + 1.

#ifdef __sun__
// Solaris doesn't know PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif

/*
 *	Set the 'highest_fd' value to 1 + <largest fd>.
 */
inline void Set_highest_fd
	(
	)
	{
	highest_fd = xfd;		// Figure highest to listen to.
	if (listen_socket > highest_fd)
		highest_fd = listen_socket;
	if (client_socket > highest_fd)
		highest_fd = client_socket;
	highest_fd++;			// Select wants 1+highest.
	}

/*
 *	Initialize server for map-editing.  Call this AFTER xfd has been set.
 */

void Server_init
	(
	)
	{
#ifndef WIN32
	// Get location of socket file.
	std::string servename = get_system_path("<GAMEDAT>/exultserver");
	// Make sure it isn't there.
	unlink(servename.c_str());
#if HAVE_GETADDRINFOX
	// Don't use the old deprecated network API
	int r;
	struct addrinfo hints,*ai;

	memset(&hints,0,sizeof(hints));
	hints.ai_flags=AI_PASSIVE;
	r=getaddrinfo(	0,
			servename.c_str(),
			&hints,
			&ai);
	if(r!=0)
		{
		cerr << "getaddrinfo(): "<<gai_strerror(r) << endl;
		return;
		}

	listen_socket = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
#else
	// Deprecated
	listen_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
#endif
	if (listen_socket < 0)
		perror("Failed to open map-editor socket");
	else 
		{
#if HAVE_GETADDRINFOX
		if(bind(listen_socket,ai->ai_addr,ai->ai_addrlen) == -1 ||
			listen(listen_socket,1) == -1)
#else
		struct sockaddr_un addr;
		addr.sun_family = AF_UNIX;
		strcpy(addr.sun_path, servename.c_str());
		if (bind(listen_socket, (struct sockaddr *) &addr, 
		      sizeof(addr.sun_family) + strlen(addr.sun_path)) == -1 ||
		    listen(listen_socket, 1) == -1)
#endif
			{
			perror("Bind or listen on socket failed");
			close(listen_socket);
			listen_socket = -1;
			}
		else			// Set to be non-blocking.
			{
			cout << "Listening on socket " << listen_socket
								<< endl;
			fcntl(listen_socket, F_SETFL, 
				fcntl(listen_socket, F_GETFL) | O_NONBLOCK);
			}
#if HAVE_GETADDRINFOX
		freeaddrinfo(ai);
#endif
		}
	Set_highest_fd();
#else

	listen_socket = client_socket = -1;

	std::string servename = get_system_path("<STATIC>/");

	if (Exult_server::create_pipe(servename.c_str())) listen_socket = 1;

#endif
	}

/*
 *	Close the server.
 */

void Server_close
	(
	)
	{
#ifdef WIN32
	Exult_server::close_pipe();
	listen_socket = client_socket = -1;
#else
	// unlink socket file+++++++
#endif
	}

/*
 *	A message from a client is available, so handle it.
 */

static void Handle_client_message
	(
	int& fd				// Socket to client.  May be closed.
	)
	{
	unsigned char data[Exult_server::maxlength];
	Exult_server::Msg_type id;
	int datalen = Exult_server::Receive_data(fd, id, data, sizeof(data));
	if (datalen < 0)
		return;
	unsigned char *ptr = &data[0];
	Game_window *gwin = Game_window::get_game_window();
	switch (id)
		{
	case Exult_server::obj:
		Game_object::update_from_studio(&data[0], datalen);
		break;
	case Exult_server::egg:
		Egg_object::update_from_studio(&data[0], datalen);
		break;
	case Exult_server::npc:
		Actor::update_from_studio(&data[0], datalen);
		break;
	case Exult_server::num_npcs:
		{
		unsigned char data[16];
		unsigned char *ptr = &data[0];
		Write2(ptr, gwin->get_num_npcs());
		Exult_server::Send_data(client_socket, Exult_server::num_npcs, data,
							ptr - data);
		break;
		}
	case Exult_server::write_map:
		gwin->write_map();	// Send feedback?+++++
		break;
	case Exult_server::read_map:
		gwin->read_map();
		break;
	case Exult_server::map_editing_mode:
		{
		int onoff = Read2(ptr);
		if ((onoff != 0) != cheat.in_map_editor())
			cheat.toggle_map_editor();
		break;
		}
	case Exult_server::tile_grid:
		{
		int onoff = Read2(ptr);
		if ((onoff != 0) != cheat.show_tile_grid())
			cheat.toggle_tile_grid();
		break;
		}
	case Exult_server::edit_lift:
		{
		int lift = Read2(ptr);
		cheat.set_edit_lift(lift);
		break;
		}
	case Exult_server::reload_usecode:
		gwin->reload_usecode();
		break;
	case Exult_server::locate_terrain:
		{
		int tnum = Read2(ptr);
		int cx = (short) Read2(ptr);
		int cy = (short) Read2(ptr);
		bool up = *ptr++ ? true : false;
		bool okay = gwin->get_map()->locate_terrain(tnum, cx, cy, up);
		ptr = &data[2];		// Set back reply.
		Write2(ptr, cx);
		Write2(ptr, cy);
		ptr++;			// Skip 'up' flag.
		*ptr++ = okay ? 1 : 0;
		Exult_server::Send_data(client_socket, Exult_server::locate_terrain, data,
							ptr - data);
		break;
		}
	case Exult_server::swap_terrain:
		{
		int tnum = Read2(ptr);
		bool okay = gwin->get_map()->swap_terrains(tnum);
		*ptr++ = okay ? 1 : 0;
		Exult_server::Send_data(client_socket, Exult_server::swap_terrain, data,
							ptr - data);
		break;
		}
	case Exult_server::insert_terrain:
		{
		int tnum = (short) Read2(ptr);
		bool dup = *ptr++ ? true : false;
		bool okay = gwin->get_map()->insert_terrain(tnum, dup);
		*ptr++ = okay ? 1 : 0;
		Exult_server::Send_data(client_socket, 
			Exult_server::insert_terrain, data, ptr - data);
		break;
		}
	case Exult_server::send_terrain:
		{			// Send back #, total, 512-bytes data.
		int tnum = (short) Read2(ptr);
		Write2(ptr, gwin->get_map()->get_num_chunk_terrains());
		Chunk_terrain *ter = gwin->get_map()->get_terrain(tnum);
		ter->write_flats(ptr);	// Serialize it.
		ptr += 512;		// I just happen to know the length...
		Exult_server::Send_data(client_socket, 
			Exult_server::send_terrain, data, ptr - data);
		break;
		}
	case Exult_server::terrain_editing_mode:
		{			// 1=on, 0=off, -1=undo.
		int onoff = (short) Read2(ptr);
					// skip_lift==0 <==> terrain-editing.
		gwin->skip_lift = onoff == 1 ? 0 : 16;
		static char *msgs[3] = {"Terrain-Editing Aborted",
					"Terrain-Editing Done",
					"Terrain-Editing Enabled"};
		if (onoff == 0)		// End/commit.
			gwin->get_map()->commit_terrain_edits();
		else if (onoff == -1)
			gwin->get_map()->abort_terrain_edits();
		if (onoff >= -1 && onoff <= 1)
			gwin->center_text(msgs[onoff + 1]);
		gwin->set_all_dirty();
		break;
		}
	case Exult_server::set_edit_shape:
		{
		int shnum = (short) Read2(ptr);
		int frnum = (short) Read2(ptr);
		cheat.set_edit_shape(shnum, frnum);
		break;
		}
	case Exult_server::view_pos:
		{
		int tx = Read4(ptr);
		if (tx == -1)		// This is a query?
			{
			gwin->send_location();
			break;
			}
		int ty = Read4(ptr);
		// +++Later int txs = Read4(ptr);
		// int tys = Read4(ptr);
		// int scale = Read4(ptr);
					// Only set if chunk changed.
		if (tx/c_tiles_per_chunk != 
			gwin->get_scrolltx()/c_tiles_per_chunk || 
		    ty/c_tiles_per_chunk != 
			gwin->get_scrollty()/c_tiles_per_chunk)
			{
			gwin->set_scrolls(tx, ty);
			gwin->set_all_dirty();
			}
		break;
		}
		}
	}

/*
 *	Delay for a fraction of a second, or until there's data available.
 *	If a server request comes, it's handled here.
 */

void Server_delay
	(
	)
	{
#ifndef WIN32
	extern int xfd;
	fd_set rfds;
	struct timeval timer;
	timer.tv_sec = 0;
	timer.tv_usec = 50000;		// Try 1/50 second.
	FD_ZERO(&rfds);
	FD_SET(xfd, &rfds);
	if (listen_socket >= 0)
		FD_SET(listen_socket, &rfds);
	if (client_socket >= 0)
		FD_SET(client_socket, &rfds);
					// Wait for timeout or event.
	if (select(highest_fd, &rfds, 0, 0, &timer) > 0)
		{			// Something's come in.
		if (listen_socket >= 0 && FD_ISSET(listen_socket, &rfds))
			{		// New client connection.
					// For now, just one at a time.
			if (client_socket >= 0)
				close(client_socket);
			client_socket = accept(listen_socket, 0, 0);
			cout << "Accept returned client_socket = " <<
							client_socket << endl;
					// Non-blocking.
			fcntl(client_socket, F_SETFL, 
				fcntl(client_socket, F_GETFL) | O_NONBLOCK);
			Set_highest_fd();
			}
		if (client_socket >= 0 && FD_ISSET(client_socket, &rfds))
			{
			Handle_client_message(client_socket);
					// Client gone?
			if (client_socket == -1)
				Set_highest_fd();
			}
		}
#else
	if (listen_socket == -1) return;

	if (Exult_server::is_broken()) {
		std::cout << "Client disconneted." << endl;
		Exult_server::disconnect_from_client();
		Exult_server::setup_connect();
		client_socket = -1;
	}

	SleepEx(20, TRUE);

	if (client_socket == -1) {
		// If 9x, only do this in map edit mode
		if (Exult_server::is_win9x() && !cheat.in_map_editor()) return;

		std::string servename = get_system_path("<STATIC>/");
		if (!Exult_server::try_connect_to_client(servename.c_str())) return;
		else client_socket = 1;
		std::cout << "Connected to client" << endl;
	}

	if (Exult_server::peek_pipe() > 0)
		Handle_client_message(client_socket);
		
	if (Exult_server::is_broken()) {
		if (Exult_server::notify_connection_lost()) std::cout << "Client disconneted." << endl;
		Exult_server::disconnect_from_client();
		Exult_server::setup_connect();
		client_socket = -1;
	}
#endif

	}

#endif