private void OnReceiveHeadComp(IAsyncResult result) { Debug.Log("FUN >>> OnReceiveHeadComp"); if (m_isClosed) { return; } int receiveLength = 0; try { if (m_stream != null) { receiveLength = m_stream.EndRead(result); } } catch (Exception e) { Debug.Log("!Catch exception, " + e); } //Debug.Log("receiveLength = " + receiveLength); if (receiveLength <= 0) { Close(); return; } MessageHeader.Header header = (MessageHeader.Header)ObjectBytesTrans.BytesToStruct(m_buffer, typeof(MessageHeader.Header)); int bodyLength = header.messageSize - HEAD_LENGTH; Debug.Log("bodyLength = " + bodyLength); #if UNITY_EDITOR if (bodyLength > m_buffer.Length) { Debug.LogError(">>> receive data length exceeds buffer length!"); } #endif m_bodyLength = bodyLength; m_command = header.messageCommand; if (bodyLength > 0) { ReceiveBody(bodyLength, HEAD_LENGTH); } else { if (m_dataProcesser != null) { m_dataProcesser(m_command, m_receiveBodyAccumulation); } ReceiveHead(); } }
public int MessageObj2Bytes <T>(T message_obj, int message_command) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); ProtoBuf.Serializer.Serialize(stream, message_obj); int messageSize = HEAD_LENGTH + (int)stream.Length; MessageHeader.Header header = new MessageHeader.Header(messageSize, message_command); Debug.Log("header : " + header.ToString()); byte[] headerBytes = ObjectBytesTrans.StructToBytes(header); int headerBytesLength = headerBytes.Length; System.Array.Copy(headerBytes, 0, m_buffer, 0, headerBytesLength); byte[] bodyBytes = stream.GetBuffer(); long streamLength = stream.Length; System.Array.Copy(bodyBytes, 0, m_buffer, headerBytesLength, streamLength); int bytesLength = (int)(headerBytesLength + streamLength); return(bytesLength); }