[go: up one dir, main page]

Menu

[904618]: / src / sn_comm.c  Maximize  Restore  History

Download this file

2369 lines (1934 with data), 52.3 kB

   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
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2011-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.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 3
// 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.
// ----------------------------------------------------------------------------
// DESCRIPTION: Network transmission
/***************
*** INCLUDES ***
***************/
#include "sn.h"
#include "i_util.h"
#include "console.h"
#include "dstrings.h"
#include "p_saveg.h"
#include "p_demcmp.h"
#include "p_info.h"
#include "g_state.h"
#include "z_zone.h"
#include "d_player.h"
#include "w_wad.h"
#include "d_main.h"
#include "d_netcmd.h"
#include "i_system.h"
/****************
*** CONSTANTS ***
****************/
/* D_ClientStage_t -- Client connect stage */
typedef enum D_ClientStage_e
{
DCS_LISTWADS,
DCS_CHECKWADS,
DCS_DOWNLOADWADS,
DCS_SWITCHWADS,
DCS_REQUESTSAVE,
DCS_GETSAVE,
DCS_CANPLAYNOW,
DCS_PLAYING
} D_ClientStage_t;
#define GHOSTS (*g_HostsP)
#define GNUMHOSTS (*g_NumHostsP)
/*****************
*** STRUCTURES ***
*****************/
#define CHAINSIZE 128
#define ENCODESIZE 16384
#define MAXJOBHOSTS 64
#define JOBDEFAULTTARGET 3 // target at 3 tics retrans
#define JOBMINTICCAP 5 // Do not retransmit tics to far ahead
#define SERVERPINGOUTWARN (TICRATE * 5)
#define SERVERPINGOUTTIME (TICRATE * 30)
/* D_WADChain_t -- WAD Chain */
typedef struct D_WADChain_s
{
char DOS[CHAINSIZE];
char Full[CHAINSIZE];
char Sum[CHAINSIZE];
const WL_WADFile_t* WAD;
struct D_WADChain_s* Prev;
struct D_WADChain_s* Next;
bool_t Want; // Wants
} D_WADChain_t;
/* D_JobHost_t -- Remote host specifier in a job */
typedef struct D_JobHost_s
{
SN_Host_t* Host; // Host pointer
int32_t ArrID; // Array ID
bool_t Clear; // Clear host
tic_t LastSend; // Last Send
tic_t NextSend; // Time to send next
tic_t Counter; // Counter
tic_t Target; // Target delay
} D_JobHost_t;
/* D_XMitJob_t -- Transmission jobs, who needs what */
typedef struct D_XMitJob_s
{
tic_t GameTic; // Tic job is for
uint8_t EncData[ENCODESIZE]; // Encoded data buffer
uint32_t EncSize; // Encode size
D_JobHost_t Dests[MAXJOBHOSTS]; // Job hosts
int32_t NumDests; // Number of destinations
} D_XMitJob_t;
/**************
*** GLOBALS ***
**************/
extern SN_Host_t*** g_HostsP;
extern int32_t* g_NumHostsP;
extern int32_t g_IgnoreWipeTics;
/*************
*** LOCALS ***
*************/
static I_NetSocket_t* l_Sock;
static I_HostAddress_t l_HostAddr;
static D_BS_t* l_BS;
static D_WADChain_t* l_WADTail;
static D_ClientStage_t l_Stage;
static uint8_t l_IsIWADMap, l_IsFreeDoom, l_IWADMission;
static tic_t l_LastRanTime;
static D_XMitJob_t l_XStack[MAXNETXTICS]; // Transmission stack
static int32_t l_XAt; // Current transmission at
static SN_PingWin_t l_SvPings[MAXPINGWINDOWS]; // Ping windows
static int8_t l_SvPingAt; // Current window
static tic_t l_SvNextPing; // Time of next ping
static tic_t l_SvLastPing; // Last ping time
static int32_t l_SvPing; // Ping of server
// net_lanbroadcast -- Broadcast to LAN
CONL_StaticVar_t l_NETLanBroadcast =
{
CLVT_INTEGER, c_CVPVBoolean, CLVF_SAVE,
"net_lanbroadcast", DSTR_CVHINT_CONMONOSPACE, CLVVT_STRING, "true",
NULL
};
/****************
*** FUNCTIONS ***
****************/
/* SN_DeleteJob() -- Delets job from list */
// Assumes for loop with i++ in for statement
static void SN_DeleteJob(D_XMitJob_t* const a_Job, int32_t* const a_iP)
{
int32_t i;
D_XMitJob_t* Job;
/* Find job in buffer */
for (i = 0; i < l_XAt; i++)
if ((Job = &l_XStack[i]) == a_Job)
{
// Wipe any trace of the job
memmove(&l_XStack[i], &l_XStack[i + 1], sizeof(l_XStack[0]) * (MAXNETXTICS - (i)));
l_XAt--;
// If i was passed, lower value
if (a_iP)
if (*a_iP >= i)
*a_iP -= 1;
// Job was deleted
return;
}
}
/* SN_DeleteIndexInJob() -- Deletes index in this job */
static void SN_DeleteIndexInJob(D_XMitJob_t* const a_Job, int32_t* const a_iP)
{
int32_t i;
/* Check */
if (!a_Job || !a_iP)
return;
/* Move down */
for (i = *a_iP; i < a_Job->NumDests; i++)
a_Job->Dests[i] = a_Job->Dests[i + 1];
// Decrement
*a_iP -= 1;
a_Job->NumDests -= 1;
}
/* SN_ClearJobs() -- Clears all jobs */
void SN_ClearJobs(void)
{
/* Loop deletion */
while (l_XAt > 0)
SN_DeleteJob(&l_XStack[0], NULL);
}
/* SN_XMitTics() -- Transmit tics to local client */
void SN_XMitTics(const tic_t a_GameTic, SN_TicBuf_t* const a_Buffer)
{
static D_JobHost_t* HostL;
static int32_t NumHostL, MaxHostL;
D_XMitJob_t* Job;
int32_t i;
SN_Host_t* Host;
uint8_t* OutD;
/* Wipe the host list */
if (HostL)
{
memset(HostL, 0, sizeof(*HostL) * MaxHostL);
NumHostL = 0;
}
/* Go through hosts and determine who needs this nice new tic */
for (i = 0; i < GNUMHOSTS; i++)
if ((Host = GHOSTS[i]))
{
// Ignore local hosts (they are the server!)
if (Host->Local)
continue;
// Host disconnected and will be cleaned up
if (Host->Cleanup)
continue;
// Host is in initial connect stage
if (!Host->Save.Has)
{
// Does not want save game
if (!Host->Save.Want)
continue;
// This tic is before their join window tic
if (a_GameTic < Host->Save.TicTime)
continue;
}
// Need to resize host list?
if (NumHostL >= MaxHostL)
{
Z_ResizeArray((void**)&HostL, sizeof(*HostL),
MaxHostL, MaxHostL + 1);
MaxHostL++;
}
// At end of jobs?
if (NumHostL >= MAXJOBHOSTS - 1)
{
SN_DisconnectHost(Host, "No more jobs available.");
continue;
}
// Place host here
HostL[NumHostL].Host = Host;
HostL[NumHostL].Target = JOBDEFAULTTARGET;
HostL[NumHostL++].ArrID = i;
}
/* No hosts want this tic */
if (!NumHostL)
return;
/* Place job at end */
Job = NULL;
if (l_XAt < MAXNETXTICS)
Job = &l_XStack[l_XAt++];
// really bad!
if (!Job)
return;
/* Initialize job */
memset(Job, 0, sizeof(*Job));
Job->GameTic = a_GameTic;
// Encode tic buffer and clone the data into the job
SN_EncodeTicBuf(a_Buffer, &OutD, &Job->EncSize, DXNTBV_LATEST);
memmove(Job->EncData, OutD, (Job->EncSize < ENCODESIZE ? Job->EncSize : ENCODESIZE));
// Put all players that should get this job
memmove(Job->Dests, HostL, sizeof(*HostL) * NumHostL);
Job->NumDests = NumHostL;
}
/* SN_OkTics() -- Tics that can be run by the game */
int32_t SN_OkTics(tic_t* const a_LocalP, tic_t* const a_LastP)
{
int32_t i;
SN_Host_t* Host;
int32_t SaveID;
bool_t Kick;
D_XMitJob_t* Job;
tic_t LowTic;
/* Do not move forward if not connected */
if (!SN_IsConnected())
return 0;
/* Server */
if (SN_IsServer())
{
// Job tranmission buffer is almost full!
// A client is lagging
if (l_XAt >= MAXNETXTICS - 2)
{
*a_LastP = *a_LocalP;
return 0;
}
// Get the earliest gametic in the job buffer
LowTic = (tic_t)-1;
for (i = 0; i < l_XAt; i++)
{
Job = &l_XStack[i];
if (Job->GameTic < LowTic)
LowTic = Job->GameTic;
}
// No low tics?
if (LowTic == (tic_t)-1)
LowTic = gametic;
// Did not make save
SaveID = -1;
Kick = false;
// Go through hosts
for (i = 0; i < (*g_NumHostsP); i++)
if ((Host = (*g_HostsP)[i]))
{
// Remote has no save game?
if (!Host->Cleanup && !Host->Local && !Host->Save.Has && Host->Save.Want && gametic >= Host->Save.TicTime)
{
// Not latched
if (!Host->Save.Latched)
{
// Latch
Host->Save.PTimer = g_ProgramTic;
Host->Save.Latched = true;
// Setup save slot
if (!Kick && SaveID < 0)
{
SaveID = SN_PrepSave();
// Failed to make save
if (SaveID < 0)
Kick = true;
}
// Send file
if (SaveID >= 0)
SN_SendFile(SaveID, Host);
}
// Kick off player
if (Kick)
{
SN_DisconnectHost(Host, "Failed to save game");
continue;
}
// Reset time and delay until transfer
*a_LastP = *a_LocalP;
return 0;
}
}
// Single tics, screw networked players
if (singletics)
return 1;
// Not enough time has passed
if (*a_LocalP <= *a_LastP)
return 0;
// Ignore tics during wipe
if (g_IgnoreWipeTics)
{
g_IgnoreWipeTics = false;
*a_LastP = *a_LocalP;
return 0;
}
// Keep running normal time
i = *a_LocalP - *a_LastP;
*a_LastP = *a_LocalP;
// If the run count exceeds the oldest job count
if (gametic + i > LowTic + (MAXNETXTICS - 1))
return 0;
// Return count
return i;
}
/* Client */
else
{
return SN_NumSeqTics();
}
}
/* SN_NetCreate() -- Creates network connection */
// Either listener or remote
bool_t SN_NetCreate(const bool_t a_Listen, const char* const a_Addr, const uint16_t a_Port)
{
I_HostAddress_t Host;
uint32_t Flags, Fails;
uint16_t Port;
static bool_t Regged;
/* Register Vars */
if (!Regged)
{
CONL_VarRegister(&l_NETLanBroadcast);
Regged = true;
}
/* Check */
// Need address if not listening
if (!a_Listen && !a_Addr)
return false;
/* If socket exists, terminate */
if (l_Sock)
SN_NetTerm("Creating new socket");
/* Create socket */
// Clear
memset(&Host, 0, sizeof(Host));
// Initial Flags
Flags = 0;
// Setup host
if (a_Addr)
I_NetNameToHost(NULL, &Host, a_Addr);
// IPv6?
if (Host.IPvX == INIPVN_IPV6)
Flags |= INSF_V6;
// Port
Port = 0;
if (a_Port != 0)
Port = a_Port;
// Do not use port obtained from address if connecting
// Because it will fail on the same system
else if (a_Listen && Host.IPvX && Host.Port != 0)
Port = Host.Port;
// Cap, port
if (Port < 1 || Port >= 65536)
{
// Use default port if hosting server
if (a_Listen)
Port = __REMOOD_BASEPORT;
// Otherwise as client, use some random port
else
{
do
{
Port = D_CMakePureRandom() >> UINT32_C(16);
} while (Port < 32767 || Port >= 65535);
}
}
// Create socket
for (Fails = 0; Fails < 10; Fails++)
if ((l_Sock = I_NetOpenSocket(Flags, (a_Listen && Host.IPvX ? &Host : NULL), Port + Fails)))
break; // it worked!
// Failed completely?
if (!l_Sock)
return false;
/* Copy host and setup stream */
if (a_Listen) // Copy port for server
Host.Port = Port + Fails;
memmove(&l_HostAddr, &Host, sizeof(Host));
l_BS = D_BSCreateNetStream(l_Sock);
/* Socket created */
return true;
}
/* SN_NetTerm() -- Terminates network connection */
void SN_NetTerm(const char* const a_Reason)
{
const char* Reason;
int32_t i;
/* If there is no socket, do not terminate */
if (!l_Sock)
return;
/* Reason */
if (a_Reason)
Reason = a_Reason;
else
Reason = "None given";
/* Send Bye */
D_BSBaseBlock(l_BS, "QUIT");
D_BSws(l_BS, Reason);
for (i = 0; i < 5; i++)
D_BSRecordNetBlock(l_BS, &l_HostAddr);
/* Drop all clients */
SN_DropAllClients(a_Reason);
/* Clear file transfers */
SN_ClearFiles();
/* Close socket */
D_BSCloseStream(l_BS);
l_BS = NULL;
I_NetCloseSocket(l_Sock);
l_Sock = NULL;
/* Clear host */
memset(&l_HostAddr, 0, sizeof(l_HostAddr));
/* Disconnect message */
CONL_OutputUT(CT_NETWORK, DSTR_DXP_DISCONNED, "%s\n", Reason);
}
/* SN_HasSocket() -- Socket exists */
bool_t SN_HasSocket(void)
{
return !!l_Sock;
}
/* SN_DisconnectHost() -- Disconnects another host */
void SN_DisconnectHost(SN_Host_t* const a_Host, const char* const a_Reason)
{
int i;
/* Check */
if (!l_BS || !a_Host || !SN_IsServer() || a_Host->Local)
return;
/* Already cleaning up */
if (a_Host->Cleanup)
return;
/* Cleanup */
a_Host->Cleanup = true;
// Change reason
if (a_Reason)
{
strncpy(a_Host->QuitReason, (a_Reason ? a_Reason : "No Reason"), MAXQUITREASON);
a_Host->QuitReason[MAXQUITREASON - 1] = 0;
}
/* Send them a packet */
D_BSBaseBlock(l_BS, "QUIT");
D_BSws(l_BS, a_Host->QuitReason);
for (i = 0; i < 5; i++)
D_BSRecordNetBlock(l_BS, &a_Host->Addr);
}
/* SN_RequestPortNet() -- Request port from the network */
void SN_RequestPortNet(const uint32_t a_ProcessID)
{
/* If there is no socket, do not ask */
// Also do not ask if we are the server
if (!l_BS || !l_Sock || SN_IsServer())
return;
/* Build packet */
D_BSBaseBlock(l_BS, "WANT");
D_BSwu32(l_BS, a_ProcessID);
D_BSwcu64(l_BS, g_ProgramTic);
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
/* SN_UnplugPortNet() -- Request that the server unplug this port */
void SN_UnplugPortNet(SN_Port_t* const a_Port)
{
/* Check */
if (!a_Port)
return;
}
/* SN_PortJoinGame() -- Request that port join the game */
void SN_PortJoinGame(SN_Port_t* const a_Port)
{
/* Check */
if (!l_BS || !l_Sock || SN_IsServer() || !a_Port)
return;
/* Build packet */
D_BSBaseBlock(l_BS, "PJGG");
D_BSwu32(l_BS, a_Port->ID);
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
/* SN_ReqSpectatePort() -- Request port spectate */
void SN_ReqSpectatePort(const uint32_t a_Player)
{
/* Check */
if (!l_BS || !l_Sock || SN_IsServer() || a_Player < 0 || a_Player >= MAXPLAYERS)
return;
/* Build packet */
D_BSBaseBlock(l_BS, "RSPP");
D_BSwi32(l_BS, a_Player);
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
/* SN_WaitingForSave() -- Waiting for save game */
bool_t SN_WaitingForSave(void)
{
// Only if wanting save
if (l_Sock && SN_IsConnected() && (l_Stage == DCS_REQUESTSAVE || l_Stage == DCS_GETSAVE))
return true;
return false;
}
/* SN_SendSyncCode() -- Sends sync code to client */
void SN_SendSyncCode(const tic_t a_GameTic, const uint32_t a_Code)
{
/* Check */
if (!l_BS || !l_Sock || SN_IsServer())
return;
/* Build packet */
D_BSBaseBlock(l_BS, "SYNC");
D_BSwcu64(l_BS, a_GameTic);
D_BSwu32(l_BS, a_Code);
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
/* SN_SendChat() -- Sends chat command */
void SN_SendChat(SN_Port_t* const a_Port, const bool_t a_Team, const char* const a_Text)
{
uint8_t Mode;
const char* p;
I_HostAddress_t* AddrP;
SN_Port_t* Target;
/* No origin */
if (!a_Port || !a_Text)
return;
/* Base pointer */
p = a_Text;
Target = NULL;
/* Who should recieve message? */
if (a_Team)
if (!a_Port->Player)
Mode = 2; // Spec
else
Mode = 1; // Team
else
Mode = 0; // Everyone
/* Special commands */
// Force team chat?
if (!strncmp("/team ", p, 6))
{
Mode = 1;
p += 6;
}
/* Limitize Value? */
// Team mode but no teams
if (Mode == 1 && !P_GMIsTeam())
Mode = 0;
/* Server can directly encode message */
if (SN_IsServer())
{
SN_DirectChat(a_Port->Host->ID, a_Port->ID, Mode, 0, p);
}
/* Otherwise, send request to server */
else if (l_Sock)
{
D_BSBaseBlock(l_BS, "CHAT");
D_BSwu32(l_BS, a_Port->ID);
D_BSwu8(l_BS, Mode);
D_BSwu32(l_BS, (Target ? Target->ID : 0));
D_BSwu32(l_BS, ++a_Port->ChatID);
D_BSws(l_BS, p);
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
}
/* SN_SetLastTic() -- Sets the last tic running for */
void SN_SetLastTic(void)
{
l_LastRanTime = g_ProgramTic;
}
/* SN_AppendLocalCmds() -- Append local commands */
void SN_AppendLocalCmds(D_BS_t* const a_BS)
{
SN_Host_t* Host;
SN_Port_t* Port;
int32_t i, p;
SN_TicBuf_t TicBuf;
ticcmd_t* Cmd;
uint8_t* OutD;
uint32_t OutS;
/* Check */
if (!a_BS || !(Host = SN_MyHost()))
return;
/* Clear Buffer */
memset(&TicBuf, 0, sizeof(TicBuf));
/* Go through ports */
for (i = 0; i < Host->NumPorts; i++)
if ((Port = Host->Ports[i]))
{
// No commands to send or not playing
if (!Port->LocalAt || !Port->Player)
continue;
// Get player number
p = Port->Player - players;
// Setup area to place commands
Cmd = &TicBuf.Tics[p];
TicBuf.PIGRevMask |= (1 << p); // clear PIG mask
// Merge commands into this
D_XNetMergeTics(Cmd, Port->LocalBuf, Port->LocalAt);
Port->LocalAt = 0;
memset(Port->LocalBuf, 0, sizeof(Port->LocalBuf));
// Do local turning and aiming
SN_LocalTurn(Port, Cmd);
}
/* Check to see if anything was ever encoded */
if (!TicBuf.PIGRevMask)
return;
// Reverse mask
TicBuf.PIGRevMask = ~TicBuf.PIGRevMask;
/* Encode and write */
OutD = NULL;
OutS = 0;
SN_EncodeTicBuf(&TicBuf, &OutD, &OutS, DXNTBV_LATEST);
// Write
D_BSwu32(a_BS, OutS);
D_BSWriteChunk(a_BS, OutD, OutS);
}
/* SN_SendSettings() -- Sends setting to server */
void SN_SendSettings(SN_Port_t* const a_Port, const SN_PortSetting_t a_Setting, const int32_t a_IntVal, const char* const a_StrVal, const uint32_t a_StrLen)
{
const uint8_t* p;
int32_t i;
/* Check */
if (!l_BS || !l_Sock || SN_IsServer())
return;
/* Build packet */
D_BSBaseBlock(l_BS, "SETT");
// Write Data
D_BSwu32(l_BS, a_Port->Host->ID);
D_BSwu32(l_BS, a_Port->ID);
D_BSwu8(l_BS, (a_Port->Player ? a_Port->Player - players : 255));
D_BSwu16(l_BS, a_Setting);
// String?
if (a_StrVal)
{
// No length specified
if (!a_StrLen)
for (p = a_StrVal, i = 0; i < MAXTCSTRINGCAT; i++)
{
// Write
D_BSwu8(l_BS, *p);
// Increase p as long as it is not NULL
if (*p)
p++;
}
// Length Specified
else
for (i = 0; i < MAXTCSTRINGCAT; i++)
if (i < a_StrLen)
D_BSwu8(l_BS, ((uint8_t*)a_StrVal)[i]);
else
D_BSwu8(l_BS, 0);
}
// Integer
else
{
D_BSwi32(l_BS, a_IntVal);
// Padding
for (i = 4; i < MAXTCSTRINGCAT; i++)
D_BSwu8(l_BS, 0);
}
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
/* SN_DoConnect() -- Do connection logic */
void SN_DoConnect(void)
{
#define BUFSIZE 128
char Buf[BUFSIZE];
static tic_t Timeout;
/* Not yet timed out */
if (g_ProgramTic < Timeout)
return;
/* Time out in the future */
Timeout = g_ProgramTic + TICRATE;
I_NetHostToString(&l_HostAddr, Buf, BUFSIZE);
CONL_OutputUT(CT_NETWORK, DSTR_DXP_CONNECTING, "%s\n", Buf);
/* Build connect info */
D_BSBaseBlock(l_BS, "CONN");
D_BSwu8(l_BS, SN_IsServer());
D_BSRecordNetBlock(l_BS, &l_HostAddr);
#undef BUFSIZE
}
/* SN_DoServer() -- Do server stuff */
void SN_DoServer(D_BS_t* const a_BS)
{
D_XMitJob_t* Job;
D_JobHost_t* JHost;
SN_Host_t* Host;
int32_t i, j;
SN_PingWin_t* PWin;
static int32_t LastSlot;
tic_t PCap, GCap;
static tic_t LastAdvert;
I_HostAddress_t Addr;
/* Transmit jobs to hosts that need them */
for (i = 0; i < l_XAt; i++)
{
// Get current job
Job = &l_XStack[i];
// If job has no transmission sources remaining, delete
// Or it is in the past!
if (!Job->NumDests || (gametic > MAXNETXTICS && Job->GameTic < gametic - MAXNETXTICS))
{
SN_DeleteJob(Job, &i);
continue;
}
// Go through destination hosts
for (j = 0; j < Job->NumDests; j++)
{
// Get job host
JHost = &Job->Dests[j];
// Find host
Host = NULL;
if (JHost->Host == GHOSTS[JHost->ArrID])
Host = JHost->Host;
// Host is no longer a valid destination (or want to clear)
if (JHost->Clear || !Host || (Host &&
(Host->Cleanup || Host->MinTic > Job->GameTic)
))
{
SN_DeleteIndexInJob(Job, &j);
continue;
}
// Not ready to send yet
if (JHost->NextSend > 0 && g_ProgramTic < JHost->NextSend)
continue;
// Calculate propogating delay for the next send request
if (JHost->NextSend)
{
// Too far in the future (they do not need this tic yet)
if (Job->GameTic > JHost->Host->MinTic + JOBMINTICCAP)
continue;
// Increase counter
JHost->Counter++;
// Counter exceeded target (increase target time)
if (JHost->Counter >= JHost->Target)
{
JHost->Target += 2;
JHost->Counter = 0;
}
}
// Time to send the command again
JHost->NextSend = g_ProgramTic + JHost->Target;
JHost->LastSend = g_ProgramTic;
// Build packet
D_BSBaseBlock(a_BS, "JOBT");
D_BSwcu64(a_BS, Job->GameTic);
D_BSwu32(a_BS, Job->EncSize);
D_BSWriteChunk(a_BS, Job->EncData, Job->EncSize);
D_BSRecordNetBlock(a_BS, &Host->Addr);
}
}
/* Determine if players get kicked, by timeouts */
// The last seen time of the host is the time of the latest ack of a tic.
// So if the game pauses because someone is dropping, then their timer will
// incrase also.
// Waiting for players after 3 seconds
if (g_ProgramTic > l_LastRanTime + (TICRATE * 3))
; // TODO FIXME
// Ping cap
// TODO FIXME: make CVar
if (g_ProgramTic < (TICRATE * 12))
PCap = 0;
else
PCap = g_ProgramTic - (TICRATE * 12);
// Gametic cap
if (gametic < (MAXNETXTICS - 2))
GCap = gametic;
else
GCap = gametic - (MAXNETXTICS - 2);
// TODO FIXME: Make CVar rather than hardcoded (10 secs currently)
if (g_ProgramTic > l_LastRanTime + (TICRATE * 10))
for (i = 0; i < GNUMHOSTS; i++)
if ((Host = GHOSTS[i]))
{
// Ignore locals and cleanups
if (Host->Local || Host->Cleanup)
continue;
// Ping timeout
if (Host->LastPing < PCap)
{
SN_DisconnectHost(Host, "Timed out");
continue;
}
// This host appears to be frozen
// Game too far in the past
if (Host->MinTic <= GCap)
{
SN_DisconnectHost(Host, "Failed to acknowlegde");
continue;
}
}
/* Calculate Pings */
// Lower calculating by slotting ping requests
for (; LastSlot < GNUMHOSTS; LastSlot++)
if ((Host = GHOSTS[LastSlot]))
{
// Ignore locals and cleanups
if (Host->Local || Host->Cleanup)
continue;
// No ping yet
if (g_ProgramTic < Host->NextPing)
continue;
// Ping again in two seconds (every second is not that important)
Host->NextPing = g_ProgramTic + (TICRATE << 1);
// Get the next ping window
++Host->PingAt;
Host->PingAt &= MAXPINGWINDOWS - 1;
PWin = &Host->Pings[Host->PingAt];
// Generate new random number
PWin->Code = D_CMakePureRandom();
PWin->SendTime = g_ProgramTic;
PWin->Millis = I_GetTimeMS();
// Build packet to send to them
D_BSBaseBlock(l_BS, "PING");
D_BSwu8(l_BS, Host->PingAt);
D_BSwu32(l_BS, PWin->Code);
D_BSRecordNetBlock(l_BS, &Host->Addr);
// Do not ping anyone else
LastSlot++; // And go to next slot
break;
}
// Ran out of slots
if (LastSlot >= GNUMHOSTS)
LastSlot = 0;
/* Advertise server over LAN */
if (l_NETLanBroadcast.Value->Int)
{
// Advertise to LAN
if (g_ProgramTic < LastAdvert)
return;
// Advertise this
LastAdvert = g_ProgramTic + (TICRATE * 10);
// Currently just send to IPv4 multicast address
memset(&Addr, 0, sizeof(Addr));
if (I_NetNameToHost(l_Sock, &Addr, "224.0.0.167:29500"))
{
D_BSBaseBlock(l_BS, "ADVR");
// Port because it is lost in a multicast
D_BSwu16(l_BS, l_HostAddr.Port);
D_BSRecordNetBlock(l_BS, &Addr);
}
}
}
/* SN_DoClient() -- Do client stuff */
void SN_DoClient(D_BS_t* const a_BS)
{
static tic_t WADTimeout, SaveTimeout, PlayTimeout;
D_WADChain_t* CWAD, *FirstCWAD;
const WL_WADFile_t* WAD, *ExtraWAD;
int32_t i, j, k, l;
D_IWADInfoEx_t* Info;
SN_PingWin_t* PWin;
/* Send ping to server (to make sure it is alive) */
if (g_ProgramTic >= l_SvNextPing)
{
// Ping again in two seconds (every second is not that important)
l_SvNextPing = g_ProgramTic + (TICRATE << 1);
// Get the next ping window
++l_SvPingAt;
l_SvPingAt &= MAXPINGWINDOWS - 1;
PWin = &l_SvPings[l_SvPingAt];
// Generate new random number
PWin->Code = D_CMakePureRandom();
PWin->SendTime = g_ProgramTic;
PWin->Millis = I_GetTimeMS();
// Build packet to send to them
D_BSBaseBlock(l_BS, "PING");
D_BSwu8(l_BS, l_SvPingAt);
D_BSwu32(l_BS, PWin->Code);
D_BSRecordNetBlock(l_BS, &l_HostAddr);
}
/* Server is going to ping out */
if (g_ProgramTic > l_SvLastPing + SERVERPINGOUTWARN)
SN_SetServerLagWarn(l_SvLastPing + SERVERPINGOUTTIME);
else
SN_SetServerLagWarn(0);
/* Server is pinging out? */
if (g_ProgramTic > l_SvLastPing + SERVERPINGOUTTIME)
SN_PartialDisconnect("Connection to server lost");
/* Which stage? */
switch (l_Stage)
{
// Ask server for list of WADs
case DCS_LISTWADS:
if (g_ProgramTic < WADTimeout)
return;
WADTimeout = g_ProgramTic + TICRATE;
D_BSBaseBlock(a_BS, "LIST");
D_BSRecordNetBlock(a_BS, &l_HostAddr);
return;
// Check local WADs
case DCS_CHECKWADS:
// No WADs? Now we are feeling OK!
if (!l_WADTail)
{
l_Stage = DCS_REQUESTSAVE;
return;
}
// Go through all WADs
FirstCWAD = 0;
i = j = k = l = 0;
for (CWAD = l_WADTail; CWAD; CWAD = CWAD->Prev)
{
// First?
if (!CWAD->Prev && !FirstCWAD)
FirstCWAD = CWAD;
// Count of WADs
i++;
// If WAD is loaded, ignore
if (CWAD->WAD)
continue;
// Otherwise, try to find the WAD file for this
if (!(WAD = WL_OpenWAD(CWAD->Full, CWAD->Sum)))
if (!(WAD = WL_OpenWAD(CWAD->DOS, CWAD->Sum)))
if (!l_IsFreeDoom || (l_IsFreeDoom && !(WAD = WL_OpenWAD(CWAD->DOS, NULL)))) // Try by name if the server is using FreeDoom (maybe we have it too?)
{
// Need to download
j++;
CONL_OutputUT(CT_NETWORK, DSTR_DTRANSC_GETWAD, "%s\n",
CWAD->Full);
// Want this now, provided not a blacklisted sum
if (!D_CheckWADBlacklist(CWAD->Sum))
CWAD->Want = true;
else
CONL_OutputUT(CT_NETWORK, DSTR_DTRANSC_BLACKLIST, "%s\n",
CWAD->Full);
// Try other WADs
continue;
}
// Got this WAD
if (WAD)
{
CWAD->WAD = WAD;
k++;
}
}
// Need at least one WAD file
if (j > 0)
{
// Client joins server, client gets...
// v Client \ Server > | Doom | Doom II | Free1 | Free2 |
// --------------------+------+---------+-------+-------+
// Doom | PLAY | KICK | PLAY* | KICK |
// Doom II | KICK | PLAY | KICK | PLAY* |
// FreeDoom (Doom) | PLAY*| KICK | PLAY | KICK |
// FreeDoom (Doom II) | KICK | PLAY* | KICK | PLAY |
// * = Not playing IWAD Level
// You AWLAYS need an IWAD (even if it is just shareware or FreeDoom)
if (!FirstCWAD->WAD)
{
// Current IWAD selected
WAD = WL_IterateVWAD(NULL, true);
// Get Info
if (!(Info = D_GetThisIWAD()))
{
SN_Disconnect(false, "No information on current IWAD being used.");
return;
}
// Are we FreeDooming?
i = 0;
if (Info->Flags & CIF_FREEDOOM)
i = 1;
// Mode mismatch (Doom vs Doom 2 vs Heretic vs Hexen vs ...)
if (Info->mission != l_IWADMission)
{
SN_Disconnect(false, "Playing different game (e.g. Doom vs Doom 2).");
return;
}
// Server is FreeDooming
if (l_IsFreeDoom)
{
// If server is on IWAD level and we are not FreeDooming
if (l_IsIWADMap && !i)
{
SN_Disconnect(false, "Incompatible IWAD level.");
return;
}
// Server is on some PWAD, so that is OK
else
{
FirstCWAD->WAD = WAD;
j--;
}
}
// Server is not FreeDooming
else
{
// If server is on an IWAD level, just die
if (l_IsIWADMap)
{
SN_Disconnect(false, "Incompatible IWAD level.");
return;
}
// Otherwise, use equiv. FreeDoom WAD (or Plutonia/TNT??)
else
{
FirstCWAD->WAD = WAD;
j--;
}
}
}
// Go to download stage (if still unresolved)
if (j > 0)
l_Stage = DCS_DOWNLOADWADS;
}
// No WADs needed
else
l_Stage = DCS_SWITCHWADS;
break;
// Download WADs
case DCS_DOWNLOADWADS:
SN_Disconnect(false, "Downloading WADs not implemented");
break;
case DCS_SWITCHWADS:
// Do not need to redo
j = 0;
// Find first WAD
FirstCWAD = NULL;
for (CWAD = l_WADTail; CWAD; CWAD = CWAD->Prev)
{
// First?
if (!CWAD->Prev && !FirstCWAD)
FirstCWAD = CWAD;
}
// Go through WADs owned by server and see if they differ.
// If there is any difference, then WADs need to be cycled
for (WAD = NULL, i = 0, CWAD = FirstCWAD; CWAD; CWAD = CWAD->Next, i++)
{
// Get current WAD
WAD = WL_IterateVWAD(WAD, true);
// Link for ReMooD.WAD
if (!i)
ExtraWAD = WL_IterateVWAD(WAD, true);
else
ExtraWAD = WAD;
// WAD is not matched
if (CWAD->WAD != WAD || (!CWAD->Next != !WL_IterateVWAD(ExtraWAD, true)))
{
j = 1;
break;
}
// Skip ReMooD.WAD
if (!i)
WAD = WL_IterateVWAD(WAD, true);
}
// Need to redo
if (j)
{
// Lock OCCB
WL_LockOCCB(true);
// Remember ReMooD.WAD
WAD = WL_IterateVWAD(WL_IterateVWAD(NULL, true), true);
// Pop all WADs
while (WL_PopWAD())
;
// Go through CWADs and repop
for (i = 0, CWAD = FirstCWAD; CWAD; CWAD = CWAD->Next, i++)
{
WL_PushWAD(CWAD->WAD);
// Push ReMooD.WAD back on the stack
if (!i)
WL_PushWAD(WAD);
}
// Unlock OCCB
WL_LockOCCB(false);
}
// Go to next stage
l_Stage++;
break;
// Request save game
case DCS_REQUESTSAVE:
if (g_ProgramTic < SaveTimeout)
return;
SaveTimeout = g_ProgramTic + TICRATE;
D_BSBaseBlock(a_BS, "SAVE");
D_BSRecordNetBlock(a_BS, &l_HostAddr);
break;
// Get savegame
case DCS_GETSAVE:
break;
// Can play now
case DCS_CANPLAYNOW:
if (g_ProgramTic < PlayTimeout)
return;
PlayTimeout = g_ProgramTic + TICRATE;
D_BSBaseBlock(a_BS, "PLAY");
D_BSRecordNetBlock(a_BS, &l_HostAddr);
break;
// Now playing game
case DCS_PLAYING:
break;
// Unknown Stage
default:
return;
}
}
/*****************************************************************************/
/* DT_CONN() -- Connection request */
void DT_CONN(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
#define BUFSIZE 256
char Buf[BUFSIZE];
uint8_t IsServer, *Wp;
SN_Host_t* New;
/* If not allocated, build host for it */
if (!a_Host)
{
// Read data bits
IsServer = D_BSru8(a_BS);
// Same side?
if (!!IsServer == SN_IsServer())
return;
// Put trying to connect...
I_NetHostToString(a_Addr, Buf, BUFSIZE);
CONL_OutputUT(CT_NETWORK, DSTR_DXP_CLCONNECT, "%s\n", Buf);
// Create a host for them
New = SN_CreateHost();
// Create unique ID
do
{
New->ID = D_CMakePureRandom();
} while (!New->ID || SN_HostByID(New->ID) != New);
// Fill info
memmove(&New->Addr, a_Addr, sizeof(*a_Addr));
New->BS = a_BS;
// Create packet
if (SN_ExtCmdInGlobal(DTCT_SNJOINHOST, &Wp))
{
LittleWriteUInt32((uint32_t**)&Wp, New->ID);
LittleWriteUInt32((uint32_t**)&Wp, 0);
WriteUInt8(&Wp, 0);
}
}
// Use host here
else
New = a_Host;
/* Reply with host info */
D_BSBaseBlock(a_BS, "HELO");
// Write ID
D_BSwu32(a_BS, New->ID);
D_BSRecordNetBlock(a_BS, a_Addr);
#undef BUFSIZE
}
/* DT_HELO() -- Connection granted */
void DT_HELO(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
SN_Host_t* New;
/* Ignore if already connected */
if (SN_IsConnected())
return;
/* Set connected */
SN_SetConnected(true);
l_SvLastPing = g_ProgramTic; // So there is no partial disconn
/* Create host */
New = SN_CreateHost();
New->ID = D_BSru32(a_BS);
New->Local = true;
SN_SetMyHost(New);
l_Stage = 0;
}
/* DT_LIST() -- List WADS */
void DT_LIST(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
const WL_WADFile_t* Rover, *First;
int32_t i;
D_IWADInfoEx_t* Info;
/* Reply with wad list */
D_BSBaseBlock(a_BS, "WADL");
// If playing a level and that level is an IWAD map
// This is for FreeDoom to prevent joining when the IWAD mismatches.
First = WL_IterateVWAD(NULL, true);
if (g_CurrentLevelInfo && g_CurrentLevelInfo->WAD == First)
D_BSwu8(a_BS, 1);
else
D_BSwu8(a_BS, 0);
// Doing a FreeDoom game (allow FreeDoomers with differing FreeDoom.WADs)
Info = D_GetThisIWAD();
if (Info && (Info->Flags & CIF_FREEDOOM))
D_BSwu8(a_BS, 1);
else
D_BSwu8(a_BS, 0);
// Mission of IWAD
if (Info)
D_BSwu8(a_BS, Info->mission);
else
D_BSwu8(a_BS, 0);
// Send WAD Order
for (Rover = First, i = 0; Rover; Rover = WL_IterateVWAD(Rover, true), i++)
{
// Ignore ReMooD.WAD
if (i == 1)
continue;
// Names and MD5
D_BSws(a_BS, WL_GetWADName(Rover, false));
D_BSws(a_BS, WL_GetWADName(Rover, true));
D_BSws(a_BS, Rover->CheckSumChars);
}
// NUL for end
D_BSwu32(a_BS, 0);
/* Send */
D_BSRecordNetBlock(a_BS, a_Addr);
}
/* DT_WADL() -- WAD List */
void DT_WADL(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
D_WADChain_t* Rover;
D_WADChain_t Temp;
const WL_WADFile_t* WAD;
/* Only on first stage */
if (l_Stage != DCS_LISTWADS || SN_IsServer())
return;
/* Store WADs from server */
// Clear tail
for (Rover = l_WADTail; Rover; Rover = l_WADTail)
{
// Set previous
l_WADTail = Rover->Prev;
// Free current
Z_Free(Rover);
}
l_WADTail = NULL;
CONL_OutputUT(CT_NETWORK, DSTR_DXP_WADHEADER, "\n");
// Is this an IWAD map? What about FreeDoom?
l_IsIWADMap = D_BSru8(a_BS);
l_IsFreeDoom = D_BSru8(a_BS);
l_IWADMission = D_BSru8(a_BS);
// Read in contents
for (;;)
{
// Clear
memset(&Temp, 0, sizeof(Temp));
// DOS, Full, Sum
D_BSrs(a_BS, Temp.DOS, CHAINSIZE);
D_BSrs(a_BS, Temp.Full, CHAINSIZE);
D_BSrs(a_BS, Temp.Sum, CHAINSIZE);
// End
if (!Temp.DOS[0])
break;
// Print
CONL_OutputUT(CT_NETWORK, DSTR_DXP_WADENTRY, "%s%s%s\n", Temp.DOS, Temp.Full, Temp.Sum);
// Try to find WAD by sum (not by name, in case of renames??)
for (WAD = WL_IterateVWAD(NULL, true); WAD; WAD = WL_IterateVWAD(WAD, true))
if (!strcasecmp(Temp.Sum, WAD->CheckSumChars))
{
Temp.WAD = WAD;
break;
}
// Allocate
Rover = Z_Malloc(sizeof(*Rover), PU_STATIC, NULL);
memmove(Rover, &Temp, sizeof(*Rover));
// Chain in
if (l_WADTail)
l_WADTail->Next = Rover;
Rover->Prev = l_WADTail;
l_WADTail = Rover;
}
/* Increase Stage */
l_Stage++;
}
/* DT_QUIT() -- Quit */
void DT_QUIT(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
#define BUFSIZE 128
char Buf[BUFSIZE];
/* Read reason */
D_BSrs(a_BS, Buf, BUFSIZE);
/* If not connected, disconnect */
// Could be in the middle of a connect request, or not playing yet
if (!SN_IsConnected() || (!SN_IsServer() && l_Stage < DCS_PLAYING))
{
SN_Disconnect(false, Buf);
return;
}
/* If we are the server, and this host is local do not disconnect */
else if (SN_IsServer() && a_Host && a_Host->Local)
return;
/* Cleanup remote client if non-local (they quit) */
else if (a_Host && !a_Host->Local)
SN_DisconnectHost(a_Host, Buf);
/* Otherwise, perform a partial disconnect */
// As long as we are not the server!
else if (!SN_IsServer())
SN_PartialDisconnect(Buf);
#undef BUFSIZE
}
/* DT_SAVE() -- Requests save game */
void DT_SAVE(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
/* Check */
if (!SN_IsServer() || !a_Host || a_Host->Local)
return;
/* If already has save, ignore */
if (a_Host->Save.Has)
return;
/* If save not wanted, give them it */
if (!a_Host->Save.Want)
{
// Now wants
a_Host->Save.Want = true;
a_Host->Save.TicTime = gametic + (TICRATE - (gametic % TICRATE));
}
/* Inform them */
D_BSBaseBlock(a_BS, "PSAV");
D_BSRecordNetBlock(a_BS, a_Addr);
}
/* DT_PSAV() -- Requests save game */
void DT_PSAV(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
/* Client only */
if (SN_IsServer() || l_Stage != DCS_REQUESTSAVE)
return;
/* Go to save OK */
l_Stage = DCS_GETSAVE;
}
/* DT_PLAY() -- Client can play now! */
void DT_PLAY(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
/* Server only */
if (!SN_IsServer() || !a_Host || a_Host->Local)
return;
/* Mark them as in the game */
a_Host->Save.Has = true;
/* Inform them */
D_BSBaseBlock(a_BS, "COOL");
D_BSRecordNetBlock(a_BS, a_Addr);
}
/* DT_COOL() -- Our play request was accepted! */
void DT_COOL(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
/* Client Only */
if (SN_IsServer() || l_Stage != DCS_CANPLAYNOW)
return;
/* Start playing */
l_Stage = DCS_PLAYING;
}
/* DT_JOBT() -- Received a job tic */
void DT_JOBT(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
tic_t GameTic;
uint32_t Size;
SN_TicBuf_t* TicBuf;
static uint8_t* Buf;
static uint32_t BufSize;
/* Client Only that is connected */
// Accept can play now in case of some packets being missed.
if (SN_IsServer() || !(l_Stage == DCS_CANPLAYNOW || l_Stage == DCS_PLAYING))
return;
/* Read packet data */
GameTic = D_BSrcu64(a_BS);
Size = D_BSru32(a_BS);
// Size limitation
if (Size > 32767)
Size = 32767;
// Allocate buffer, if needed
if (!Buf || Size > BufSize)
{
// Clear old
if (Buf)
{
Z_Free(Buf);
Buf = NULL;
}
// Make new
Buf = Z_Malloc(Size, PU_STATIC, NULL);
BufSize = Size;
}
// Find tic buffer for this tic
TicBuf = SN_BufForGameTic(GameTic);
// Oops!
if (!TicBuf)
return;
// Never got tic
if (!TicBuf->GotTic)
{
// Read packet into buffer (this is here to reduce CPU cost)
memset(Buf, 0, BufSize);
D_BSReadChunk(a_BS, Buf, Size);
// Decode
if (!SN_DecodeTicBuf(TicBuf, Buf, Size))
{
SN_PartialDisconnect("Tic decode error");
return;
}
// Got tic now yay!
TicBuf->GotTic = true;
// Set the last ping time, to prevent time out
l_SvLastPing = g_ProgramTic;
}
/* Reply to server saying, the tic was recieved and buffered */
D_BSBaseBlock(a_BS, "JOBA");
D_BSwcu64(a_BS, GameTic);
D_BSwcu64(a_BS, gametic);
// Append local port tics to the server
SN_AppendLocalCmds(a_BS);
// Send
D_BSRecordNetBlock(a_BS, a_Addr);
}
/* DT_JOBA() -- Acknowledged job tic */
void DT_JOBA(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
int32_t i, j;
D_XMitJob_t* Job;
tic_t GameTic, ClientGT;
static uint8_t* Buf;
static uint32_t BufSize;
uint32_t Size;
SN_TicBuf_t TicBuf;
ticcmd_t* Cmd;
SN_Port_t* Port;
/* Server Only */
if (!SN_IsServer() || !a_Host)
return;
/* Get gametic */
GameTic = D_BSrcu64(a_BS);
ClientGT = D_BSrcu64(a_BS);
/* Set host to this minimum tic */
if (ClientGT > a_Host->MinTic)
a_Host->MinTic = ClientGT;
/* Find job and verify */
for (i = 0; i < l_XAt; i++)
if ((Job = &l_XStack[i]))
{
// Wrong gametic
if (Job->GameTic != GameTic)
continue;
// Find sub host
for (j = 0; j < Job->NumDests; j++)
if (a_Host == Job->Dests[j].Host)
{
Job->Dests[j].Clear = true;
a_Host->LastSeen = g_ProgramTic;
break;
}
// If found, would have been cleared
break;
}
/* Read client tic commands */
Size = D_BSru32(a_BS);
// Nothing to read
if (Size < 0)
return;
// Cap size
if (Size > 16383)
Size = 16383;
// Need to resize buffer
if (BufSize < Size)
{
if (Buf)
Z_Free(Buf);
BufSize = Size;
Buf = Z_Malloc(BufSize, PU_STATIC, NULL);
}
// Read
D_BSReadChunk(a_BS, Buf, Size);
// Decode commands
if (!SN_DecodeTicBuf(&TicBuf, Buf, Size))
return; // Who cares if it is bad
// reverse the pig mask
TicBuf.PIGRevMask = ~TicBuf.PIGRevMask;
/* Copy decoded commands to player ports */
for (i = 0; i < MAXPLAYERS; i++)
{
// No commands for this player
if (!(TicBuf.PIGRevMask & (1 << i)))
continue;
// Find port for this player
for (j = 0; j < a_Host->NumPorts; j++)
if ((Port = a_Host->Ports[j]))
{
// Not playing or wrong player
if (!Port->Player || Port->Player - players != i)
continue;
// Append command to this location
if (Port->LocalAt < MAXLBTSIZE - 1)
Cmd = &Port->LocalBuf[Port->LocalAt++];
else
Cmd = &Port->LocalBuf[0];
// Copy
memmove(Cmd, &TicBuf.Tics[i], sizeof(*Cmd));
}
}
}
/* DT_WANT() -- Client wants another port */
void DT_WANT(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
int32_t i, Total;
SN_Port_t* Port, *PIDMatch;
uint32_t ProcessID, ID;
tic_t GameTic;
uint8_t* Wp;
/* Server Only */
if (!SN_IsServer() || !a_Host)
return;
/* Read Packet */
ProcessID = D_BSru32(a_BS);
GameTic = D_BSrcu64(a_BS);
/* Count the number of ports in this host */
Total = 0;
PIDMatch = NULL;
for (i = 0; i < a_Host->NumPorts; i++)
if ((Port = a_Host->Ports[i]))
{
// If process ID matches, remember that
if (ProcessID && ProcessID == Port->ProcessID)
PIDMatch = Port;
// Increase total
Total++;
}
/* No match and total exceeds count */
// They can only have 4 players connected at once
if (!PIDMatch && Total >= MAXSPLITS)
{
D_BSBaseBlock(a_BS, "FULL");
D_BSwu8(a_BS, Total);
D_BSwu32(a_BS, ProcessID);
D_BSwcu64(a_BS, GameTic);
D_BSRecordNetBlock(a_BS, a_Addr);
return;
}
/* Otherwise, use said (new) port */
if (!PIDMatch)
{
// Create a new port for them
PIDMatch = SN_AddPort(a_Host);
// Setup local port info
if (ProcessID)
PIDMatch->ProcessID = ProcessID;
// Need a unique process ID
else
{
// Pure random may be random enough to prevent mishaps
do
{
ProcessID = D_CMakePureRandom();
} while (!ProcessID);
}
// Generate unique port ID
do
{
ID = D_CMakePureRandom();
} while (!ID || SN_PortByID(ID) || SN_HostByID(ID));
PIDMatch->ID = ID;
// Create packet
if (SN_ExtCmdInGlobal(DTCT_SNJOINPORT, &Wp))
{
LittleWriteUInt32((uint32_t**)&Wp, a_Host->ID);
LittleWriteUInt32((uint32_t**)&Wp, PIDMatch->ID);
WriteUInt8(&Wp, 0);
LittleWriteUInt32((uint32_t**)&Wp, PIDMatch->ProcessID);
}
}
/* Send a basic packet */
D_BSBaseBlock(a_BS, "GIVE");
D_BSwu8(a_BS, Total);
D_BSwu32(a_BS, PIDMatch->Host->ID);
D_BSwu32(a_BS, PIDMatch->ID);
D_BSwu32(a_BS, PIDMatch->ProcessID);
D_BSwcu64(a_BS, GameTic);
D_BSRecordNetBlock(a_BS, a_Addr);
}
/* DT_GIVE() -- Client wants another port */
void DT_GIVE(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
uint8_t Total;
uint32_t ID, HostID, ProcessID;
tic_t GameTic;
SN_Host_t* Host, *MyHost;
SN_Port_t* Port;
/* Client Only */
if (SN_IsServer() || !(l_Stage == DCS_CANPLAYNOW || l_Stage == DCS_PLAYING))
return;
/* Read Packet */
Total = D_BSru8(a_BS);
HostID = D_BSru32(a_BS);
ID = D_BSru32(a_BS);
ProcessID = D_BSru32(a_BS);
GameTic = D_BSrcu64(a_BS);
/* Check to see if it exists already */
if (SN_PortByID(ID))
return;
/* Get current host */
Host = SN_HostByID(HostID);
MyHost = SN_MyHost();
// If this is not our host, ignore
if (!Host || Host != MyHost)
return;
/* Create a new port */
Port = SN_AddPort(MyHost);
// Set local port fields
Port->ID = ID;
Port->ProcessID = ProcessID;
}
/* DT_PING() -- Ping request */
void DT_PING(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
uint8_t At;
uint32_t Code;
/* Read data */
At = D_BSru8(a_BS);
Code = D_BSru32(a_BS);
/* Reply with pong */
D_BSBaseBlock(a_BS, "PONG");
D_BSwu8(a_BS, At);
D_BSwu32(a_BS, Code);
D_BSwcu64(a_BS, g_ProgramTic);
D_BSRecordNetBlock(a_BS, a_Addr);
}
/* DT_PONG() -- Ping reply */
void DT_PONG(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
uint8_t At;
uint32_t Code;
SN_PingWin_t* Win;
tic_t TimeDiff;
int32_t MilliDiff;
/* Read */
At = D_BSru8(a_BS);
Code = D_BSru32(a_BS);
// Illegal at? Or code is zero
if (!Code || At < 0 || At >= MAXPINGWINDOWS)
return;
/* Determine the window to use */
Win = NULL;
// Client
if (!SN_IsServer())
Win = &l_SvPings[At];
// Server
else if (a_Host)
Win = &a_Host->Pings[At];
// Unknown (die)
else
return;
/* Make sure the code matches */
if (!Win->Code || Win->Code != Code)
return;
/* Calculate time difference */
TimeDiff = g_ProgramTic - Win->SendTime;
MilliDiff = I_GetTimeMS() - Win->Millis;
/* Set timers (average) */
// Client
if (!SN_IsServer())
{
l_SvPing = (l_SvPing + MilliDiff) >> 1;
l_SvLastPing = g_ProgramTic;
}
// Server
else if (a_Host)
{
a_Host->Ping = (a_Host->Ping + MilliDiff) >> 1;
a_Host->LastPing = g_ProgramTic;
}
/* Clear window, so it cannot be used again */
// This prevents ping reply cheating to hold a game in WFP forever.
memset(Win, 0, sizeof(*Win));
}
/* DT_PJGG() -- Port join request */
void DT_PJGG(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
uint32_t ID;
SN_Port_t* Port;
/* Server Only */
if (!a_Host || !SN_IsServer())
return;
/* Get Port */
ID = D_BSru32(a_BS);
Port = SN_PortByID(ID);
// No port?
if (!Port)
return;
// Host does not own this port (cannot join other ports)
if (Port->Host != a_Host)
return;
/* Mark them to join */
Port->WillJoin = true;
}
/* DT_SYNC() -- Client Game Synchronization Code */
void DT_SYNC(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
tic_t GameTic;
uint32_t Code;
/* Check */
if (!a_Host || !SN_IsServer())
return;
/* Read Data */
GameTic = D_BSrcu64(a_BS);
Code = D_BSru32(a_BS);
/* Handle sync code from host */
SN_CheckSyncCode(a_Host, GameTic, Code);
}
/* DT_CHAT() -- Client Chats */
void DT_CHAT(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
#define BUFSIZE 128
uint32_t Source, Target;
uint8_t Mode;
SN_Port_t* MyPort, *TargetPort;
char Buf[BUFSIZE];
int32_t Len;
uint32_t ChatID;
/* Check */
if (!a_Host)
return;
/* Read Info */
Source = D_BSru32(a_BS);
Mode = D_BSru8(a_BS);
Target = D_BSru32(a_BS);
ChatID = D_BSru32(a_BS);
memset(Buf, 0, sizeof(Buf));
D_BSrs(a_BS, Buf, BUFSIZE);
// Get source player
MyPort = SN_PortByID(Source);
TargetPort = SN_PortByID(Target);
// Wrong host? indiv and no target?
if (MyPort->Host != a_Host || (Mode == 3 && !TargetPort))
return;
// Already sent this or older message?
if (ChatID <= MyPort->ChatID)
return;
// Length of current message
Len = strlen(Buf);
// Needs cooling down?
if (g_ProgramTic + Len < MyPort->ChatCoolDown)
return;
// Set cooldown to length of string
MyPort->ChatCoolDown = g_ProgramTic + Len;
MyPort->ChatID = ChatID; // to prevent same message spam due to lag
/* Direct encode */
SN_DirectChat(a_Host->ID, Source, Mode, Target, Buf);
#undef BUFSIZE
}
/* DT_SETT() -- Change Setting */
void DT_SETT(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
uint32_t HostID, PortID;
uint16_t Setting;
uint8_t Player;
SN_Port_t* Port;
uint8_t Data[MAXTCSTRINGCAT];
/* Check */
if (!a_Host || !SN_IsServer())
return;
/* Read Data */
HostID = D_BSru32(a_BS);
PortID = D_BSru32(a_BS);
Player = D_BSru8(a_BS);
Setting = D_BSru16(a_BS);
// Find port
Port = SN_PortByID(PortID);
// Invalid mismatch
if (a_Host->ID != HostID || !Port || Port->Host != a_Host)
return;
// Invalid player
if (Player != 255 && Port->Player != &players[Player])
return;
// Read giant data
D_BSReadChunk(a_BS, Data, MAXTCSTRINGCAT);
/* Send to setting writer */
SN_PortSetting(Port, Setting, 0, Data, MAXTCSTRINGCAT);
}
/* DT_RSPP() -- Request Spectation of player */
void DT_RSPP(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr)
{
int32_t PlayerID, i;
SN_Port_t* Port;
/* Check */
if (!SN_IsServer() || !a_Host)
return;
/* Read player ID */
PlayerID = D_BSri32(a_BS);
// Check bounds
if (PlayerID < 0 || PlayerID >= MAXPLAYERS)
return;
/* Already not playing? */
if (!playeringame[PlayerID])
return;
/* Security check, be sure host OWNS this player */
for (i = 0; i < a_Host->NumPorts; i++)
if ((Port = a_Host->Ports[i]))
if (Port->Player && PlayerID == (Port->Player - players))
{
SN_RemovePlayer(PlayerID);
break;
}
}
/* l_Packets -- Data packets */
static const struct
{
union
{
uint8_t Char[4];
uint32_t Int;
} H;
void (*Func)(D_BS_t* const a_BS, SN_Host_t* const a_Host, I_HostAddress_t* const a_Addr);
bool_t RemoteOnly; // Only from remote end
} l_Packets[] =
{
{{"PING"}, DT_PING, false},
{{"JOBT"}, DT_JOBT, false},
{{"PONG"}, DT_PONG, false},
{{"JOBA"}, DT_JOBA, false},
{{"SYNC"}, DT_SYNC, false},
{{"CONN"}, DT_CONN, false},
{{"HELO"}, DT_HELO, true},
{{"LIST"}, DT_LIST, false},
{{"WADL"}, DT_WADL, true},
{{"QUIT"}, DT_QUIT, false},
{{"SAVE"}, DT_SAVE, false},
{{"PSAV"}, DT_PSAV, false},
{{"PLAY"}, DT_PLAY, false},
{{"COOL"}, DT_COOL, false},
{{"WANT"}, DT_WANT, false},
{{"GIVE"}, DT_GIVE, false},
//{{"FULL"}, DT_FULL, false}, // TODO FIXME
{{"PJGG"}, DT_PJGG, false},
{{"CHAT"}, DT_CHAT, false},
{{"SETT"}, DT_SETT, false},
{{"RSPP"}, DT_RSPP, false},
{{"FPUT"}, SN_FileRecv, false},
{{"FOPN"}, SN_FileInit, false},
{{"FRDY"}, SN_FileReady, false},
{{{0}}}
};
/* SN_DoTrans() -- Do transmission */
void SN_DoTrans(void)
{
union
{
char Char[4];
uint32_t Int;
} Header;
bool_t Continue;
I_HostAddress_t Addr;
SN_Host_t* Host;
int32_t i;
/* No Socket */
if (!l_Sock)
return;
/* Perform packet updates */
for (Continue = true; Continue;)
{
// Clear
memset(&Header, 0, sizeof(Header));
memset(&Addr, 0, sizeof(Addr));
// Play block
if ((Continue = D_BSPlayNetBlock(l_BS, Header.Char, &Addr)))
{
// Find host, if any
Host = SN_HostByAddr(&Addr);
// Which type?
for (i = 0; l_Packets[i].H.Char[0]; i++)
if (l_Packets[i].H.Int == Header.Int)
{
// Needs to be from remote
if (l_Packets[i].RemoteOnly)
if (!I_NetCompareHost(&Addr, &l_HostAddr))
break;
l_Packets[i].Func(l_BS, Host, &Addr);
break;
}
}
}
/* If not connected, connect to remote side */
if (!SN_IsConnected())
{
SN_DoConnect();
return;
}
/* Files */
SN_FileLoop();
/* Do client or server stuff */
if (SN_IsServer())
SN_DoServer(l_BS);
else
SN_DoClient(l_BS);
}
/* SN_GotFile() -- Received file */
bool_t SN_GotFile(const char* const a_PathName)
{
char* Ext;
/* Check */
if (!a_PathName)
return false;
/* Grab extension */
Ext = strrchr(a_PathName, '.');
/* Save game */
if (!strcasecmp(Ext, ".rsv"))
{
// Load save game
if (!P_LoadGameEx(NULL, a_PathName, strlen(a_PathName), NULL, NULL))
SN_Disconnect(false, "Failed to load save game");
// Tell server, we are playing
else
l_Stage = DCS_CANPLAYNOW;
// Delete save games after they are used
return true;
}
/* Assume WAD content data thing */
else
{
// Do not delete WADs!
return false;
}
}