Esempio n. 1
0
 public OwinWebSocket(IDictionary <string, object> owinEnvironment)
 {
     mSendAsync    = (WebSocketSendAsync)owinEnvironment["websocket.SendAsync"];
     mReceiveAsync = (WebSocketReceiveAsync)owinEnvironment["websocket.ReceiveAsync"];
     mCloseAsync   = (WebSocketCloseAsync)owinEnvironment["websocket.CloseAsync"];
     mSendQueue    = new TaskQueue();
 }
Esempio n. 2
0
        /// <summary>
        /// 响应客户端握手请求
        /// </summary>
        /// <returns>返回真表示按WebSocket的方式成功连接</returns>
        public bool Accept()
        {
            if (_accept == null)
            {
                return(false);
            }

            //执行连接操作
            _accept(null, sckEnv =>
            {
                //从字典中取出服务器提供的WebSocket操作函数
                _sendAsync    = sckEnv.Get <WebSocketSendAsync>("websocket.SendAsync");
                _receiveAsync = sckEnv.Get <WebSocketReceiveAsync>("websocket.ReceiveAsync");
                _closeAsync   = sckEnv.Get <WebSocketCloseAsync>("websocket.CloseAsync");

                //标记连接成功
                _isClosed = false;

                //通知服务器(容器),表示连接事件已经处理完成
                return(Task.Delay(0));
            });

            //表示是websocket消息并同意握手
            return(true);
        }
Esempio n. 3
0
 public OwinWebSocket(IDictionary<string,object> owinEnvironment)
 {
     mSendAsync = (WebSocketSendAsync)owinEnvironment["websocket.SendAsync"];
     mReceiveAsync = (WebSocketReceiveAsync)owinEnvironment["websocket.ReceiveAsync"];
     mCloseAsync = (WebSocketCloseAsync)owinEnvironment["websocket.CloseAsync"];
     mSendQueue = new TaskQueue();
 }
Esempio n. 4
0
        private int messageType; // 0x1 for Text, 0x2 for binary

        public BinaryStream(WebSocketSendAsync sendAsync, WebSocketReceiveAsync receiveAsync, WebSocketCloseAsync closeAsync, int messageType)
        {
            this.sendAsync    = sendAsync;
            this.receiveAsync = receiveAsync;
            this.closeAsync   = closeAsync;
            this.messageType  = messageType;
        }
 public OwinWebSocketAdapter(IDictionary<string, object> websocketContext, string subProtocol)
 {
     _websocketContext = websocketContext;
     _sendAsync = (WebSocketSendAsync)websocketContext[OwinConstants.WebSocket.SendAsync];
     _receiveAsync = (WebSocketReceiveAsync)websocketContext[OwinConstants.WebSocket.ReceiveAsync];
     _closeAsync = (WebSocketCloseAsync)websocketContext[OwinConstants.WebSocket.CloseAsync];
     _state = WebSocketState.Open;
     _subProtocol = subProtocol;
 }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of <see cref="OwinWebSocketAdapter"/>.
 /// </summary>
 /// <param name="websocketContext">WebSocket context options.</param>
 /// <param name="subProtocol">The WebSocket subprotocol.</param>
 public OwinWebSocketAdapter(IDictionary <string, object> websocketContext, string subProtocol)
 {
     _websocketContext = websocketContext;
     _sendAsync        = (WebSocketSendAsync)websocketContext[OwinConstants.WebSocket.SendAsync];
     _receiveAsync     = (WebSocketReceiveAsync)websocketContext[OwinConstants.WebSocket.ReceiveAsync];
     _closeAsync       = (WebSocketCloseAsync)websocketContext[OwinConstants.WebSocket.CloseAsync];
     _state            = WebSocketState.Open;
     _subProtocol      = subProtocol;
 }
 public OwinWebsocketWrapper(IDictionary <string, object> ws)
 {
     _wsSendAsync     = (WebSocketSendAsync)ws["websocket.SendAsync"];
     _wsCloseAsync    = (WebSocketCloseAsync)ws["websocket.CloseAsync"];
     _wsCallCancelled = (CancellationToken)ws["websocket.CallCancelled"];
     _wsRecieveAsync  = (WebSocketReceiveAsync)ws["websocket.ReceiveAsync"];
     ws.TryGetValue("websocket.SubProtocol", out var sp);
     SubProtocol = sp as string;
 }
