File: network.c

package info (click to toggle)
xfrisk 1.2-10
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,344 kB
  • sloc: ansic: 16,942; makefile: 272; sh: 29
file content (1298 lines) | stat: -rw-r--r-- 36,533 bytes parent folder | download | duplicates (7)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
/*
 *   XFrisk - The classic board game for X
 *   Copyright (C) 1993-1999 Elan Feingold (elan@aetherworks.com)
 *
 *   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
 *
 *   $Id: network.c,v 1.6 1999/12/26 15:06:09 morphy Exp $
 *
 *   $Log: network.c,v $
 *   Revision 1.6  1999/12/26 15:06:09  morphy
 *   Doxygen comments
 *   Added static qualifier to local functions
 *   Miscellaneous editorial changes
 *
 */

/**
 * Messaging interface to/from the network.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "riskgame.h"
#include "types.h"
#include "network.h"
#include "debug.h"

/* Useful macro */
#define ReturnIfError(foo) if ((foo) <= 0) return (-1);
  
/* Local prototypes */
static Int32 _NET_SendString(Int32 iSocket, CString strCString);
static Int32 _NET_SendLong(Int32 iSocket, Int32 iLong);
static Int32 _NET_RecvString(Int32 iSocket, CString *pstrCString);
static Int32 _NET_SocketRead(Int32 iSocket, void *ptr, Int32 iNumBytes);
static Int32 _NET_SocketWrite(Int32 iSocket, void *ptr, Int32 iNumBytes);
static Int32 _NET_RecvLong(Int32 iSocket, Int32 *piLong);

/* Private to this routine */
static char strLogging[1024];


/**
 * Send a message to the given socket, field by field.
 *
 * \bug void pointer
 * \bug massive switch - case construct
 *
 * \b History:
 * \arg 01.24.94  ESF  Created 
 * \arg 01.28.94  ESF  Added strFrom in MSG_MESSAGEPACKET.
 * \arg 03.04.94  ESF  Changed MSG_UPDATE mesage.
 * \arg 03.05.94  ESF  Added MSG_ENTERSTATE for fault tolerance.
 * \arg 03.28.94  ESF  Added MSG_DEADPLAYER & MSG_ENDOFGAME.
 * \arg 03.28.94  ESF  Changed MSG_REQUESTCARDS to ...CARD.
 * \arg 03.29.94  ESF  Changed MSG_UPDATECARDS to MSG_EXCHANGECARDS.
 * \arg 03.29.94  ESF  Changed uiReply to be an Int32.
 * \arg 04.11.94  ESF  Added a player parameter to MSG_CARDPACKET.
 * \arg 04.11.94  ESF  Added a killer paramter to MSG_DEADPLAYER.
 * \arg 05.03.94  ESF  Added MSG_OBJ*UPDATE msgs.
 * \arg 05.03.94  ESF  Removed MSG_REGISTERPLAYER and MSG_UPDATEARMIES.
 * \arg 05.05.94  ESF  Added MSG_OBJ* msgs.
 * \arg 05.12.94  ESF  Removed MSG_OBJ* msgs and added GAME messages.
 * \arg 05.12.94  ESF  Added MSG_DEREGISTERCLIENT.
 * \arg 05.12.94  ESF  Added MSG_DELETEMSGDST.
 * \arg 05.13.94  ESF  Added MSG_STARTREGISTRATION.
 * \arg 05.15.94  ESF  Added MSG_[FREE|ALLOC]PLAYER
 * \arg 05.15.94  ESF  Removed MSG_DEADPLAYER.
 * \arg 05.17.94  ESF  Added MSG_NETMESSAGE.
 * \arg 07.27.94  ESF  Removed MSG_STARTREGISTRATION.
 * \arg 07.31.94  ESF  Added MSG_NETPOPUP.
 * \arg 08.03.94  ESF  Fixed to return error message. 
 * \arg 08.28.94  ESF  Added MSG_POPUPREGISTERBOX.
 * \arg 09.31.94  ESF  Changed MSG_ENDOFGAME to take a string parameter.
 * \arg 10.29.94  ESF  Added MSG_DICEROLL.
 * \arg 10.30.94  ESF  Added MSG_PLACENOTIFY.
 * \arg 10.30.94  ESF  Added MSG_ATTACKNOTIFY.
 * \arg 10.30.94  ESF  Added MSG_MOVENOTIFY.
 * \arg 01.17.95  ESF  Removed MSG_DELETEMSGDST.
 * \arg 02.21.95  ESF  Added MSG_HELLO, MSG_VERSION.
 * \arg 02.21.95  ESF  Modified MSG_REGISTERCLIENT to include type of client.
 * \arg 02.23.95  ESF  Added MSG_OLDREGISTERCLIENT for backwards compatibility.
 * \arg 02.23.95  ESF  Added MSG_SPECIESIDENT.
 * \arg 24.08.95  JC   Added MSG_MISSION.
 * \arg 28.08.95  JC   Added MSG_ENDOFMISSION and MSG_VICTORY.
 * \arg 30.08.95  JC   Added MSG_FORCEEXCHANGECARDS.
 */
