From 18642e01ae4ac3a93b9ff36bf53fb5ebc9293390 Mon Sep 17 00:00:00 2001 From: Fuhaifeng Date: Wed, 6 Jan 2021 17:35:48 +0800 Subject: [PATCH] =?UTF-8?q?LUA=20=20=E4=B8=AD=E6=A0=A1=E9=AA=8C=E8=A2=AB?= =?UTF-8?q?=E6=8F=90=E5=87=BA=E7=9A=84=E5=90=88=E7=90=86=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Network/SocketClient.cs | 39 +++++++++++++++---- .../ManagedResources/~Lua/Logic/Network.lua | 7 ++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Assets/LuaFramework/Scripts/Network/SocketClient.cs b/Assets/LuaFramework/Scripts/Network/SocketClient.cs index a4bdec5436..41124af004 100644 --- a/Assets/LuaFramework/Scripts/Network/SocketClient.cs +++ b/Assets/LuaFramework/Scripts/Network/SocketClient.cs @@ -231,7 +231,7 @@ public class SocketClient } } - + int _NNN; /// /// 读取消息 /// @@ -240,11 +240,18 @@ public class SocketClient int bytesRead = 0; try { + + + //System.Net.Sockets.Socket.SocketAsyncResult //NetBug.Fix by FHF.. //甄别socket当前是否正在连接中,避免重连的间隔内收到服务端消息。. if (client == null || !client.Connected) return; + //int ilen = client.GetStream().EndRead(asr); + // Debug.Log("[SocketClient] From Socket: EndRead.Len:" + ilen); + + lock (client.GetStream()) { //读取字节流到缓冲区 @@ -257,6 +264,8 @@ public class SocketClient AddStateInfo(NetworkStateType.Disconnect, "bytesRead < 1"); return; } + Debug.Log("[SocketClient] NowClient:@" + ++_NNN+" _:"+bytesRead + ":" + client.Client.Handle); + OnReceive(byteBuffer, bytesRead); //分析数据包内容,抛给逻辑层 lock (client.GetStream()) { @@ -264,8 +273,8 @@ public class SocketClient Array.Clear(byteBuffer, 0, byteBuffer.Length); //清空数组 client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); } - - Debug.Log("SocketClient.OnRead " + byteBuffer.ToString()); + + //Debug.Log("SocketClient.OnRead " + byteBuffer.ToString()); } catch (Exception ex) { @@ -427,18 +436,31 @@ public class SocketClient /// public void Close() { - Debug.Log("****Socket*********************Close ---" + IpAddress); + //确保 关闭Client的链接状态。 UnregistNetMessage(INDICATION_ERROR_CODE, Ygqmgx_indicationErrorAction); if (client != null) { + + + + try + { + client.Client.Disconnect(false); + Debug.Log("****Socket*********************Disconnect() ---" + client.Client.Handle+ "@"+ client.Client.LocalEndPoint.ToString()); + } + catch { } + + Debug.Log("****Socket*********************Close ---" + client.Client.Handle); try { client.Close(); - } - finally - { client = null; + + } + catch + { + } } @@ -719,6 +741,7 @@ public class SocketClient var ipType = GetIPAddressType(IpAddress); client = null; client = new TcpClient(ipType); + Debug.Log("****Socket*********************NEW ---" + client.Client.Handle); //client.SendTimeout = 1000; //client.ReceiveTimeout = 1000; client.NoDelay = true; @@ -797,7 +820,7 @@ public class SocketClient outStream = client.GetStream(); client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); AddStateInfo(NetworkStateType.Reconnected, null); - Debug.Log("****Socket*********************OnReconnect SUCCESS ---" + IpAddress); + Debug.Log("****Socket*********************OnReconnect SUCCESS @"+ client.Client.LocalEndPoint.ToString()); } catch (Exception e) { diff --git a/Assets/ManagedResources/~Lua/Logic/Network.lua b/Assets/ManagedResources/~Lua/Logic/Network.lua index 1137a189cf..be63f8b6ae 100644 --- a/Assets/ManagedResources/~Lua/Logic/Network.lua +++ b/Assets/ManagedResources/~Lua/Logic/Network.lua @@ -27,6 +27,8 @@ function Network.New(type, socket) instance.re_sendWaitTime = 0 --记录断线等待时间(秒) instance.re_maxSendWaitTime = 5 --消息断线重发等待时间(秒),超过该时间会重连 + instance.ReconnectTime=0 -- 重连的发生时间 + setmetatable(instance, Network) return instance end @@ -66,6 +68,7 @@ function Network:OnDisconnect(err) RequestPanel.Show("您已掉线,正在请求重连")--..err) self.socket:TryReconnect() + self.ReconnectTime= Time.time end function Network:SendClientHeartBeat() @@ -231,6 +234,10 @@ function Network:ReceiveErrorInfo(buffer) end else if msg.errCode == -101 or msg.errCode == -102 then + + if(Time.time- self.ReconnectTime<10)then + return + end --被顶号或者被封号 Game.Logout() AppConst.Code = msg.errMsg --TODO:重登会重置所有lua变量信息,用一个不用的C#变量记录,用于记录异常退出信息