/// <summary> /// Serializes the control frame. /// </summary> /// <param name="frame">The frame.</param> /// <returns>Serialized control frame.</returns> private byte[] SerializeControlFrame(ControlFrame frame) { var byteList = new List <byte>(); byteList.Add((byte)(((frame.Version & 0xFF00) >> 8) | 0x80)); byteList.Add((byte)(frame.Version & 0x00FF)); byteList.Add((byte)(((Int16)frame.Type & 0xFF00) >> 8)); byteList.Add((byte)((Int16)frame.Type & 0x00FF)); byteList.AddRange(BinaryHelper.Int32ToBytes(frame.StreamId)); var headersArray = new byte[0]; switch (frame.Type) { case FrameType.SynStream: byteList.Add(Convert.ToByte(frame.Flags | (frame.IsFinal ? 0x01 : 0x00))); byteList.Add(Convert.ToByte(frame.Priority >> 5)); byteList.Add(Unused); byteList.Add(Unused); headersArray = SerializeControlFrameHeaders(frame.Headers); break; case FrameType.RTS: byteList.AddRange(BinaryHelper.Int32ToBytes((int)frame.StatusCode)); break; case FrameType.SynReply: byteList.Add(frame.Flags); byteList.Add(Unused); byteList.Add(Unused); byteList.Add(Unused); headersArray = SerializeControlFrameHeaders(frame.Headers); break; case FrameType.CreditUpdate: byteList.AddRange(BinaryHelper.Int64ToBytes(((CreditUpdateFrame)frame).CreditAddition)); break; } if (headersArray.Length > 0) { ProcessorRun(ref headersArray, DirectionProcessType.Outbound, frame.Flags); } byteList.AddRange(headersArray); return(byteList.ToArray()); }
private byte[] SerializeControlFrameHeaders(SMHeaders frameHeaders) { var headers = new List <byte>(); foreach (KeyValuePair <string, string> pair in frameHeaders) { byte[] nameBin = Encoding.UTF8.GetBytes(pair.Key); headers.AddRange(BinaryHelper.Int32ToBytes(nameBin.Length)); headers.AddRange(nameBin); byte[] valBin = Encoding.UTF8.GetBytes(pair.Value); headers.AddRange(BinaryHelper.Int32ToBytes(valBin.Length)); headers.AddRange(valBin); } return(headers.ToArray()); }
/// <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; }