Int32 NET_SendMessage(Int32 iSocket, Int32 iMessType, void *pvMessage)
{
  Int32 i;

  /* Send the message ID */
  ReturnIfError(_NET_SendLong(iSocket, (Int32)iMessType));

  switch(iMessType)
    {
    case MSG_OLDREGISTERCLIENT:  
      {
	MsgOldRegisterClient *pmsgMess = (MsgOldRegisterClient *)pvMessage;
	
	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strClientAddress));
      }
      break;

    case MSG_REGISTERCLIENT:  
      {
	MsgRegisterClient *pmsgMess = (MsgRegisterClient *)pvMessage;
	
	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strClientAddress));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iClientType));
      }
      break;

    case MSG_EXCHANGECARDS:
      {
	MsgExchangeCards *pmsgMess = (MsgExchangeCards *)pvMessage;
	
	for(i=0; i!=3; i++)
	  ReturnIfError(_NET_SendLong(iSocket, (Int32)pmsgMess->piCards[i]));
      }
      break;

    case MSG_CARDPACKET:
      {
	MsgCardPacket *pmsgMess = (MsgCardPacket *)pvMessage;

	ReturnIfError(_NET_SendLong(iSocket, (Int32)pmsgMess->iPlayer));
	ReturnIfError(_NET_SendLong(iSocket, (Int32)pmsgMess->cdCard));
      }
      break;
      
    case MSG_REPLYPACKET:
      {
	MsgReplyPacket *pmsgMess = (MsgReplyPacket *)pvMessage;

	ReturnIfError(_NET_SendLong(iSocket, (Int32)pmsgMess->iReply));
      }
      break;

    case MSG_SENDMESSAGE:
      {
	MsgSendMessage *pmsgMess = (MsgSendMessage *)pvMessage;

	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strMessage));
	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strDestination));
      } 
      break;

    case MSG_MESSAGEPACKET:
      {
	MsgMessagePacket *pmsgMess = (MsgMessagePacket *)pvMessage;

	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strMessage));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iFrom));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iTo));
      }
      break;

    case MSG_TURNNOTIFY:
      {
	MsgTurnNotify *pmsgMess = (MsgTurnNotify *)pvMessage;
	
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iPlayer));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iClient));
      }
      break;
      
    case MSG_CLIENTIDENT:
      {
	MsgClientIdent *pmsgMess = (MsgClientIdent *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iClientID));
      }
      break;

    case MSG_REQUESTCARD:
      {
	MsgRequestCard *pmsgMess = (MsgRequestCard *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iPlayer));
      }
      break;

    case MSG_ENTERSTATE:
      {
	MsgEnterState *pmsgMess = (MsgEnterState *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iState));
      }
      break;

    case MSG_OBJSTRUPDATE:
      {
	MsgObjStrUpdate *pmsgMess = (MsgObjStrUpdate *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iField));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iIndex1));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iIndex2));
	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strNewValue));
      }
      break;

    case MSG_OBJINTUPDATE:
      {
	MsgObjIntUpdate *pmsgMess = (MsgObjIntUpdate *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iField));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iIndex1));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iIndex2));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iNewValue));
      }
      break;

    case MSG_FREEPLAYER:
      {
	MsgFreePlayer *pmsgMess = (MsgFreePlayer *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iPlayer));
      }
      break;

    /* These happen to be identical, so we can lump them. */  
    case MSG_NETMESSAGE:
    case MSG_NETPOPUP:
      {
	MsgNetMessage *pmsgMess = (MsgNetMessage *)pvMessage;
	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strMessage));
      }
      break;

    case MSG_ENDOFMISSION:
      {
	MsgEndOfMission *pmsgMess = (MsgEndOfMission *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iWinner));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iTyp));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iNum1));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iNum2));
      }
      break;

    case MSG_VICTORY:
      {
	MsgVictory *pmsgMess = (MsgVictory *)pvMessage;
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iWinner));
      }
      break;

    case MSG_DICEROLL:
      {
	MsgDiceRoll *pmsgMess = (MsgDiceRoll *)pvMessage;

	for (i=0; i!=3; i++)
	  ReturnIfError(_NET_SendLong(iSocket, pmsgMess->pAttackDice[i]));

	for (i=0; i!=3; i++)
	  ReturnIfError(_NET_SendLong(iSocket, pmsgMess->pDefendDice[i]));

	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iDefendingPlayer));
      }
      break;

    case MSG_PLACENOTIFY:
      {
	MsgPlaceNotify *pmsgMess = (MsgPlaceNotify *)pvMessage;

	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iCountry));
      }
      break;

    case MSG_ATTACKNOTIFY:
      {
	MsgAttackNotify *pmsgMess = (MsgAttackNotify *)pvMessage;

	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iSrcCountry));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iDstCountry));
      }
      break;

    case MSG_MOVENOTIFY:
      {
	MsgMoveNotify *pmsgMess = (MsgMoveNotify *)pvMessage;
	
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iSrcCountry));
	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iDstCountry));
      }
      break;

    case MSG_VERSION:
      {
	MsgVersion *pmsgMess = (MsgVersion *)pvMessage;
	
	ReturnIfError(_NET_SendString(iSocket, pmsgMess->strVersion));
      }
      break;

    case MSG_SPECIESIDENT:
      {
	MsgSpeciesIdent *pmsgMess = (MsgSpeciesIdent *)pvMessage;

	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iSpeciesID));
      }
      break;

    case MSG_FORCEEXCHANGECARDS:
      {
	MsgForceExchangeCards *pmsgMess = (MsgForceExchangeCards *)pvMessage;

	ReturnIfError(_NET_SendLong(iSocket, pmsgMess->iPlayer));
      }

    case MSG_EXIT:
    case MSG_STARTGAME:
    case MSG_ENDTURN:
    case MSG_DEREGISTERCLIENT:
    case MSG_ALLOCPLAYER:
    case MSG_POPUPREGISTERBOX:
    case MSG_HELLO:
    case MSG_MISSION:
    case MSG_ENDOFGAME:
      /* No parameters */
      break;

    default:
      printf("NETWORK: Illegal message!\n");
    }

  return (1);
}


