Ejemplo n.º 1
0
        /// <summary>
        /// callback from WebSocket after socket is closed
        /// clean up and error reporting
        /// </summary>
        /// <param name="sender">The sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void OnSocketClose(object sender, WebSocketProtocolEventArgs e)
        {
            // if WebSocket initiated close by itself, SMProtocol is not aware yet
            // In this case this.opened will be true and SMProtocol is forced to close
            if (this.webSocket.LastError != null)
            {
                if (this.OnError != null)
                {
                    // General error state, do not attempt to communicate
                    this.OnError(this, new SMProtocolErrorEventArgs(this.webSocket.LastError));
                }
                else
                {
                    // fallback to notify user anyhow
                    Console.WriteLine("ERROR: WebSocket error {0}", this.webSocket.LastError);
                }
            }
            else
            {
                try
                {
                    if (e.BinaryData != null)
                    {
                        // We received WS close frame from server with extension data.
                        // Attempt to unpack the data
                        CloseFrameExt closeData = this.serializer.DeserializeCloseFrameExt(e.BinaryData);

                        if (this.OnClose != null)
                        {
                            // Send unpacked extension data to event listener
                            this.OnClose(this, new CloseFrameEventArgs(closeData));
                        }
                    }
                    else
                    {
                        // We received WS close frame without extension data. If may be
                        // response to our frame. Or it may be server initiated by server.
                        if (this.OnClose != null)
                        {
                            this.OnClose(this, null);
                        }
                    }

                    // this object is now closed
                    this.opened = false;
                }
                catch (Exception protocolError)
                {
                    if (this.OnError != null)
                    {
                        this.OnError(this, new SMProtocolErrorEventArgs(protocolError));
                    }
                }
                finally
                {
                    // in any case, close WS socket
                    this.CloseInternal();
                }
            }
        }
        /// <summary>
        /// Deserializes the Http2 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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CloseFrameEventArgs"/> class.
 /// </summary>
 /// <param name="frameExt">The Http Close frame extension data.</param>
 public CloseFrameEventArgs(CloseFrameExt frameExt)
 {
     this.FrameExt = frameExt;
 }