添加日志输出
parent
4f4efeeea4
commit
385bfdbf20
|
@ -44,6 +44,7 @@ namespace GameLogic
|
|||
|
||||
void Awake()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
|
@ -87,6 +88,7 @@ namespace GameLogic
|
|||
|
||||
public SocketClient AddSocket(string ipAddress, int port)
|
||||
{
|
||||
Debug.Log("SocketClient AddSocket");
|
||||
SocketClient socket = new SocketClient(ipAddress, port);
|
||||
socket.netMgr = this;
|
||||
socketList.Add(socket);
|
||||
|
|
|
@ -20,7 +20,6 @@ public enum NetworkStateType
|
|||
Disconnect,
|
||||
}
|
||||
|
||||
|
||||
static class IPAddressExtensions
|
||||
{
|
||||
public static IPAddress MapToIPv6(this IPAddress addr)
|
||||
|
@ -106,16 +105,17 @@ public class SocketClient
|
|||
// Use this for initialization
|
||||
public SocketClient(string ipAddress, int port)
|
||||
{
|
||||
|
||||
//Debug.Log("****Socket*********************创建一个Socket ---" + ipAddress);
|
||||
IpAddress = ipAddress;
|
||||
Port = port;
|
||||
_memStream = new MemoryStream();
|
||||
_reader = new BinaryReader(_memStream);
|
||||
|
||||
Debug.Log("SocketClient SocketClient ipAddress: " + ipAddress + " port: " + port);
|
||||
}
|
||||
|
||||
public void SetIpAddress(string ipAddress, int port)
|
||||
{
|
||||
Debug.Log("SocketClient SetIpAddress ipAddress: " + ipAddress + " port: " + port);
|
||||
if (!IsConnected())
|
||||
{
|
||||
IpAddress = ipAddress;
|
||||
|
@ -130,10 +130,8 @@ public class SocketClient
|
|||
/// </summary>
|
||||
public void Connect()
|
||||
{
|
||||
|
||||
//Debug.Log("****Socket*********************Connect ---" + IpAddress);
|
||||
Close();
|
||||
|
||||
Debug.Log("SocketClient Connect ipAddress: " + IpAddress);
|
||||
try
|
||||
{
|
||||
var ipType = GetIPAddressType(IpAddress);
|
||||
|
@ -147,14 +145,13 @@ public class SocketClient
|
|||
catch (Exception e)
|
||||
{
|
||||
OnConnectFail();
|
||||
Debug.LogWarning(e.Message);
|
||||
Debug.LogError("SocketClient Connect catch: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnConnectFail()
|
||||
{
|
||||
//Debug.Log("****Socket*********************OnConnectFail ---" + IpAddress);
|
||||
AddStateInfo(NetworkStateType.ConnectFail, null);
|
||||
AddStateInfo(NetworkStateType.ConnectFail, "OnConnectFail");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -162,10 +159,10 @@ public class SocketClient
|
|||
/// </summary>
|
||||
void OnConnect(IAsyncResult asr)
|
||||
{
|
||||
//Debug.Log("****Socket*********************OnConnect ---" + IpAddress);
|
||||
Debug.LogError("SocketClient OnConnect");
|
||||
outStream = client.GetStream();
|
||||
client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null);
|
||||
AddStateInfo(NetworkStateType.Connected, null);
|
||||
AddStateInfo(NetworkStateType.Connected, "OnConnect");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -199,7 +196,7 @@ public class SocketClient
|
|||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("client.connected----->>false");
|
||||
Debug.LogError("SocketClient WriteMessage !connected");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -249,6 +246,7 @@ public class SocketClient
|
|||
info.type = dis;
|
||||
info.msg = msg;
|
||||
stateInfoStack.Push(info);
|
||||
Debug.Log("SocketClient AddStateInfo dis: " + dis + " msg: " + msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -262,7 +260,7 @@ public class SocketClient
|
|||
{
|
||||
returnStr += byteBuffer[i].ToString("X2");
|
||||
}
|
||||
Debug.LogError(returnStr);
|
||||
Debug.LogError("SocketClient PrintBytes Str: " + returnStr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -281,7 +279,7 @@ public class SocketClient
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError("OnWrite--->>>" + ex.Message);
|
||||
Debug.LogError("SocketClient OnWrite catch: " + ex.Message.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -290,8 +288,6 @@ public class SocketClient
|
|||
/// </summary>
|
||||
void OnReceive(byte[] bytes, int length)
|
||||
{
|
||||
|
||||
//Debug.Log("****Socket*********************OnReceive ---" + IpAddress);
|
||||
try
|
||||
{
|
||||
var stream = CurMemoryStream;
|
||||
|
@ -330,7 +326,7 @@ public class SocketClient
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError("Network Read Exception: " + e.ToString());
|
||||
Debug.LogError("SocketClient OnReceive catch: " + e.ToString());
|
||||
AddStateInfo(NetworkStateType.Disconnect, "Network Read Exception: " + e.ToString());
|
||||
}
|
||||
}
|
||||
|
@ -377,8 +373,7 @@ public class SocketClient
|
|||
|
||||
private void indicationErrorAction(ByteBuffer buffer)
|
||||
{
|
||||
|
||||
//Debug.Log("****Socket*********************indicationErrorAction ---" + IpAddress);
|
||||
Debug.Log("SocketClient indicationErrorAction");
|
||||
Util.CallMethod("SocketManager", "ReceiveErrorInfo", this, buffer);
|
||||
}
|
||||
|
||||
|
@ -388,7 +383,7 @@ public class SocketClient
|
|||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
//Debug.Log("****Socket*********************Close ---" + IpAddress);
|
||||
Debug.Log("SocketClient Close");
|
||||
UnregistNetMessage(INDICATION_ERROR_CODE, indicationErrorAction);
|
||||
m_isConnected = false;
|
||||
m_isConnecting = false;
|
||||
|
@ -406,6 +401,9 @@ public class SocketClient
|
|||
client.Close();
|
||||
client = null;
|
||||
}
|
||||
else {
|
||||
Debug.Log("SocketClient Close client==null");
|
||||
}
|
||||
|
||||
if (_memStream != null)
|
||||
{
|
||||
|
@ -489,10 +487,6 @@ public class SocketClient
|
|||
int msgId = _event.msgId;
|
||||
ByteBuffer msg = _event.msg;
|
||||
|
||||
//Debug.LogError("msgId:" + msgId);
|
||||
//Debug.LogError("m_indicationSid:" + m_indicationSid);
|
||||
//Debug.LogError("sid:" + _event.sid);
|
||||
|
||||
//处理心跳包回调
|
||||
if (msgId == 1001)
|
||||
{
|
||||
|
@ -502,6 +496,7 @@ public class SocketClient
|
|||
//处理错误码
|
||||
if (_event.result == 0)
|
||||
{
|
||||
Debug.Log("SocketClient DispatchMessage result == 0 msgID: " + msgId + " msg: " + msg);
|
||||
m_serialId++;
|
||||
Util.CallMethod("SocketManager", "ReceiveErrorInfo", this, msg);
|
||||
if (callbacks.ContainsKey(msgId))
|
||||
|
@ -518,12 +513,11 @@ public class SocketClient
|
|||
m_indicationSid++;
|
||||
if (!dispatchers[msgId].Process(msg))
|
||||
{
|
||||
UnityEngine.Debug.LogErrorFormat("Failed to process message. msgId : {0}", msgId);
|
||||
Debug.Log("SocketClient DispatchMessage Failed to process message. msgId : " + msgId);
|
||||
}
|
||||
}
|
||||
if (_event.sid <= m_indicationSid)
|
||||
{
|
||||
//Debug.LogError("INDICATION_RESPONSE" + msgId);
|
||||
m_SendMessage(INDICATION_RESPONSE, null, _event.sid);
|
||||
}
|
||||
}
|
||||
|
@ -535,14 +529,14 @@ public class SocketClient
|
|||
m_serialId++;
|
||||
if (!call.Process(msg))
|
||||
{
|
||||
UnityEngine.Debug.LogErrorFormat("Failed to process message. msgId : {0}", msgId);
|
||||
Debug.Log("SocketClient DispatchMessage Failed to process message2. msgId : " + msgId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
//Debug.Log("****Socket*********************Disconnect ---" + IpAddress);
|
||||
Debug.Log("SocketClient Disconnect");
|
||||
Close(); //关掉客户端链接
|
||||
Util.CallMethod("SocketManager", "OnDisconnect", this);
|
||||
}
|
||||
|
@ -551,12 +545,13 @@ public class SocketClient
|
|||
IEnumerator CheckConnect_Co()
|
||||
{
|
||||
yield return new WaitForEndOfFrame();
|
||||
Debug.LogWarning("Start Connecting IP: " + IpAddress + " Port: " + Port);
|
||||
Debug.Log("SocketClient CheckConnect_Co IP: " + IpAddress + " Port: " + Port);
|
||||
Connect();
|
||||
m_isConnecting = true;
|
||||
yield return new WaitForSeconds(AppConst.ConnectTimeout);
|
||||
if (!IsConnected())
|
||||
{
|
||||
Debug.Log("SocketClient CheckConnect_Co connect Fail");
|
||||
OnConnectFail();
|
||||
}
|
||||
|
||||
|
@ -568,9 +563,9 @@ public class SocketClient
|
|||
/// </summary>
|
||||
public void TryConnect()
|
||||
{
|
||||
//Debug.Log("****Socket*********************TryConnect ---" + IpAddress);
|
||||
if (IsConnected())
|
||||
{
|
||||
Debug.Log("SocketClient TryConnect isConnected");
|
||||
NetworkStateInfo info = new NetworkStateInfo();
|
||||
info.type = NetworkStateType.Connected;
|
||||
info.msg = null;
|
||||
|
@ -578,25 +573,32 @@ public class SocketClient
|
|||
return;
|
||||
}
|
||||
if (m_isConnecting)
|
||||
{
|
||||
Debug.Log("SocketClient TryConnect m_isConnecting");
|
||||
return;
|
||||
}
|
||||
|
||||
if (checkConnect_co != null)
|
||||
{
|
||||
Debug.Log("SocketClient TryConnect checkConnect_co != nul");
|
||||
netMgr.StopCoroutine(checkConnect_co);
|
||||
}
|
||||
|
||||
checkConnect_co = netMgr.StartCoroutine(CheckConnect_Co());
|
||||
}
|
||||
|
||||
private Coroutine reconnect_co;
|
||||
IEnumerator CheckReconnect_Co()
|
||||
{
|
||||
Debug.Log("SocketClient TryConnect CheckReconnect_Co");
|
||||
yield return new WaitForEndOfFrame();
|
||||
Reconnect();
|
||||
m_isConnecting = true;
|
||||
yield return new WaitForSeconds(AppConst.ConnectTimeout);
|
||||
if (!IsConnected())
|
||||
{
|
||||
Debug.Log("SocketClient CheckReconnect_Co connect Fail");
|
||||
OnReconnectFail();
|
||||
}
|
||||
|
||||
m_isConnecting = false;
|
||||
reconnect_co = null;
|
||||
|
@ -604,9 +606,9 @@ public class SocketClient
|
|||
|
||||
public void TryReconnect()
|
||||
{
|
||||
//Debug.Log("****Socket*********************TryReconnect ---" + IpAddress);
|
||||
if (IsConnected())
|
||||
{
|
||||
Debug.Log("SocketClient TryReconnect IsConnected");
|
||||
NetworkStateInfo info = new NetworkStateInfo();
|
||||
info.type = NetworkStateType.Reconnected;
|
||||
info.msg = null;
|
||||
|
@ -614,10 +616,15 @@ public class SocketClient
|
|||
return;
|
||||
}
|
||||
if (m_isConnecting)
|
||||
{
|
||||
Debug.Log("SocketClient TryReconnect m_isConnecting");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (reconnect_co != null)
|
||||
{
|
||||
Debug.Log("SocketClient TryReconnect reconnect_co != nul");
|
||||
netMgr.StopCoroutine(reconnect_co);
|
||||
}
|
||||
reconnect_co = netMgr.StartCoroutine(CheckReconnect_Co());
|
||||
|
@ -625,10 +632,10 @@ public class SocketClient
|
|||
|
||||
public void Reconnect()
|
||||
{
|
||||
//Debug.Log("****Socket*********************Reconnect ---" + IpAddress);
|
||||
Close();
|
||||
try
|
||||
{
|
||||
Debug.Log("SocketClient Reconnect");
|
||||
var ipType = GetIPAddressType(IpAddress);
|
||||
client = null;
|
||||
client = new TcpClient(ipType);
|
||||
|
@ -639,7 +646,7 @@ public class SocketClient
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError(e.Message);
|
||||
Debug.Log("SocketClient Reconnect catch :" + e.Message);
|
||||
OnReconnectFail();
|
||||
}
|
||||
}
|
||||
|
@ -671,7 +678,7 @@ public class SocketClient
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogWarning("Network GetIPAddressType Exception: " + ex);
|
||||
Debug.Log("SocketClient GetIPAddressType catch :" + ex.Message);
|
||||
return AddressFamily.InterNetwork;
|
||||
}
|
||||
|
||||
|
@ -686,16 +693,14 @@ public class SocketClient
|
|||
|
||||
public void OnReconnectFail()
|
||||
{
|
||||
//Debug.Log("****Socket*********************OnReconnectFail ---" + IpAddress);
|
||||
AddStateInfo(NetworkStateType.ReconnectFail, null);
|
||||
AddStateInfo(NetworkStateType.ReconnectFail, "OnReconnectFail");
|
||||
}
|
||||
|
||||
void OnReconnect(IAsyncResult result)
|
||||
{
|
||||
//Debug.Log("****Socket*********************OnReconnect ---" + IpAddress);
|
||||
outStream = client.GetStream();
|
||||
client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null);
|
||||
AddStateInfo(NetworkStateType.Reconnected, null);
|
||||
AddStateInfo(NetworkStateType.Reconnected, "OnReconnect");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -703,11 +708,11 @@ public class SocketClient
|
|||
/// </summary>
|
||||
public void SendMessage(int msgId, ByteBuffer msg)
|
||||
{
|
||||
Debug.Log("SocketClient SendMessage msgID: " + msgId);
|
||||
m_serialId++;
|
||||
m_SendMessage(msgId, msg, m_serialId);
|
||||
}
|
||||
|
||||
|
||||
void m_SendMessage(int msgId, ByteBuffer msg, int sn)
|
||||
{
|
||||
if (!IsConnected())
|
||||
|
@ -753,7 +758,6 @@ public class SocketClient
|
|||
{
|
||||
if (!dispatchers.ContainsKey(msgId))
|
||||
dispatchers.Add(msgId, new SimpleDispatcher());
|
||||
//XDebug.Log.l("++++++++++++++++RegistNetMessage================================", msgId, IpAddress, Port);
|
||||
SimpleDispatcher find = (SimpleDispatcher)dispatchers[msgId];
|
||||
find.processor += handle;
|
||||
}
|
||||
|
@ -762,7 +766,6 @@ public class SocketClient
|
|||
{
|
||||
if (!dispatchers.ContainsKey(msgId))
|
||||
return;
|
||||
XDebug.Log.l("-------------------UnregistNetMessage================================", msgId, IpAddress, Port);
|
||||
SimpleDispatcher find = (SimpleDispatcher)dispatchers[msgId];
|
||||
find.processor -= handle;
|
||||
}
|
||||
|
@ -770,6 +773,7 @@ public class SocketClient
|
|||
|
||||
public void SendMessageWithCallBack(int msgId, int receiveMsgId, ByteBuffer message, LuaFunction callback)
|
||||
{
|
||||
Debug.Log("SocketClient SendMessageWithCallBack msgID:" + msgId.ToString());
|
||||
if (!IsConnected())
|
||||
{
|
||||
UnityEngine.Debug.LogErrorFormat("Try to send message with invalid connection.");
|
||||
|
@ -787,4 +791,6 @@ public class SocketClient
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue