Пример #1
0
        /// <summary>
        /// 收到请求数据
        /// </summary>
        /// <param name="frame">请求数据</param>
        private void OnFrameRequest(FrameRequest frame)
        {
            switch (frame.Frame)
            {
            case FrameCodes.Close:
                var closeFrame = new CloseRequest(frame);
                this.OnClose(closeFrame.StatusCode, closeFrame.CloseReason);
                base.Close();
                break;

            case FrameCodes.Binary:
                this.OnBinary(frame.Content);
                break;

            case FrameCodes.Text:
                var content = Encoding.UTF8.GetString(frame.Content);
                this.OnText(content);
                break;

            case FrameCodes.Ping:
                var pongValue = new FrameResponse(FrameCodes.Pong, frame.Content);
                this.TrySend(pongValue);
                this.OnPing(frame.Content);
                break;

            case FrameCodes.Pong:
                this.ProcessPingPong(frame.Content);
                this.OnPong(frame.Content);
                break;

            default:
                break;
            }
        }
Пример #2
0
        /// <summary>
        /// 解析请求帧
        /// </summary>
        /// <param name="buffer">接收到的历史数据</param>
        /// <returns></returns>
        private IEnumerable <FrameRequest> GetFrameRequestsFromBuffer(ReceiveBuffer buffer)
        {
            FrameRequest request;

            while ((request = FrameRequest.From(buffer)) != null)
            {
                yield return(request);
            }
        }
 /// <summary>
 /// 解析生成请求帧
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 private IEnumerable <FrameRequest> GenerateWebSocketRequest(IContenxt context)
 {
     while (true)
     {
         var request = FrameRequest.Parse(context.Buffer);
         if (request == null)
         {
             yield break;
         }
         yield return(request);
     }
 }
Пример #4
0
        /// <summary>
        /// 关闭请求帧
        /// </summary>
        /// <param name="request">请求帧</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public CloseRequest(FrameRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException();
            }
            if (request.Frame != FrameCodes.Close)
            {
                throw new ArgumentException();
            }

            this.request = request;
        }
        /// <summary>
        /// 解析生成请求帧
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private IList <FrameRequest> GenerateWebSocketRequest(IContenxt context)
        {
            var list = new List <FrameRequest>();

            while (true)
            {
                var request = FrameRequest.Parse(context.Buffer);
                if (request == null)
                {
                    return(list);
                }
                list.Add(request);
            }
        }
        /// <summary>
        /// 收到到数据帧请求
        /// </summary>
        /// <param name="context">会话对象</param>
        /// <param name="frameRequest">数据帧</param>
        private void OnWebSocketRequest(IContenxt context, FrameRequest frameRequest)
        {
            switch (frameRequest.Frame)
            {
            case FrameCodes.Close:
                var reason = StatusCodes.NormalClosure;
                if (frameRequest.Content.Length > 1)
                {
                    var status = ByteConverter.ToUInt16(frameRequest.Content, 0, Endians.Big);
                    reason = (StatusCodes)status;
                }
                this.OnClose(context, reason);
                context.Session.Close();
                break;

            case FrameCodes.Binary:
                this.OnBinary(context, frameRequest.Content);
                break;

            case FrameCodes.Text:
                var content = Encoding.UTF8.GetString(frameRequest.Content);
                this.OnText(context, content);
                break;

            case FrameCodes.Ping:
                try
                {
                    var session = (WebSocketSession)context.Session.Wrapper;
                    session.SendAsync(new FrameResponse(FrameCodes.Pong, frameRequest.Content));
                }
                catch (Exception)
                {
                }
                finally
                {
                    this.OnPing(context, frameRequest.Content);
                }
                break;

            case FrameCodes.Pong:
                this.OnPong(context, frameRequest.Content);
                break;

            default:
                break;
            }
        }
