Beispiel #1
0
        private bool ProcessPacket(UserTokenEventArgs userToken)
        {
            if (userToken.ClientType != ClientType.WebSocket)//第一次进来握手操作
            {
                byte[]             recvMsg   = userToken.ResvBufferManager.GetCopyBuffer();
                WebSocketHandshake handshake = new WebSocketHandshake(recvMsg);
                if (handshake.IsWebSocket)
                {
                    userToken.UserSocket.Send(Encoding.UTF8.GetBytes(handshake.Response));
                    userToken.ResvBufferManager.Reset();
                    userToken.ClientType = ClientType.WebSocket;
                }
                handshake = null;
                return(true);
            }
            if (userToken.ClientType == ClientType.WebSocket)
            {
                #region 处理WebSocket
                try
                {
                    if (userToken.ResvBufferManager.Length % this.receiveBufferSize == 0) //客户端一次发送的数据大于服务端缓冲区
                    {
                        return(true);
                    }
                    if (userToken.ResvBufferManager.Length >= 1024 * 120)
                    {
                        this.RaiseErrorEvent(userToken, new Exception("WebSocket暂不能解析大于120K的单个包"));
                        return(false);
                    }
                    while (userToken.ResvBufferManager.Length != 0)
                    {
                        int       clearCount = 0;
                        DataFrame dr         = new DataFrame(userToken.ResvBufferManager.Buffer, ref clearCount);
                        if (dr.Header.OpCode == OpCode.Close)
                        {
                            this.RaiseErrorEvent(userToken, new Exception("33333333333用户主动断开"));
                            return(false);
                        }
                        if (dr.Header.OpCode == OpCode.Binary)
                        {
                            #region 正常解析为二进制


                            MessageHeader header = new MessageHeader();
                            header.ProccessHeader(dr.BinaryContent);
                            if (header.MessageId > UInt16.MaxValue || header.MessageId < 0 || header.PacketLength > this.maxPacketLength | userToken.ResvBufferManager.Length > this.maxPacketLength)
                            {
                                this.RaiseErrorEvent(userToken, new Exception("解析消息类型错误|包长度超过最大长度"));
                                return(false);
                            }
                            if (userToken.ResvBufferManager.Length < header.PacketLength + clearCount)//包不足够的情况
                            {
                                return(true);
                            }
                            byte[]     content    = BytesHelper.CopyArrayData(dr.BinaryContent, header.Length, dr.BinaryContent.Length - header.Length);
                            NetMessage netMessage = new NetMessage(header, content);
                            userToken.NetMessage = netMessage;
                            //userToken.ResvBufferManager.SetOffset(header.Length-1);
                            if (this.customizeHander != null)
                            {
                                customizeHander.HandleInformation(userToken);
                                //customizeHander.HandleInformation(userToken, userToken.ConnectedId, netMessage.Header.MessageId, content);
                            }
                            userToken.ResvBufferManager.Clear(header.PacketLength + clearCount);
                            #endregion
                        }
                        else
                        {
                            this.RaiseErrorEvent(userToken, new Exception("WebScoket未能正确解析包 OP代码" + dr.Header.OpCode));
                            userToken.ResvBufferManager.Reset();
                            return(true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.RaiseErrorEvent(userToken, new Exception("处理WebSocket包出现异常:" + ex.ToString()));
                    return(false);
                }
                #endregion
            }
            if (userToken.ClientType == ClientType.DotNET)//继续处理其他平台
            {
                //string userId = userToken.UserSocket.RemoteEndPoint.ToString();
            }
            return(true);
        }