Esempio n. 8
0
        public async Task SocketLoop(IDictionary <string, object> websocketContext)
        {
            m_SendAsync    = (WebSocketSendAsync)websocketContext["websocket.SendAsync"];
            m_ReceiveAsync = (WebSocketReceiveAsync)websocketContext["websocket.ReceiveAsync"];
            m_CloseAsync   = (WebSocketCloseAsync)websocketContext["websocket.CloseAsync"];

            var  bytes  = new byte[MaxMessageSize];
            bool bClose = true;
            Tuple <int, bool, int> received = null;

            try
            {
                do
                {
                    // keep track of item3 (size). the rest of the buffer
                    // will evantually become garbage as messages received.

                    ArraySegment <byte> buffer = new ArraySegment <byte>(bytes);
                    received = await m_ReceiveAsync(buffer, m_root_factory_working_token);

                    if (received.Item3 > MaxMessageSize)
                    {
                        await Close(SocketCloseStatus.MessageTooBig);

                        break;
                    }

                    if (received.Item3 > 0)
                    {
                        await OnReceiveAsync(buffer, received);
                    }
                }while (received.Item1 != (int)SocketMessageType.Close &&
                        !m_root_factory_working_token.IsCancellationRequested);
            }
            catch (AggregateException ae)
            {
                var flat = ae.Flatten();
                Trace.WriteLine(string.Format("socket {0} encountered {1} error during receiving and is aborting", m_SessionId, ae.Message), "error");
                await Close(SocketCloseStatus.EndpointUnavailable);

                bClose = false;
            }
            catch (Exception E)
            {
                Trace.WriteLine(string.Format("socket {0} encountered {1} error dropped by client, aborting", m_SessionId, E.Message), "warning");
                await Close(SocketCloseStatus.EndpointUnavailable);

                bClose = false;
            }

            if (bClose)
            {
                await Close(SocketCloseStatus.NormalClosure);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 握手完成后的回调函数
        /// </summary>
        /// <param name="env">底层提供的SendAsync、ReceiveAsync等方法</param>
        /// <returns></returns>
        private Task AcceptCallback(IDictionary <string, object> env)
        {
            if (env == null)
            {
                _acceptwaiter.Set();
                return(Task.Delay(0));
            }

            //从字典中取出服务器提供的WebSocket操作方法
            _sendAsync    = env.Get <WebSocketSendAsync>("websocket.SendAsync");
            _receiveAsync = env.Get <WebSocketReceiveAsync>("websocket.ReceiveAsync");
            _closeAsync   = env.Get <WebSocketCloseAsync>("websocket.CloseAsync");

            //标记连接成功
            _isClosed = false;
            _acceptwaiter.Set();

            //通知服务器(容器),表示连接事件已经处理完成
            return(Task.Delay(1));
        }
Esempio n. 10
0
 public OwinWebSocket(IDictionary<string, object> env)
 {
     _sendAsync = (WebSocketSendAsync)env[WebSocketConstants.WebSocketSendAsyncKey];
     _receiveAsync = (WebSocketReceiveAsync)env[WebSocketConstants.WebSocketReceiveAyncKey];
     _closeAsync = (WebSocketCloseAsync)env[WebSocketConstants.WebSocketCloseAsyncKey];
 }
Esempio n. 11
0
 public OwinWebSocket(IDictionary <string, object> env)
 {
     _sendAsync    = (WebSocketSendAsync)env[WebSocketConstants.WebSocketSendAsyncKey];
     _receiveAsync = (WebSocketReceiveAsync)env[WebSocketConstants.WebSocketReceiveAyncKey];
     _closeAsync   = (WebSocketCloseAsync)env[WebSocketConstants.WebSocketCloseAsyncKey];
 }
Esempio n. 12
0
        private int messageType; // 0x1 for Text, 0x2 for binary

        public BinaryStream(WebSocketSendAsync sendAsync, WebSocketReceiveAsync receiveAsync, WebSocketCloseAsync closeAsync, int messageType)
        {
            this.sendAsync = sendAsync;
            this.receiveAsync = receiveAsync;
            this.closeAsync = closeAsync;
            this.messageType = messageType;
        }