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>
/// 读取消息 /// 读取消息
/// </summary> /// </summary>
@ -240,11 +240,18 @@ public class SocketClient
int bytesRead = 0; int bytesRead = 0;
try try
{ {
//System.Net.Sockets.Socket.SocketAsyncResult
//NetBug.Fix by FHF.. //NetBug.Fix by FHF..
//甄别socket当前是否正在连接中避免重连的间隔内收到服务端消息。. //甄别socket当前是否正在连接中避免重连的间隔内收到服务端消息。.
if (client == null || !client.Connected) if (client == null || !client.Connected)
return; return;
//int ilen = client.GetStream().EndRead(asr);
// Debug.Log("[SocketClient] From Socket: EndRead.Len:" + ilen);
lock (client.GetStream()) lock (client.GetStream())
{ {
//读取字节流到缓冲区 //读取字节流到缓冲区
@ -257,6 +264,8 @@ public class SocketClient
AddStateInfo(NetworkStateType.Disconnect, "bytesRead < 1"); AddStateInfo(NetworkStateType.Disconnect, "bytesRead < 1");
return; return;
} }
Debug.Log("[SocketClient] NowClient:@" + ++_NNN+" _:"+bytesRead + ":" + client.Client.Handle);
OnReceive(byteBuffer, bytesRead); //分析数据包内容,抛给逻辑层 OnReceive(byteBuffer, bytesRead); //分析数据包内容,抛给逻辑层
lock (client.GetStream()) lock (client.GetStream())
{ {
@ -265,7 +274,7 @@ public class SocketClient
client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); 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) catch (Exception ex)
{ {
@ -394,7 +403,7 @@ public class SocketClient
buffer.ReadIntToByte(); buffer.ReadIntToByte();
//协议号 //协议号
int msgid = buffer.ReadIntToByte(); int msgid = buffer.ReadIntToByte();
Debug.Log("[SocketClient:OnReceivedMessage]msgid:" + msgid);
//收到心跳包 //收到心跳包
if (msgid == 1001) if (msgid == 1001)
{ {
@ -427,18 +436,28 @@ public class SocketClient
/// </summary> /// </summary>
public void Close() public void Close()
{ {
Debug.Log("****Socket*********************Close ---" + IpAddress);
//确保 关闭Client的链接状态。 //确保 关闭Client的链接状态。
UnregistNetMessage(INDICATION_ERROR_CODE, Ygqmgx_indicationErrorAction); UnregistNetMessage(INDICATION_ERROR_CODE, Ygqmgx_indicationErrorAction);
if (client != null) if (client != null)
{ {
try 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; client = null;
}
catch
{
} }
} }
@ -684,20 +703,22 @@ public class SocketClient
float _LastReconectime = 0; float _LastReconectime = 0;
public void TryReconnect() 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; return;
_LastReconectime = Time.time; _LastReconectime = Time.time;
Debug.Log("****Socket*********************TryReconnect ---" + IpAddress); 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) if (reconnect_co != null)
@ -719,6 +740,7 @@ public class SocketClient
var ipType = GetIPAddressType(IpAddress); var ipType = GetIPAddressType(IpAddress);
client = null; client = null;
client = new TcpClient(ipType); client = new TcpClient(ipType);
Debug.Log("****Socket*********************NEW ---" + client.Client.Handle);
//client.SendTimeout = 1000; //client.SendTimeout = 1000;
//client.ReceiveTimeout = 1000; //client.ReceiveTimeout = 1000;
client.NoDelay = true; client.NoDelay = true;
@ -780,6 +802,7 @@ public class SocketClient
void OnReconnect(IAsyncResult result) void OnReconnect(IAsyncResult result)
{ {
_LastReconectime = 0;
m_isConnecting = false; m_isConnecting = false;
//检测是否成功的建立了链接。.. //检测是否成功的建立了链接。..
@ -797,7 +820,8 @@ public class SocketClient
outStream = client.GetStream(); outStream = client.GetStream();
client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null);
AddStateInfo(NetworkStateType.Reconnected, 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) catch (Exception e)
{ {
@ -838,7 +862,7 @@ public class SocketClient
buffer.Close(); buffer.Close();
//UnityEngine.Debug.Log("SocketClient.m_SendMessage..."+ msgId); Debug.Log("[SocketClient.m_SendMessage]..."+ msgId);
} }
/// <summary> /// <summary>

View File

@ -27,6 +27,8 @@ function Network.New(type, socket)
instance.re_sendWaitTime = 0 --记录断线等待时间(秒) instance.re_sendWaitTime = 0 --记录断线等待时间(秒)
instance.re_maxSendWaitTime = 5 --消息断线重发等待时间(秒),超过该时间会重连 instance.re_maxSendWaitTime = 5 --消息断线重发等待时间(秒),超过该时间会重连
instance.ReconnectTime=0 -- 重连的发生时间
setmetatable(instance, Network) setmetatable(instance, Network)
return instance return instance
end end
@ -66,6 +68,7 @@ function Network:OnDisconnect(err)
RequestPanel.Show("您已掉线,正在请求重连")--..err) RequestPanel.Show("您已掉线,正在请求重连")--..err)
self.socket:TryReconnect() self.socket:TryReconnect()
self.ReconnectTime= Time.time
end end
function Network:SendClientHeartBeat() function Network:SendClientHeartBeat()
@ -231,6 +234,10 @@ function Network:ReceiveErrorInfo(buffer)
end end
else else
if msg.errCode == -101 or msg.errCode == -102 then if msg.errCode == -101 or msg.errCode == -102 then
if(Time.time- self.ReconnectTime<10)then
return
end
--被顶号或者被封号 --被顶号或者被封号
Game.Logout() Game.Logout()
AppConst.Code = msg.errMsg --TODO:重登会重置所有lua变量信息用一个不用的C#变量记录,用于记录异常退出信息 AppConst.Code = msg.errMsg --TODO:重登会重置所有lua变量信息用一个不用的C#变量记录,用于记录异常退出信息