File: netsession_client.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (433 lines) | stat: -rw-r--r-- 10,246 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
/*
	$Id: netsession_client.cpp,v 1.12 2001/11/01 20:27:57 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#ifdef WIN32
#pragma warning (disable:4786)
#endif

#include <API/Core/System/error.h>
#include <API/Core/System/mutex.h>
#include <API/Network/netcomputer.h>
#include <API/Network/netgroup.h>
#include <API/Network/netmessage.h>
#include <API/Core/System/error.h>
#include <API/Core/System/thread.h>
#include <API/Core/System/cl_assert.h>
#include <API/Core/System/system.h>
#include <Network/Generic/network_delivery_impl.h>
#include <Network/Generic/netsession_client.h>
#include <Network/Generic/netsession_generic.h>
#include <Network/Generic/network_generic.h>
#include <Core/IOData/Generic/outputsource_memory_generic.h>
#include <Core/IOData/Generic/inputsource_memory_generic.h>

CL_NetSession_Client::CL_NetSession_Client(
	int ip_addr,
	int port,
	const std::string &game_id,
	CL_ConnectionProvider *provider)
: CL_NetSession_Generic(provider)
{
	mutex = CL_Mutex::create();

	tcp_connection = provider->create_tcp_connection(
		ip_addr,
		port);

	if (tcp_connection == NULL)
	{
		throw CL_Error("Could not connect to host.");
	}

	udp_connection = NULL; // still no support for udp in client.

	// client code currently only knows about the server.
	server = CL_NetComputer(new CL_NetComputer_Server(this));
	all.add(server);

	// do "hello to you too" handshake.
	CL_ConnectionPacket p = tcp_connection->receive();
	while (p.size == 0) // wait until we get a hello packet.
	{
		provider->wait_for_connection_data(mutex);
		p = tcp_connection->receive();
	}
	
	CL_InputSource_MemoryGeneric input(p.data, p.size, true);
	if (input.read_int32() != Packet_Hello)
	{
		throw CL_Error("Protocol error. Didn't get a Hello.");
	}
	our_id = input.read_int32();

	int size = input.read_int32();
	if (size > 1000)
		throw CL_Error("Protocol error. Game ID size above 1000.");

	char *str = new char[size+1];
	str[size] = 0; // null-terminate.
	input.read(str, size);
	
	if (game_id != str) // not same game_id. probably an other clanlib game.
	{
		delete[] str;
		throw CL_Error("Wrong netsession id.");
	}

	delete[] str;
	
	// say "HelloToYouToo" to officially join the netsession:
	CL_OutputSource_MemoryGeneric output;
	output.write_int32(Packet_Hello_ToYouToo);
	tcp_connection->send(
		CL_ConnectionPacket(
			output.get_data(),
			output.size()));

	exit_thread = false;
	thread = CL_Thread::create(this);
	thread->start();
	thread->set_priority(cl_priority_highest); // we need more power on the warp engines!
}

CL_NetSession_Client::~CL_NetSession_Client()
{
	exit_thread = true;
	thread->wait();
	delete thread;

	delete tcp_connection;
	delete udp_connection;

	// clean up connections to clients:
	for (
		std::list<CL_NetChannelQueue_Client*>::iterator it = netchannels.begin();
		it != netchannels.end();
		it++)
	{
		delete *it;
	}
/*
	should be enabled when the client knows about other clients...

	while (leave_queue.empty() == false)
	{
		delete leave_queue.pop();
	}
*/
	delete mutex;
}

CL_NetComputer &CL_NetSession_Client::get_server()
{
	return server;
}

CL_NetGroup &CL_NetSession_Client::get_all()
{
	return all;
}

bool CL_NetSession_Client::peek(int channel) const
{
	CL_MutexSection mutex_section(mutex);

	CL_NetChannelQueue_Client *queue = find_queue(channel);
	if (queue != NULL)
	{
		return !queue->empty();
	}

	return false;
}

CL_NetMessage CL_NetSession_Client::receive(int channel, int timeout)
{
	CL_MutexSection mutex_section(mutex);

	CL_NetChannelQueue_Client *queue = create_queue(channel);

	while (queue->empty()) 
	{
		int s = timeout;
		if (timeout < 0 || timeout > 20) s = 20;
		mutex->leave();
		CL_System::sleep(s);
		mutex->enter();

		if (timeout != -1)
		{
			timeout -= s;
			if (timeout <= 0) break;
		}
	}

	if (queue->empty()) throw CL_Error("No message to receive!");
	CL_NetMessage msg = queue->front(); queue->pop();
	check_trigger();
	return msg;
}

void CL_NetSession_Client::send(
	const int dest_channel,
	const CL_NetGroup &dest,
	const CL_NetMessage &message,
	bool reliable /*= true*/)
{
	CL_MutexSection mutex_section(mutex);

	static bool warning = true;
	if (warning && reliable == false)
	{
		cl_info(info_network, "cannot send data unreliable (udp): not implemented yet!");
		warning = false;
	}
/*	
	CL_NetChannelQueue_Client *queue = find_queue(dest_channel);

	if (queue == NULL)
		throw CL_Error("No write access to netchannel.");

	if ((queue->access & ACCESS_CHANNEL_WRITE) != ACCESS_CHANNEL_WRITE)
		throw CL_Error("No write access to netchannel.");
*/
	// permissions ok, send message:
	CL_OutputSource_MemoryGeneric output;
	output.write_int32(Packet_NetChannel_Message_ToServer);
	output.write_int32(dest_channel);

	// todo: write the computer id's on destination computers here.

	output.write_int32(message.data.size());
	output.write(message.data.data(), message.data.size());

	CL_ConnectionPacket packet;
	packet.size = output.size();
	packet.data = output.get_data();

	tcp_connection->send(packet);
}