/**
 * Receive one message from the network, field by field.
 *
 * \bug pointer to void pointer
 * \bug massive switch - case construct
 *
 * \b History:
 * \arg 01.24.94  ESF  Created.
 * \arg 01.27.94  ESF  Fixed bug in MSG_MESSAGEPACKET case.
 * \arg 01.28.94  ESF  Added strFrom in MSG_MESSAGEPACKET.
 * \arg 03.04.94  ESF  Changed _UPDATE mesage.
 * \arg 03.05.94  ESF  Added _ENTERSTATE for fault tolerance.
 * \arg 03.28.94  ESF  Added MSG_DEADPLAYER & MSG_ENDOFGAME.
 * \arg 03.28.94  ESF  Changed MSG_REQUESTCARDS to ...CARD.
 * \arg 03.29.94  ESF  Changed MSG_UPDATECARDS to MSG_EXCHANGECARDS.
 * \arg 03.29.94  ESF  Changed uiReply to be an Int32.
 * \arg 04.11.94  ESF  Added a player parameter to MSG_CARDPACKET.
 * \arg 04.11.94  ESF  Added a killer paramter to MSG_DEADPLAYER.
 * \arg 05.03.94  ESF  Added MSG_OBJ*UPDATE msgs.
 * \arg 05.03.94  ESF  Removed MSG_REGISTERPLAYER and MSG_UPDATEARMIES.
 * \arg 05.05.94  ESF  Added MSG_OBJ* msgs.
 * \arg 05.12.94  ESF  Removed MSG_OBJ* msgs and added GAME messages.
 * \arg 05.12.94  ESF  Added MSG_DEREGISTERCLIENT.
 * \arg 05.12.94  ESF  Added MSG_DELETEMSGDST.
 * \arg 05.13.94  ESF  Added MSG_STARTREGISTRATION.
 * \arg 05.15.94  ESF  Added MSG_[FREE|ALLOC]PLAYER
 * \arg 05.15.94  ESF  Removed MSG_DEADPLAYER.
 * \arg 05.17.94  ESF  Added MSG_NETMESSAGE.
 * \arg 07.27.94  ESF  Removed MSG_STARTREGISTRATION.
 * \arg 07.31.94  ESF  Added MSG_NETPOPUP.
 * \arg 08.03.94  ESF  Fixed to return error message. 
 * \arg 08.28.94  ESF  Added MSG_POPUPREGISTERBOX.
 * \arg 09.31.94  ESF  Changed MSG_ENDOFGAME to take a string parameter.
 * \arg 10.29.94  ESF  Added MSG_DICEROLL.
 * \arg 10.30.94  ESF  Added MSG_PLACENOTIFY.
 * \arg 10.30.94  ESF  Added MSG_ATTACKNOTIFY.
 * \arg 10.30.94  ESF  Added MSG_MOVENOTIFY.
 * \arg 01.17.95  ESF  Removed MSG_DELETEMSGDST.
 * \arg 02.21.95  ESF  Added MSG_HELLO, MSG_VERSION.
 * \arg 02.21.95  ESF  Modified MSG_REGISTERCLIENT to include type of client.
 * \arg 02.23.95  ESF  Added MSG_OLDREGISTERCLIENT.
 * \arg 02.23.95  ESF  Added MSG_SPECIESIDENT.
 * \arg 24.08.95  JC   Added MSG_MISSION.
 * \arg 28.08.95  JC   Added MSG_ENDOFMISSION and MSG_VICTORY.
 * \arg 30.08.95  JC   Added MSG_FORCEEXCHANGECARDS.
 */
