summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8640d90)
raw | patch | inline | side by side (parent: 8640d90)
author | Ketmar Dark <ketmar@ketmar.no-ip.org> | |
Tue, 15 Oct 2019 19:44:57 +0000 (22:44 +0300) | ||
committer | Ketmar Dark <ketmar@ketmar.no-ip.org> | |
Tue, 15 Oct 2019 21:10:30 +0000 (00:10 +0300) |
src/game/g_net.pas | patch | blob | history | |
src/game/g_netmaster.pas | patch | blob | history | |
src/game/g_window.pas | patch | blob | history |
diff --git a/src/game/g_net.pas b/src/game/g_net.pas
index 11acd5f079370213b7f3db2e0c7799149a4670d2..e6c1fbccb6b19deaec6cbb8e58ec3d41c9681eab 100644 (file)
--- a/src/game/g_net.pas
+++ b/src/game/g_net.pas
@@ -244,6 +244,10 @@ function g_Net_RequestResFileInfo (resIndex: LongInt; out tf: TNetFileTransfer):
function g_Net_AbortResTransfer (var tf: TNetFileTransfer): Boolean;
function g_Net_ReceiveResourceFile (resIndex: LongInt; var tf: TNetFileTransfer; strm: TStream): Integer;
+function g_Net_IsNetworkAvailable (): Boolean;
+procedure g_Net_InitLowLevel ();
+procedure g_Net_DeinitLowLevel ();
+
implementation
FILE_CHUNK_SIZE = 8192;
var
+ enet_init_success: Boolean = false;
g_Net_DownloadTimeout: Single;
trans_omsg: TMsg;
+function g_Net_IsNetworkAvailable (): Boolean;
+begin
+ result := enet_init_success;
+end;
+
+procedure g_Net_InitLowLevel ();
+begin
+ if enet_init_success then raise Exception.Create('wuta?!');
+ enet_init_success := (enet_initialize() = 0);
+end;
+
+procedure g_Net_DeinitLowLevel ();
+begin
+ if enet_init_success then
+ begin
+ enet_deinitialize();
+ enet_init_success := false;
+ end;
+end;
+
+
//**************************************************************************
//
// SERVICE FUNCTIONS
Result := N;
end;
+
function g_Net_Init(): Boolean;
var
F: TextFile;
g_Net_SaveBanList();
end;
- Result := (enet_initialize() = 0);
+ //Result := (enet_initialize() = 0);
+ Result := enet_init_success;
end;
procedure g_Net_Flush();
begin
g_Net_Cleanup();
- enet_deinitialize();
+ //enet_deinitialize();
NetInitDone := False;
end;
index 660d0052bd928d29709b03488f482437fe9539f1..aee13eb1c2ebb5d027ac02b9dada3af5f44802b7 100644 (file)
--- a/src/game/g_netmaster.pas
+++ b/src/game/g_netmaster.pas
NetUpdatePending: Boolean; // should we send an update after connection completes?
updateSent: Boolean;
lastUpdateTime: Int64;
- addressInited: Boolean;
// server list request working flags
srvAnswered: Integer;
srvAnswer: array of TNetServer;
function setAddress (hostandport: AnsiString): Boolean;
+ function isSameAddress (hostandport: AnsiString): Boolean;
+
function isValid (): Boolean;
function isAlive (): Boolean; // not disconnected
function isConnecting (): Boolean; // is connection in progress?
// but try to call this at least once in 100 msecs
procedure pulse ();
- procedure disconnect ();
+ procedure disconnect (forced: Boolean);
function connect (): Boolean;
procedure update ();
procedure TMasterHost.clear ();
begin
updateSent := false; // do not send 'remove'
- disconnect();
+ disconnect(true);
hostName := '';
hostPort := 25665;
netmsg.Free();
end;
+//==========================================================================
+//
+// TMasterHost.isSameAddress
+//
+//==========================================================================
+function TMasterHost.isSameAddress (hostandport: AnsiString): Boolean;
+var
+ cp, pp: Integer;
+ hn: AnsiString;
+begin
+ result := false;
+ if not isValid() then exit;
+ hostandport := Trim(hostandport);
+ if (length(hostandport) = 0) then exit;
+ hn := hostandport;
+ cp := Pos(':', hostandport);
+ if (cp > 0) then
+ begin
+ hn := Copy(hostandport, 1, cp-1);
+ Delete(hostandport, 1, cp);
+ if (length(hostandport) > 0) then
+ begin
+ try
+ pp := StrToInt(hostandport);
+ except
+ pp := -1;
+ end;
+ end;
+ end
+ else
+ begin
+ pp := 25665;
+ end;
+ result := strEquCI1251(hn, hostName) and (hostPort = pp);
+end;
+
+
//==========================================================================
//
// TMasterHost.setAddress
slUrgent := '';
slReadUrgent := true;
updateSent := false; // do not send 'remove'
- disconnect();
- addressInited := false;
+ disconnect(true);
hostName := '';
hostPort := 25665;
+
+ if (not g_Net_IsNetworkAvailable()) then exit;
+
hostandport := Trim(hostandport);
if (length(hostandport) > 0) then
begin
- hostName := hostandport;
+ hostName := hostandport;
cp := Pos(':', hostandport);
if (cp > 0) then
begin
end;
if not isValid() then exit;
- if (NetInitDone) then
+
+ if (enet_address_set_host(@enetAddr, PChar(Addr(hostName[1]))) <> 0) then
begin
- if (enet_address_set_host(@enetAddr, PChar(Addr(hostName[1]))) <> 0) then
- begin
- hostName := '';
- hostPort := 0;
- end;
- enetAddr.Port := hostPort;
+ writeln('SHIT!');
+ hostName := '';
+ hostPort := 0;
end;
+ enetAddr.Port := hostPort;
result := isValid();
+ //writeln('*********************: ', hostandport, ' [', hostName, ':', hostPort, '] ', result);
end;
begin
if not isAlive() then exit;
e_LogWritefln('disconnected from master at [%s:%u]', [hostName, hostPort], TMsgType.Notify);
- enet_peer_reset(peer);
- peer := nil;
- NetHostConnected := False;
- NetHostConReqTime := 0;
- NetUpdatePending := false;
- updateSent := false;
+ disconnect(true);
//if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
end;
Cnt: Byte;
f: Integer;
s: AnsiString;
- {
- I, RX: Integer;
- T: Int64;
- Sock: ENetSocket;
- Buf: ENetBuffer;
- InMsg: TMsg;
- SvAddr: ENetAddress;
- FromSL: Boolean;
- MyVer, Str: AnsiString;
- }
begin
e_LogWritefln('received packed from master at [%s:%u]', [hostName, hostPort], TMsgType.Notify);
if not msg.Init(pkt^.data, pkt^.dataLength, True) then exit;
// TMasterHost.disconnect
//
//==========================================================================
-procedure TMasterHost.disconnect ();
+procedure TMasterHost.disconnect (forced: Boolean);
begin
if not isAlive() then exit;
//if (NetMode = NET_SERVER) and isConnected() and updateSent then remove();
- enet_peer_disconnect_later(peer, 0);
- // main pulse will take care of the rest
+ if (forced) then
+ begin
+ enet_peer_reset(peer);
+ peer := nil;
+ NetHostConReqTime := 0;
+ end
+ else
+ begin
+ enet_peer_disconnect_later(peer, 0);
+ // main pulse will take care of the rest
+ NetHostConReqTime := -1;
+ end;
+
NetHostConnected := false;
- NetHostConReqTime := -1;
NetUpdatePending := false;
updateSent := false;
-
//if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
end;
function TMasterHost.connect (): Boolean;
begin
result := false;
- if not isValid() or (NetHostConReqTime = -1) then exit;
- if isAlive() then begin result := true; exit; end;
+ if not isValid() then exit;
+ if (NetHostConReqTime = -1) then
+ begin
+ disconnect(true);
+ end
+ else
+ begin
+ if isAlive() then begin result := true; exit; end;
+ end;
SetLength(srvAnswer, 0);
srvAnswered := 0;
NetHostConReqTime := 0;
NetUpdatePending := false;
updateSent := false;
- if (not NetInitDone) then exit;
-
- if (not addressInited) then
- begin
- if (enet_address_set_host(@enetAddr, PChar(Addr(hostName[1]))) <> 0) then
- begin
- hostName := '';
- hostPort := 0;
- exit;
- end;
- enetAddr.Port := hostPort;
- addressInited := true;
- end;
peer := enet_host_connect(NetMHost, @enetAddr, NET_MCHANS, 0);
if (peer = nil) then
//**************************************************************************
procedure g_Net_Slist_Set (IP: AnsiString; Port: Word);
+var
+ f: Integer;
+ sa: AnsiString;
begin
- if (length(mlist) = 0) then
- begin
- SetLength(mlist, 1);
- mlist[0].Create(ip+':'+IntToStr(Port));
- end
- else
- begin
- mlist[0].setAddress(ip+':'+IntToStr(Port));
- end;
+ if (not g_Net_IsNetworkAvailable()) then exit;
+ IP := Trim(IP);
+ if (length(IP) = 0) or (Port = 0) then exit;
+ sa := IP+':'+IntToStr(Port);
+ for f := 0 to High(mlist) do if (mlist[f].isSameAddress(sa)) then exit;
+ SetLength(mlist, length(mlist)+1);
+ mlist[High(mlist)].Create(sa);
+ mlist[High(mlist)].setAddress(sa);
e_LogWritefln('Masterserver address set to [%s:%u]', [IP, Port], TMsgType.Notify);
- {
- if NetInitDone then
- begin
- enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
- NetSlistAddr.Port := Port;
- e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), TMsgType.Notify);
- end;
- }
end;
sres: Integer;
idx: Integer;
begin
+ if (not g_Net_IsNetworkAvailable()) then exit;
+
if (length(mlist) = 0) then
begin
if (NetMHost <> nil) then
procedure DisconnectAll ();
var
f: Integer;
+ hasAlive: Boolean;
+ stt, ct: Int64;
begin
- for f := 0 to High(mlist) do
+ stt := GetTimerMS();
+ while (length(mlist) > 0) do
begin
- if (mlist[f].isAlive()) then mlist[f].disconnect();
+ hasAlive := false;
+ for f := 0 to High(mlist) do
+ begin
+ if (mlist[f].isAlive()) then
+ begin
+ hasAlive := true;
+ mlist[f].disconnect(false);
+ end;
+ end;
+ if not hasAlive then break;
+ g_Net_Slist_Pulse(100);
+ ct := GetTimerMS();
+ if (ct < stt) or (ct-stt > 800) then break;
end;
end;
result := false;
SL := nil;
+ if (not g_Net_IsNetworkAvailable()) then
+ begin
+ SetLength(SL, 0);
+ exit;
+ end;
+
g_Net_Slist_Pulse(); // this will create mhost
NetOut.Clear();
NetOut.Write(MyVer);
try
- aliveCount := 0;
- for f := 0 to High(mlist) do
- begin
- mlist[f].srvAnswered := 0;
- if (not mlist[f].isValid()) then continue;
- if (not mlist[f].isConnected()) then mlist[f].connect();
- if (not mlist[f].isAlive()) then continue;
- if (mlist[f].isConnected()) then
- begin
- pkt := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
- if assigned(pkt) then
- begin
- if (enet_peer_send(mlist[f].peer, NET_MCHAN_MAIN, pkt) = 0) then
- begin
- Inc(aliveCount);
- mlist[f].srvAnswered := 1;
- end;
- end;
- end
- else if (mlist[f].isConnecting()) then
- begin
- Inc(aliveCount);
- end;
- end;
-
- if (aliveCount = 0) then
- begin
- DisconnectAll();
- CheckLocalServers();
- exit;
- end;
-
e_WriteLog('Fetching serverlist...', TMsgType.Notify);
g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
hasUnanswered := false;
for f := 0 to High(mlist) do
begin
+ {
+ e_LogWritefln(' master #%d: [%s:%u] valid=%d; alive=%d; connected=%d; connecting=%d',
+ [f, mlist[f].hostName, mlist[f].hostPort, Integer(mlist[f].isValid()), Integer(mlist[f].isAlive()),
+ Integer(mlist[f].isConnected()), Integer(mlist[f].isConnecting())], TMsgType.Notify);
+ }
if (not mlist[f].isValid()) then continue;
- if (mlist[f].isConnected()) then
+ if (not mlist[f].isAlive()) then
+ begin
+ mlist[f].connect();
+ if (mlist[f].isAlive()) then
+ begin
+ hasUnanswered := true;
+ stt := GetTimerMS();
+ end;
+ end
+ else if (mlist[f].isConnected()) then
begin
if (mlist[f].srvAnswered = 0) then
begin
begin
hasUnanswered := true;
mlist[f].srvAnswered := 1;
+ stt := GetTimerMS();
end;
end;
end
diff --git a/src/game/g_window.pas b/src/game/g_window.pas
index 6cce5613bb0afa67e1014baf3915e9ed0b806e3a..6491048c5f94e6406461aea3bdc99c8403fa4671 100644 (file)
--- a/src/game/g_window.pas
+++ b/src/game/g_window.pas
Init;
Time_Old := sys_GetTicks();
+ g_Net_InitLowLevel();
+
// Êîìàíäíàÿ ñòðîêà
if (ParamCount > 0) then g_Game_Process_Params();
while not ProcessMessage() do begin end;
Release();
+
+ g_Net_DeinitLowLevel();
result := 0;
end;