public virtual byte[] GetMessageData(IMessage msg) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); BufferStream writer = new BufferStream(stream, LittleEndian); WriteMessageType(writer, msg); msg.Save(writer); byte[] result = new byte[stream.Length+4]; using (System.IO.MemoryStream resultStream = new System.IO.MemoryStream(result)) { writer = new BufferStream(resultStream, LittleEndian); writer.Write((int)stream.Length); stream.Position = 0; stream.Read(result, 4, (int)stream.Length); } return result; }
private void OnDataReceive(System.IO.Stream stream) { try { stream.Position = 0; BufferStream reader = new BufferStream(stream, LittleEndian); IMessage msg = mPackage.FromStream(reader); if (msg == null) throw new Exception("message type not found!"); Handler.ClientReceive(this, mPackage.ReceiveCast(msg)); } catch (Exception e_) { OnError(e_); } finally { mPackage.Reset(); } }
public virtual IMessage FromStream(BufferStream reader) { try { IMessage msg = GetMessage(reader); msg.Load(reader); return msg; } catch (Exception e_) { throw new Exception("read message error!", e_); } }