Int32 NET_RecvMessage(Int32 iSocket, Int32 *piMessType, void **ppvMessage)
{
  Int32 i;

  /* Get the message ID */
  ReturnIfError(_NET_RecvLong(iSocket, (Int32 *)piMessType));

  switch(*piMessType)
    {
    case MSG_OLDREGISTERCLIENT:  
      {
	MsgOldRegisterClient *pmsgMess = 
	  (MsgOldRegisterClient *)MEM_Alloc(sizeof(MsgOldRegisterClient));
	
	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strClientAddress));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_REGISTERCLIENT:  
      {
	MsgRegisterClient *pmsgMess = 
	  (MsgRegisterClient *)MEM_Alloc(sizeof(MsgRegisterClient));
	
	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strClientAddress));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iClientType));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_EXCHANGECARDS:
      {
	MsgExchangeCards *pmsgMess = 
	  (MsgExchangeCards *)MEM_Alloc(sizeof(MsgExchangeCards));
	
	for(i=0; i!=3; i++)
	  ReturnIfError(_NET_RecvLong(iSocket, 
				      (Int32 *)&pmsgMess->piCards[i]));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_CARDPACKET:
      {
	MsgCardPacket *pmsgMess = 
	  (MsgCardPacket *)MEM_Alloc(sizeof(MsgCardPacket));

	ReturnIfError(_NET_RecvLong(iSocket, (Int32 *)&pmsgMess->iPlayer));
	ReturnIfError(_NET_RecvLong(iSocket, (Int32 *)&pmsgMess->cdCard));

	*ppvMessage = (void *)pmsgMess;
      }
      break;
      
    case MSG_REPLYPACKET:
      {
	MsgReplyPacket *pmsgMess = 
	  (MsgReplyPacket *)MEM_Alloc(sizeof(MsgReplyPacket));

	ReturnIfError(_NET_RecvLong(iSocket, (Int32 *)&pmsgMess->iReply));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_SENDMESSAGE:
      {
	MsgSendMessage *pmsgMess = 
	  (MsgSendMessage *)MEM_Alloc(sizeof(MsgSendMessage));

	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strMessage));
	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strDestination));

	*ppvMessage = (void *)pmsgMess;
      } 
      break;

    case MSG_MESSAGEPACKET:
      {
	MsgMessagePacket *pmsgMess = 
	  (MsgMessagePacket *)MEM_Alloc(sizeof(MsgMessagePacket));

	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strMessage));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iFrom));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iTo));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_TURNNOTIFY:
      {
	MsgTurnNotify *pmsgMess = 
	  (MsgTurnNotify *)MEM_Alloc(sizeof(MsgTurnNotify));
	
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iPlayer));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iClient));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_CLIENTIDENT:
      {
	MsgClientIdent *pmsgMess = 
	  (MsgClientIdent *)MEM_Alloc(sizeof(MsgClientIdent));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iClientID));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_REQUESTCARD:
      {
	MsgRequestCard *pmsgMess = 
	  (MsgRequestCard *)MEM_Alloc(sizeof(MsgRequestCard));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iPlayer));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_ENTERSTATE:
      {
	MsgEnterState *pmsgMess = 
	  (MsgEnterState *)MEM_Alloc(sizeof(MsgEnterState));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iState));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_OBJSTRUPDATE:
      {
	MsgObjStrUpdate *pmsgMess = 
	  (MsgObjStrUpdate *)MEM_Alloc(sizeof(MsgObjStrUpdate));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iField));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iIndex1));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iIndex2));
	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strNewValue));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_OBJINTUPDATE:
      {
	MsgObjIntUpdate *pmsgMess = 
	  (MsgObjIntUpdate *)MEM_Alloc(sizeof(MsgObjIntUpdate));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iField));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iIndex1));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iIndex2));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iNewValue));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_FREEPLAYER:
      {
	MsgFreePlayer *pmsgMess = 
	  (MsgFreePlayer *)MEM_Alloc(sizeof(MsgFreePlayer));
	
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iPlayer));
	*ppvMessage = (void *)pmsgMess;
      }
      break;      

    /* These happen to be identical, so we can lump them. */  
    case MSG_NETMESSAGE:
    case MSG_NETPOPUP:
      {
	MsgNetMessage *pmsgMess = 
	  (MsgNetMessage *)MEM_Alloc(sizeof(MsgNetMessage));
	
	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strMessage));
	*ppvMessage = (void *)pmsgMess;
      }
      break;      

    case MSG_ENDOFMISSION:
      {
	MsgEndOfMission *pmsgMess = 
	  (MsgEndOfMission *)MEM_Alloc(sizeof(MsgEndOfMission));
	
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iWinner));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iTyp));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iNum1));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iNum2));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_VICTORY:
      {
	MsgVictory *pmsgMess = 
	  (MsgVictory *)MEM_Alloc(sizeof(MsgVictory));
	
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iWinner));
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_DICEROLL:
      {
	MsgDiceRoll *pmsgMess = 
	  (MsgDiceRoll *)MEM_Alloc(sizeof(MsgDiceRoll));

    	for (i=0; i!=3; i++)
	  ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->pAttackDice[i]));

    	for (i=0; i!=3; i++)
	  ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->pDefendDice[i]));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iDefendingPlayer));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_PLACENOTIFY:
      {
	MsgPlaceNotify *pmsgMess = 
	  (MsgPlaceNotify *)MEM_Alloc(sizeof(MsgPlaceNotify));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iCountry));
	
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_ATTACKNOTIFY:
      {
	MsgAttackNotify *pmsgMess = 
	  (MsgAttackNotify *)MEM_Alloc(sizeof(MsgAttackNotify));

	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iSrcCountry));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iDstCountry));
	
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_MOVENOTIFY:
      {
	MsgMoveNotify *pmsgMess = 
	  (MsgMoveNotify *)MEM_Alloc(sizeof(MsgMoveNotify));
		
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iSrcCountry));
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iDstCountry));

	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_VERSION:
      {
	MsgVersion *pmsgMess = 
	  (MsgVersion *)MEM_Alloc(sizeof(MsgVersion));

	ReturnIfError(_NET_RecvString(iSocket, &pmsgMess->strVersion));
	
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_SPECIESIDENT:
      {
	MsgSpeciesIdent *pmsgMess = 
	  (MsgSpeciesIdent *)MEM_Alloc(sizeof(MsgSpeciesIdent));
	
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iSpeciesID));
	
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_FORCEEXCHANGECARDS:
      {
	MsgForceExchangeCards *pmsgMess = 
	  (MsgForceExchangeCards *)MEM_Alloc(sizeof(MsgForceExchangeCards));
	
	ReturnIfError(_NET_RecvLong(iSocket, &pmsgMess->iPlayer));
	
	*ppvMessage = (void *)pmsgMess;
      }
      break;

    case MSG_EXIT:
    case MSG_STARTGAME:
    case MSG_ENDTURN:
    case MSG_DEREGISTERCLIENT:
    case MSG_ALLOCPLAYER:
    case MSG_POPUPREGISTERBOX:
    case MSG_HELLO:
    case MSG_MISSION:
    case MSG_ENDOFGAME:
      *ppvMessage = NULL;
      break;

    default:
      printf("NETWORK: Illegal message!\n");
    }

  return (1);
}

