public void SerializeWithHead(MemoryStream stream) { long begin = stream.Position; ProtocolHead head = ProtocolHead.ShareHead; head.Reset(); head.msgId = GetMessageID(); head.Serialize(stream); Serialize(stream); long position = stream.Position; int length = (int)(position - begin - 4); stream.Position = begin; stream.Write(head.GetBytes(length), 0, 4); stream.Position = position; }
/* * private void OnReceive() * { * _recvQueue.Switch(); * while (!_recvQueue.Empty()) * { * byte[] bytes = _recvQueue.Pop(); * _prtHead.Reset(); * _prtHead.Deserialize(bytes); * _recvStream.Seek(0, SeekOrigin.Begin); * _recvStream.SetLength(0); * _recvStream.Write(bytes, _prtHead.Size, (int)_prtHead.len + 4 - _prtHead.Size); * _recvStream.Seek(0, SeekOrigin.Begin); * Protocol protocol = Protocol.GetProtocolThreadSafe(_prtHead.msgId); * if (protocol == null) * { * continue; * } * try * { * protocol.ThreadErrorCode = ProtocolErrorCode.NO_ERROR; * protocol.DeSerialize(_recvStream); * Protocol.ReturnProtocolThreadSafe(protocol); * } * catch (Exception ex) * { * Debug.LogWarning("Ptc " + _prtHead.msgId.ToString() + " deserialize fail: " + ex.Message.ToString()); * if (protocol != null) * { * protocol.ThreadErrorCode = ProtocolErrorCode.DESERIALIZE_ERROR; * } * } * if (protocol.ThreadErrorCode == ProtocolErrorCode.NO_ERROR) * { * try * { * protocol.Process(); * } * catch (Exception ex) * { * Debug.LogWarning("Ptc " + _prtHead.msgId.ToString() + " Process fail: " + ex.Message.ToString()); * if (protocol == null) * { * protocol.ThreadErrorCode = ProtocolErrorCode.PROCESS_ERROR; * } * } * finally * { * Protocol.ReturnProtocolThreadSafe(protocol); * } * } * } * }*/ private void OnReceive() { _recvQueue.Switch(); while (!_recvQueue.Empty()) { byte[] bytes = _recvQueue.Pop(); _prtHead.Reset(); _prtHead.Deserialize(bytes); _recvStream.Seek(0, SeekOrigin.Begin); _recvStream.SetLength(0); _recvStream.Write(bytes, _prtHead.Size, (int)_prtHead.len + 4 - _prtHead.Size); _recvStream.Seek(0, SeekOrigin.Begin); Protocol protocol = Protocol.GetProtocolThreadSafe(_prtHead.msgId); if (protocol == null) { #if DEBUG Debug.LogError("Ptc Not found: " + _prtHead.msgId.ToString()); #endif continue; } try { #if DEBUG if (_prtHead.len > 1024) { Debug.LogWarning("Recv Ptc:" + protocol.GetMessageID().ToString() + " to long:" + _prtHead.len.ToString()); } #endif protocol.ThreadErrorCode = ProtocolErrorCode.NO_ERROR; protocol.DeSerialize(_recvStream); Protocol.ReturnProtocolThreadSafe(protocol); } catch (Exception ex) { Debug.LogWarning("Ptc " + _prtHead.msgId.ToString() + " deserialize fail: " + ex.Message.ToString()); if (protocol != null) { protocol.ThreadErrorCode = ProtocolErrorCode.DESERIALIZE_ERROR; } } if (protocol.ThreadErrorCode == ProtocolErrorCode.NO_ERROR) { try { protocol.Process(); } catch (Exception ex) { Debug.LogWarning("Ptc " + _prtHead.msgId.ToString() + " Process fail: " + ex.Message.ToString()); if (protocol != null) { protocol.ThreadErrorCode = ProtocolErrorCode.PROCESS_ERROR; } } finally { Protocol.ReturnProtocolThreadSafe(protocol); } } } }