1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
18 interface
20 uses
23 const
33 type
57 type
59 public
62 public
65 // inside the game, calling `connect()` is disasterous, as it is blocking.
66 // so we'll use this variable to indicate if "connected" event is received.
73 // server list request working flags
79 // temporary mark
82 private
85 public
97 // call as often as you want, the object will do the rest
98 // but try to call this at least once in 100 msecs
115 var
128 // make this server private
130 // make this server public
133 // called while the server is running
135 // called when the server is started
137 // called when the server is stopped
140 // called when new netword player comes
142 // called when new netword player comes
144 // started new map
146 // this server renamed (or password mode changed, or other params changed)
160 implementation
162 uses
168 // ////////////////////////////////////////////////////////////////////////// //
169 var
182 //==========================================================================
183 //
184 // GetTimerMS
185 //
186 //==========================================================================
188 begin
193 //==========================================================================
194 //
195 // findByPeer
196 //
197 //==========================================================================
199 var
201 begin
207 //==========================================================================
208 //
209 // ShutdownAll
210 //
211 //==========================================================================
213 var
217 begin
220 begin
222 begin
225 begin
229 //mlist[f].disconnect(false);
236 begin
246 begin
248 continue;
252 begin
254 //mlist[idx].disconnect(false);
256 end
258 begin
261 end
263 begin
273 //==========================================================================
274 //
275 // DisconnectAll
276 //
277 //==========================================================================
279 var
281 begin
283 begin
289 //==========================================================================
290 //
291 // ConnectAll
292 //
293 //==========================================================================
295 var
297 begin
299 begin
300 // force reconnect
302 //if (not mlist[f].isAlive()) then continue;
303 // force updating
305 begin
313 //==========================================================================
314 //
315 // UpdateAll
316 //
317 //==========================================================================
319 var
321 begin
323 begin
331 //**************************************************************************
332 //
333 // public api
334 //
335 //**************************************************************************
337 //==========================================================================
338 //
339 // g_Net_Slist_Private
340 //
341 // make this server private
342 //
343 //==========================================================================
345 begin
351 //==========================================================================
352 //
353 // g_Net_Slist_Public
354 //
355 // make this server public
356 //
357 //==========================================================================
359 begin
361 begin
368 //==========================================================================
369 //
370 // g_Net_Slist_ServerUpdate
371 //
372 // called while the server is running
373 //
374 //==========================================================================
376 begin
381 // called when the server is started
383 begin
386 begin
393 //==========================================================================
394 //
395 // g_Net_Slist_ServerClosed
396 //
397 // called when the server is stopped
398 //
399 //==========================================================================
401 var
403 begin
405 begin
408 begin
416 //==========================================================================
417 //
418 // g_Net_Slist_ServerPlayerComes
419 //
420 // called when new netword player comes
421 //
422 //==========================================================================
424 begin
429 //==========================================================================
430 //
431 // g_Net_Slist_ServerPlayerLeaves
432 //
433 // called when new netword player comes
434 //
435 //==========================================================================
437 begin
442 //==========================================================================
443 //
444 // g_Net_Slist_ServerMapStarted
445 //
446 // started new map
447 //
448 //==========================================================================
450 begin
455 //==========================================================================
456 //
457 // g_Net_Slist_ServerRenamed
458 //
459 // this server renamed (or password mode changed, or other params changed)
460 //
461 //==========================================================================
463 begin
468 //**************************************************************************
469 //
470 // TMasterHost
471 //
472 //**************************************************************************
474 //==========================================================================
475 //
476 // TMasterHost.Create
477 //
478 //==========================================================================
480 begin
500 //==========================================================================
501 //
502 // TMasterHost.clear
503 //
504 //==========================================================================
506 begin
520 //==========================================================================
521 //
522 // TMasterHost.setAddress
523 //
524 //==========================================================================
526 begin
542 if (length(hostStr) > 0) then hostName := hostStr else hostName := IntToStr(enetAddr.host)+':'+IntToStr(ea.port);
548 //==========================================================================
549 //
550 // TMasterHost.isValid
551 //
552 //==========================================================================
554 begin
559 //==========================================================================
560 //
561 // TMasterHost.isAlive
562 //
563 // not disconnected
564 //
565 //==========================================================================
567 begin
572 //==========================================================================
573 //
574 // TMasterHost.isConnecting
575 //
576 // is connection in progress?
577 //
578 //==========================================================================
580 begin
585 //==========================================================================
586 //
587 // TMasterHost.isConnected
588 //
589 //==========================================================================
591 begin
596 //==========================================================================
597 //
598 // TMasterHost.connectedEvent
599 //
600 //==========================================================================
602 begin
610 //==========================================================================
611 //
612 // TMasterHost.disconnectedEvent
613 //
614 //==========================================================================
616 begin
620 //if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
624 //==========================================================================
625 //
626 // TMasterHost.receivedEvent
627 //
628 // `pkt` is never `nil`
629 //
630 //==========================================================================
632 var
638 begin
641 // packet type
648 //slUrgent := '';
650 // number of items
654 begin
657 begin
675 begin
676 // new master, supports version reports
679 begin
680 { TODO }
683 // even newer master, supports extra info
685 begin
688 // check if the message has updated and the user has to read it again
696 //==========================================================================
697 //
698 // TMasterHost.pulse
699 //
700 // this performs various scheduled tasks, if necessary
701 //
702 //==========================================================================
704 var
707 begin
711 // process pending connection timeout
713 begin
715 begin
717 // do not spam with error messages, it looks like the master is down
718 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
720 // main pulse will take care of the rest
722 exit;
724 // send update, if necessary
726 begin
731 begin
739 //==========================================================================
740 //
741 // TMasterHost.disconnect
742 //
743 //==========================================================================
745 begin
749 begin
753 end
754 else
755 begin
757 // main pulse will take care of the rest
763 //updateSent := false;
765 //lastConnectTime := 0;
766 //if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
770 //==========================================================================
771 //
772 // TMasterHost.connect
773 //
774 //==========================================================================
776 begin
780 begin
782 end
783 else
784 begin
799 begin
801 exit;
809 //==========================================================================
810 //
811 // TMasterHost.writeInfo
812 //
813 //==========================================================================
815 var
817 begin
835 //==========================================================================
836 //
837 // TMasterHost.update
838 //
839 //==========================================================================
841 var
843 begin
846 begin
848 exit;
854 begin
855 try
863 begin
865 begin
871 finally
874 end
875 else
876 begin
882 //==========================================================================
883 //
884 // TMasterHost.remove
885 //
886 //==========================================================================
888 var
890 begin
898 try
904 begin
907 finally
913 //**************************************************************************
914 //
915 // other functions
916 //
917 //**************************************************************************
918 type
921 var
925 //==========================================================================
926 //
927 // parseAddressPort
928 //
929 //==========================================================================
931 var
935 begin
947 begin
952 begin
953 try
955 except
967 begin
969 end
970 else
971 begin
973 begin
975 exit;
984 //==========================================================================
985 //
986 // addMasterRecord
987 //
988 //==========================================================================
990 var
993 begin
996 begin
998 begin
1000 exit;
1005 begin
1016 //==========================================================================
1017 //
1018 // g_Net_Slist_Set
1019 //
1020 //==========================================================================
1022 var
1027 begin
1034 begin
1040 //writeln('list=[', list, ']');
1042 begin
1047 //writeln(' sa=[', sa, ']');
1051 // remove unknown master servers
1054 begin
1057 begin
1066 //**************************************************************************
1067 //
1068 // main pulse
1069 //
1070 //**************************************************************************
1072 //==========================================================================
1073 //
1074 // g_Net_Slist_Pulse
1075 //
1076 //==========================================================================
1078 var
1083 begin
1087 begin
1089 begin
1092 exit;
1097 begin
1100 begin
1104 Exit;
1110 begin
1113 begin
1115 begin
1116 if (mlist[f].lastConnectTime = 0) or (ct < mlist[f].lastConnectTime) or (ct-mlist[f].lastConnectTime >= 1000*60*5) then
1117 begin
1121 end
1122 else
1123 begin
1125 begin
1134 begin
1137 begin
1143 exit;
1149 begin
1152 continue;
1156 begin
1158 end
1160 begin
1162 end
1164 begin
1172 //**************************************************************************
1173 //
1174 // gui and server list
1175 //
1176 //**************************************************************************
1178 //==========================================================================
1179 //
1180 // PingServer
1181 //
1182 //==========================================================================
1184 var
1188 begin
1202 //==========================================================================
1203 //
1204 // PingBcast
1205 //
1206 //==========================================================================
1208 var
1210 begin
1220 //==========================================================================
1221 //
1222 // g_Net_Slist_Fetch
1223 //
1224 //==========================================================================
1226 var
1239 begin
1243 begin
1261 begin
1276 begin
1297 var
1302 begin
1307 begin
1309 exit;
1317 // TODO: what should we identify the build with?
1321 try
1325 // wait until all servers connected and answered
1328 begin
1333 begin
1334 {
1335 e_LogWritefln(' master #%d: [%s] valid=%d; alive=%d; connected=%d; connecting=%d',
1336 [f, mlist[f].hostName, Integer(mlist[f].isValid()), Integer(mlist[f].isAlive()),
1337 Integer(mlist[f].isConnected()), Integer(mlist[f].isConnecting())], TMsgType.Notify);
1338 }
1341 begin
1344 begin
1348 end
1350 begin
1352 begin
1355 begin
1357 begin
1363 end
1365 begin
1367 end
1369 begin
1372 end
1374 begin
1379 // check for timeout
1385 begin
1388 exit;
1392 {
1393 slUrgent := '';
1394 slReadUrgent := true;
1395 }
1399 begin
1402 begin
1405 begin
1407 begin
1409 break;
1413 begin
1421 begin
1423 begin
1429 begin
1437 begin
1439 exit;
1458 begin
1474 begin
1476 begin
1492 break;
1500 finally
1506 //==========================================================================
1507 //
1508 // GetServerFromTable
1509 //
1510 //==========================================================================
1511 function GetServerFromTable (Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
1512 begin
1528 Exit;
1530 Exit;
1535 //==========================================================================
1536 //
1537 // g_Serverlist_Draw
1538 //
1539 //==========================================================================
1541 var
1549 begin
1566 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
1568 // MOTD
1570 begin
1576 // Urgent message
1578 begin
1597 Exit;
1601 begin
1608 Exit;
1613 begin
1620 else
1622 end else
1649 begin
1651 // Name and map
1655 // Ping and similar count
1658 else
1661 else
1662 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
1665 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
1667 // Game mode
1670 // Players
1671 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
1672 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
1674 // Version
1687 //==========================================================================
1688 //
1689 // g_Serverlist_GenerateTable
1690 //
1691 //==========================================================================
1693 var
1697 var
1699 begin
1702 Exit;
1704 begin
1706 continue;
1708 begin
1710 Exit;
1715 var
1717 begin
1725 var
1728 begin
1732 begin
1739 var
1742 begin
1746 begin
1752 begin
1755 Exit;
1757 begin
1760 begin
1766 end
1767 else
1768 begin
1781 //==========================================================================
1782 //
1783 // g_Serverlist_Control
1784 //
1785 //==========================================================================
1787 var
1790 begin
1794 Exit;
1801 begin
1809 Exit;
1812 // if there's a message on the screen,
1814 begin
1815 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1816 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1818 Exit;
1822 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
1823 begin
1825 begin
1828 g_Game_Draw;
1829 sys_Repaint;
1832 begin
1835 end
1836 else
1843 end
1844 else
1849 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1850 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1851 begin
1853 begin
1856 begin
1864 Exit;
1865 end
1866 else
1871 Exit;
1873 end
1874 else
1878 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
1879 begin
1881 begin
1889 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
1890 begin
1892 begin
1901 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
1902 begin
1904 begin
1912 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
1913 begin
1915 begin
1916 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
1935 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
1936 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
1937 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
1938 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
1939 then