Ejemplo n.º 1
0
        public void Execute(PacketBodyBase requestPacketBody, ReceivedSocketState receivedSocketState)
        {
            LoginResponse _response  = null;
            string        _exception = string.Empty;

            try
            {
                var _request = (LoginRequest)requestPacketBody;
                _response = this.CheckLogin(_request);
            }
            catch (Exception ex)
            {
                _response = new LoginResponse
                {
                    Status  = false,
                    Message = ResponseMessage.COMM_NETWORK_ERROR,
                };
                _exception = ex.ToString();
            }

            try
            {
                receivedSocketState.Response(PacketUtil.Pack(_response));
                Logger.LogInfo(string.Format("收到来自{0}的请求,Request = {1},Response = {2},Exception = {3}"
                                             , receivedSocketState.ClientIpAddress, requestPacketBody.ToString(), _response.ToString(), _exception));
            }
            catch (Exception ex)
            {
                throw new ResponseException(ex.ToString());
            }
        }
Ejemplo n.º 2
0
 public T Send <T>(PacketBodyBase packageBody) where T : PacketBodyBase
 {
     try
     {
         var _data          = PacketUtil.Pack(packageBody);
         var _bytesReceived = this.SocketSender.Send(_data);
         var _packageBody   = PacketUtil.UnPackResponse(_bytesReceived);
         return((T)_packageBody);
     }
     catch
     {
         throw;
     }
 }
Ejemplo n.º 3
0
        public void Execute(PacketBodyBase requestPacketBody, ReceivedSocketState receivedSocketState)
        {
            try
            {
                var _request  = (RegisterRequest)requestPacketBody;
                var _response = this.CheckRegister(_request);

                Logger.LogInfo(string.Format("收到来自{0}的注册请求,处理结果:{1}", receivedSocketState.ClientIpAddress, _response.Message));
                receivedSocketState.Response(PacketUtil.Pack(_response));
            }
            catch (Exception ex)
            {
                Logger.LogError(string.Format("收到来自{0}的注册请求,处理失败,错误信息:{1}", receivedSocketState.ClientIpAddress, ex));
                var _response = new RegisterResponse
                {
                    Status  = false,
                    Message = "网络异常,请重试!"
                };
                receivedSocketState.Response(PacketUtil.Pack(_response));
            }
        }
Ejemplo n.º 4
0
        private void RequestHandlerEngine_SocketReceiveCompleted(object sender, SocketReceiveCompletedEventArgs e)
        {
            if (e == null || (e != null && e.ReceivedSocketState == null))
            {
                return;
            }
            if (e.ReceivedSocketState.DataReceived == null)
            {
                return;
            }
            var _bytes = e.ReceivedSocketState.DataReceived;

            try
            {
                PacketBodyBase _packetBody = PacketUtil.UnPackRequest(_bytes);
                if (_packetBody == null)
                {
                    throw new UnPackException("解包后的数据包为空", null);
                }

                var             _packetType = (PacketType)Enum.Parse(typeof(PacketType), _packetBody.GetMsgId().ToString());
                IRequestHandler _handler    = null;

                if (typeof(RequestPacket).IsAssignableFrom(_packetBody.GetType()))
                {
                    switch (_packetType)
                    {
                    case PacketType.Register:
                        _handler = new RegisterHandler(); break;

                    case PacketType.Login:
                        _handler = new LoginHandler(); break;

                    default:
                        throw new NotSupportedPacketTypeException(string.Format("暂不支持类型为{0}的数据包", _packetBody.GetType().ToString()), _packetBody.GetType());
                    }
                }
                else
                {
                    throw new NotSupportedPacketTypeException(string.Format("暂不支持类型为{0}的数据包", _packetBody.GetType().ToString()), _packetBody.GetType());
                }

                _handler.Execute(_packetBody, e.ReceivedSocketState);
            }
            catch (ResponseException ex)
            {
                ex.Source += ".RequestHandlerEngine.RequestHandlerEngine_SocketReceiveCompleted";
                Logger.LogError(string.Format("处理来自{0}的请求后,发回响应包时出现异常,异常信息 = {1}", e.ReceivedSocketState.ClientIpAddress, ex));
            }
            catch (NotSupportedPacketTypeException ex)
            {
                var _response = new CommErrorResponse
                {
                    Status  = false,
                    Message = ResponseMessage.COMM_NOTSUPPORT_FUNCTION,
                };
                e.ReceivedSocketState.Response(PacketUtil.Pack(_response));

                ex.Source += ".RequestHandlerEngine.RequestHandlerEngine_SocketReceiveCompleted";
                Logger.LogError(string.Format("处理来自{0}的请求时,遇到未支持功能,异常信息 = {1}", e.ReceivedSocketState.ClientIpAddress, ex));
            }
            catch (UnPackException ex)
            {
                var _response = new CommErrorResponse
                {
                    Status  = false,
                    Message = ResponseMessage.COMM_NETWORK_ERROR,
                };
                e.ReceivedSocketState.Response(PacketUtil.Pack(_response));

                ex.Source += ".RequestHandlerEngine.RequestHandlerEngine_SocketReceiveCompleted";
                Logger.LogError(string.Format("处理来自{0}的请求时,解包失败,异常信息 = {1}", e.ReceivedSocketState.ClientIpAddress, ex));
            }
            catch (PackException ex)
            {
                var _response = new CommErrorResponse
                {
                    Status  = false,
                    Message = ResponseMessage.COMM_NETWORK_ERROR,
                };
                e.ReceivedSocketState.Response(PacketUtil.Pack(_response));

                ex.Source += ".RequestHandlerEngine.RequestHandlerEngine_SocketReceiveCompleted";
                Logger.LogError(string.Format("处理来自{0}的请求时,打包失败,异常信息 = {1}", e.ReceivedSocketState.ClientIpAddress, ex));
            }
            catch (Exception ex)
            {
                ex.Source += ".RequestHandlerEngine.RequestHandlerEngine_SocketReceiveCompleted";
                Logger.LogError(string.Format("处理来自{0}的请求时,出现异常,异常信息 = {1}", e.ReceivedSocketState.ClientIpAddress, ex));
            }
        }