/// <summary>
 /// Serializes the specified frame.
 /// </summary>
 /// <param name="frame">The frame.</param>
 /// <returns>Binary representation of the frame.</returns>
 public byte[] Serialize(BaseFrame frame)
 {
     if (frame is ControlFrame)
     {
         return SerializeControlFrame(frame as ControlFrame);
     }
     else
     {
         return SerializeDataFrame(frame as DataFrame);
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="FrameLogItem"/> struct.
        /// </summary>
        /// <param name="frame">The frame.</param>
        public FrameLogItem(BaseFrame frame)
        {
            this.Length = frame.Length;
            this.StreamId = frame.StreamId;

            if (frame is ControlFrame)
            {
                this.Type = ((ControlFrame)frame).Type;
                this.Headers = ((ControlFrame)frame).Headers.ToString();
                this.TimeStamp = DateTime.Now;
                this.Duration = TimeSpan.Zero;
            }
            else
            {
                this.Type = FrameType.Data;
                this.TimeStamp = DateTime.Now;
                this.Duration = TimeSpan.Zero;
                this.Headers = string.Empty;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FrameEventArgs"/> class.
 /// </summary>
 /// <param name="frame">The frame.</param>
 public FrameEventArgs(BaseFrame frame)
 {
     this.Frame = frame;
 }
 /// <summary>
 /// Serializes the specified frame.
 /// </summary>
 /// <param name="frame">The frame.</param>
 /// <returns>Binary representation of the frame.</returns>
 public byte[] Serialize(BaseFrame frame)
 {
     if (frame is ControlFrame)
     {
         if (Option.UseCompression)
         {
             frame.Flags &= (byte)FrameFlags.FlagNormal;
         }
         else
         {
             frame.Flags &= (byte)FrameFlags.FlagNoHeaderCompression1;
         }
         return SerializeControlFrame(frame as ControlFrame);
     }
     return SerializeDataFrame(frame as DataFrame);
 }
        /// <summary>
        /// Sends the data.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="frame">The base frame.</param>
        private void SendFrame(SMStream stream, BaseFrame frame)
        {
            try
            {
                byte[] frameBinary = this.serializer.Serialize(frame);
                frame.Length = frameBinary.Length;

                this.webSocket.SendMessage(frameBinary);

                if (frame.IsFinal)
                {
                    stream.State = SMStreamState.HalfClosed;
                }

                if (this.OnFrameSent != null)
                {
                    this.OnFrameSent(this, new FrameEventArgs(frame));
                }
            }
            catch (Exception e)
            {
                if (this.OnStreamError != null)
                {
                    this.OnStreamError(this, new StreamErrorEventArgs(stream, e));
                }
                else
                {
                    throw;
                }
            }
        }
 /// <summary>
 /// Selects frame based on type
 /// </summary>
 /// <param name="frame">The frame.</param>
 /// <returns>
 /// TRUE if frame is SM control frame of SYNSTREAM or SYNREPLY type
 /// </returns>
 private bool SessionFramesFilter(BaseFrame frame)
 {
     var cntrlframe = frame as ControlFrame;
     return cntrlframe != null && (cntrlframe.Type == FrameType.SynStream || cntrlframe.Type == FrameType.SynReply);
 }