Пример #7
0
        /// <summary>
        /// 收到到数据帧请求
        /// </summary>
        /// <param name="session">会话对象</param>
        /// <param name="request">数据帧</param>
        private void OnRecvRequest(T session, FrameRequest request)
        {
            switch (request.Frame)
            {
            case FrameCodes.Close:
                var reason = StatusCodes.NormalClosure;
                if (request.Content.Length > 1)
                {
                    var status = ByteConverter.ToUInt16(request.Content, 0, Endians.Big);
                    reason = (StatusCodes)status;
                }
                this.OnClose(session, reason);
                session.Close();
                break;

            case FrameCodes.Binary:
                this.OnBinary(session, request.Content);
                break;

            case FrameCodes.Text:
                var content = Encoding.UTF8.GetString(request.Content);
                this.OnText(session, content);
                break;

            case FrameCodes.Ping:
                try
                {
                    session.SendResponse(new FrameResponse(FrameCodes.Pong, request.Content));
                }
                catch (Exception)
                {
                }
                finally
                {
                    this.OnPing(session, request.Content);
                }
                break;

            case FrameCodes.Pong:
                this.OnPong(session, request.Content);
                break;

            default:
                break;
            }
        }
Пример #8
0
        /// <summary>
        /// 收到到数据帧请求
        /// </summary>
        /// <param name="context">会话对象</param>
        /// <param name="frameRequest">数据帧</param>
        /// <returns></returns>
        private async Task OnWebSocketRequestAsync(IContenxt context, FrameRequest frameRequest)
        {
            switch (frameRequest.Frame)
            {
            case FrameCodes.Close:
                var frame = new CloseRequest(frameRequest);
                this.OnClose(context, frame.StatusCode, frame.CloseReason);
                context.Session.Close();
                break;

            case FrameCodes.Binary:
                await this.OnBinaryAsync(context, frameRequest.Content);

                break;

            case FrameCodes.Text:
                var content = Encoding.UTF8.GetString(frameRequest.Content);
                await this.OnTextAsync(context, content);

                break;

            case FrameCodes.Ping:
                try
                {
                    var pong        = new FrameResponse(FrameCodes.Pong, frameRequest.Content);
                    var pongContent = pong.ToArraySegment(mask: false);
                    context.Session.Send(pongContent);
                }
                catch (Exception)
                {
                }
                finally
                {
                    this.OnPing(context, frameRequest.Content);
                }
                break;

            case FrameCodes.Pong:
                context.Session.Publish("Pong", frameRequest.Content);
                this.OnPong(context, frameRequest.Content);
                break;

            default:
                break;
            }
        }
Пример #9
0
        /// <summary>
        /// 收到文本内容
        /// </summary>
        /// <param name="frame">数据帧</param>
        protected sealed override async void OnText(FrameRequest frame)
        {
            var text    = Encoding.UTF8.GetString(frame.Content);
            var package = this.GenerateJsonPacket(text);

            if (package == null)
            {
                return;
            }

            if (package.state == false)
            {
                this.ProcessRemoteException(package);
            }
            else
            {
                await this.ProcessRequestAsync(package);
            }
        }
Пример #10
0
        /// <summary>
        /// 接收到文本信息时
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="frame">数据帧</param>
        protected sealed override async void OnText(IContenxt context, FrameRequest frame)
        {
            var text           = Encoding.UTF8.GetString(frame.Content);
            var requestContext = this.GenerateRequestContext(context, text);

            if (requestContext == null)
            {
                return;
            }

            if (requestContext.Packet.state == false)
            {
                this.ProcessRemoteException(requestContext);
            }
            else
            {
                await this.ProcessRequestAsync(requestContext);
            }
        }
Пример #11
0
        /// <summary>
        /// 接收到文本信息时
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="frame">数据帧</param>
        protected sealed override async void OnText(IContenxt context, FrameRequest frame)
        {
            var text       = Encoding.UTF8.GetString(frame.Content);
            var jsonPacket = this.TryGetJsonPacket(context, text);

            if (jsonPacket == null)
            {
                return;
            }

            var session        = (JsonWebSocketSession)context.Session.Wrapper;
            var requestContext = new RequestContext(session, jsonPacket, context.AllSessions);

            if (jsonPacket.state == false)
            {
                this.ProcessRemoteException(requestContext);
            }
            else
            {
                await this.ProcessRequestAsync(requestContext);
            }
        }
