DEADSOFTWARE

attempt to unfuck serverlist
[d2df-sdl.git] / src / game / g_netmaster.pas
index 1703da27cdcbe44c21bf4f0b6bbc9875c97af211..391d7729cf405f7e38ba25324f95318487e76af2 100644 (file)
@@ -86,6 +86,7 @@ type
     slReadUrgent: Boolean;
     // temporary mark
     justAdded: Boolean;
+    connectCount: Integer;
 
   private
     netmsg: TMsg;
@@ -132,7 +133,7 @@ var
   NMASTER_FORCE_UPDATE_TIMEOUT: Integer = 0; // fuck you, fpc, and your idiotic "diagnostics"
 
 
-procedure g_Net_Slist_Set (IP: AnsiString; Port: Word; list: AnsiString='');
+procedure g_Net_Slist_Set (list: AnsiString);
 function g_Net_Slist_Fetch (var SL: TNetServerList): Boolean;
 
 // make this server private
@@ -507,6 +508,8 @@ begin
   slMOTD := '';
   slUrgent := '';
   slReadUrgent := true;
+  justAdded := false;
+  connectCount := 0;
   netmsg.Alloc(NET_BUFSIZE);
   setAddress(ea, '');
 end;
@@ -779,6 +782,7 @@ begin
   NetUpdatePending := false;
   updateSent := false;
   lastUpdateTime := 0;
+  Inc(connectCount);
 
   peer := enet_host_connect(NetMHost, @enetAddr, NET_MCHANS, 0);
   if (peer = nil) then
@@ -1049,7 +1053,7 @@ end;
 //  g_Net_Slist_Set
 //
 //==========================================================================
-procedure g_Net_Slist_Set (IP: AnsiString; Port: Word; list: AnsiString='');
+procedure g_Net_Slist_Set (list: AnsiString);
 var
   f, dest: Integer;
   sa: AnsiString;
@@ -1060,13 +1064,6 @@ begin
 
   for f := 0 to High(mlist) do mlist[f].justAdded := false;
 
-  IP := Trim(IP);
-  if (length(IP) > 0) and (Port > 0) then
-  begin
-    sa := IP+':'+IntToStr(Port);
-    if parseAddressPort(ea, sa) then addMasterRecord(ea, sa);
-  end;
-
   list := Trim(list);
   //writeln('list=[', list, ']');
   while (length(list) > 0) do
@@ -1160,7 +1157,7 @@ begin
     if (not mlist[f].isAlive()) then
     begin
       // not connected; try to reconnect if we're asking for a host list, or we are in netgame, and we are the host
-      if isListQuery or isMasterReportsEnabled() then
+      if (not isListQuery) and isMasterReportsEnabled() then
       begin
         if (mlist[f].lastDisconnectTime = 0) or (ct < mlist[f].lastDisconnectTime) or (ct-mlist[f].lastDisconnectTime >= 1000*NMASTER_TIMEOUT_RECONNECT) then
         begin
@@ -1369,6 +1366,7 @@ var
   aliveCount: Integer;
   hasUnanswered: Boolean;
   stt, ct: Int64;
+  tmpsv: TNetServer;
 begin
   result := false;
   SL := nil;
@@ -1383,6 +1381,12 @@ begin
 
   DisconnectAll(true); // forced disconnect
 
+  for f := 0 to High(mlist) do
+  begin
+    mlist[f].connectCount := 0;
+    mlist[f].srvAnswered := 0;
+  end;
+
   NetOut.Clear();
   NetOut.Write(Byte(NET_MMSG_GET));
 
@@ -1410,12 +1414,19 @@ begin
         if (not mlist[f].isValid()) then continue;
         if (not mlist[f].isAlive()) then
         begin
-          mlist[f].connect();
-          if (mlist[f].isAlive()) then
+          if (mlist[f].connectCount = 0) then
           begin
