void Socket_OnData(object sender, EventSocket.DataEventArgs args) { // Store the data Cache.Concat(args.Data); // Try to read messages while (true) { int length; BufferView backup = new BufferView(Cache); try { // Extract the size of the message length = (int)InflateData.ReadUint(Cache); } catch (Exception e) { if (!(e is InflateData.NotEnoughData)) { ProtocolError(); } break; } if (Cache.Length < length) { Cache = backup; break; } ProcessMessage(Cache.ExtractSlice(length)); } }
void ProcessMessage(BufferView message) { uint type, callId; // Extract the message type and sequence id try { type = (uint)InflateData.ReadUint(message); callId = (uint)InflateData.ReadUint(message); } catch { ProtocolError(); return; } if (type != 0) { // A call from the other side ProcessCall(callId, type, message); } else { try { type = (uint)InflateData.ReadUint(message); } catch { ProtocolError(); return; } if (type != 0) { // An exception from the other side ProcessException(callId, type, message); } else { // A return from the other side ProcessReturn(callId, message); } } }