Ejemplo n.º 1
0
        private IRequestContext CreateRequestContext()
        {
            _logger.Debug("Creating request context...");
            var requestContext = _requestContextFactory.Create();

            requestContext.Policies = _policyRegistry;
            requestContext.Bag      = new Dictionary <string, object>();

            return(requestContext);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public async Task Run(RequestDelegate pipeline, CancellationToken token)
        {
            if (_config.Value == null)
            {
                throw new InvalidOperationException("Configuration not provided");
            }
            _logger.LogInformation($"Starting udp server on {_config.Value.EndPoint}");
            using (var client = new UdpClient(_config.Value.EndPoint))
            {
                if (_config.Value.AllowNatTraversal != null)
                {
                    client.AllowNatTraversal(_config.Value.AllowNatTraversal.Value);
                }

                client.DontFragment    = _config.Value.DontFragment;
                client.EnableBroadcast = _config.Value.EnableBroadcast;
                if (_config.Value.ExclusiveAddressUse != null)
                {
                    client.ExclusiveAddressUse = _config.Value.ExclusiveAddressUse.GetValueOrDefault();
                }
                client.MulticastLoopback = _config.Value.MulticastLoopback;
                client.Ttl = _config.Value.Ttl;
                if (_config.Value.MulticastGroups != null)
                {
                    foreach (var addr in _config.Value.MulticastGroups)
                    {
                        client.JoinMulticastGroup(addr);
                    }
                }
                while (token.IsCancellationRequested == false)
                {
                    var receiveTask = client.ReceiveAsync();
                    receiveTask.Wait(token);
                    var received = await receiveTask;
                    _logger.LogDebug($"Received {received.Buffer.Length} bytes from {received.RemoteEndPoint}");
                    if (received.Buffer != null)
                    {
                        var id = Convert.ToBase64String(BitConverter.GetBytes(Interlocked.Increment(ref contextId)));
                        _logger.LogDebug($"{id} Context started");
                        var context = _factory.Create(received.RemoteEndPoint, received.Buffer, id);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        Task.Run(async() =>
                        {
                            try
                            {
                                await pipeline(context);
                                if (context.ResponsePacket != null)
                                {
                                    _logger.LogDebug($"{id} Response generated by pipeline");
                                    var bytes = await client.SendAsync(context.ResponsePacket, context.ResponsePacket.Length, context.RemoteEndpoint);
                                    _logger.LogDebug($"{id} {bytes} bytes send to {context.RemoteEndpoint}");
                                }
                            }
                            catch (Exception e)
                            {
                                _logger.LogError(exception: e, message: $"{id} Unhandled exception during pipeline execution");
                            }
                        }).ConfigureAwait(false);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    }
                }
            }
        }