forked from 173210/snes9xTYL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netplay.cpp
1227 lines (1050 loc) · 33.1 KB
/
netplay.cpp
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com),
Jerremy Koot (jkoot@snes9x.com)
(c) Copyright 2002 - 2004 Matthew Kendora
(c) Copyright 2002 - 2005 Peter Bortas (peter@bortas.org)
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
(c) Copyright 2001 - 2006 John Weidman (jweidman@slip.net)
(c) Copyright 2002 - 2006 funkyass (funkyass@spam.shaw.ca),
Kris Bleakley (codeviolation@hotmail.com)
(c) Copyright 2002 - 2010 Brad Jorsch (anomie@users.sourceforge.net),
Nach (n-a-c-h@users.sourceforge.net),
(c) Copyright 2002 - 2011 zones (kasumitokoduck@yahoo.com)
(c) Copyright 2006 - 2007 nitsuja
(c) Copyright 2009 - 2011 BearOso,
OV2
BS-X C emulator code
(c) Copyright 2005 - 2006 Dreamer Nom,
zones
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 _Demo_ (_demo_@zsnes.com),
Nach,
zsKnight (zsknight@zsnes.com)
C4 C++ code
(c) Copyright 2003 - 2006 Brad Jorsch,
Nach
DSP-1 emulator code
(c) Copyright 1998 - 2006 _Demo_,
Andreas Naive (andreasnaive@gmail.com),
Gary Henderson,
Ivar (ivar@snes9x.com),
John Weidman,
Kris Bleakley,
Matthew Kendora,
Nach,
neviksti (neviksti@hotmail.com)
DSP-2 emulator code
(c) Copyright 2003 John Weidman,
Kris Bleakley,
Lord Nightmare (lord_nightmare@users.sourceforge.net),
Matthew Kendora,
neviksti
DSP-3 emulator code
(c) Copyright 2003 - 2006 John Weidman,
Kris Bleakley,
Lancer,
z80 gaiden
DSP-4 emulator code
(c) Copyright 2004 - 2006 Dreamer Nom,
John Weidman,
Kris Bleakley,
Nach,
z80 gaiden
OBC1 emulator code
(c) Copyright 2001 - 2004 zsKnight,
pagefault (pagefault@zsnes.com),
Kris Bleakley
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code used in 1.39-1.51
(c) Copyright 2002 Matthew Kendora with research by
zsKnight,
John Weidman,
Dark Force
SPC7110 and RTC C++ emulator code used in 1.52+
(c) Copyright 2009 byuu,
neviksti
S-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive,
John Weidman
S-RTC C emulator code
(c) Copyright 2001 - 2006 byuu,
John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather,
John Weidman,
Kris Bleakley,
Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 _Demo_,
pagefault,
zsKnight
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar,
Gary Henderson,
John Weidman
Sound emulator code used in 1.5-1.51
(c) Copyright 1998 - 2003 Brad Martin
(c) Copyright 1998 - 2006 Charles Bilyue'
Sound emulator code used in 1.52+
(c) Copyright 2004 - 2007 Shay Green (gblargg@gmail.com)
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se)
2xSaI filter
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
HQ2x, HQ3x, HQ4x filters
(c) Copyright 2003 Maxim Stepin (maxim@hiend3d.com)
NTSC filter
(c) Copyright 2006 - 2007 Shay Green
GTK+ GUI code
(c) Copyright 2004 - 2011 BearOso
Win32 GUI code
(c) Copyright 2003 - 2006 blip,
funkyass,
Matthew Kendora,
Nach,
nitsuja
(c) Copyright 2009 - 2011 OV2
Mac OS GUI code
(c) Copyright 1998 - 2001 John Stiles
(c) Copyright 2001 - 2011 zones
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com/
Permission to use, copy, modify and/or distribute Snes9x in both binary
and source form, for non-commercial purposes, is hereby granted without
fee, providing that this license information and copyright notice appear
with all copies and any derived work.
This software is provided 'as-is', without any express or implied
warranty. In no event shall the authors be held liable for any damages
arising from the use of this software or it's derivatives.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes,
but is not limited to, charging money for Snes9x or software derived from
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
using Snes9x as a promotion for your commercial product.
The copyright holders request that bug fixes and improvements to the code
should be forwarded to them so everyone can benefit from the modifications
in future versions.
Super NES and Super Nintendo Entertainment System are trademarks of
Nintendo Co., Limited and its subsidiary companies.
***********************************************************************************/
#ifdef NETPLAY_SUPPORT
#ifdef _DEBUG
#define NP_DEBUG 1
#endif
#define NP_DEBUG 3 // FF-FIXME
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <memory.h>
#include <sys/types.h>
#ifdef __WIN32__
#include <winsock.h>
#include <process.h>
#include "win32/wsnes9x.h"
#define ioctl ioctlsocket
#define close(h) if(h){closesocket(h);}
#define read(a,b,c) recv(a, b, c, 0)
#define write(a,b,c) send(a, b, c, 0)
#else
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef __SVR4
#include <sys/stropts.h>
#endif
#endif
#ifdef USE_THREADS
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#endif
#include "snes9x.h"
#include "memmap.h"
#include "netplay.h"
#include "snapshot.h"
#include "display.h"
void S9xNPClientLoop (void *);
bool8 S9xNPLoadROM (uint32 len);
bool8 S9xNPLoadROMDialog (const char *);
bool8 S9xNPGetROMImage (uint32 len);
void S9xNPGetSRAMData (uint32 len);
void S9xNPGetFreezeFile (uint32 len);
unsigned long START = 0;
bool8 S9xNPConnect ();
bool8 S9xNPConnectToServer (const char *hostname, int port,
const char *rom_name)
{
if (!S9xNPInitialise ())
return (FALSE);
S9xNPDisconnect ();
NetPlay.MySequenceNum = 0;
NetPlay.ServerSequenceNum = 0;
NetPlay.Connected = FALSE;
NetPlay.Abort = FALSE;
NetPlay.Player = 0;
NetPlay.Paused = FALSE;
NetPlay.PercentageComplete = 0;
NetPlay.Socket = 0;
if (NetPlay.ServerHostName)
free ((char *) NetPlay.ServerHostName);
NetPlay.ServerHostName = strdup (hostname);
if (NetPlay.ROMName)
free ((char *) NetPlay.ROMName);
NetPlay.ROMName = strdup (rom_name);
NetPlay.Port = port;
NetPlay.PendingWait4Sync = FALSE;
#ifdef __WIN32__
if (GUI.ClientSemaphore == NULL)
GUI.ClientSemaphore = CreateSemaphore (NULL, 0, NP_JOYPAD_HIST_SIZE, NULL);
if (NetPlay.ReplyEvent == NULL)
NetPlay.ReplyEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
_beginthread (S9xNPClientLoop, 0, NULL);
return (TRUE);
#endif
return S9xNPConnect();
}
bool8 S9xNPConnect ()
{
struct sockaddr_in address;
struct hostent *hostinfo;
unsigned int addr;
address.sin_family = AF_INET;
address.sin_port = htons (NetPlay.Port);
#ifdef NP_DEBUG
printf ("CLIENT: Looking up server's hostname (%s) @%ld\n", NetPlay.ServerHostName, S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Looking up server's hostname...");
if ((int) (addr = inet_addr (NetPlay.ServerHostName)) == -1)
{
if ((hostinfo = gethostbyname (NetPlay.ServerHostName)))
{
memcpy ((char *)&address.sin_addr, hostinfo->h_addr,
hostinfo->h_length);
}
else
{
S9xNPSetError ("\
Unable to look up server's IP address from hostname.\n\n\
Unknown hostname or may be your nameserver isn't set\n\
up correctly?");
return (FALSE);
}
}
else
{
memcpy ((char *)&address.sin_addr, &addr, sizeof (addr));
}
#ifdef NP_DEBUG
printf ("CLIENT: Creating socket @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Creating network socket...");
if ((NetPlay.Socket = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
S9xNPSetError ("Creating network socket failed.");
return (FALSE);
}
#ifdef NP_DEBUG
printf ("CLIENT: Trying to connect to server @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Trying to connect to Snes9X server...");
if (connect (NetPlay.Socket, (struct sockaddr *) &address, sizeof (address)) < 0)
{
char buf [100];
#ifdef __WIN32__
if (WSAGetLastError () == WSAECONNREFUSED)
#else
if (errno == ECONNREFUSED)
#endif
{
S9xNPSetError ("\
Connection to remote server socket refused:\n\n\
Is there actually a Snes9X NetPlay server running\n\
on the remote machine on this port?");
}
else
{
sprintf (buf, "Connection to server failed with error number %d",
#ifdef __WIN32__
WSAGetLastError ()
#else
errno
#endif
);
S9xNPDisconnect ();
}
return (FALSE);
}
NetPlay.Connected = TRUE;
#ifdef NP_DEBUG
printf ("CLIENT: Sending 'HELLO' message @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Sending 'HELLO' message...");
/* Send the server a HELLO packet*/
int len = 7 + 4 + strlen (NetPlay.ROMName) + 1;
uint8 *tmp = new uint8 [len];
uint8 *ptr = tmp;
*ptr++ = NP_CLNT_MAGIC;
*ptr++ = NetPlay.MySequenceNum++;
*ptr++ = NP_CLNT_HELLO;
WRITE_LONG (ptr, len);
ptr += 4;
#ifdef __WIN32__
uint32 ft = Settings.FrameTime;
WRITE_LONG (ptr, ft);
#else
WRITE_LONG (ptr, Settings.FrameTime);
#endif
ptr += 4;
strcpy ((char *) ptr, NetPlay.ROMName);
if (!S9xNPSendData (NetPlay.Socket, tmp, len))
{
S9xNPSetError ("Sending 'HELLO' message failed.");
S9xNPDisconnect ();
delete tmp;
return (FALSE);
}
delete tmp;
#ifdef NP_DEBUG
printf ("CLIENT: Waiting for 'WELCOME' reply from server @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Waiting for 'HELLO' reply from server...");
uint8 header [7];
if (!S9xNPGetData (NetPlay.Socket, header, 7) ||
header [0] != NP_SERV_MAGIC || header [1] != 0 ||
(header [2] & 0x1f) != NP_SERV_HELLO)
{
S9xNPSetError ("Error in 'HELLO' reply packet received from server.");
S9xNPDisconnect ();
return (FALSE);
}
#ifdef NP_DEBUG
printf ("CLIENT: Got 'WELCOME' reply @%ld\n", S9xGetMilliTime () - START);
#endif
len = READ_LONG (&header [3]);
if (len > 256)
{
S9xNPSetError ("Error in 'HELLO' reply packet received from server.");
S9xNPDisconnect ();
return (FALSE);
}
uint8 *data = new uint8 [len];
if (!S9xNPGetData (NetPlay.Socket, data, len - 7))
{
S9xNPSetError ("Error in 'HELLO' reply packet received from server.");
delete data;
S9xNPDisconnect ();
return (FALSE);
}
if (data [0] != NP_VERSION)
{
S9xNPSetError ("\
The Snes9X NetPlay server implements a different\n\
version of the protocol. Disconnecting.");
delete data;
S9xNPDisconnect ();
return (FALSE);
}
NetPlay.FrameCount = READ_LONG (&data [2]);
if (!(header [2] & 0x80) &&
strcmp ((char *) data + 4 + 2, NetPlay.ROMName) != 0)
{
if (!S9xNPLoadROMDialog ((char *) data + 4 + 2))
{
delete data;
S9xNPDisconnect ();
return (FALSE);
}
}
NetPlay.Player = data [1];
delete data;
NetPlay.PendingWait4Sync = TRUE;
Settings.NetPlay = TRUE;
S9xNPResetJoypadReadPos ();
NetPlay.ServerSequenceNum = 1;
#ifdef NP_DEBUG
printf ("CLIENT: Sending 'READY' to server @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Sending 'READY' to the server...");
return (S9xNPSendReady ((header [2] & 0x80) ?
NP_CLNT_WAITING_FOR_ROM_IMAGE :
NP_CLNT_READY));
}
bool8 S9xNPSendReady (uint8 op)
{
uint8 ready [7];
uint8 *ptr = ready;
*ptr++ = NP_CLNT_MAGIC;
*ptr++ = NetPlay.MySequenceNum++;
*ptr++ = op;
WRITE_LONG (ptr, 7);
ptr += 4;
if (!S9xNPSendData (NetPlay.Socket, ready, 7))
{
S9xNPDisconnect ();
S9xNPSetError ("Sending 'READY' message failed.");
return (FALSE);
}
return (TRUE);
}
bool8 S9xNPSendPause (bool8 paused)
{
#ifdef NP_DEBUG
printf ("CLIENT: Pause - %s @%ld\n", paused ? "YES" : "NO", S9xGetMilliTime () - START);
#endif
uint8 pause [7];
uint8 *ptr = pause;
*ptr++ = NP_CLNT_MAGIC;
*ptr++ = NetPlay.MySequenceNum++;
*ptr++ = NP_CLNT_PAUSE | (paused ? 0x80 : 0);
WRITE_LONG (ptr, 7);
ptr += 4;
if (!S9xNPSendData (NetPlay.Socket, pause, 7))
{
S9xNPSetError ("Sending 'PAUSE' message failed.");
S9xNPDisconnect ();
return (FALSE);
}
return (TRUE);
}
#ifdef __WIN32__
void S9xNPClientLoop (void *)
{
NetPlay.Waiting4EmulationThread = FALSE;
if (S9xNPConnect ())
{
S9xClearPause (PAUSE_NETPLAY_CONNECT);
while (NetPlay.Connected)
{
if (S9xNPWaitForHeartBeat ())
{
LONG prev;
if (!ReleaseSemaphore (GUI.ClientSemaphore, 1, &prev))
{
#ifdef NP_DEBUG
printf ("CLIENT: ReleaseSemaphore failed - already hit max count (%d) %ld\n", NP_JOYPAD_HIST_SIZE, S9xGetMilliTime () - START);
#endif
S9xNPSetWarning ("NetPlay: Client may be out of sync with server.");
}
else
{
if (!NetPlay.Waiting4EmulationThread &&
prev == (int) NetPlay.MaxBehindFrameCount)
{
NetPlay.Waiting4EmulationThread = TRUE;
S9xNPSendPause (TRUE);
}
}
}
else
S9xNPDisconnect ();
}
}
else
{
S9xClearPause (PAUSE_NETPLAY_CONNECT);
}
#ifdef NP_DEBUG
printf ("CLIENT: Client thread exiting @%ld\n", S9xGetMilliTime () - START);
#endif
}
#endif
bool8 S9xNPCheckForHeartBeat (uint32 time_msec)
{
fd_set read_fds;
struct timeval timeout;
int res;
int i;
int max_fd = NetPlay.Socket;
FD_ZERO (&read_fds);
FD_SET (NetPlay.Socket, &read_fds);
timeout.tv_sec = 0;
timeout.tv_usec = time_msec * 1000;
res = select (max_fd + 1, &read_fds, NULL, NULL, &timeout);
i = (res > 0 && FD_ISSET(NetPlay.Socket, &read_fds));
#if defined(NP_DEBUG) && NP_DEBUG >= 4
printf ("CLIENT: S9xCheckForHeartBeat %s @%ld\n", (i?"successful":"still waiting"), S9xGetMilliTime () - START);
#endif
return i;
}
bool8 S9xNPWaitForHeartBeatDelay (uint32 time_msec)
{
if (!S9xNPCheckForHeartBeat(time_msec))
return FALSE;
if (!S9xNPWaitForHeartBeat())
{
S9xNPDisconnect();
return FALSE;
}
return TRUE;
}
bool8 S9xNPWaitForHeartBeat ()
{
uint8 header [3 + 4 + 4 * 5];
while (S9xNPGetData (NetPlay.Socket, header, 3 + 4))
{
if (header [0] != NP_SERV_MAGIC)
{
S9xNPSetError ("Bad magic value from server while waiting for heart-beat message\n");
S9xNPDisconnect ();
return (FALSE);
}
if (header [1] != NetPlay.ServerSequenceNum)
{
char buf [200];
sprintf (buf, "Unexpected message sequence number from server, expected %d, got %d\n", NetPlay.ServerSequenceNum, header [1]);
S9xNPSetWarning (buf);
NetPlay.ServerSequenceNum = header [1] + 1;
}
else
NetPlay.ServerSequenceNum++;
if ((header [2] & 0x1f) == NP_SERV_JOYPAD)
{
// Top 2 bits + 1 of opcode is joypad data count.
int num = (header [2] >> 6) + 1;
if (num)
{
if (!S9xNPGetData (NetPlay.Socket, header + 3 + 4, num * 4))
{
S9xNPSetError ("Error while receiving 'JOYPAD' message.");
S9xNPDisconnect ();
return (FALSE);
}
}
NetPlay.Frame [NetPlay.JoypadWriteInd] = READ_LONG (&header [3]);
int i;
for (i = 0; i < num; i++)
NetPlay.Joypads [NetPlay.JoypadWriteInd][i] = READ_LONG (&header [3 + 4 + i * sizeof (uint32)]);
for (i = 0; i < NP_MAX_CLIENTS; i++)
NetPlay.JoypadsReady [NetPlay.JoypadWriteInd][i] = TRUE;
NetPlay.Paused = (header [2] & 0x20) != 0;
NetPlay.JoypadWriteInd = (NetPlay.JoypadWriteInd + 1) % NP_JOYPAD_HIST_SIZE;
if (NetPlay.JoypadWriteInd != (NetPlay.JoypadReadInd + 1) % NP_JOYPAD_HIST_SIZE)
{
//printf ("(%d)", (NetPlay.JoypadWriteInd - NetPlay.JoypadReadInd) % NP_JOYPAD_HIST_SIZE); fflush (stdout);
}
//printf ("CLIENT: HB: @%d\n", S9xGetMilliTime () - START);
return (TRUE);
}
else
{
uint32 len = READ_LONG (&header [3]);
switch (header [2] & 0x1f)
{
case NP_SERV_RESET:
#ifdef NP_DEBUG
printf ("CLIENT: RESET received @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPDiscardHeartbeats ();
S9xReset ();
NetPlay.FrameCount = READ_LONG (&header [3]);
S9xNPResetJoypadReadPos ();
S9xNPSendReady ();
break;
case NP_SERV_PAUSE:
NetPlay.Paused = (header [2] & 0x20) != 0;
if (NetPlay.Paused)
S9xNPSetWarning("CLIENT: Server has paused.");
else
S9xNPSetWarning("CLIENT: Server has resumed.");
break;
case NP_SERV_LOAD_ROM:
#ifdef NP_DEBUG
printf ("CLIENT: LOAD_ROM received @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPDiscardHeartbeats ();
if (S9xNPLoadROM (len - 7))
S9xNPSendReady (NP_CLNT_LOADED_ROM);
break;
case NP_SERV_ROM_IMAGE:
#ifdef NP_DEBUG
printf ("CLIENT: ROM_IMAGE received @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPDiscardHeartbeats ();
if (S9xNPGetROMImage (len - 7))
S9xNPSendReady (NP_CLNT_RECEIVED_ROM_IMAGE);
break;
case NP_SERV_SRAM_DATA:
#ifdef NP_DEBUG
printf ("CLIENT: SRAM_DATA received @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPDiscardHeartbeats ();
S9xNPGetSRAMData (len - 7);
break;
case NP_SERV_FREEZE_FILE:
#ifdef NP_DEBUG
printf ("CLIENT: FREEZE_FILE received @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPDiscardHeartbeats ();
S9xNPGetFreezeFile (len - 7);
S9xNPResetJoypadReadPos ();
S9xNPSendReady ();
break;
default:
#ifdef NP_DEBUG
printf ("CLIENT: UNKNOWN received @%ld\n", S9xGetMilliTime () - START);
#endif
S9xNPDisconnect ();
return (FALSE);
}
}
}
S9xNPDisconnect ();
return (FALSE);
}
bool8 S9xNPLoadROMDialog (const char *rom_name)
{
NetPlay.Answer = FALSE;
#ifdef __WIN32__
ResetEvent (NetPlay.ReplyEvent);
#ifdef NP_DEBUG
printf ("CLIENT: Asking GUI thread to open ROM load dialog...\n");
#endif
PostMessage (GUI.hWnd, WM_USER + 3, (uint32) rom_name, (uint32) rom_name);
#ifdef NP_DEBUG
printf ("CLIENT: Waiting for reply from GUI thread...\n");
#endif
WaitForSingleObject (NetPlay.ReplyEvent, INFINITE);
#ifdef NP_DEBUG
printf ("CLIENT: Got reply from GUI thread (%d)\n", NetPlay.Answer);
#endif
#else
NetPlay.Answer = TRUE;
#endif
return (NetPlay.Answer);
}
bool8 S9xNPLoadROM (uint32 len)
{
uint8 *data = new uint8 [len];
S9xNPSetAction ("Receiving ROM name...");
if (!S9xNPGetData (NetPlay.Socket, data, len))
{
S9xNPSetError ("Error while receiving ROM name.");
delete data;
S9xNPDisconnect ();
return (FALSE);
}
S9xNPSetAction ("Opening LoadROM dialog...");
if (!S9xNPLoadROMDialog ((char *) data))
{
S9xNPSetError ("Disconnected from NetPlay server because you are playing a different game!");
delete data;
S9xNPDisconnect ();
return (FALSE);
}
delete data;
return (TRUE);
}
bool8 S9xNPGetROMImage (uint32 len)
{
uint8 rom_info [5];
S9xNPSetAction ("Receiving ROM information...");
if (!S9xNPGetData (NetPlay.Socket, rom_info, 5))
{
S9xNPSetError ("Error while receiving ROM information.");
S9xNPDisconnect ();
return (FALSE);
}
uint32 CalculatedSize = READ_LONG (&rom_info [1]);
#ifdef NP_DEBUG
printf ("CLIENT: Hi-ROM: %s, Size: %04x\n", rom_info [0] ? "Y" : "N", CalculatedSize);
#endif
if (CalculatedSize + 5 >= len ||
CalculatedSize >= Memory.MAX_ROM_SIZE)
{
S9xNPSetError ("Size error in ROM image data received from server.");
S9xNPDisconnect ();
return (FALSE);
}
Memory.HiROM = rom_info [0];
Memory.LoROM = !Memory.HiROM;
Memory.HeaderCount = 0;
Memory.CalculatedSize = CalculatedSize;
// Load up ROM image
#ifdef NP_DEBUG
printf ("CLIENT: Receiving ROM image @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Receiving ROM image...");
if (!S9xNPGetData (NetPlay.Socket, Memory.ROM, Memory.CalculatedSize))
{
S9xNPSetError ("Error while receiving ROM image from server.");
Settings.StopEmulation = TRUE;
S9xNPDisconnect ();
return (FALSE);
}
#ifdef NP_DEBUG
printf ("CLIENT: Receiving ROM filename @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Receiving ROM filename...");
uint32 filename_len = len - Memory.CalculatedSize - 5;
if (filename_len > PATH_MAX ||
!S9xNPGetData (NetPlay.Socket, (uint8 *) Memory.ROMFilename, filename_len))
{
S9xNPSetError ("Error while receiving ROM filename from server.");
S9xNPDisconnect ();
Settings.StopEmulation = TRUE;
return (FALSE);
}
Memory.InitROM ();
S9xReset ();
S9xNPResetJoypadReadPos ();
Settings.StopEmulation = FALSE;
#ifdef __WIN32__
PostMessage (GUI.hWnd, WM_NULL, 0, 0);
#endif
return (TRUE);
}
void S9xNPGetSRAMData (uint32 len)
{
if (len > 0x10000)
{
S9xNPSetError ("Length error in S-RAM data received from server.");
S9xNPDisconnect ();
return;
}
S9xNPSetAction ("Receiving S-RAM data...");
if (len > 0 && !S9xNPGetData (NetPlay.Socket, Memory.SRAM, len))
{
S9xNPSetError ("Error while receiving S-RAM data from server.");
S9xNPDisconnect ();
}
S9xNPSetAction ("", TRUE);
}
void S9xNPGetFreezeFile (uint32 len)
{
uint8 frame_count [4];
#ifdef NP_DEBUG
printf ("CLIENT: Receiving freeze file information @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Receiving freeze file information...");
if (!S9xNPGetData (NetPlay.Socket, frame_count, 4))
{
S9xNPSetError ("Error while receiving freeze file information from server.");
S9xNPDisconnect ();
return;
}
NetPlay.FrameCount = READ_LONG (frame_count);
#ifdef NP_DEBUG
printf ("CLIENT: Receiving freeze file @%ld...\n", S9xGetMilliTime () - START);
#endif
S9xNPSetAction ("Receiving freeze file...");
uint8 *data = new uint8 [len];
if (!S9xNPGetData (NetPlay.Socket, data, len - 4))
{
S9xNPSetError ("Error while receiving freeze file from server.");
S9xNPDisconnect ();
delete data;
return;
}
S9xNPSetAction ("", TRUE);
//FIXME: Setting umask here wouldn't hurt.
FILE *file;
#ifdef HAVE_MKSTEMP
int fd;
char fname[] = "/tmp/snes9x_fztmpXXXXXX";
if ((fd = mkstemp(fname)) >= 0)
{
if ((file = fdopen (fd, "wb")))
#else
char fname [L_tmpnam];
if (tmpnam (fname))
{
if ((file = fopen (fname, "wb")))
#endif
{
if (fwrite (data, 1, len, file) == len)
{
fclose(file);
#ifndef __WIN32__
/* We need .s96 extension, else .s96 is addded by unix code */
char buf[PATH_MAX +1 ];
strncpy(buf, fname, PATH_MAX);
strcat(buf, ".s96");
rename(fname, buf);
if (!S9xUnfreezeGame (buf))
#else
if (!S9xUnfreezeGame (fname))
#endif
S9xNPSetError ("Unable to load freeze file just received.");
} else {
S9xNPSetError ("Failed to write to temporary freeze file.");
fclose(file);
}
} else
S9xNPSetError ("Failed to create temporary freeze file.");
remove (fname);
} else
S9xNPSetError ("Unable to get name for temporary freeze file.");
delete data;
}
uint32 S9xNPGetJoypad (int which1)
{
if (Settings.NetPlay && which1 < 8)
{
#ifdef NP_DEBUG
if(!NetPlay.JoypadsReady [NetPlay.JoypadReadInd][which1])
{
S9xNPSetWarning ("Missing input from server!");
}
#endif
NetPlay.JoypadsReady [NetPlay.JoypadReadInd][which1] = FALSE;
return (NetPlay.Joypads [NetPlay.JoypadReadInd][which1]);
}
return (0);
}
void S9xNPStepJoypadHistory ()
{
if ((NetPlay.JoypadReadInd + 1) % NP_JOYPAD_HIST_SIZE != NetPlay.JoypadWriteInd)
{
NetPlay.JoypadReadInd = (NetPlay.JoypadReadInd + 1) % NP_JOYPAD_HIST_SIZE;
if (NetPlay.FrameCount != NetPlay.Frame [NetPlay.JoypadReadInd])
{
S9xNPSetWarning ("This Snes9X session may be out of sync with the server.");
#ifdef NP_DEBUG
printf ("*** CLIENT: client out of sync with server (%d, %d) @%ld\n", NetPlay.FrameCount, NetPlay.Frame [NetPlay.JoypadReadInd], S9xGetMilliTime () - START);
#endif
}
}
else
{
#ifdef NP_DEBUG
printf ("*** CLIENT: S9xNPStepJoypadHistory NOT OK@%ld\n", S9xGetMilliTime () - START);
#endif
}
}
void S9xNPResetJoypadReadPos ()
{
#ifdef NP_DEBUG
printf ("CLIENT: ResetJoyReadPos @%ld\n", S9xGetMilliTime () - START); fflush (stdout);
#endif
NetPlay.JoypadWriteInd = 0;
NetPlay.JoypadReadInd = NP_JOYPAD_HIST_SIZE - 1;
for (int h = 0; h < NP_JOYPAD_HIST_SIZE; h++)
memset ((void *) &NetPlay.Joypads [h], 0, sizeof (NetPlay.Joypads [0]));
for (int h = 0; h < NP_JOYPAD_HIST_SIZE; h++)
memset ((void *) &NetPlay.JoypadsReady [h], 0, sizeof (NetPlay.JoypadsReady [0]));
}
bool8 S9xNPSendJoypadUpdate (uint32 joypad)
{
uint8 data [7];
uint8 *ptr = data;
*ptr++ = NP_CLNT_MAGIC;
*ptr++ = NetPlay.MySequenceNum++;
*ptr++ = NP_CLNT_JOYPAD;
joypad |= 0x80000000;
WRITE_LONG (ptr, joypad);
if (!S9xNPSendData (NetPlay.Socket, data, 7))
{
S9xNPSetError ("Error while sending joypad data server.");
S9xNPDisconnect ();
return (FALSE);
}
return (TRUE);
}