public static void OnDataReceived(IClientConnection connection, byte[] data)
        {
            try
            {
                ProtocolEncoder encoder = new ProtocolEncoder();

                MediumDeserializedObject requestObject = null;
                try
                {
                    requestObject = ProtocolSerializer.Deserialize(data);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                string controlCommand = encoder.Decode(requestObject.ControlCommand);

                IList <ArraySegment <byte> > serializedResponse = null;
                Type requestType = _availableRequests.GetRequestType(controlCommand);
                if (requestType != null)
                {
                    try
                    {
                        Type protocolRequestInterface = requestType.GetInterfaces().FirstOrDefault(x => typeof(IProtocolRequest <IProtocolResponse>).IsAssignableFrom(x));
                        Type responseType             = protocolRequestInterface.GenericTypeArguments[0];
                        serializedResponse = (IList <ArraySegment <byte> >)_executeMethod.MakeGenericMethod(requestType, responseType).Invoke(null, new object[] { requestObject.Body });
                    }
                    catch
                    {
                        serializedResponse = SerializeResponse(new ErrorResponse
                        {
                            Code    = 500,
                            Message = "Internal server error."
                        });
                    }
                }
                else
                {
                    serializedResponse = SerializeResponse(new ErrorResponse
                    {
                        Code    = 400,
                        Message = "Unsupported request."
                    });
                }

                connection.Send(serializedResponse);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private static IList <ArraySegment <byte> > SerializeResponse <TResponse>(TResponse protocolResponse)
            where TResponse : IProtocolResponse, new()
        {
            ProtocolEncoder encoder = new ProtocolEncoder();

            MessageHeadersDictionary responseHeaders = new MessageHeadersDictionary();

            byte[] identifier     = encoder.Encode(protocolResponse.GetIdentifier());
            byte[] headers        = ProtocolMessageHeaderSerializer.Serialize(responseHeaders);
            byte[] serializedBody = ProtocolBodySerializer.Serialize(protocolResponse);

            return(ProtocolSerializer.Serialize(identifier, headers, serializedBody));
        }
Пример #3
0
        /// <summary>
        /// 解码缓存字节为协议包
        /// </summary>
        private void Decode()
        {
            var package = ProtocolEncoder.Decode(_processBuffer.ToArray());

            AsyncCleanBuffer(package);
            if (!package.Finalized)
            {
                return;
            }

            package.ClientSource = ClientSource;
            package.SetupProtocolData();
            OnClientDecoded(package.ProtocolData);
            EncodingManager.RunBuinessHandler(package);
        }
        /// <summary>
        /// 设备认证
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static AuthResult DeviceAuthcation(byte[] buffer)
        {
            var package = ProtocolEncoder.Decode(buffer, ProtocolInfoManager.AllProtocols);

            if (!package.Finalized)
            {
                return(new AuthResult(AuthResultType.DecodedFailed, package));
            }

            var device = GetAuthedDevice(package);

            return(device == null
                ? new AuthResult(AuthResultType.DeviceNotRegisted, package)
                : new AuthResult(AuthResultType.Success, package, device, package.NeedReply));
        }
Пример #5
0
        /// <summary>
        /// 身份验证
        /// </summary>
        private IProtocolPackage Authentication()
        {
            var result = AuthenticationService.DeviceAuthcation(_processBuffer.ToArray());

            if (result.ResultType == AuthResultType.Success)
            {
                ClientDevice     = result.AuthDevice;
                ReceiverName     = $"{ClientDevice.DeviceCode} - {ClientDevice.Id.ToString().ToUpper()}";
                _protocolEncoder = new ProtocolEncoder(ClientDevice);
                _authStatus      = AuthenticationStatus.Authed;
                if (result.NeedReply)
                {
                    Send(result.ReplyBytes);
                }

                OnClientAuthentication();
            }
            else if (result.ResultType == AuthResultType.DeviceNotRegisted || result.ResultType == AuthResultType.DecodedFailed)
            {
                _authStatus = AuthenticationStatus.AuthFailed;
            }

            return(result.Package);
        }