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
63 public
66 // inside the game, calling `connect()` is disasterous, as it is blocking.
67 // so we'll use this variable to indicate if "connected" event is received.
74 // server list request working flags
81 private
84 public
98 // call as often as you want, the object will do the rest
99 // but try to call this at least once in 100 msecs
116 var
129 // make this server private
131 // make this server public
134 // called while the server is running
136 // called when the server is started
138 // called when the server is stopped
141 // called when new netword player comes
143 // called when new netword player comes
145 // started new map
147 // this server renamed (or password mode changed, or other params changed)
161 implementation
163 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.isSameAddress
523 //
524 //==========================================================================
526 var
529 begin
537 begin
541 begin
542 try
544 except
548 end
549 else
550 begin
557 //==========================================================================
558 //
559 // TMasterHost.setAddress
560 //
561 //==========================================================================
563 var
565 begin
581 begin
585 begin
589 begin
590 try
592 except
603 begin
611 //writeln('*********************: ', hostandport, ' [', hostName, ':', hostPort, '] ', result);
615 //==========================================================================
616 //
617 // TMasterHost.isValid
618 //
619 //==========================================================================
621 begin
626 //==========================================================================
627 //
628 // TMasterHost.isAlive
629 //
630 // not disconnected
631 //
632 //==========================================================================
634 begin
639 //==========================================================================
640 //
641 // TMasterHost.isConnecting
642 //
643 // is connection in progress?
644 //
645 //==========================================================================
647 begin
652 //==========================================================================
653 //
654 // TMasterHost.isConnected
655 //
656 //==========================================================================
658 begin
663 //==========================================================================
664 //
665 // TMasterHost.connectedEvent
666 //
667 //==========================================================================
669 begin
677 //==========================================================================
678 //
679 // TMasterHost.disconnectedEvent
680 //
681 //==========================================================================
683 begin
687 //if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
691 //==========================================================================
692 //
693 // TMasterHost.receivedEvent
694 //
695 // `pkt` is never `nil`
696 //
697 //==========================================================================
699 var
705 begin
708 // packet type
711 e_LogWritefln('received list packet from master at [%s:%u]', [hostName, hostPort], TMsgType.Notify);
715 //slUrgent := '';
717 // number of items
721 begin
724 begin
742 begin
743 // new master, supports version reports
746 begin
747 { TODO }
750 // even newer master, supports extra info
752 begin
755 // check if the message has updated and the user has to read it again
763 //==========================================================================
764 //
765 // TMasterHost.pulse
766 //
767 // this performs various scheduled tasks, if necessary
768 //
769 //==========================================================================
771 var
774 begin
778 // process pending connection timeout
780 begin
782 begin
784 // do not spam with error messages, it looks like the master is down
785 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
787 // main pulse will take care of the rest
789 exit;
791 // send update, if necessary
793 begin
798 begin
806 //==========================================================================
807 //
808 // TMasterHost.disconnect
809 //
810 //==========================================================================
812 begin
816 begin
820 end
821 else
822 begin
824 // main pulse will take care of the rest
830 //updateSent := false;
832 //lastConnectTime := 0;
833 //if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
837 //==========================================================================
838 //
839 // TMasterHost.connect
840 //
841 //==========================================================================
843 begin
847 begin
849 end
850 else
851 begin
866 begin
868 exit;
876 //==========================================================================
877 //
878 // TMasterHost.writeInfo
879 //
880 //==========================================================================
882 var
884 begin
902 //==========================================================================
903 //
904 // TMasterHost.update
905 //
906 //==========================================================================
908 var
910 begin
913 begin
915 exit;
921 begin
922 try
930 begin
932 begin
937 finally
940 end
941 else
942 begin
948 //==========================================================================
949 //
950 // TMasterHost.remove
951 //
952 //==========================================================================
954 var
956 begin
964 try
970 begin
973 finally
979 //**************************************************************************
980 //
981 // other functions
982 //
983 //**************************************************************************
986 var
989 begin
1002 //**************************************************************************
1003 //
1004 // main pulse
1005 //
1006 //**************************************************************************
1008 var
1013 begin
1017 begin
1019 begin
1022 exit;
1027 begin
1030 begin
1034 Exit;
1040 begin
1043 begin
1045 begin
1046 if (mlist[f].lastConnectTime = 0) or (ct < mlist[f].lastConnectTime) or (ct-mlist[f].lastConnectTime >= 1000*60*5) then
1047 begin
1051 end
1052 else
1053 begin
1055 begin
1064 begin
1067 begin
1073 exit;
1079 begin
1082 continue;
1086 begin
1088 end
1090 begin
1092 end
1094 begin
1102 //**************************************************************************
1103 //
1104 // gui and server list
1105 //
1106 //**************************************************************************
1108 //==========================================================================
1109 //
1110 // PingServer
1111 //
1112 //==========================================================================
1114 var
1118 begin
1132 //==========================================================================
1133 //
1134 // PingBcast
1135 //
1136 //==========================================================================
1138 var
1140 begin
1150 //==========================================================================
1151 //
1152 // g_Net_Slist_Fetch
1153 //
1154 //==========================================================================
1156 var
1169 begin
1173 begin
1191 begin
1206 begin
1227 var
1232 begin
1237 begin
1239 exit;
1247 // TODO: what should we identify the build with?
1251 try
1255 // wait until all servers connected and answered
1258 begin
1263 begin
1264 {
1265 e_LogWritefln(' master #%d: [%s:%u] valid=%d; alive=%d; connected=%d; connecting=%d',
1266 [f, mlist[f].hostName, mlist[f].hostPort, Integer(mlist[f].isValid()), Integer(mlist[f].isAlive()),
1267 Integer(mlist[f].isConnected()), Integer(mlist[f].isConnecting())], TMsgType.Notify);
1268 }
1271 begin
1274 begin
1278 end
1280 begin
1282 begin
1285 begin
1287 begin
1293 end
1295 begin
1297 end
1299 begin
1302 end
1304 begin
1309 // check for timeout
1315 begin
1318 exit;
1322 {
1323 slUrgent := '';
1324 slReadUrgent := true;
1325 }
1329 begin
1332 begin
1335 begin
1337 begin
1339 break;
1343 begin
1351 begin
1353 begin
1359 begin
1367 begin
1369 exit;
1388 begin
1404 begin
1406 begin
1422 break;
1430 finally
1436 //==========================================================================
1437 //
1438 // GetServerFromTable
1439 //
1440 //==========================================================================
1441 function GetServerFromTable (Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
1442 begin
1458 Exit;
1460 Exit;
1465 //==========================================================================
1466 //
1467 // g_Serverlist_Draw
1468 //
1469 //==========================================================================
1471 var
1479 begin
1496 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
1498 // MOTD
1500 begin
1506 // Urgent message
1508 begin
1527 Exit;
1531 begin
1538 Exit;
1543 begin
1550 else
1552 end else
1579 begin
1581 // Name and map
1585 // Ping and similar count
1588 else
1591 else
1592 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
1595 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
1597 // Game mode
1600 // Players
1601 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
1602 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
1604 // Version
1617 //==========================================================================
1618 //
1619 // g_Serverlist_GenerateTable
1620 //
1621 //==========================================================================
1623 var
1627 var
1629 begin
1632 Exit;
1634 begin
1636 continue;
1638 begin
1640 Exit;
1645 var
1647 begin
1655 var
1658 begin
1662 begin
1669 var
1672 begin
1676 begin
1682 begin
1685 Exit;
1687 begin
1690 begin
1696 end
1697 else
1698 begin
1711 //==========================================================================
1712 //
1713 // g_Serverlist_Control
1714 //
1715 //==========================================================================
1717 var
1720 begin
1724 Exit;
1731 begin
1739 Exit;
1742 // if there's a message on the screen,
1744 begin
1745 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1746 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1748 Exit;
1752 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
1753 begin
1755 begin
1758 g_Game_Draw;
1759 sys_Repaint;
1762 begin
1765 end
1766 else
1773 end
1774 else
1779 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1780 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1781 begin
1783 begin
1786 begin
1794 Exit;
1795 end
1796 else
1801 Exit;
1803 end
1804 else
1808 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
1809 begin
1811 begin
1819 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
1820 begin
1822 begin
1831 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
1832 begin
1834 begin
1842 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
1843 begin
1845 begin
1846 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
1865 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
1866 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
1867 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
1868 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
1869 then