Example #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();
 }
Example #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);
        }
Example #3
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;
        }
Example #4
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();
 }
 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;
 }
Example #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;
 }
Example #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);
            }
        }
        public Task Invoke(IDictionary <string, object> context)
        {
            _sendAsync    = (WebSocketSendAsync)context["websocket.SendAsyncFunc"];
            _receiveAsync = (WebSocketReceiveAsync)context["websocket.ReceiveAsyncFunc"];

            var task = _callback(this)
                       .Then(cts => cts.Cancel(false), _cts)
                       .Catch(ex => _cts.Cancel(false));

            StartReceiving();
            return(task);
        }
Example #10
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));
        }
Example #11
0
        protected override Task SendMessage(MemoryStream memoryStream, JsonSerializer serializer, object message, WebSocketSendAsync sendAsync, CancellationToken callCancelled)
        {
            var typedMessage = message as LogEventInfo;

            if (typedMessage != null)
            {
                message = new LogEventInfoFormatted(typedMessage);
            }

            return base.SendMessage(memoryStream, serializer, message, sendAsync, callCancelled);
        }
Example #12
0
        protected virtual async Task SendMessage(MemoryStream memoryStream, JsonSerializer serializer, object message, WebSocketSendAsync sendAsync, CancellationToken callCancelled)
        {
            memoryStream.Position = 0;
            var jsonTextWriter = new JsonTextWriter(new StreamWriter(memoryStream));
            serializer.Serialize(jsonTextWriter, message);
            jsonTextWriter.Flush();

            var arraySegment = new ArraySegment<byte>(memoryStream.GetBuffer(), 0, (int)memoryStream.Position);
            await sendAsync(arraySegment, 1, true, callCancelled);
        }
Example #13
0
        protected virtual async Task SendMessage(MemoryStream memoryStream, JsonSerializer serializer, object message, WebSocketSendAsync sendAsync, CancellationToken callCancelled)
        {
            memoryStream.Position = 0;
            var jsonTextWriter = new JsonTextWriter(new StreamWriter(memoryStream));

            serializer.Serialize(jsonTextWriter, message);
            jsonTextWriter.Flush();

            var arraySegment = new ArraySegment <byte>(memoryStream.GetBuffer(), 0, (int)memoryStream.Position);

            await sendAsync(arraySegment, 1, true, callCancelled).ConfigureAwait(false);
        }
Example #14
0
 static Task SendAsync(WebSocketSendAsync _sendAsync, ArraySegment <byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
 {
     return(_sendAsync(buffer, EnumToOpCode(messageType), endOfMessage, cancellationToken));
 }
 public NoteSocketHandler(WebSocketSendAsync sendFunc, CancellationToken token)
 {
     _sendFunc = sendFunc;
     _token    = token;
 }
Example #16
0
 public OwinWebSocket(IDictionary <string, object> env)
 {
     _sendAsync    = (WebSocketSendAsync)env[WebSocketConstants.WebSocketSendAsyncKey];
     _receiveAsync = (WebSocketReceiveAsync)env[WebSocketConstants.WebSocketReceiveAyncKey];
     _closeAsync   = (WebSocketCloseAsync)env[WebSocketConstants.WebSocketCloseAsyncKey];
 }
Example #17
0
 public OwinWebSocket(IDictionary<string, object> env)
 {
     _sendAsync = (WebSocketSendAsync)env[WebSocketConstants.WebSocketSendAsyncKey];
     _receiveAsync = (WebSocketReceiveAsync)env[WebSocketConstants.WebSocketReceiveAyncKey];
     _closeAsync = (WebSocketCloseAsync)env[WebSocketConstants.WebSocketCloseAsyncKey];
 }
Example #18
0
        protected override Task SendMessage(MemoryStream memoryStream, JsonSerializer serializer, object message, WebSocketSendAsync sendAsync, CancellationToken callCancelled)
        {
            var typedMessage = message as LogEventInfo;

            if (typedMessage != null)
            {
                message = new LogEventInfoFormatted(typedMessage);
            }

            return(base.SendMessage(memoryStream, serializer, message, sendAsync, callCancelled));
        }
Example #19
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;
        }