Ejemplo n.º 1
0
        //public void HandleRequest(Socket clientSocket, byte[] buffer)
        //{
        //    var messageWrapper = MessageWrapper.Load(buffer, _messageReaders);

        //    switch (messageWrapper.ClientType)
        //    {
        //        case ClientType.Scavenger:
        //            var scavengerService = new ScavengerService(new ScavengerClient(clientSocket));
        //            _scavengerHandlers[messageWrapper.MessageType](scavengerService, messageWrapper);
        //            break;
        //        case ClientType.Guide:
        //            var guideService = new GuideService(new GuideClient(clientSocket));
        //            _guideHandlers[messageWrapper.MessageType](guideService, messageWrapper);
        //            break;
        //        default:
        //            throw new ArgumentOutOfRangeException();
        //    }
        //}

        internal async Task HandleClient(TcpClient client, CancellationToken cancellationToken)
        {
            using (client)
            {
                var buffer = new byte[bufferLength];
                var stream = client.GetStream();
                while (!cancellationToken.IsCancellationRequested)
                {
                    var amountReadTask = stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
                    var amountRead     = amountReadTask.Result;
                    if (amountRead == 0)
                    {
                        break;
                    }

                    var messageWrapper = MessageWrapper.Load(buffer, _messageReaders);

                    switch (messageWrapper.ClientType)
                    {
                    case ClientType.Scavenger:
                        var scavengerService = new ScavengerService();
                        await scavengerService.SetClient(new ScavengerClient(stream, messageWrapper.ClientId));

                        await _scavengerHandlers[messageWrapper.MessageType](scavengerService, messageWrapper);
                        break;

                    case ClientType.Guide:
                        var guideService = new GuideService();
                        await guideService.SetClient(new GuideClient(stream, messageWrapper.ClientId));

                        await _guideHandlers[messageWrapper.MessageType](guideService, messageWrapper);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void HandleRequest(IScavengerClient scavengerClient, Socket clientSocket, byte[] buffer)
        {
            var messageWrapper = MessageWrapper.Load(buffer, _messageReaders);

            _scavengerHandlers[messageWrapper.MessageType](scavengerClient, messageWrapper);
        }
Ejemplo n.º 3
0
        public void HandleRequest(IGuideClient GuideClient, Socket clientSocket, byte[] buffer)
        {
            var messageWrapper = MessageWrapper.Load(buffer, _messageReaders);

            _guideHandlers[messageWrapper.MessageType](GuideClient, messageWrapper);
        }