e24bed7accc9c70301965d3614103043016e528e
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
938 finally
941 end
942 else
943 begin
949 //==========================================================================
950 //
951 // TMasterHost.remove
952 //
953 //==========================================================================
955 var
957 begin
965 try
971 begin
974 finally
980 //**************************************************************************
981 //
982 // other functions
983 //
984 //**************************************************************************
987 var
990 begin
1003 //**************************************************************************
1004 //
1005 // main pulse
1006 //
1007 //**************************************************************************
1009 var
1014 begin
1018 begin
1020 begin
1023 exit;
1028 begin
1031 begin
1035 Exit;
1041 begin
1044 begin
1046 begin
1047 if (mlist[f].lastConnectTime = 0) or (ct < mlist[f].lastConnectTime) or (ct-mlist[f].lastConnectTime >= 1000*60*5) then
1048 begin
1052 end
1053 else
1054 begin
1056 begin
1065 begin
1068 begin
1074 exit;
1080 begin
1083 continue;
1087 begin
1089 end
1091 begin
1093 end
1095 begin
1103 //**************************************************************************
1104 //
1105 // gui and server list
1106 //
1107 //**************************************************************************
1109 //==========================================================================
1110 //
1111 // PingServer
1112 //
1113 //==========================================================================
1115 var
1119 begin
1133 //==========================================================================
1134 //
1135 // PingBcast
1136 //
1137 //==========================================================================
1139 var
1141 begin
1151 //==========================================================================
1152 //
1153 // g_Net_Slist_Fetch
1154 //
1155 //==========================================================================
1157 var
1170 begin
1174 begin
1192 begin
1207 begin
1228 var
1233 begin
1238 begin
1240 exit;
1248 // TODO: what should we identify the build with?
1252 try
1256 // wait until all servers connected and answered
1259 begin
1264 begin
1265 {
1266 e_LogWritefln(' master #%d: [%s:%u] valid=%d; alive=%d; connected=%d; connecting=%d',
1267 [f, mlist[f].hostName, mlist[f].hostPort, Integer(mlist[f].isValid()), Integer(mlist[f].isAlive()),
1268 Integer(mlist[f].isConnected()), Integer(mlist[f].isConnecting())], TMsgType.Notify);
1269 }
1272 begin
1275 begin
1279 end
1281 begin
1283 begin
1286 begin
1288 begin
1294 end
1296 begin
1298 end
1300 begin
1303 end
1305 begin
1310 // check for timeout
1316 begin
1319 exit;
1323 {
1324 slUrgent := '';
1325 slReadUrgent := true;
1326 }
1330 begin
1333 begin
1336 begin
1338 begin
1340 break;
1344 begin
1352 begin
1354 begin
1360 begin
1368 begin
1370 exit;
1389 begin
1405 begin
1407 begin
1423 break;
1431 finally
1437 //==========================================================================
1438 //
1439 // GetServerFromTable
1440 //
1441 //==========================================================================
1442 function GetServerFromTable (Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
1443 begin
1459 Exit;
1461 Exit;
1466 //==========================================================================
1467 //
1468 // g_Serverlist_Draw
1469 //
1470 //==========================================================================
1472 var
1480 begin
1497 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
1499 // MOTD
1501 begin
1507 // Urgent message
1509 begin
1528 Exit;
1532 begin
1539 Exit;
1544 begin
1551 else
1553 end else
1580 begin
1582 // Name and map
1586 // Ping and similar count
1589 else
1592 else
1593 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
1596 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
1598 // Game mode
1601 // Players
1602 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
1603 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
1605 // Version
1618 //==========================================================================
1619 //
1620 // g_Serverlist_GenerateTable
1621 //
1622 //==========================================================================
1624 var
1628 var
1630 begin
1633 Exit;
1635 begin
1637 continue;
1639 begin
1641 Exit;
1646 var
1648 begin
1656 var
1659 begin
1663 begin
1670 var
1673 begin
1677 begin
1683 begin
1686 Exit;
1688 begin
1691 begin
1697 end
1698 else
1699 begin
1712 //==========================================================================
1713 //
1714 // g_Serverlist_Control
1715 //
1716 //==========================================================================
1718 var
1721 begin
1725 Exit;
1732 begin
1740 Exit;
1743 // if there's a message on the screen,
1745 begin
1746 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1747 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1749 Exit;
1753 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
1754 begin
1756 begin
1759 g_Game_Draw;
1760 sys_Repaint;
1763 begin
1766 end
1767 else
1774 end
1775 else
1780 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1781 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1782 begin
1784 begin
1787 begin
1795 Exit;
1796 end
1797 else
1802 Exit;
1804 end
1805 else
1809 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
1810 begin
1812 begin
1820 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
1821 begin
1823 begin
1832 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
1833 begin
1835 begin
1843 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
1844 begin
1846 begin
1847 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
1866 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
1867 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
1868 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
1869 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
1870 then