DEADSOFTWARE

net: enet+shitdoze == clusterfuck; added workaround for `enet_host_service()` (and...
[d2df-sdl.git] / src / game / g_net.pas
index e6c1fbccb6b19deaec6cbb8e58ec3d41c9681eab..261df8cad6e0aa6e08b42ccd41c3a312f8601410 100644 (file)
@@ -147,9 +147,9 @@ var
   NetPongSock:   ENetSocket = ENET_SOCKET_NULL;
 
   NetUseMaster: Boolean = True;
-  NetSlistAddr: ENetAddress;
   NetSlistIP:   string = 'mpms.doom2d.org';
   NetSlistPort: Word = 25665;
+  NetSlistList: string = 'deadsoftware.ru:25665';
 
   NetClientIP:   string = '127.0.0.1';
   NetClientPort: Word   = 25666;
@@ -251,6 +251,11 @@ procedure g_Net_DeinitLowLevel ();
 
 implementation
 
+// *enet_host_service()*
+// fuck! https://www.mail-archive.com/enet-discuss@cubik.org/msg00852.html
+// tl;dr: on shitdows, we can get -1 sometimes, and it is *NOT* a failure.
+//        thank you, enet. let's ignore failures altogether then.
+
 uses
   SysUtils,
   e_input, g_nethandler, g_netmsg, g_netmaster, g_player, g_window, g_console,
@@ -358,6 +363,7 @@ begin
   e_LogWritefln('disconnected client #%d due to file transfer error', [nc.ID], TMsgType.Warning);
   enet_peer_disconnect(nc.Peer, NET_DISC_FILE_TIMEOUT);
   clearNetClientTransfers(nc);
+  g_Net_Slist_ServerPlayerLeaves();
 end;
 
 
@@ -766,13 +772,15 @@ begin
     ett := getNewTimeoutEnd();
     repeat
       status := enet_host_service(NetHost, @ev, 300);
+      {
       if (status < 0) then
       begin
         g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
         Result := -1;
         exit;
       end;
-      if (status = 0) then
+      }
+      if (status <= 0) then
       begin
         // check for timeout
         ct := GetTimerMS();
@@ -945,13 +953,15 @@ begin
     ett := getNewTimeoutEnd();
     repeat
       status := enet_host_service(NetHost, @ev, 300);
+      {
       if (status < 0) then
       begin
         g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
         Result := -1;
         exit;
       end;
-      if (status = 0) then
+      }
+      if (status <= 0) then
       begin
         // check for timeout
         ct := GetTimerMS();
@@ -1127,13 +1137,15 @@ begin
     repeat
       //stx := -GetTimerMS();
       status := enet_host_service(NetHost, @ev, 300);
+      {
       if (status < 0) then
       begin
         g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
         Result := -1;
         exit;
       end;
-      if (status = 0) then
+      }
+      if (status <= 0) then
       begin
         // check for timeout
         ct := GetTimerMS();
@@ -1413,8 +1425,7 @@ begin
 
   NetPeer := nil;
   NetHost := nil;
-  g_Net_Slist_NetworkStopped();
-  //g_Net_Slist_Disconnect(false); // do not spam console
+  g_Net_Slist_ServerClosed();
   NetMyID := -1;
   NetPlrUID1 := -1;
   NetPlrUID2 := -1;
@@ -1549,8 +1560,7 @@ begin
     end;
 
   clearNetClients(false); // don't clear array
-  //if (g_Net_Slist_IsConnectionActive) then g_Net_Slist_Disconnect;
-  g_Net_Slist_NetworkStopped();
+  g_Net_Slist_ServerClosed();
   if NetPongSock <> ENET_SOCKET_NULL then
     enet_socket_destroy(NetPongSock);
 
@@ -1620,7 +1630,6 @@ begin
     NetOut.Write(Byte(Ord('F')));
     NetOut.Write(NetPort);
     NetOut.Write(ClTime);
-    //g_Net_Slist_WriteInfo();
     TMasterHost.writeInfo(NetOut);
     NPl := 0;
     if gPlayer1 <> nil then Inc(NPl);
@@ -1648,12 +1657,8 @@ begin
   IP := '';
   Result := 0;
 
-  if NetUseMaster then
-  begin
-    //g_Net_Slist_Check;
-    g_Net_Slist_Pulse();
-  end;
-  g_Net_Host_CheckPings;
+  if NetUseMaster then g_Net_Slist_Pulse();
+  g_Net_Host_CheckPings();
 
   while (enet_host_service(NetHost, @NetEvent, 0) > 0) do
   begin
@@ -1757,11 +1762,7 @@ begin
         g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_MSG_HOST_DISC], [ID]));
         Dec(NetClientCount);
 
-        if NetUseMaster then
-        begin
-          //g_Net_Slist_Update;
-          g_Net_Slist_Pulse();
-        end;
+        if NetUseMaster then g_Net_Slist_ServerPlayerLeaves();
       end;
     end;
   end;