public Message Deserialize(CircularBuffer buffer) { // Read the message size if (buffer.Available < 4) { return null; } int offset = 0; uint size = (uint)((buffer.PeekByte(offset++) << 0) | (buffer.PeekByte(offset++) << 8) | (buffer.PeekByte(offset++) << 16) | (buffer.PeekByte(offset++) << 24)); if (buffer.Available < (int)size) { return null; } // read the message's contents byte[] data = new byte[size]; buffer.Read(data, 0, (int)size); // decode the message's contents MemoryStream ms = new MemoryStream(data, false); BinaryReader reader = new BinaryReader(ms); reader.ReadInt32(); MessageType type = (MessageType)reader.ReadInt32(); MessageDecoder decoder; if (!m_decoders.TryGetValue(type, out decoder)) { throw new NotImplementedException("Message type not supported: " + type); } Message msg = decoder(reader, type); LogMessage(msg, "<<"); return msg; }
public Message Deserialize(CircularBuffer buffer) { // Detect the first newline in the buffered data. int lineSize = 0; bool lineFound = false; while(lineSize < buffer.Available) { byte c = buffer.PeekByte(lineSize); lineSize++; if((char)c == '\n') { lineFound = true; break; } } if(!lineFound) { return null; } // Read the first line byte[] messageData = new byte[lineSize]; buffer.Read(messageData, 0, lineSize); string line = m_encoding.GetString(messageData); if(Configuration.LogNetworkMessages) { Trace.WriteLine(String.Format("<< {0}", line.Substring(0, line.Length - 1))); } return ParseMessage(line); }