DEADSOFTWARE

Revert "fixed weapon switching over the network; bumped protocol version"
[d2df-sdl.git] / src / game / g_net.pas
index 93525c990b7913d41175c6f7ee67d724f713ce7a..c15df2bae9406b2cc043fce515bdc11b113b21da 100644 (file)
@@ -19,10 +19,10 @@ unit g_net;
 interface
 
 uses
-  e_log, e_msg, ENet, miniupnpc, Classes, MAPDEF;
+  e_log, e_msg, ENet, Classes, MAPDEF{$IFDEF USE_MINIUPNPC}, miniupnpc;{$ELSE};{$ENDIF}
 
 const
-  NET_PROTOCOL_VER = 173;
+  NET_PROTOCOL_VER = 174;
 
   NET_MAXCLIENTS = 24;
   NET_CHANS = 11;
@@ -44,6 +44,7 @@ const
   NET_CLIENT = 2;
 
   NET_BUFSIZE = $FFFF;
+  NET_PING_PORT = $DF2D;
 
   NET_EVERYONE = -1;
 
@@ -65,6 +66,12 @@ const
   BANLIST_FILENAME = 'banlist.txt';
   NETDUMP_FILENAME = 'netdump';
 
+  {$IFDEF FREEBSD}
+    NilThreadId = nil;
+  {$ELSE}
+    NilThreadId = 0;
+  {$ENDIF}
+
 type
   TNetClient = record
     ID:      Byte;
@@ -141,10 +148,14 @@ var
   NetGotEverything: Boolean = False;
   NetGotKeys:       Boolean = False;
 
+{$IFDEF USE_MINIUPNPC}
   NetPortForwarded: Word = 0;
   NetPongForwarded: Boolean = False;
   NetIGDControl: AnsiString;
   NetIGDService: TURLStr;
+{$ENDIF}
+
+  NetPortThread: TThreadID = NilThreadId;
 
   NetDumpFile: TStream;
 
@@ -168,7 +179,7 @@ function  g_Net_Client_ByName(Name: string): pTNetClient;
 function  g_Net_Client_ByPlayer(PID: Word): pTNetClient;
 function  g_Net_ClientName_ByID(ID: Integer): string;
 
-procedure g_Net_SendData(Data:AByte; peer: pENetPeer; Reliable: Boolean; Chan: Byte = NET_CHAN_DOWNLOAD);
+procedure g_Net_SendData(Data: AByte; peer: pENetPeer; Reliable: Boolean; Chan: Byte = NET_CHAN_DOWNLOAD);
 function  g_Net_Wait_Event(msgId: Word): TMemoryStream;
 
 function  IpToStr(IP: LongWord): string;
@@ -314,6 +325,10 @@ begin
 
   NetMode := NET_NONE;
 
+  if NetPortThread <> NilThreadId then
+    WaitForThreadTerminate(NetPortThread, 66666);
+
+  NetPortThread := NilThreadId;
   g_Net_UnforwardPorts();
 
   if NetDump then
@@ -332,6 +347,12 @@ end;
 { /// SERVER FUNCTIONS /// }
 
 
+function ForwardThread(Param: Pointer): PtrInt;
+begin
+  Result := 0;
+  if not g_Net_ForwardPorts() then Result := -1;
+end;
+
 function g_Net_Host(IPAddr: LongWord; Port: enet_uint16; MaxClients: Cardinal = 16): Boolean;
 begin
   if NetMode <> NET_NONE then
@@ -359,7 +380,7 @@ begin
   NetAddr.host := IPAddr;
   NetAddr.port := Port;
 
-  if NetForwardPorts then g_Net_ForwardPorts();
+  if NetForwardPorts then NetPortThread := BeginThread(ForwardThread);
 
   NetHost := enet_host_create(@NetAddr, NET_MAXCLIENTS, NET_CHANS, 0, 0);
 
@@ -375,7 +396,7 @@ begin
   if NetPongSock <> ENET_SOCKET_NULL then
   begin
     NetPongAddr.host := IPAddr;
-    NetPongAddr.port := Port + 1;
+    NetPongAddr.port := NET_PING_PORT;
     if enet_socket_bind(NetPongSock, @NetPongAddr) < 0 then
     begin
       enet_socket_destroy(NetPongSock);
@@ -490,6 +511,7 @@ begin
     NetOut.Clear();
     NetOut.Write(Byte(Ord('D')));
     NetOut.Write(Byte(Ord('F')));
+    NetOut.Write(NetPort);
     NetOut.Write(ClTime);
     g_Net_Slist_WriteInfo();
     NPl := 0;
@@ -518,10 +540,8 @@ begin
   Result := 0;
 
   if NetUseMaster then
-  begin
     g_Net_Slist_Check;
-    g_Net_Host_CheckPings;
-  end;
+  g_Net_Host_CheckPings;
 
   while (enet_host_service(NetHost, @NetEvent, 0) > 0) do
   begin
@@ -802,7 +822,7 @@ begin
 
     ProcessLoading(true);
 
-    if e_KeyPressed(IK_ESCAPE) or e_KeyPressed(IK_SPACE) then
+    if e_KeyPressed(IK_ESCAPE) or e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_ESCAPE) then
       OuterLoop := False;
   end;
 
