e696d873a41e14ac95dc8f0b220ffbdfe1e2b27f
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)
149 // non-zero timeout ignores current status (used to fetch server list)
161 implementation
163 uses
169 // ////////////////////////////////////////////////////////////////////////// //
170 var
183 //==========================================================================
184 //
185 // GetTimerMS
186 //
187 //==========================================================================
189 begin
194 //==========================================================================
195 //
196 // findByPeer
197 //
198 //==========================================================================
200 var
202 begin
208 //==========================================================================
209 //
210 // ShutdownAll
211 //
212 //==========================================================================
214 var
218 begin
221 begin
223 begin
226 begin
230 //mlist[f].disconnect(false);
237 begin
242 // if (sres < 0) then break;
247 begin
249 continue;
253 begin
255 //mlist[idx].disconnect(false);
257 end
259 begin
262 end
264 begin
274 //==========================================================================
275 //
276 // DisconnectAll
277 //
278 //==========================================================================
280 var
282 begin
284 begin
290 //==========================================================================
291 //
292 // ConnectAll
293 //
294 //==========================================================================
296 var
298 begin
300 begin
301 // force reconnect
303 //if (not mlist[f].isAlive()) then continue;
304 // force updating
306 begin
314 //==========================================================================
315 //
316 // UpdateAll
317 //
318 //==========================================================================
320 var
322 begin
324 begin
332 //**************************************************************************
333 //
334 // public api
335 //
336 //**************************************************************************
338 //==========================================================================
339 //
340 // g_Net_Slist_Private
341 //
342 // make this server private
343 //
344 //==========================================================================
346 begin
352 //==========================================================================
353 //
354 // g_Net_Slist_Public
355 //
356 // make this server public
357 //
358 //==========================================================================
360 begin
362 begin
369 //==========================================================================
370 //
371 // g_Net_Slist_ServerUpdate
372 //
373 // called while the server is running
374 //
375 //==========================================================================
377 begin
382 // called when the server is started
384 begin
387 begin
394 //==========================================================================
395 //
396 // g_Net_Slist_ServerClosed
397 //
398 // called when the server is stopped
399 //
400 //==========================================================================
402 var
404 begin
406 begin
409 begin
417 //==========================================================================
418 //
419 // g_Net_Slist_ServerPlayerComes
420 //
421 // called when new netword player comes
422 //
423 //==========================================================================
425 begin
430 //==========================================================================
431 //
432 // g_Net_Slist_ServerPlayerLeaves
433 //
434 // called when new netword player comes
435 //
436 //==========================================================================
438 begin
443 //==========================================================================
444 //
445 // g_Net_Slist_ServerMapStarted
446 //
447 // started new map
448 //
449 //==========================================================================
451 begin
456 //==========================================================================
457 //
458 // g_Net_Slist_ServerRenamed
459 //
460 // this server renamed (or password mode changed, or other params changed)
461 //
462 //==========================================================================
464 begin
469 //**************************************************************************
470 //
471 // TMasterHost
472 //
473 //**************************************************************************
475 //==========================================================================
476 //
477 // TMasterHost.Create
478 //
479 //==========================================================================
481 begin
501 //==========================================================================
502 //
503 // TMasterHost.clear
504 //
505 //==========================================================================
507 begin
521 //==========================================================================
522 //
523 // TMasterHost.setAddress
524 //
525 //==========================================================================
527 begin
543 if (length(hostStr) > 0) then hostName := hostStr else hostName := IntToStr(enetAddr.host)+':'+IntToStr(ea.port);
549 //==========================================================================
550 //
551 // TMasterHost.isValid
552 //
553 //==========================================================================
555 begin
560 //==========================================================================
561 //
562 // TMasterHost.isAlive
563 //
564 // not disconnected
565 //
566 //==========================================================================
568 begin
573 //==========================================================================
574 //
575 // TMasterHost.isConnecting
576 //
577 // is connection in progress?
578 //
579 //==========================================================================
581 begin
586 //==========================================================================
587 //
588 // TMasterHost.isConnected
589 //
590 //==========================================================================
592 begin
597 //==========================================================================
598 //
599 // TMasterHost.connectedEvent
600 //
601 //==========================================================================
603 begin
608 //g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_CONN], [mlist[f].hostName]));
612 //==========================================================================
613 //
614 // TMasterHost.disconnectedEvent
615 //
616 //==========================================================================
618 begin
622 //if (spamConsole) then g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_DISC], [hostName]));
626 //==========================================================================
627 //
628 // TMasterHost.receivedEvent
629 //
630 // `pkt` is never `nil`
631 //
632 //==========================================================================
634 var
640 begin
643 // packet type
650 //slUrgent := '';
652 // number of items
654 //g_Console_Add(_lc[I_NET_MSG]+Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt, hostName]), True);
657 begin
660 begin
678 begin
679 // new master, supports version reports
682 begin
683 { TODO }
686 // even newer master, supports extra info
688 begin
690 if (slMOTD <> '') then e_LogWritefln('got MOTD from master at [%s]: %s', [hostName, slMOTD], TMsgType.Notify);
692 // check if the message has updated and the user has to read it again
695 if (s <> '') then e_LogWritefln('got urgent from master at [%s]: %s', [hostName, s], TMsgType.Notify);
701 //==========================================================================
702 //
703 // TMasterHost.pulse
704 //
705 // this performs various scheduled tasks, if necessary
706 //
707 //==========================================================================
709 var
712 begin
716 // process pending connection timeout
718 begin
720 begin
722 // do not spam with error messages, it looks like the master is down
723 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
725 // main pulse will take care of the rest
728 exit;
730 // send update, if necessary
732 begin
737 begin
745 //==========================================================================
746 //
747 // TMasterHost.disconnect
748 //
749 //==========================================================================
751 begin
755 begin
759 end
760 else
761 begin
763 // main pulse will take care of the rest
769 //updateSent := false;
771 //lastConnectTime := 0;
772 //if (spamConsole) then g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_DISC], [hostName]));
776 //==========================================================================
777 //
778 // TMasterHost.connect
779 //
780 //==========================================================================
782 begin
786 begin
788 end
789 else
790 begin
805 begin
807 exit;
815 //==========================================================================
816 //
817 // TMasterHost.writeInfo
818 //
819 //==========================================================================
821 var
823 begin
841 //==========================================================================
842 //
843 // TMasterHost.update
844 //
845 //==========================================================================
847 var
849 begin
852 begin
854 exit;
860 begin
861 try
864 //writeln(formatstrf('%08x', [NetAddr.host]), ' : ', NetAddr.host);
870 begin
872 begin
878 finally
881 end
882 else
883 begin
889 //==========================================================================
890 //
891 // TMasterHost.remove
892 //
893 //==========================================================================
895 var
897 begin
905 try
911 begin
914 finally
920 //**************************************************************************
921 //
922 // other functions
923 //
924 //**************************************************************************
925 type
928 var
932 //==========================================================================
933 //
934 // parseAddressPort
935 //
936 //==========================================================================
938 var
942 begin
954 begin
959 begin
960 try
962 except
974 begin
976 end
977 else
978 begin
980 begin
982 exit;
991 //==========================================================================
992 //
993 // addMasterRecord
994 //
995 //==========================================================================
997 var
1000 begin
1003 begin
1005 begin
1007 exit;
1012 begin
1023 //==========================================================================
1024 //
1025 // g_Net_Slist_Set
1026 //
1027 //==========================================================================
1029 var
1034 begin
1041 begin
1047 //writeln('list=[', list, ']');
1049 begin
1054 //writeln(' sa=[', sa, ']');
1058 // remove unknown master servers
1061 begin
1064 begin
1073 //**************************************************************************
1074 //
1075 // main pulse
1076 //
1077 //**************************************************************************
1079 //==========================================================================
1080 //
1081 // g_Net_Slist_Pulse
1082 //
1083 // non-zero timeout ignores current status (used to fetch server list)
1084 //
1085 //==========================================================================
1087 var
1092 begin
1096 begin
1098 begin
1101 exit;
1106 begin
1109 begin
1110 e_LogWriteln(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT] + ' (host_create)', TMsgType.Notify);
1113 Exit;
1119 begin
1122 begin
1123 if (timeout > 0) or (reportsEnabled and g_Game_IsServer() and g_Game_IsNet() and NetUseMaster) then
1124 begin
1125 if (mlist[f].lastConnectTime = 0) or (ct < mlist[f].lastConnectTime) or (ct-mlist[f].lastConnectTime >= 1000*60*5) then
1126 begin
1131 end
1132 else
1133 begin
1134 if (timeout = 0) and (not reportsEnabled or not g_Game_IsServer() or not g_Game_IsNet() or not NetUseMaster) then
1135 begin
1137 begin
1141 //e_LogWritefln('disconnecting from master [%s]', [mlist[f].hostName], TMsgType.Notify);
1150 begin
1152 begin
1153 e_LogWriteln(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT] + ' (host_service)', TMsgType.Notify);
1158 exit;
1163 begin
1166 end
1167 else
1168 begin
1170 begin
1172 end
1174 begin
1176 end
1178 begin
1188 //**************************************************************************
1189 //
1190 // gui and server list
1191 //
1192 //**************************************************************************
1194 //==========================================================================
1195 //
1196 // PingServer
1197 //
1198 //==========================================================================
1200 var
1204 begin
1218 //==========================================================================
1219 //
1220 // PingBcast
1221 //
1222 //==========================================================================
1224 var
1226 begin
1236 //==========================================================================
1237 //
1238 // g_Net_Slist_Fetch
1239 //
1240 //==========================================================================
1242 var
1255 begin
1259 begin
1277 begin
1292 begin
1313 var
1318 begin
1323 begin
1325 exit;
1335 // TODO: what should we identify the build with?
1339 try
1343 // wait until all servers connected and answered
1346 begin
1350 begin
1351 {
1352 e_LogWritefln(' master #%d: [%s] valid=%d; alive=%d; connected=%d; connecting=%d',
1353 [f, mlist[f].hostName, Integer(mlist[f].isValid()), Integer(mlist[f].isAlive()),
1354 Integer(mlist[f].isConnected()), Integer(mlist[f].isConnecting())], TMsgType.Notify);
1355 }
1358 begin
1361 begin
1362 //g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_WCONN], [mlist[f].hostName]));
1366 end
1368 begin
1369 //g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_CONN], [mlist[f].hostName]));
1371 begin
1374 begin
1376 begin
1382 end
1384 begin
1386 end
1388 begin
1391 end
1393 begin
1398 // check for timeout
1405 begin
1408 exit;
1412 {
1413 slUrgent := '';
1414 slReadUrgent := true;
1415 }
1419 begin
1422 begin
1425 begin
1427 begin
1429 break;
1433 begin
1441 begin
1443 begin
1449 begin
1457 begin
1459 exit;
1478 begin
1494 begin
1496 begin
1512 break;
1520 finally
1526 //==========================================================================
1527 //
1528 // GetServerFromTable
1529 //
1530 //==========================================================================
1531 function GetServerFromTable (Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
1532 begin
1548 Exit;
1550 Exit;
1555 //==========================================================================
1556 //
1557 // g_Serverlist_Draw
1558 //
1559 //==========================================================================
1561 var
1569 begin
1586 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
1588 // MOTD
1590 begin
1596 // Urgent message
1598 begin
1617 Exit;
1621 begin
1628 Exit;
1633 begin
1640 else
1642 end else
1669 begin
1671 // Name and map
1675 // Ping and similar count
1678 else
1681 else
1682 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
1685 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
1687 // Game mode
1690 // Players
1691 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
1692 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
1694 // Version
1707 //==========================================================================
1708 //
1709 // g_Serverlist_GenerateTable
1710 //
1711 //==========================================================================
1713 var
1717 var
1719 begin
1722 Exit;
1724 begin
1726 continue;
1728 begin
1730 Exit;
1735 var
1737 begin
1745 var
1748 begin
1752 begin
1759 var
1762 begin
1766 begin
1772 begin
1775 Exit;
1777 begin
1780 begin
1786 end
1787 else
1788 begin
1801 //==========================================================================
1802 //
1803 // g_Serverlist_Control
1804 //
1805 //==========================================================================
1807 var
1810 begin
1814 Exit;
1821 begin
1829 Exit;
1832 // if there's a message on the screen,
1834 begin
1835 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1836 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1838 Exit;
1842 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
1843 begin
1845 begin
1848 g_Game_Draw;
1849 sys_Repaint;
1852 begin
1855 end
1856 else
1863 end
1864 else
1869 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1870 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1871 begin
1873 begin
1876 begin
1884 Exit;
1885 end
1886 else
1891 Exit;
1893 end
1894 else
1898 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
1899 begin
1901 begin
1909 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
1910 begin
1912 begin
1921 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
1922 begin
1924 begin
1932 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
1933 begin
1935 begin
1936 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
1955 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
1956 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
1957 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
1958 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
1959 then