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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
// Copyright (C) 2003-2009 Dolphin Project.
// 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, version 2.0.
// 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "NetWindow.h"
//--------------------------------
// GUI EVENTS
//--------------------------------
void NetEvent::AppendText(const wxString text)
{
// I have the feeling SendEvent may be a bit safer/better...
#if 1
SendEvent(ADD_TEXT, std::string(text.mb_str()));
#else
wxMutexGuiEnter();
m_netptr->AppendText(text);
wxMutexGuiLeave();
#endif
}
void NetEvent::SendEvent(int EventType, const std::string text, int integer)
{
wxCommandEvent event(wxEVT_HOST_COMMAND, wxID_ANY);
event.SetId( EventType );
event.SetInt( integer );
event.SetString( wxString::FromAscii(text.c_str()) );
m_netptr->AddPendingEvent(event);
}
//--------------------------------
// SERVER SIDE THREAD
//--------------------------------
ServerSide::ServerSide(NetPlay* netptr, sf::SocketTCP socket, sf::SocketUDP socketUDP, int netmodel, std::string nick)
: wxThread()
{
m_numplayers = 0;
m_data_received = false;
m_netmodel = netmodel;
m_socket = socket;
m_socketUDP = socketUDP;
m_netptr = netptr;
m_nick = nick;
Event = new NetEvent(m_netptr);
}
char ServerSide::GetSocket(sf::SocketTCP Socket)
{
for (int i=0; i < m_numplayers; i++)
{
if(m_client[i].socket == Socket)
return i;
}
return 0xE;
}
bool ServerSide::RecvT(sf::SocketUDP Socket, char * Data, size_t Max, size_t& Recvd, float Time)
{
sf::SelectorUDP Selector;
sf::IPAddress Addr;
Selector.Add(Socket);
if (Selector.Wait(Time) > 0)
{
Socket.Receive(Data, Max, Recvd, Addr);
return true;
}
else
{
return false;
}
}
void *ServerSide::Entry()
{
// Add listening socket
m_selector.Add(m_socket);
while (1)
{
char nbSocketReady = m_selector.Wait(0.5);
for (char i = 0; i < nbSocketReady; ++i)
{
m_CriticalSection.Enter();
sf::SocketTCP Socket = m_selector.GetSocketReady(i);
if (Socket == m_socket)
{
// Incoming connection
Event->AppendText(_("*Connection Request... "));
sf::SocketTCP Incoming;
sf::IPAddress Address;
m_socket.Accept(Incoming, &Address);
unsigned char sent = 0x12;
if ((m_netmodel == 0 && m_numplayers > 0) || m_numplayers == 3)
{
Incoming.Send((const char *)&sent, 1); // Tell it the server is full...
Incoming.Close(); // Then close the connection
Event->AppendText(_(" Server is Full !\n"));
}
else
{
Event->AppendText(_(" Connection accepted\n"));
m_client[m_numplayers].socket = Incoming;
m_client[m_numplayers].address = Address;
if (SyncValues(m_numplayers, Address))
{
// Add it to the selector
m_selector.Add(Incoming);
Event->SendEvent(HOST_NEWPLAYER);
m_numplayers++;
}
else
{
Event->AppendText(_("ERROR : Unable to establish UDP connection !\n"));
Incoming.Close();
}
}
}
else
{
unsigned char recv;
int socket_nb;
size_t recv_size;
sf::Socket::Status recv_status;
socket_nb = GetSocket(Socket);
if ((recv_status = Socket.Receive((char *)&recv, 1, recv_size)) == sf::Socket::Done)
{
#ifdef NET_DEBUG
char recv_str[32];
sprintf(recv_str, "received: 0x%02x | %c\n", recv, recv);
Event->AppendText(wxString::FromAscii(recv_str));
#endif
OnServerData(socket_nb, recv);
}
else
{
if (recv_status == sf::Socket::Disconnected)
{
Event->SendEvent(HOST_PLAYERLEFT);
m_numplayers--;
std::string player_left = m_client[socket_nb].nick;
Event->AppendText( wxString::Format(wxT("*Player : %s left the game.\n\n"),
player_left.c_str()) );
// We need to adjust the struct...
for (int j = socket_nb; j < m_numplayers; j++)
{
m_client[j].socket = m_client[j+1].socket;
m_client[j].nick = m_client[j+1].nick;
m_client[j].ready = m_client[j+1].ready;
}
// Send disconnected message to all
unsigned char send = 0x11;
unsigned int str_size = (int)player_left.size();
for (int j=0; j < m_numplayers ; j++)
{
m_client[j].socket.Send((const char*)&send, 1);
m_client[j].socket.Send((const char*)&str_size, 4);
m_client[j].socket.Send(player_left.c_str(), (int)str_size + 1);
}
}
else
{
// Hopefully this should never happen, the client is not
// Even warned that he is being dropped...
Event->SendEvent(HOST_ERROR, m_client[socket_nb].nick);
}
m_selector.Remove(Socket);
Socket.Close();
}
}
m_CriticalSection.Leave();
}
if(TestDestroy())
{
// Stop listening
m_socket.Close();
// Delete the Thread and close clients sockets
for (int i=0; i < m_numplayers ; i++)
m_client[i].socket.Close();
break;
}
}
return NULL;
}
bool ServerSide::SyncValues(unsigned char socketnb, sf::IPAddress Address)
{
sf::SocketTCP Socket = m_client[socketnb].socket;
std::string buffer_str = m_netptr->GetSelectedGame();
char *buffer = NULL;
unsigned char init_number;
u32 buffer_size = (u32)buffer_str.size();
size_t received;
bool errorUDP = false;
// First, Send the number of connected clients & netmodel
Socket.Send((const char *)&m_numplayers, 1);
Socket.Send((const char *)&m_netmodel, 4);
// Send the Game String
Socket.Send((const char *)&buffer_size, 4);
Socket.Send(buffer_str.c_str(), buffer_size + 1);
// Send the host Nickname
buffer_size = (u32)m_nick.size();
Socket.Send((const char *)&buffer_size, 4);
Socket.Send(m_nick.c_str(), buffer_size + 1);
// Read client's UDP Port
Socket.Receive((char *)&m_client[m_numplayers].port, sizeof(short), received);
// Read returned nickname
Socket.Receive((char *)&buffer_size, 4, received);
buffer = new char[buffer_size + 1];
Socket.Receive(buffer, buffer_size + 1, received);
m_client[socketnb].nick = std::string(buffer);
m_client[socketnb].ready = false;
///////////////////
// Test UDP Socket
///////////////////
if (m_socketUDP.Send((const char*)&m_numplayers, 1, Address, m_client[m_numplayers].port) == sf::Socket::Done)
{
// Test UDP Socket Receive, 2s timeout
if (!RecvT(m_socketUDP, (char*)&init_number, 1, received, 2))
errorUDP = true;
}
else
errorUDP = true;
// Check if the client has the game
Socket.Receive((char *)&init_number, 1, received);
delete[] buffer;
if (!errorUDP)
{
// Send to all connected clients
if (m_numplayers > 0)
{
unsigned char send = 0x10;
buffer_size = (int)m_client[socketnb].nick.size();
for (int i=0; i < m_numplayers ; i++)
{
// Do not send to connecting player
if (i == socketnb)
continue;
m_client[i].socket.Send((const char *)&send, 1); // Init new connection
m_client[i].socket.Send((const char *)&init_number, 1); // Send Game found ?
m_client[i].socket.Send((const char *)&buffer_size, 4); // Send client nickname
m_client[i].socket.Send(m_client[socketnb].nick.c_str(), buffer_size + 1);
}
}
Event->AppendText( wxString::Format(wxT("*Connection established to %s (%s:%d)\n"),
m_client[socketnb].nick.c_str(), Address.ToString().c_str(), m_client[m_numplayers].port) );
if (init_number != 0x1F) // Not Found
for (int i = 0; i < 4; i++)
Event->AppendText(_("WARNING : Game Not Found on Client Side !\n"));
// UDP connecton successful
init_number = 0x16;
Socket.Send((const char *)&init_number, 1);
}
else // UDP Error, disconnect client
{
// UDP connecton failed
init_number = 0x17;
Socket.Send((const char *)&init_number, 1);
return false;
}
return true;
}
void ServerSide::Write(int socknb, const char *data, size_t size, long *ping)
{
wxCriticalSectionLocker lock(m_CriticalSection);
if (ping != NULL)
{
// Ask for ping
unsigned char value = 0x15;
size_t recv_size;
u32 four_bytes = 0x101A7FA6;
Common::Timer timer;
timer.Start();
for (int i=0; i < m_numplayers ; i++)
{
m_client[i].socket.Send((const char*)&value, 1);
timer.Update();
m_client[i].socket.Send((const char*)&four_bytes, 4);
m_client[i].socket.Receive((char*)&four_bytes, 4, recv_size);
ping[i] = (long)timer.GetTimeDifference();
}
return;
}
// Send the data safely, without intefering with another call
m_client[socknb].socket.Send(data, size);
}
void ServerSide::WriteUDP(int socknb, const char *data, size_t size)
{
wxCriticalSectionLocker lock(m_CriticalSection);
m_socketUDP.Send(data, size, m_client[socknb].address, m_client[socknb].port);
}
//--------------------------------
// CLIENT SIDE THREAD
//--------------------------------
ClientSide::ClientSide(NetPlay* netptr, sf::SocketTCP socket, sf::SocketUDP socketUDP, std::string addr, std::string nick)
: wxThread()
{
m_numplayers = 0;
m_data_received = false;
m_netmodel = 0;
m_socket = socket;
m_socketUDP = socketUDP;
m_port = m_socketUDP.GetPort();
m_netptr = netptr;
m_nick = nick;
m_addr = addr;
Event = new NetEvent(m_netptr);
}
bool ClientSide::RecvT(sf::SocketUDP Socket, char * Data, size_t Max, size_t& Recvd, float Time)
{
sf::SelectorUDP Selector;
sf::IPAddress addr;
Selector.Add(Socket);
if (Selector.Wait(Time) > 0)
{
Socket.Receive(Data, Max, Recvd, addr);
return true;
}
else
{
return false;
}
}
void *ClientSide::Entry()
{
Event->AppendText(_("*Connection Request... "));
// If we get here, the connection is already accepted, however the game may be full
if (SyncValues())
{
// Tell the server if the client has the game
CheckGameFound();
// Check UDP connection
unsigned char value;
size_t val_sz;
m_socket.Receive((char *)&value, 1, val_sz);
if (value == 0x16) // UDP connection successful
{
Event->AppendText(_("Connection successful !\n"));
Event->AppendText( wxString::Format(wxT("*Connection established to %s (%s)\n*Game is : %s\n"),
m_hostnick.c_str(), m_addr.c_str(), m_selectedgame.c_str() ) );
}
else
{
Event->AppendText(_("UDP Connection FAILED !\n"
"ERROR : Unable to establish UDP Connection, please Check UDP Port forwarding !"));
m_socket.Close();
Event->SendEvent(HOST_ERROR);
return NULL;
}
}
else // Server is Full
{
m_socket.Close();
Event->SendEvent(HOST_FULL);
return NULL;
}
m_netptr->ChangeSelectedGame(m_selectedgame);
Event->SendEvent(HOST_NEWPLAYER, "NULL", m_netmodel);
Event->SendEvent(GUI_UPDATE);
m_selector.Add(m_socket);
while (1)
{
unsigned char recv;
size_t recv_size;
sf::Socket::Status recv_status;
// we use a selector because of the useful timeout
if (m_selector.Wait(0.5) > 0)
{
m_CriticalSection.Enter();
if ((recv_status = m_socket.Receive((char *)&recv, 1, recv_size)) == sf::Socket::Done)
{
#ifdef NET_DEBUG
char recv_str[32];
sprintf(recv_str, "received: 0x%02x | %c\n", recv, recv);
Event->AppendText(wxString::FromAscii(recv_str));
#endif
OnClientData(recv);
}
else
{
if (recv_status == sf::Socket::Disconnected)
{
Event->SendEvent(HOST_DISCONNECTED);
}
else
{
Event->SendEvent(HOST_ERROR);
}
m_selector.Remove(m_socket);
m_socket.Close();
return NULL;
}
m_CriticalSection.Leave();
}
if(TestDestroy())
{
m_socket.Close();
break;
}
}
return NULL;
}
bool ClientSide::SyncValues()
{
unsigned int buffer_size = (int)m_nick.size();
unsigned char byterecv;
bool errorUDP;
char *buffer = NULL;
size_t recv_size;
unsigned short server_port;
std::string host = m_addr.substr(0, m_addr.find(':'));
TryParseInt(m_addr.substr(m_addr.find(':')+1).c_str(), (int *)&server_port);
// First, Read the init number : nbplayers (0-2) or server full (0x12)
m_socket.Receive((char *)&m_numplayers, 1, recv_size);
if (m_numplayers == 0x12)
return false;
m_socket.Receive((char *)&m_netmodel, 4, recv_size);
// Send client's UDP Port
m_socket.Send((const char *)&m_port, sizeof(short));
// Send client's nickname
m_socket.Send((const char *)&buffer_size, 4);
m_socket.Send(m_nick.c_str(), buffer_size + 1);
// Read the Game String
m_socket.Receive((char *)&buffer_size, 4, recv_size);
buffer = new char[buffer_size + 1];
m_socket.Receive(buffer, buffer_size + 1, recv_size);
m_selectedgame = std::string(buffer);
// Read the host Nickname
m_socket.Receive((char *)&buffer_size, 4, recv_size);
buffer = new char[buffer_size + 1];
m_socket.Receive(buffer, buffer_size + 1, recv_size);
m_hostnick = std::string(buffer);
///////////////////
// Test UDP Socket
///////////////////
if (m_socketUDP.Send((const char*)&m_numplayers, 1, host.c_str(), server_port) == sf::Socket::Done)
{
// Test UDP Socket Receive, 2s timeout
if (!RecvT(m_socketUDP, (char*)&byterecv, 1, recv_size, 2))
errorUDP = true;
}
else
errorUDP = true;
delete[] buffer;
return true;
}
void ClientSide::CheckGameFound()
{
unsigned char send_value;
// Check if the game selected by Host is in Client's Game List
m_netptr->IsGameFound(&send_value, m_selectedgame);
if (send_value == 0x1F) // Found
{
m_socket.Send((const char *)&send_value, 1);
}
else
{
m_socket.Send((const char *)&send_value, 1);
for (int i = 0; i < 2; i++)
Event->AppendText(_("WARNING : You do not have the Selected Game !\n"));
}
}
void ClientSide::Write(const char *data, size_t size, long *ping)
{
wxCriticalSectionLocker lock(m_CriticalSection);
if (ping != NULL)
{
// Ask for ping
unsigned char value = 0x15;
size_t recv_size;
int four_bytes = 0x101A7FA6;
Common::Timer timer;
timer.Start();
m_socket.Send((const char*)&value, 1);
m_socket.Send((const char*)&four_bytes, 4);
m_socket.Receive((char*)&four_bytes, 4, recv_size);
*ping = (long)timer.GetTimeElapsed();
return;
}
m_socket.Send(data, size);
}
void ClientSide::WriteUDP(const char *data, size_t size)
{
wxCriticalSectionLocker lock(m_CriticalSection);
unsigned short server_port;
std::string host = m_addr.substr(0, m_addr.find(':'));
TryParseInt(m_addr.substr(m_addr.find(':')+1).c_str(), (int *)&server_port);
m_socketUDP.Send(data, size, host.c_str(), server_port);
}
|