Merge branch 'gx/test_new' of http://60.1.1.230/gaoxin/JL_Client into gx/test_new

dev_chengFeng
JieLing 2021-01-07 11:25:29 +08:00
commit 6dea99ece5
2 changed files with 49 additions and 18 deletions

View File

@ -231,7 +231,7 @@ public class SocketClient
}
}
int _NNN;
/// <summary>
/// 读取消息
/// </summary>
@ -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())
{
@ -265,7 +274,7 @@ public class SocketClient
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)
{
@ -394,7 +403,7 @@ public class SocketClient
buffer.ReadIntToByte();
//协议号
int msgid = buffer.ReadIntToByte();
Debug.Log("[SocketClient:OnReceivedMessage]msgid:" + msgid);
//收到心跳包
if (msgid == 1001)
{
@ -427,18 +436,28 @@ public class SocketClient
/// </summary>
public void Close()
{
Debug.Log("****Socket*********************Close ---" + IpAddress);
//确保 关闭Client的链接状态。
UnregistNetMessage(INDICATION_ERROR_CODE, Ygqmgx_indicationErrorAction);
if (client != null)
{
try
{
client.Close();
client.Client.Disconnect(false);
//Debug.Log("****Socket*********************Disconnect() ---" + client.Client.Handle+ "@"+ client.Client.LocalEndPoint.ToString());
}
finally
catch { }
Debug.Log("****Socket*********************Close ---" + client.Client.Handle);
try
{
client.Close();
client = null;
}
catch
{
}
}
@ -684,20 +703,22 @@ public class SocketClient
float _LastReconectime = 0;
public void TryReconnect()
{
if (Time.time - _LastReconectime < AppConst.ConnectTimeout + 1)
if (IsConnected())
{
NetworkStateInfo info = new NetworkStateInfo();
info.type = NetworkStateType.Reconnected;
info.msg = null;
stateInfoStack.Push(info);
return;
}
if (Time.time - _LastReconectime < AppConst.ConnectTimeout )
return;
_LastReconectime = Time.time;
Debug.Log("****Socket*********************TryReconnect ---" + IpAddress);
//if (IsConnected())
//{
// NetworkStateInfo info = new NetworkStateInfo();
// info.type = NetworkStateType.Reconnected;
// info.msg = null;
// stateInfoStack.Push(info);
// return;
//}
if (reconnect_co != null)
@ -719,6 +740,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;
@ -780,6 +802,7 @@ public class SocketClient
void OnReconnect(IAsyncResult result)
{
_LastReconectime = 0;
m_isConnecting = false;
//检测是否成功的建立了链接。..
@ -797,7 +820,8 @@ 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);
_LastReconectime = 0;
Debug.Log("****Socket*********************OnReconnect SUCCESS @"+ client.Client.LocalEndPoint.ToString());
}
catch (Exception e)
{
@ -838,7 +862,7 @@ public class SocketClient
buffer.Close();
//UnityEngine.Debug.Log("SocketClient.m_SendMessage..."+ msgId);
Debug.Log("[SocketClient.m_SendMessage]..."+ msgId);
}
/// <summary>

View File

@ -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#变量记录,用于记录异常退出信息