/// <summary> /// Deserializes the data into control frame. /// </summary> /// <param name="data">The data.</param> /// <returns>Deserialized frame.</returns> private BaseFrame DeserializeControlFrame(byte[] data) { var frame = new ControlFrame(); ParseControlFrameHeader(ref frame, data); switch (frame.Type) { case FrameType.RTS: frame.StatusCode = (StatusCode)BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, 8, 4)); break; case FrameType.Headers: case FrameType.SynReply: ParseControlFrameHeaders(ref frame, data, 12); break; case FrameType.SynStream: frame.Priority = (byte)(data[9] >> 5); ParseControlFrameHeaders(ref frame, data, 12); break; case FrameType.CreditUpdate: ((CreditUpdateFrame)frame).CreditAddition = BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, 8, 4)); break; } return(frame); }
/// <summary> /// Parses HTTP headers of SM control frame. /// </summary> /// <param name="frame">The frame.</param> /// <param name="data">The data.</param> /// <param name="offset">Offset of HTTP headers in the data.</param> private void ParseControlFrameHeaders(ref ControlFrame frame, byte[] data, int offset) { var headers = new byte[data.Length - offset]; Array.Copy(data, offset, headers, 0, headers.Length); if (headers.Length > 0) { ProcessorRun(ref data, DirectionProcessType.Outbound, frame.Flags); } for (int i = 0; /*i < headersCount*/ offset < data.Length; ++i) { int nameLength = BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, offset, 4)); offset += 4; string name = Encoding.UTF8.GetString(data, offset, nameLength); offset += nameLength; int valLength = BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, offset, 4)); offset += 4; string val = Encoding.UTF8.GetString(data, offset, valLength); offset += valLength; // Ensure no duplicates. if (frame.Headers.ContainsKey(name)) { throw new SMProtocolExeption(StatusCode.InternalError); } frame.Headers.Add(name, val); } }
/// <summary> /// Deserializes the WebSocket Close Extension Data. /// </summary> /// <param name="data">The data.</param> /// <returns>Deserialized Extension Data.</returns> public CloseFrameExt DeserializeCloseFrameExt(byte[] data) { var extData = new CloseFrameExt(); extData.StatusCode = BinaryHelper.Int16FromBytes(data[0], data[1], 0); extData.LastGoodSessionId = BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, 2, 4)); return(extData); }
/// <summary> /// Parses HTTP header of SM control frame. /// </summary> /// <param name="frame">The frame.</param> /// <param name="data">The data.</param> private static void ParseControlFrameHeader(ref ControlFrame frame, byte[] data) { frame.Version = BinaryHelper.Int16FromBytes(data[0], data[1], 1); frame.Type = (FrameType)BinaryHelper.Int16FromBytes(data[2], data[3]); frame.StreamId = BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, 4, 4), 0); frame.Flags = data[8]; // frame.Length = BinaryHelper.Int32FromBytes(new ArraySegment<byte>(data, 5, 3)); frame.IsFinal = (frame.Flags & 0x01) != 0; }
/// <summary> /// Parses HTTP header of SM data frame. /// </summary> /// <param name="frame">The frame.</param> /// <param name="data">The data.</param> private static void ParseDataFrameHeader(ref DataFrame frame, byte[] data) { frame.StreamId = BinaryHelper.Int32FromBytes(new ArraySegment <byte>(data, 0, 4), 0); frame.Flags = data[4]; frame.IsFinal = (frame.Flags & 0x01) != 0; }