void ProcessReturn(uint callId, BufferView data) { PendingCall callInfo = FetchPendingCall(callId); if (callInfo == null) { // Received a timeouted (or invalid) answer ProtocolError(); return; } // Read the incoming data object inflatedData; try { inflatedData = InflateData.Inflate(data, callInfo.Call.ReturnFormat); } catch { ProtocolError(); return; } // Clear the timeout if (callInfo.Interval != null) { callInfo.Interval.Stop(); } // Call the callback if (callInfo.OnReturn != null) { callInfo.OnReturn(this, inflatedData); } }
void ProcessCall(uint callId, uint type, BufferView data) { object inflatedData; // Check the sequence id if (callId != ++LastReceivedId) { ProtocolError(); return; } // Get call definition Registry.RegisteredCall call = Registry.GetServerCall(type); if (call == null) { ProtocolError(); return; } // Read the incoming data try { inflatedData = InflateData.Inflate(data, call.ArgsFormat); } catch { ProtocolError(); return; } // Emit the "call" event if (OnCall != null) { OnCall(this, new CallEventArgs(call, inflatedData, this, callId)); } }
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)); } }
/// <summary> /// Inflate the data with the given format stored in a buffer /// </summary> /// <param name="buffer">A view with the encoded data</param> /// <param name="format">An inflated format object</param> /// <returns>Return null, an ArrayList or a single value (ulong, long, float, Token or string)</returns> internal static object Inflate(BufferView buffer, Format format) { ArrayList data = new ArrayList(); if (InflateData.ReadElement(buffer, data, format.Root).Length != 0) { throw new ArgumentException("Unable to read data in the given format"); } return(data.Count == 0 ? null : (data.Count == 1 ? data[0] : data)); }
void ProcessException(uint callId, uint type, BufferView data) { PendingCall callInfo = FetchPendingCall(callId); if (callInfo == null) { // Received a timeouted (or invalid) answer ProtocolError(); return; } if (!callInfo.Call.HasException(type)) { // Received an invalid exception type ProtocolError(); return; } // Get exception definition Registry.RegisteredException exception = Registry.GetException(type); if (exception == null) { ProtocolError(); return; } // Read the incoming data object inflatedData; try { inflatedData = InflateData.Inflate(data, exception.DataFormat); } catch { ProtocolError(); return; } // Clear the timeout if (callInfo.Interval != null) { callInfo.Interval.Stop(); } // Call the callback if (callInfo.OnException != null) { callInfo.OnException(this, (int)type, inflatedData); } }
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); } } }