@@ -887,7 +907,7 @@ begin
     end;
 end;
 
-procedure g_Net_SendData(Data:AByte; peer: pENetPeer; Reliable: Boolean; Chan: Byte = NET_CHAN_DOWNLOAD);
+procedure g_Net_SendData(Data: AByte; peer: pENetPeer; Reliable: Boolean; Chan: Byte = NET_CHAN_DOWNLOAD);
 var
   P: pENetPacket;
   F: enet_uint32;
@@ -965,7 +985,7 @@ begin
 
     ProcessLoading(true);
 
-    if e_KeyPressed(IK_ESCAPE) or e_KeyPressed(IK_SPACE) then
+    if e_KeyPressed(IK_ESCAPE) or e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_ESCAPE) then
       break;
   end;
   Result := msgStream;
@@ -1111,12 +1131,12 @@ begin
 end;
 
 function g_Net_ForwardPorts(ForwardPongPort: Boolean = True): Boolean;
+{$IFDEF USE_MINIUPNPC}
 var
   DevList: PUPNPDev;
   Urls: TUPNPUrls;
   Data: TIGDDatas;
   LanAddr: array [0..255] of Char;
-  ExtAddr: array [0..40] of Char;
   StrPort: AnsiString;
   Err, I: Integer;
 begin
@@ -1128,15 +1148,13 @@ begin
     exit;
   end;
 
-  conwriteln('trying to forward server ports...');
-
   NetPongForwarded := False;
   NetPortForwarded := 0;
 
-  DevList := upnpDiscover(2000, nil, nil, 0, 0, Addr(Err));
+  DevList := upnpDiscover(1000, nil, nil, 0, 0, 2, Addr(Err));
   if DevList = nil then
   begin
-    conwritefln('  upnpDiscover() failed: %d', [Err]);
+    conwritefln('port forwarding failed: upnpDiscover() failed: %d', [Err]);
     exit;
   end;
 
@@ -1144,19 +1162,11 @@ begin
 
   if I = 0 then
   begin
-    conwriteln('  could not find an IGD device on this LAN, aborting');
+    conwriteln('port forwarding failed: could not find an IGD device on this LAN');
     FreeUPNPDevList(DevList);
     FreeUPNPUrls(@Urls);
     exit;
-  end
-  else if I = 1 then
-    conwritefln('  found IGD @ %s', [Urls.controlURL])
-  else
-    conwritefln('  found some kind of UPNP device @ %s, maybe it''ll work', [Urls.controlURL]);
-
-  UPNP_GetExternalIPAddress(Urls.controlURL, Addr(data.first.servicetype[1]), Addr(ExtAddr[0]));
-  if ExtAddr[0] <> #0 then
-    conwritefln('  external IP address: %s', [Addr(ExtAddr[0])]);
+  end;
 
   StrPort := IntToStr(NetPort);
   I := UPNP_AddPortMapping(
@@ -1167,7 +1177,7 @@ begin
 
   if I <> 0 then
   begin
-    conwritefln('  forwarding port %d failed: error %d', [NetPort, I]);
+    conwritefln('forwarding port %d failed: error %d', [NetPort, I]);
     FreeUPNPDevList(DevList);
     FreeUPNPUrls(@Urls);
     exit;
@@ -1175,7 +1185,7 @@ begin
 
   if ForwardPongPort then
   begin
-    StrPort := IntToStr(NetPort + 1);
+    StrPort := IntToStr(NET_PING_PORT);
     I := UPNP_AddPortMapping(
       Urls.controlURL, Addr(data.first.servicetype[1]),
       PChar(StrPort), PChar(StrPort), Addr(LanAddr[0]), PChar('D2DF'),
@@ -1184,17 +1194,17 @@ begin
 
     if I <> 0 then
     begin
-      conwritefln('  forwarding port %d failed: error %d', [NetPort + 1, I]);
+      conwritefln('forwarding port %d failed: error %d', [NetPort + 1, I]);
       NetPongForwarded := False;
     end
     else
     begin
-      conwritefln('  forwarded port %d successfully', [NetPort + 1]);
+      conwritefln('forwarded port %d successfully', [NetPort + 1]);
       NetPongForwarded := True;
     end;
   end;
 
-  conwritefln('  forwarded port %d successfully', [NetPort]);
+  conwritefln('forwarded port %d successfully', [NetPort]);
   NetIGDControl := AnsiString(Urls.controlURL);
   NetIGDService := data.first.servicetype;
   NetPortForwarded := NetPort;
@@ -1203,8 +1213,14 @@ begin
   FreeUPNPUrls(@Urls);
   Result := True;
 end;
+{$ELSE}
+begin
+  Result := False;
+end;
+{$ENDIF}
 
 procedure g_Net_UnforwardPorts();
+{$IFDEF USE_MINIUPNPC}
 var
   I: Integer;
   StrPort: AnsiString;
@@ -1233,6 +1249,10 @@ begin
 
   NetPortForwarded := 0;
 end;
+{$ELSE}
+begin
+end;
+{$ENDIF}
 
 initialization