CL_NetComputer CL_NetSession_Client::receive_computer_leave()
{
	throw CL_Error("Computer leave queue empty.");
}

CL_NetComputer CL_NetSession_Client::receive_computer_join()
{
	throw CL_Error("Computer join queue empty.");
}

CL_NetComputer CL_NetSession_Client::receive_computer_rejoin()
{
	throw CL_Error("Computer rejoin queue empty.");
}

bool CL_NetSession_Client::receive_session_closed()
{
	CL_MutexSection mutex_section(mutex);
	return tcp_connection->connection_lost();
}

int CL_NetSession_Client::access_status(int channel) const
{
	CL_MutexSection mutex_section(mutex);

	CL_NetChannelQueue_Client *queue = find_queue(channel);
	return (queue == NULL) ? 0 : queue->access;
}

bool CL_NetSession_Client::is_writable(int channel) const
{
	CL_MutexSection mutex_section(mutex);

	CL_NetChannelQueue_Client *queue = find_queue(channel);
	if (queue == NULL) return false;

	return (queue->access & ACCESS_CHANNEL_WRITE) == ACCESS_CHANNEL_WRITE;
}

bool CL_NetSession_Client::is_readable(int channel) const
{
	CL_MutexSection mutex_section(mutex);

	CL_NetChannelQueue_Client *queue = find_queue(channel);
	if (queue == NULL) return false;

	return (queue->access & ACCESS_CHANNEL_READ) == ACCESS_CHANNEL_READ;
}

// Client side only:
int CL_NetSession_Client::receive_access_changed()
{
	CL_MutexSection mutex_section(mutex);

	if (access_queue.empty()) return -1;
	int id = access_queue.front()->channel_id;
	access_queue.pop();
	check_trigger();
	return id;
}

// Server side only:
void CL_NetSession_Client::set_access(
	int channel,
	const CL_NetComputer &computer,
	int access_rights)
{
	throw CL_Error("We are not the network server. Cannot change access.");
}

void CL_NetSession_Client::set_access(
	int channel,
	const CL_NetGroup &group,
	int access_rights)
{
	throw CL_Error("We are not the network server. Cannot change access.");
}

void CL_NetSession_Client::keep_alive()
{
	CL_MutexSection mutex_section(mutex);

	if (tcp_connection->connection_lost())
	{
		provider->remove_connection(tcp_connection);
		return;
	}

	while (tcp_connection->peek())
	{
		if (exit_thread) return;

		CL_ConnectionPacket msg = tcp_connection->receive();
			
		CL_InputSource_MemoryGeneric input(msg.data, msg.size, true);
		switch (input.read_int32())
		{
		case Packet_NetChannel_AccessChange:
			{
				CL_NetChannelQueue_Client *queue = create_queue(input.read_int32());
				queue->access = input.read_int32();
				access_queue.push(queue);
				trigger.set_flag();
			}
			break;
		
		case Packet_NetChannel_Message_ToClient:
			{
				CL_NetChannelQueue_Client *queue = create_queue(input.read_int32());
				CL_NetMessage game_msg;
				int size = input.read_int32();
				char *data = new char[size];
				game_msg.from = server;
				input.read(data, size);
				game_msg.data.append(data, size);
				queue->push(game_msg);
				trigger.set_flag();
			}
			break;

		default:
			cl_info(info_network, "Network Protocol error!");
			cl_assert(false);
		}
	}
}

CL_NetChannelQueue_Client *CL_NetSession_Client::find_queue(int netchannel) const
{
	CL_MutexSection mutex_section(mutex);

	// Note: This really should be a STL map, not a list. Too lazy to fix it now -- mbn.
	for (
		std::list<CL_NetChannelQueue_Client*>::const_iterator it = netchannels.begin();
		it != netchannels.end();
		it++)
	{
		if ((*it)->channel_id == netchannel) return *it;
	}

	return NULL;
}

CL_NetChannelQueue_Client *CL_NetSession_Client::create_queue(int netchannel)
{
	CL_MutexSection mutex_section(mutex);

	CL_NetChannelQueue_Client *found = find_queue(netchannel);
	if (found != NULL) return found;
	
	CL_NetChannelQueue_Client *c = new CL_NetChannelQueue_Client;
	c->channel_id = netchannel;
	c->access = 0;

	netchannels.push_back(c);

	return c;
}

void CL_NetSession_Client::run()
{
	while (exit_thread == false)
	{
		keep_alive();
		provider->wait_for_connection_data(mutex);
	}
}

void CL_NetSession_Client::check_trigger()
{
	bool queues_empty = access_queue.empty();

	std::list<CL_NetChannelQueue_Client*>::iterator it;
	for (it = netchannels.begin(); it != netchannels.end(); it++)
	{
		if (!(*it)->empty()) queues_empty = false;
	}

	if (queues_empty) trigger.reset();
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetComputer_Server

CL_NetComputer_Server::CL_NetComputer_Server(CL_NetSession_Client *session)
: CL_NetComputer_Generic(session)
{
}

CL_NetComputer_Server::~CL_NetComputer_Server()
{
}

unsigned long CL_NetComputer_Server::get_address() const
{
	return get_session()->tcp_connection->get_peer_address();
}

unsigned short CL_NetComputer_Server::get_port() const
{
	return get_session()->tcp_connection->get_peer_port();
}

void CL_NetComputer_Server::disconnect()
{
	get_session()->tcp_connection->disconnect();
}

CL_NetSession_Client *CL_NetComputer_Server::get_session() const
{
	return static_cast<CL_NetSession_Client*>(CL_NetComputer_Generic::get_session());
}