Пример #12
0
        /// <summary>
        /// 解析生成请求帧
        /// </summary>
        /// <param name="inputStream">数据流</param>
        /// <returns></returns>
        private IList <FrameRequest> GenerateWebSocketFrame(IStreamReader inputStream)
        {
            var list = new List <FrameRequest>();

            while (true)
            {
                try
                {
                    var request = FrameRequest.Parse(inputStream, false);
                    if (request == null)
                    {
                        return(list);
                    }
                    list.Add(request);
                }
                catch (NotSupportedException ex)
                {
                    this.Close(StatusCodes.ProtocolError, ex.Message);
                    return(list);
                }
            }
        }
        /// <summary>
        /// 解析生成请求帧
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private IList <FrameRequest> GenerateWebSocketRequest(IContenxt context)
        {
            var list = new List <FrameRequest>();

            while (true)
            {
                try
                {
                    var request = FrameRequest.Parse(context.Stream);
                    if (request == null)
                    {
                        return(list);
                    }
                    list.Add(request);
                }
                catch (NotSupportedException)
                {
                    context.Session.Close();
                    return(list);
                }
            }
        }
Пример #14
0
        /// <summary>
        /// 解析生成请求帧
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private IList <FrameRequest> GenerateWebSocketRequest(IContenxt context)
        {
            var list = new List <FrameRequest>();

            while (true)
            {
                try
                {
                    var request = FrameRequest.Parse(context.StreamReader);
                    if (request == null)
                    {
                        return(list);
                    }
                    list.Add(request);
                }
                catch (NotSupportedException ex)
                {
                    var session = new WebSocketSession(context.Session);
                    session.Close(StatusCodes.ProtocolError, ex.Message);
                    return(list);
                }
            }
        }
Пример #15
0
 /// <summary>
 /// 收到二进制类型请求时触发此方法
 /// </summary>
 /// <param name="context">会话对象</param>
 /// <param name="frame">帧</param>
 protected virtual void OnBinary(IContenxt context, FrameRequest frame)
 {
 }
        /// <summary>
        /// 收到到数据帧请求
        /// </summary>
        /// <param name="context">会话对象</param>
        /// <param name="frameRequest">数据帧</param>
        private void OnWebSocketRequest(IContenxt context, FrameRequest frameRequest)
        {
            switch (frameRequest.Frame)
            {
                case FrameCodes.Close:
                    var reason = StatusCodes.NormalClosure;
                    if (frameRequest.Content.Length > 1)
                    {
                        var status = ByteConverter.ToUInt16(frameRequest.Content, 0, Endians.Big);
                        reason = (StatusCodes)status;
                    }
                    this.OnClose(context, reason);
                    context.Session.Close();
                    break;

                case FrameCodes.Binary:
                    this.OnBinary(context, frameRequest.Content);
                    break;

                case FrameCodes.Text:
                    var content = Encoding.UTF8.GetString(frameRequest.Content);
                    this.OnText(context, content);
                    break;

                case FrameCodes.Ping:
                    try
                    {
                        var session = context.Session.Wrapper as WebSocketSession;
                        session.Send(new FrameResponse(FrameCodes.Pong, frameRequest.Content));
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        this.OnPing(context, frameRequest.Content);
                    }
                    break;

                case FrameCodes.Pong:
                    this.OnPong(context, frameRequest.Content);
                    break;

                default:
                    break;
            }
        }
Пример #17
0
 /// <summary>
 /// 收到服务端的Binary请求
 /// </summary>
 /// <param name="frame">数据帧</param>
 protected virtual void OnBinary(FrameRequest frame)
 {
 }
Пример #18
0
 /// <summary>
 /// 收到服务端的Text请求
 /// </summary>
 /// <param name="frame">数据帧</param>
 protected virtual void OnText(FrameRequest frame)
 {
 }