-            //g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_WCONN], [mlist[f].hostName]));
-            hasUnanswered := true;
-            stt := GetTimerMS();
+            mlist[f].connect();
+            if (mlist[f].isAlive()) then
+            begin
+              //g_Console_Add(Format(_lc[I_NET_MSG]+_lc[I_NET_SLIST_WCONN], [mlist[f].hostName]));
+              hasUnanswered := true;
+              stt := GetTimerMS();
+            end;
+          end
+          else if (mlist[f].srvAnswered > 1) then
+          begin
+            Inc(aliveCount);
           end;
         end
         else if (mlist[f].isConnected()) then
@@ -1441,6 +1452,7 @@ begin
           else if (mlist[f].srvAnswered > 1) then
           begin
             Inc(aliveCount);
+            mlist[f].disconnect(false); // not forced
           end;
         end
         else if (mlist[f].isConnecting()) then
@@ -1541,32 +1553,43 @@ begin
       if InMsg.ReadChar() <> 'D' then continue;
       if InMsg.ReadChar() <> 'F' then continue;
 
+      with tmpsv do
+      begin
+        Port := InMsg.ReadWord();
+        Ping := InMsg.ReadInt64();
+        Ping := GetTimerMS() - Ping;
+        Name := InMsg.ReadString();
+        Map := InMsg.ReadString();
+        GameMode := InMsg.ReadByte();
+        Players := InMsg.ReadByte();
+        MaxPlayers := InMsg.ReadByte();
+        Protocol := InMsg.ReadByte();
+        Password := InMsg.ReadByte() = 1;
+        LocalPl := InMsg.ReadByte();
+        Bots := InMsg.ReadWord();
+        PingAddr := SvAddr;
+      end;
+
       FromSL := False;
       for I := Low(SL) to High(SL) do
         if (SL[I].PingAddr.host = SvAddr.host) and
-           (SL[I].PingAddr.port = SvAddr.port) then
+           (SL[I].PingAddr.port = SvAddr.port) and
+           (SL[I].Port = tmpsv.Port) then
         begin
-          with SL[I] do
-          begin
-            Port := InMsg.ReadWord();
-            Ping := InMsg.ReadInt64();
-            Ping := GetTimerMS() - Ping;
-            Name := InMsg.ReadString();
-            Map := InMsg.ReadString();
-            GameMode := InMsg.ReadByte();
-            Players := InMsg.ReadByte();
-            MaxPlayers := InMsg.ReadByte();
-            Protocol := InMsg.ReadByte();
-            Password := InMsg.ReadByte() = 1;
-            LocalPl := InMsg.ReadByte();
-            Bots := InMsg.ReadWord();
-          end;
+          tmpsv.IP := SL[I].IP;
+          SL[I] := tmpsv;
           FromSL := True;
           Inc(Cnt);
           break;
         end;
+
       if not FromSL then
-        ProcessLocal();
+      begin
+        I := Length(SL);
+        SetLength(SL, I + 1);
+        tmpsv.IP := DecodeIPV4(SvAddr.host);
+        SL[I] := tmpsv;
+      end;
     end;
 
     InMsg.Free();
@@ -1767,7 +1790,7 @@ procedure g_Serverlist_GenerateTable (SL: TNetServerList; var ST: TNetServerTabl
 var
   i, j: Integer;
 
-  function FindServerInTable(Name: AnsiString): Integer;
+  function FindServerInTable(Name: AnsiString; Port: Word): Integer;
   var
     i: Integer;
   begin
@@ -1778,7 +1801,7 @@ var
     begin
       if Length(ST[i].Indices) = 0 then
         continue;
-      if SL[ST[i].Indices[0]].Name = Name then
+      if (SL[ST[i].Indices[0]].Name = Name) and (SL[ST[i].Indices[0]].Port = Port) then
       begin
         Result := i;
         Exit;
@@ -1827,9 +1850,10 @@ begin
   ST := nil;
   if SL = nil then
     Exit;
+
   for i := Low(SL) to High(SL) do
   begin
-    j := FindServerInTable(SL[i].Name);
+    j := FindServerInTable(SL[i].Name, SL[i].Port);
     if j = -1 then
     begin
       j := Length(ST);