/************************************************************************ 
 *  FUNCTION: _NET_SendString
 *  HISTORY: 
 *     01.24.94  ESF  Created. 
 *     08.03.94  ESF  Fixed to return error message. 
 *     03.22.95  ESF  Added handling for NULL strings.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
static Int32 _NET_SendString(Int32 iSocket, CString strCString)
{
  Int32 iLength;

  /* Send the length and then the string itself */
  if (strCString == NULL)
    iLength = 0;
  else
    iLength = strlen(strCString)+1;

  ReturnIfError(_NET_SendLong(iSocket, (Int32)iLength));
  return (_NET_SocketWrite(iSocket, (Char *)strCString, (Int32)iLength));
}


/**
 * Send a long integer to the given socket.
 *
 * \b History:
 *     01.24.94  ESF  Created 
 *     08.03.94  ESF  Fixed to return error message. 
 */
static Int32 _NET_SendLong(Int32 iSocket, Int32 iLong)
{
  Int32 iData = htonl(iLong);
  
  return (_NET_SocketWrite(iSocket, &iData, sizeof(Int32)));
}


/**
 * Receive a string from the given socket. First reads the length
 * (32bit value), then the string data.
 *
 * \bug Assumes that a nul terminating byte is in the data stream
 *
 * \b History:
 * \arg 01.24.94  ESF  Created 
 * \arg 08.03.94  ESF  Fixed to return error message. 
 * \arg 01.01.94  ESF  Added check for correct number of bytes read.
 * \arg 03.22.95  ESF  Added handling for NULL strings.
 */
static Int32 _NET_RecvString(Int32 iSocket, CString *pstrCString)
{
  Int32    iLength;
  CString  strTemp;
  Int32    iRet;

  /* Receive the length and then the byte stream */
  ReturnIfError(_NET_RecvLong(iSocket, &iLength));

  if (iLength)
    {
      strTemp = (CString)MEM_Alloc(iLength);
      iRet = _NET_SocketRead(iSocket, strTemp, iLength);

      /* Return an error if less than the string was read! */
      if (iRet<0 || iRet!=iLength)
	iRet = -1;
    }
  else
    { 
      iRet = -1;
      strTemp = NULL;
    }
  
  *pstrCString = strTemp;
  return iRet;
}


/**
 * Reads a 32bit value from the given socket.
 *
 * \b History:
 * \arg 01.24.94  ESF  Created 
 * \arg 08.03.94  ESF  Fixed to return error message. 
 * \arg 01.01.94  ESF  Added check for correct number of bytes read.
 */
static Int32 _NET_RecvLong(Int32 iSocket, Int32 *piLong)
{
  Int32 iRet = _NET_SocketRead(iSocket, piLong, sizeof(Int32));
  
  /* Adjust for the network munging */
  *piLong = ntohl(*piLong);
  
  /* Return an error if read returns an error or if the wrong number
   * of bytes come across -- there should be sizeof(Int32) bytes!
   */
  
  if (iRet<0 || iRet!=sizeof(Int32))
    iRet = -1;

  return iRet;
}


/**
 * Reads the given number of bytes from the given socket.
 *
 * \b History:
 * \arg 01.01.95  ESF  Created.
 * \arg 01.21.95  ESF  Fixed non-ANSIness.
 */
static Int32 _NET_SocketRead(Int32 iSocket, void *ptr, Int32 iNumBytes)
{
  Int32 iBytesLeft, iRet;

  /* We may have to do multiple read() calls here */
  iBytesLeft = iNumBytes;
  
  while (iBytesLeft > 0)
    {
      iRet = read(iSocket, ptr, iBytesLeft);

      /* If an error occured, return */
      if (iRet < 0)
	return iRet;
      else if (iRet == 0)
	break; /* EOF */
      
      iBytesLeft  -= iRet;
      ptr = (Byte *)ptr + iRet;
    }
  
  /* Return the number of bytes read */
  return (iNumBytes - iBytesLeft);
}


/**
 * Write the given number of bytes to the given socket.
 *
 * \b History:
 * \arg 01.24.94  ESF  Created 
 * \arg 08.03.94  ESF  Fixed to return error message. 
 * \arg 01.01.94  ESF  Added check for correct number of bytes read.
 * \arg 01.21.95  ESF  Fixed non-ANSIness.
 */
static Int32 _NET_SocketWrite(Int32 iSocket, void *ptr, Int32 iNumBytes)
{
  Int32 iBytesLeft, iRet;

  /* We may have to do multiple read() calls here */
  iBytesLeft = iNumBytes;
  
  while (iBytesLeft > 0)
    {
      iRet = write(iSocket, ptr, iBytesLeft);

      /* If an error occured, return */
      if (iRet < 0)
	return iRet;

      iBytesLeft  -= iRet;
      ptr = (Byte *)ptr + iRet;
    }
  
  /* Return the number of bytes read */
  return (iNumBytes - iBytesLeft);
}


