示例#1
0
        public async Task HandleMessage(Client client, INetworkStreamWrapper networkStream, byte[] bytes)
        {
            ICustomMessageHandler customMessageHandler = GetMessageHandler(client.Protocol);

            MessageInput messageInput = new MessageInput
            {
                Client        = client,
                NetworkStream = networkStream,
                DataMessage   = new DataMessage(bytes, client.Protocol.SplitMessageBy)
            };

            logger.LogTrace(
                $"{client.Protocol}: received {HexUtil.ConvertHexStringArrayToHexString(messageInput.DataMessage.Hex)}");

            int connectionMessageId = await connectionService.AddMessage(client.DeviceConnection.Id, messageInput.DataMessage.HexString);

            try
            {
                List <Location> locations = customMessageHandler.ParseRange(messageInput)?.ToList();

                if (locations != null && locations.Any())
                {
                    // TODO refactor this
                    await connectionService.SetDeviceId(client);

                    await locationService.AddRange(locations, connectionMessageId);
                }
            }
            catch (Exception e)
            {
                logger.LogCritical(e,
                                   $"{customMessageHandler.GetType()}: Error parsing {messageInput.DataMessage.Hex} ");
            }
        }
示例#2
0
        public void Init()
        {
            _mockResponseHandler = MockRepository.GenerateStub <IExceptionResponseHandler>();
            _controller          = new HttpCallController(_mockResponseHandler);

            _mockProcessor = MockRepository.GenerateStub <IHttpCallProcessor>();
            _mockProcessor.Stub(s => s.Method).Return("TEST");
            _controller.RegisterCallProcessor(_mockProcessor);

            _mockWrapper = MockRepository.GenerateStub <INetworkStreamWrapper>();
        }
示例#3
0
        virtual public IHttpResponse ProcessCall(INetworkStreamWrapper socketWrapper)
        {
            _socketWrapper = socketWrapper;
            _socketWrapper.SkipByteCount(_verbLength + 1);

            var httpCall = new HttpCall()
            {
                ServerSettings = ServerSettings,
                RequestHeader  = GetHeaderFromSocket(),
                RawRequestBody = GetRequestBodyFromSocket()
            };

            return(_firstCallResponder.Respond(httpCall));
        }
示例#4
0
        public async Task HandleStream(CancellationToken cancellationToken, Client client,
                                       INetworkStreamWrapper networkStream)
        {
            byte[] buffer = new byte[ServerVariables.BufferLength];
            int    length;

            do
            {
                length = ReadMessage(networkStream, buffer, client.Protocol, cancellationToken);

                if (length > 0)
                {
                    logger.LogDebug($"{client.Protocol}: received {length} bytes");

                    int startIndex = GetStartIndex(buffer, length, client.Protocol);

                    await messageHandler.HandleMessage(client, networkStream, buffer[startIndex..length]);
示例#5
0
        public void ProcessCall(INetworkStreamWrapper socketWrapper)
        {
            var result = socketWrapper.PeekToString(9);

            Exception     caughtException = null;
            IHttpResponse response        = null;

            try
            {
                IHttpCallProcessor callProcessor = VerifyAndExtractProcessor(result);
                response = callProcessor.ProcessCall(socketWrapper);
            }
            catch (Exception ex)
            {
                if (_exceptionHandler.HasResponse(ex.GetType().Name) == false)
                {
                    throw;
                }
                response = _exceptionHandler.GetResponseFromException(caughtException);
            }

            SendResponse(response, socketWrapper);
        }
示例#6
0
 private void SendResponse(IHttpResponse response, INetworkStreamWrapper socketWrapper)
 {
     socketWrapper.Send(response.GetResponseAsByteArray());
 }