private bool TryHandleOneProtocol(NetBuffer stream, uint connId) { ProtocolHead head = ProtocolHead.Deserialize(stream); if (head == null) { return(false); } if (stream.Length < head.dataSize + ProtocolHead.Length) { return(false); } if (m_recvBufferTemp.Capacity < head.dataSize) { Debuger.LogError(TAG, "OnRecv() 缓冲空间不够! connId:{0}, dataSize:{1}, RecvBuffCapacity:{2}", connId, head.dataSize, m_recvBufferTemp.Capacity); return(false); } stream.ReadBytes(m_recvBufferTemp.GetBytes(), 0, head.dataSize); m_recvBufferTemp.AddLength(head.dataSize, 0); ushort sum = SGFEncoding.CheckSum(m_recvBufferTemp.GetBytes(), head.dataSize); if (sum != head.checksum) { Debuger.LogError(TAG, "OnRecv() CheckSum失败! connId:{0}, dataSize:{1}, RecvBuffSize:{2}", connId, head.dataSize, m_recvBufferTemp.Length); //这个时候,可能要重置连接 stream.Clear(); //ClearConnection(); return(false); } object ptlObj = DeserializeProtocol(head.pid, m_recvBufferTemp.GetBytes(), m_recvBufferTemp.Length); if (ptlObj != null) { DispatchProtocol(head.pid, head.index, ptlObj); } else { m_recvBufferTemp.Position = 0; byte[] buffer = m_recvBufferTemp.ReadBytes(head.dataSize); DispatchProtocol(head.pid, head.index, buffer); } return(true); }
private bool TryHandleOneProtocol(NetBuffer stream, uint connId) { ProtocolHeader head = ProtocolHeader.Deserialize(stream); if (head == null) { return(false); } if (stream.Length < head.dataSize + ProtocolHeader.Length) { return(false); } if (m_recvBufferTemp.Capacity < head.dataSize) { MyLogger.LogError(TAG, "OnRecv() no enough buffer! connId:{0}, dataSize:{1}, RecvBuffCapacity:{2}", connId.ToString(), head.dataSize, m_recvBufferTemp.Capacity); return(false); } stream.ReadBytes(m_recvBufferTemp.GetBytes(), 0, head.dataSize); m_recvBufferTemp.AddLength(head.dataSize, 0); ushort sum = SGFEncoder.CheckSum(m_recvBufferTemp.GetBytes(), head.dataSize); if (sum != head.checksum) { MyLogger.LogError(TAG, "OnRecv() CheckSum failed! connId:{0}, dataSize:{1}, RecvBuffSize:{2}", connId.ToString(), head.dataSize, m_recvBufferTemp.Length); //reset conncetion stream.Clear(); return(false); } object ptlObj = DeserializeProtocol(head.pid, m_recvBufferTemp.GetBytes(), m_recvBufferTemp.Length); if (ptlObj != null) { DispatchProtocol(head.pid, head.index, ptlObj); } else { m_recvBufferTemp.Position = 0; byte[] buffer = m_recvBufferTemp.ReadBytes(head.dataSize); DispatchProtocol(head.pid, head.index, buffer); } return(true); }
public void Send(uint pid, object req, uint connId, Type rspType, ProtocolListener listener, ErrorListener errorListener) { //regist rspType if (rspType != null) { Register(pid + 1, rspType); } //record listner uint index = 0; if (listener != null || errorListener != null) { index = ++m_index; PTLListenerHelper helper = new PTLListenerHelper(); helper.listener = listener; helper.errorListener = errorListener; helper.timestamp = UnityEngine.Time.realtimeSinceStartup; helper.pid = pid + 1;//this is a rule helper.index = index; m_lstRspListener.Add(helper); } //serialize protocol byte[] dataBuffer = SerializeProtocol(req); //protocol header ProtocolHeader head = new ProtocolHeader(); head.pid = pid; head.index = index; head.dataSize = dataBuffer.Length; head.checksum = SGFEncoder.CheckSum(dataBuffer, dataBuffer.Length); //serilize protocol header m_sendBufferTemp.Position = 0; head.Serialize(m_sendBufferTemp); //combine buffer of the protocol m_sendBufferTemp.AddBytes(dataBuffer); ConnectManager.Instance.Send(connId, m_sendBufferTemp.GetBytes(), m_sendBufferTemp.Length); }
public void Send(uint pid, object req, uint connId, Type rspType, ProtocolListener listener, ErrorListener errorListener) { //注册Rsp类 if (rspType != null) { Register(pid + 1, rspType); } //记录Listener uint index = 0; if (listener != null || errorListener != null) { index = ++m_index; PTLListenerHelper helper = new PTLListenerHelper(); helper.listener = listener; helper.errorListener = errorListener; helper.timestamp = UnityEngine.Time.realtimeSinceStartup; helper.pid = pid + 1;//这是一个规则 helper.index = index; m_lstRspListener.Add(helper); } //序列化协议数据 byte[] dataBuffer = SerializeProtocol(req); //协议头 ProtocolHead head = new ProtocolHead(); head.pid = pid; head.index = index; head.dataSize = dataBuffer.Length; head.checksum = SGFEncoding.CheckSum(dataBuffer, dataBuffer.Length); //序列化协议头 m_sendBufferTemp.Position = 0; head.Serialize(m_sendBufferTemp); //组合整个协议Buffer m_sendBufferTemp.AddBytes(dataBuffer); ConnectManager.Instance.Send(connId, m_sendBufferTemp.GetBytes(), m_sendBufferTemp.Length); }