/**
 * Free the memory allocated for the given message.
 *
 * \b History:
 * \arg 06.24.94  ESF  Created 
 * \arg 07.31.94  ESF  Added MSG_NETPOPUP.
 * \arg 09.31.94  ESF  Changed MSG_ENDOFGAME to take a string parameter.
 * \arg 10.29.94  ESF  Added MSG_DICEROLL.
 * \arg 10.30.94  ESF  Added MSG_PLACENOTIFY.
 * \arg 10.30.94  ESF  Added MSG_ATTACKNOTIFY.
 * \arg 10.30.94  ESF  Added MSG_MOVENOTIFY.
 * \arg 01.17.95  ESF  Removed MSG_DELETEMSGDST.
 * \arg 02.21.95  ESF  Added MSG_HELLO, MSG_VERSION.
 * \arg 02.21.95  ESF  Modified MSG_REGISTERCLIENT to include type of client.
 * \arg 02.23.95  ESF  Added MSG_OLDREGISTERCLIENT.
 * \arg 02.23.95  ESF  Added MSG_SPECIESIDENT.
 * \arg 24.08.95  JC   Added MSG_MISSION.
 * \arg 30.08.95  JC   Added MSG_FORCEEXCHANGECARDS.
 */
void NET_DeleteMessage(Int32 iMessType, void *pvMessage)
{
  switch(iMessType)
    {
    case MSG_NOMESSAGE:
      break;

    case MSG_OLDREGISTERCLIENT:  
      {
	MsgOldRegisterClient *pmsgMess = (MsgOldRegisterClient *)pvMessage;
	MEM_Free(pmsgMess->strClientAddress);
	MEM_Free(pmsgMess);
      }
      break;
      
    case MSG_REGISTERCLIENT:  
      {
	MsgRegisterClient *pmsgMess = (MsgRegisterClient *)pvMessage;
	MEM_Free(pmsgMess->strClientAddress);
	MEM_Free(pmsgMess);
      }
      break;

    case MSG_EXCHANGECARDS:
      MEM_Free(pvMessage);
      break;

    case MSG_CARDPACKET:
      MEM_Free(pvMessage);
      break;
      
    case MSG_REPLYPACKET:
      MEM_Free(pvMessage);
      break;

    case MSG_SENDMESSAGE:
      {
	MsgSendMessage *pmsgMess = (MsgSendMessage *)pvMessage;

	MEM_Free(pmsgMess->strMessage);
	MEM_Free(pmsgMess->strDestination);
	MEM_Free(pvMessage);
      } 
      break;

    case MSG_MESSAGEPACKET:
      {
	MsgMessagePacket *pmsgMess = (MsgMessagePacket *)pvMessage;

	MEM_Free(pmsgMess->strMessage);
	MEM_Free(pvMessage);
      }
      break;

    case MSG_TURNNOTIFY:
      MEM_Free(pvMessage);
      break;
      
    case MSG_CLIENTIDENT:
      MEM_Free(pvMessage);
      break;

    case MSG_REQUESTCARD:
      MEM_Free(pvMessage);
      break;

    case MSG_ENTERSTATE:
      MEM_Free(pvMessage);
      break;

    case MSG_OBJSTRUPDATE:
      {
	MsgObjStrUpdate *pmsgMess = (MsgObjStrUpdate*)pvMessage;

	if (pmsgMess->strNewValue)
	  MEM_Free(pmsgMess->strNewValue);
	MEM_Free(pvMessage);
      }
      break;
      
    case MSG_OBJINTUPDATE:
      MEM_Free(pvMessage);
      break;

    case MSG_FREEPLAYER:
      MEM_Free(pvMessage);
      break;

    /* These happen to be identical, so we can lump them. */  
    case MSG_NETMESSAGE:
    case MSG_NETPOPUP:
      {
	MsgNetMessage *pmsgMess = (MsgNetMessage *)pvMessage;
	MEM_Free(pmsgMess->strMessage);
	MEM_Free(pvMessage);
      }
      break;

    case MSG_ENDOFMISSION:
      MEM_Free(pvMessage);
      break;

    case MSG_VICTORY:
      MEM_Free(pvMessage);
      break;

    case MSG_DICEROLL:
      MEM_Free(pvMessage);
      break;

    case MSG_PLACENOTIFY:
      MEM_Free(pvMessage);
      break;

    case MSG_ATTACKNOTIFY:
      MEM_Free(pvMessage);
      break;

    case MSG_MOVENOTIFY:
      MEM_Free(pvMessage);
      break;
      
    case MSG_VERSION:
      {
	MsgVersion *pmsgMess = (MsgVersion *)pvMessage;
	MEM_Free(pmsgMess->strVersion);
	MEM_Free(pvMessage);
      }
      break;

    case MSG_SPECIESIDENT:
      MEM_Free(pvMessage);
      break;

    case MSG_FORCEEXCHANGECARDS:
      MEM_Free(pvMessage);
      break;

    case MSG_EXIT:
    case MSG_STARTGAME:
    case MSG_ENDTURN:
    case MSG_DEREGISTERCLIENT:
    case MSG_ALLOCPLAYER:
    case MSG_POPUPREGISTERBOX:
    case MSG_HELLO:
    case MSG_MISSION:
    case MSG_ENDOFGAME:
      /* No parameters */
      D_Assert(pvMessage == NULL, "Hum...this shouldn't happen.");
      break;

    default:
      D_Assert(FALSE, "Add case for a new message in NET_DeleteMessage!");
    }
}


/**
 * Set socket options for better performance.
 *
 * \b History:
 * \arg 01.22.95  ESF  Created.
 * \arg 02.27.95  ESF  Added SO_REUSEADDR.
 * \arg 17.08.95  JC   iOption -> &iOption.
 */
void NET_SetCommLinkOptions(Int32 iSocket)
{
#ifdef TCP_NODELAY
  {
    Int32 iOption = 1;
    if (setsockopt(iSocket, IPPROTO_TCP, TCP_NODELAY, 
		   (char *)&iOption, sizeof(iOption)) != 0)
      printf("NETWORK: Warning -- couldn't set TCP_NODELAY on CommLink!\n");
  }
#endif
#ifdef SO_REUSEADDR
  {
    Int32 iOption = 1;
    if (setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, 
		   (char *)&iOption, sizeof(iOption)) != 0)
      printf("NETWORK: Warning -- couldn't set SO_REUSEADDR on CommLink!\n");
  }
#endif
}


/**
 * Convert the given message to a human readable string for debugging.
 *
 * \b History:
 * \arg 03.19.95  ESF  Created.
 * \arg 24.08.95  JC   Added MSG_MISSION.
 * \arg 30.08.95  JC   Added MSG_FORCEEXCHANGECARDS.
 */
CString NET_MessageToString(Int32 iMessType, void *pvMessage)
{
  switch (iMessType)
    {
    case MSG_NOMESSAGE:
      snprintf(strLogging, sizeof(strLogging), "MsgNoMessage()");
      break;

    case MSG_OLDREGISTERCLIENT:
      snprintf(strLogging, sizeof(strLogging), "MsgOldRegisterClient(strClientAddress=\"%s\")", 
	      ((MsgOldRegisterClient *)pvMessage)->strClientAddress);  
      break;
      
    case MSG_REGISTERCLIENT:  
      snprintf(strLogging, sizeof(strLogging), "MsgRegisterClient(strClientAddress=\"%s\","
	      " iClientType=%s)",
	      ((MsgRegisterClient *)pvMessage)->strClientAddress, 
	      ((MsgRegisterClient *)pvMessage)->iClientType == 
	      CLIENT_NORMAL ? "CLIENT_NORMAL" :
	      ((MsgRegisterClient *)pvMessage)->iClientType == 
	      CLIENT_AI ? "CLIENT_AI" :
	      ((MsgRegisterClient *)pvMessage)->iClientType == 
	      CLIENT_STRICTOBSERVER ? "CLIENT_STRICTOBSERVER" :
	      "*Unknown*");
      break;
      
    case MSG_EXCHANGECARDS:
      snprintf(strLogging, sizeof(strLogging), "MsgExchangeCards(piCards={%d, %d, %d})",
	      ((MsgExchangeCards *)pvMessage)->piCards[0],
	      ((MsgExchangeCards *)pvMessage)->piCards[1],
	      ((MsgExchangeCards *)pvMessage)->piCards[2]);
      break;
      
    case MSG_CARDPACKET:
      snprintf(strLogging, sizeof(strLogging), "MsgCardPacket(iPlayer=%d, iCard=%d)",
	      ((MsgCardPacket *)pvMessage)->iPlayer, 
	      ((MsgCardPacket *)pvMessage)->cdCard); 
      break;
      
    case MSG_REPLYPACKET:
      snprintf(strLogging, sizeof(strLogging), "MsgReplyPacket(iReply=%d)",
	      ((MsgReplyPacket *)pvMessage)->iReply);
      break;

    case MSG_SENDMESSAGE:
      snprintf(strLogging, sizeof(strLogging), "MsgSendMessage(strMessage=\"%s\", "
	      "strDestination=\"%s\")",
	      ((MsgSendMessage *)pvMessage)->strMessage,
	      ((MsgSendMessage *)pvMessage)->strDestination);
      break;
      
    case MSG_MESSAGEPACKET:
      snprintf(strLogging, sizeof(strLogging), "MsgMessagePacket(strMessage=\"%s\", "
	      "iFrom=\"%d\", iTo=%d)",
	      ((MsgMessagePacket *)pvMessage)->strMessage,
	      ((MsgMessagePacket *)pvMessage)->iFrom,
	      ((MsgMessagePacket *)pvMessage)->iTo);
      break;

    case MSG_TURNNOTIFY:
      snprintf(strLogging, sizeof(strLogging), "MsgTurnNotify(iPlayer=%d, iClient=%d)",
	      ((MsgTurnNotify *)pvMessage)->iPlayer,
	      ((MsgTurnNotify *)pvMessage)->iClient);
      break;
      
    case MSG_CLIENTIDENT:
      snprintf(strLogging, sizeof(strLogging), "MsgClientIdent(iClientID=%d)",
	      ((MsgClientIdent *)pvMessage)->iClientID);
      break;

    case MSG_REQUESTCARD:
      snprintf(strLogging, sizeof(strLogging), "MsgRequestCard(iPlayer=%d)",
	      ((MsgRequestCard *)pvMessage)->iPlayer);
      break;

    case MSG_ENTERSTATE:
      snprintf(strLogging, sizeof(strLogging), "MsgEnterState(iState=%d)",
	      ((MsgEnterState *)pvMessage)->iState);
      break;

    case MSG_OBJSTRUPDATE:
      snprintf(strLogging, sizeof(strLogging), "ObjStrUpdate(iField=%d, iIndex=[%d, %d], "
	      "strNewValue=\"%s\")",
	      ((MsgObjStrUpdate *)pvMessage)->iField,
	      ((MsgObjStrUpdate *)pvMessage)->iIndex1,
	      ((MsgObjStrUpdate *)pvMessage)->iIndex2,
	      ((MsgObjStrUpdate *)pvMessage)->strNewValue);
      break;
      
    case MSG_OBJINTUPDATE:
      snprintf(strLogging, sizeof(strLogging), "ObjIntUpdate(iField=%d, iIndex=[%d, %d], "
	      "iNewValue=%d)",
	      ((MsgObjIntUpdate *)pvMessage)->iField,
	      ((MsgObjIntUpdate *)pvMessage)->iIndex1,
	      ((MsgObjIntUpdate *)pvMessage)->iIndex2,
	      ((MsgObjIntUpdate *)pvMessage)->iNewValue);
      break;

    case MSG_FREEPLAYER:
      snprintf(strLogging, sizeof(strLogging), "MsgFreePlayer(iPlayer=%d)",
	      ((MsgFreePlayer *)pvMessage)->iPlayer);
      break;

    case MSG_NETMESSAGE:
      snprintf(strLogging, sizeof(strLogging), "MsgNetMessage(strMessage=\"%s\")",
	      ((MsgNetMessage *)pvMessage)->strMessage);
      break;

    case MSG_NETPOPUP:
      snprintf(strLogging, sizeof(strLogging), "MsgNetPopup(strMessage=\"%s\")",
	      ((MsgNetPopup *)pvMessage)->strMessage);
      break;

    case MSG_ENDOFMISSION:
      snprintf(strLogging, sizeof(strLogging), "MsgEndOfMission(iWinner=%d, iTyp=%d, iNum1=%d, iNum2=%d)",
	      ((MsgEndOfMission *)pvMessage)->iWinner,
	      ((MsgEndOfMission *)pvMessage)->iTyp,
	      ((MsgEndOfMission *)pvMessage)->iNum1,
	      ((MsgEndOfMission *)pvMessage)->iNum2);
      break;

    case MSG_VICTORY:
      snprintf(strLogging, sizeof(strLogging), "MsgVictory(iWinner=%d)",
	      ((MsgVictory *)pvMessage)->iWinner);
      break;

    case MSG_ENDOFGAME:
      snprintf(strLogging, sizeof(strLogging), "MsgEndOfGame()");
      break;

    case MSG_DICEROLL:
      snprintf(strLogging, sizeof(strLogging), "MsgDiceRoll(iDefendingPlayer=%d, "
	      "pAttackDice={%d, %d, %d}, pDefendDice={%d, %d, %d})",
	      ((MsgDiceRoll *)pvMessage)->iDefendingPlayer,
	      ((MsgDiceRoll *)pvMessage)->pAttackDice[0],
	      ((MsgDiceRoll *)pvMessage)->pAttackDice[1],
	      ((MsgDiceRoll *)pvMessage)->pAttackDice[2],
	      ((MsgDiceRoll *)pvMessage)->pDefendDice[0],
	      ((MsgDiceRoll *)pvMessage)->pDefendDice[1],
	      ((MsgDiceRoll *)pvMessage)->pDefendDice[2]);
      break;

    case MSG_PLACENOTIFY:
      snprintf(strLogging, sizeof(strLogging), "MsgPlaceNotify(iCountry=%d)",
	      ((MsgPlaceNotify *)pvMessage)->iCountry);
      break;

    case MSG_ATTACKNOTIFY:
      snprintf(strLogging, sizeof(strLogging), "MsgAttackNotify(iSrcCountry=%d, iDstCountry=%d)",
	      ((MsgAttackNotify *)pvMessage)->iSrcCountry,
	      ((MsgAttackNotify *)pvMessage)->iDstCountry);
      break;

    case MSG_MOVENOTIFY:
      snprintf(strLogging, sizeof(strLogging), "MsgMoveNotify(iSrcCountry=%d, iDstCountry=%d)",
	      ((MsgMoveNotify *)pvMessage)->iSrcCountry,
	      ((MsgMoveNotify *)pvMessage)->iDstCountry);
      break;
      
    case MSG_VERSION:
      snprintf(strLogging, sizeof(strLogging), "MsgVersion(strVersion=\"%s\")",
	      ((MsgVersion *)pvMessage)->strVersion);
      break;

    case MSG_SPECIESIDENT:
      snprintf(strLogging, sizeof(strLogging), "MsgSpeciesIdent(iSpeciesID=%d)",
	      ((MsgSpeciesIdent *)pvMessage)->iSpeciesID);
      break;

    case MSG_FORCEEXCHANGECARDS:
      snprintf(strLogging, sizeof(strLogging), "MsgForceExchangeCards(iPlayer=%d)",
	      ((MsgForceExchangeCards *)pvMessage)->iPlayer);
      break;

    case MSG_EXIT:
      snprintf(strLogging, sizeof(strLogging), "MsgExit()");
      break;

    case MSG_STARTGAME:
      snprintf(strLogging, sizeof(strLogging), "MsgStartGame()");
      break;

    case MSG_ENDTURN:
      snprintf(strLogging, sizeof(strLogging), "MsgEndTurn()");
      break;

    case MSG_DEREGISTERCLIENT:
      snprintf(strLogging, sizeof(strLogging), "MsgDeregisterClient()");
      break;

    case MSG_ALLOCPLAYER:
      snprintf(strLogging, sizeof(strLogging), "MsgAllocPlayer()");
      break;

    case MSG_POPUPREGISTERBOX:
      snprintf(strLogging, sizeof(strLogging), "MsgPopupRegisterBox()");
      break;

    case MSG_HELLO:
      snprintf(strLogging, sizeof(strLogging), "MsgHello()");
      break;

    case MSG_MISSION:
      snprintf(strLogging, sizeof(strLogging), "MsgMission()");
      break;

    default:
      D_Assert(FALSE, "Add case to NET_StringToMessage!");
    }
  
  return strLogging;
}

/* EOF */