Ejemplo n.º 1
0
 public NetMapTransport(IPEndPoint ipEndpoint, string interfaceName, IConnectionDispatcher dispatcher)
 {
     _endpoint             = ipEndpoint ?? throw new ArgumentNullException(nameof(ipEndpoint));
     _interfaceName        = interfaceName ?? throw new ArgumentNullException(nameof(interfaceName));
     _connectionDispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
     Console.WriteLine($"Transport started with ip endpoint {ipEndpoint} on interface name {interfaceName}");
 }
Ejemplo n.º 2
0
        public TcpConnection(Ethernet ethHeader,
                             IPv4 ipHeader,
                             Network.Header.Tcp tcpHeader,
                             TTransmitter packetTransmitter,
                             PipeScheduler readScheduler,
                             PipeScheduler writeScheduler,
                             MemoryPool <byte> memoryPool,
                             IConnectionDispatcher connectionDispatcher)
        {
            _packetTransmitter      = packetTransmitter;
            _connectionDispatcher   = connectionDispatcher;
            _remoteAddress          = ipHeader.SourceAddress;
            _localAddress           = ipHeader.DestinationAddress;
            _outboundEthernetHeader = new Ethernet()
            {
                Destination = ethHeader.Source, Ethertype = EtherType.IPv4, Source = ethHeader.Destination
            };
            var pseudo = new TcpV4PseudoHeader()
            {
                Destination = _remoteAddress, Source = _localAddress, ProtocolNumber = Internet.Ip.ProtocolNumber.Tcp, Reserved = 0
            };

            _pseudoPartialSum = Checksum.PartialCalculate(ref Unsafe.As <TcpV4PseudoHeader, byte>(ref pseudo), Unsafe.SizeOf <TcpV4PseudoHeader>());

            LocalAddress  = new System.Net.IPAddress(_localAddress.Address);
            RemoteAddress = new System.Net.IPAddress(_remoteAddress.Address);
            RemotePort    = tcpHeader.SourcePort;
            LocalPort     = tcpHeader.DestinationPort;

            OutputReaderScheduler = readScheduler ?? throw new ArgumentNullException(nameof(readScheduler));
            InputWriterScheduler  = writeScheduler ?? throw new ArgumentNullException(nameof(writeScheduler));
            MemoryPool            = memoryPool ?? throw new ArgumentNullException(nameof(memoryPool));

            ConnectionClosed = _closedToken.Token;
        }
Ejemplo n.º 3
0
        internal QuicTransport(
            IEndPointInformation endPointInformation,
            IConnectionDispatcher dispatcher,
            IApplicationLifetime applicationLifetime,
            int ioQueueCount,
            ILogger logger)
        {
            _endPointInformation = endPointInformation;
            _dispatcher          = dispatcher;
            _applicationLifetime = applicationLifetime;
            _logger = logger;

            if (ioQueueCount > 0)
            {
                _numSchedulers = ioQueueCount;
                _schedulers    = new IOQueue[_numSchedulers];

                for (var i = 0; i < _numSchedulers; i++)
                {
                    _schedulers[i] = new IOQueue();
                }
            }
            else
            {
                _numSchedulers = ThreadPoolSchedulerArray.Length;
                _schedulers    = ThreadPoolSchedulerArray;
            }
        }
Ejemplo n.º 4
0
        public SocketTransport(IEndPointInformation endPointInformation,
                               IConnectionDispatcher dispatcher,
                               int listenBacklog,
                               PipeOptions sendOptions,
                               PipeOptions receiveOptions)
        {
            if (endPointInformation == null)
            {
                throw new ArgumentNullException(nameof(endPointInformation));
            }
            if (endPointInformation.Type != ListenType.IPEndPoint)
            {
                throw new InvalidOperationException(nameof(endPointInformation.IPEndPoint));
            }
            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }
            if (listenBacklog < 0)
            {
                throw new InvalidOperationException(nameof(listenBacklog));
            }

            _endPointInformation = endPointInformation;
            _dispatcher          = dispatcher;
            _backlog             = listenBacklog;

            _numSchedulers  = 1;
            _cachedPipeOpts = new PipeOptionsPair[] { new PipeOptionsPair(sendOptions, receiveOptions) };
        }
Ejemplo n.º 5
0
 public SimpleServer(
     IConnectionDispatcher connectionDispatcher,
     IEnumerable <INetworkAcceptor> networkAcceptors)
 {
     _connectionDispatcher = connectionDispatcher;
     _networkAcceptors     = networkAcceptors;
 }
Ejemplo n.º 6
0
        public Transport(IEndPointInformation ipEndPointInformation, IConnectionDispatcher connectionDispatcher, LinuxTransportOptions transportOptions, ILoggerFactory loggerFactory)
        {
            if (connectionDispatcher == null)
            {
                throw new ArgumentNullException(nameof(connectionDispatcher));
            }
            if (transportOptions == null)
            {
                throw new ArgumentException(nameof(transportOptions));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentException(nameof(loggerFactory));
            }
            if (ipEndPointInformation == null)
            {
                throw new ArgumentException(nameof(ipEndPointInformation));
            }

            _endPoint             = ipEndPointInformation;
            _connectionDispatcher = connectionDispatcher;
            _transportOptions     = transportOptions;
            _loggerFactory        = loggerFactory;
            _logger  = loggerFactory.CreateLogger <Transport>();
            _threads = Array.Empty <TransportThread>();
        }
Ejemplo n.º 7
0
        internal SocketTransport(
            IEndPointInformation endPointInformation,
            IConnectionDispatcher dispatcher,
            IApplicationLifetime applicationLifetime,
            int ioQueueCount,
            ISocketsTrace trace)
        {
            Debug.Assert(endPointInformation != null);
            Debug.Assert(endPointInformation.Type == ListenType.IPEndPoint);
            Debug.Assert(dispatcher != null);
            Debug.Assert(applicationLifetime != null);
            Debug.Assert(trace != null);

            _endPointInformation = endPointInformation;
            _dispatcher          = dispatcher;
            _appLifetime         = applicationLifetime;
            _trace = trace;

            if (ioQueueCount > 0)
            {
                _numSchedulers = ioQueueCount;
                _schedulers    = new IOQueue[_numSchedulers];

                for (var i = 0; i < _numSchedulers; i++)
                {
                    _schedulers[i] = new IOQueue();
                }
            }
            else
            {
                _numSchedulers = ThreadPoolSchedulerArray.Length;
                _schedulers    = ThreadPoolSchedulerArray;
            }
        }
Ejemplo n.º 8
0
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
        {
            endPointInformation = endPointInformation ?? throw new ArgumentNullException(nameof(endPointInformation));
            dispatcher          = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));

            var transport = new NetMapTransport(endPointInformation.IPEndPoint, _options.InterfaceName, dispatcher);

            return(transport);
        }
Ejemplo n.º 9
0
 public UvTransport(IEndPointInformation endPoint, IConnectionDispatcher dispatcher, int threadCount = 1, ILibuvTrace log = null)
 {
     _endPointInformation = endPoint;
     _dispatcher          = dispatcher;
     _threadCount         = threadCount;
     Log        = log;
     Threads    = new List <UvThread>();
     _listeners = new List <IAsyncDisposable>();
 }
Ejemplo n.º 10
0
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
        {
            if (ConnectionDispatcher != null)
            {
                throw new InvalidOperationException("InMemoryTransportFactory doesn't support creating multiple endpoints");
            }

            ConnectionDispatcher = dispatcher;

            return(new NoopTransport());
        }
Ejemplo n.º 11
0
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
        {
            var transportContext = new LibuvTransportContext
            {
                Options              = _baseTransportContext.Options,
                AppLifetime          = _baseTransportContext.AppLifetime,
                Log                  = _baseTransportContext.Log,
                ConnectionDispatcher = dispatcher
            };

            return(new LibuvTransport(transportContext, endPointInformation));
        }
Ejemplo n.º 12
0
 public Transport(
     IListenerLogger logger,
     InlineSocketsOptions options,
     INetworkProvider networkProvider,
     IEndPointInformation endPointInformation,
     IConnectionDispatcher dispatcher)
 {
     _listener            = new Listener(logger, options, networkProvider);
     _logger              = logger;
     _endPointInformation = endPointInformation;
     _dispatcher          = dispatcher;
 }
Ejemplo n.º 13
0
 public MessageDispatchingService(
     IHermesHost hermesHost,
     IConnectionDispatcher connectionDispatcher,
     IMessageDispatcher messageDispatcher,
     IOptions <HermesHostingSettings> options,
     ILogger <MessageDispatchingService> logger)
 {
     _hermesHost           = hermesHost;
     _connectionDispatcher = connectionDispatcher;
     _messageDispatcher    = messageDispatcher;
     _hostingSettings      = options.Value;
     _logger = logger;
 }
Ejemplo n.º 14
0
            public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher handler)
            {
                var connections = new InMemoryConnection[_connectionsPerEndPoint];

                for (var i = 0; i < _connectionsPerEndPoint; i++)
                {
                    connections[i] = new InMemoryConnection();
                }

                _connections.Add(endPointInformation, connections);

                return(new InMemoryTransport(handler, connections));
            }
Ejemplo n.º 15
0
        public SocketTransport(IEndPointInformation endPointInformation,
                               IConnectionDispatcher dispatcher,
                               int listenBacklog,
                               int ioQueueCount,
                               MemoryPool <byte> pool = null,
                               ILogger logger         = null)
        {
            if (endPointInformation == null)
            {
                throw new ArgumentNullException(nameof(endPointInformation));
            }
            if (endPointInformation.Type != ListenType.IPEndPoint)
            {
                throw new InvalidOperationException(nameof(endPointInformation.IPEndPoint));
            }
            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }
            if (listenBacklog < 0)
            {
                throw new InvalidOperationException(nameof(listenBacklog));
            }

            _endPointInformation = endPointInformation;
            _dispatcher          = dispatcher;
            _backlog             = listenBacklog;
            _pool   = pool;
            _logger = logger ?? NullLoggerFactory.Instance.CreateLogger("NetGear.Core.SocketTransport");
            _trace  = new TraceDebugger(_logger);

            if (ioQueueCount > 0)
            {
                _numSchedulers  = ioQueueCount;
                _cachedPipeOpts = new PipeOptionsPair[_numSchedulers];

                for (var i = 0; i < _numSchedulers; i++)
                {
                    _cachedPipeOpts[i] = new PipeOptionsPair(
                        GetSendPipeOptions(_pool, new IOQueue()),
                        GetReceivePipeOptions(_pool, new IOQueue()));
                }
            }
            else
            {
                _numSchedulers  = 1;
                _cachedPipeOpts = new PipeOptionsPair[] { new PipeOptionsPair(
                                                              GetSendPipeOptions(_pool, new IOQueue()),
                                                              GetReceivePipeOptions(_pool, new IOQueue())) };
            }
        }
            public unsafe ThreadContext(TransportThread transportThread)
            {
                _transportThread      = transportThread;
                _connectionDispatcher = transportThread.ConnectionDispatcher;
                _sockets              = new Dictionary <int, TSocket>();
                _logger               = _transportThread.LoggerFactory.CreateLogger($"{nameof(RedHat)}.{nameof(TransportThread)}.{_transportThread.ThreadId}");
                _acceptSockets        = new List <TSocket>();
                _transportOptions     = transportThread.TransportOptions;
                _scheduledSendAdding  = new List <ScheduledSend>(1024);
                _scheduledSendRunning = new List <ScheduledSend>(1024);
                _epollState           = EPollBlocked;
                if (_transportOptions.AioReceive | _transportOptions.AioSend)
                {
                    _aioEventsMemory     = AllocMemory(sizeof(io_event) * EventBufferLength);
                    _aioCbsMemory        = AllocMemory(sizeof(iocb) * EventBufferLength);
                    _aioCbsTableMemory   = AllocMemory(IntPtr.Size * EventBufferLength);
                    _ioVectorTableMemory = AllocMemory(SizeOf.iovec * IoVectorsPerAioSocket * EventBufferLength);
                    for (int i = 0; i < EventBufferLength; i++)
                    {
                        AioCbsTable[i] = &AioCbs[i];
                    }
                    if (_transportOptions.AioSend)
                    {
                        _aioSendBuffers = new ReadOnlySequence <byte> [EventBufferLength];
                    }
                }
                int maxMemoryHandleCount = TSocket.MaxIOVectorReceiveLength;

                if (_transportOptions.AioReceive || _transportOptions.AioSend)
                {
                    maxMemoryHandleCount = Math.Max(maxMemoryHandleCount, EventBufferLength);
                }
                if (_transportOptions.DeferSend)
                {
                    maxMemoryHandleCount = Math.Max(maxMemoryHandleCount, TSocket.MaxIOVectorSendLength);
                }
                MemoryHandles = new MemoryHandle[maxMemoryHandleCount];

                // These members need to be Disposed
                _epoll     = EPoll.Create();
                _epollFd   = _epoll.DangerousGetHandle().ToInt32();
                MemoryPool = CreateMemoryPool();
                _pipeEnds  = PipeEnd.CreatePair(blocking: false);
                if (_aioEventsMemory != IntPtr.Zero)
                {
                    aio_context_t ctx;
                    AioInterop.IoSetup(EventBufferLength, &ctx).ThrowOnError();
                    _aioContext = ctx;
                }
            }
Ejemplo n.º 17
0
 public TransportThread(IPEndPoint endPoint, IConnectionDispatcher connectionDispatcher, LinuxTransportOptions options, AcceptThread acceptThread, int threadId, int cpuId, ILoggerFactory loggerFactory)
 {
     if (connectionDispatcher == null)
     {
         throw new ArgumentNullException(nameof(connectionDispatcher));
     }
     ConnectionDispatcher = connectionDispatcher;
     ThreadId             = threadId;
     CpuId            = cpuId;
     EndPoint         = endPoint;
     TransportOptions = options;
     AcceptThread     = acceptThread;
     LoggerFactory    = loggerFactory;
 }
Ejemplo n.º 18
0
    public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
    {
        if (endPointInformation == null)
        {
            throw new ArgumentNullException(nameof(endPointInformation));
        }

        if (endPointInformation.Type != ListenType.IPEndPoint)
        {
            throw new ArgumentException("OnlyIPEndPointsSupported");
        }

        if (dispatcher == null)
        {
            throw new ArgumentNullException(nameof(dispatcher));
        }

        return(new KestrelStackTransport(this.Server._NullCheck(), endPointInformation, dispatcher, AppLifeTime, Options.IOQueueCount, Trace));
    }
Ejemplo n.º 19
0
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
        {
            if (endPointInformation == null)
            {
                throw new ArgumentNullException(nameof(endPointInformation));
            }

            if (endPointInformation.Type != ListenType.IPEndPoint)
            {
                throw new ArgumentException(SocketsStrings.OnlyIPEndPointsSupported, nameof(endPointInformation));
            }

            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }

            return(new SocketTransport(endPointInformation, dispatcher, _appLifetime, _options.IOQueueCount, _trace));
        }
Ejemplo n.º 20
0
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
        {
            if (endPointInformation == null)
            {
                throw new ArgumentNullException(nameof(endPointInformation));
            }

            if (endPointInformation.Type != ListenType.IPEndPoint)
            {
                throw new ArgumentException("Only IP endpoint is supported", nameof(endPointInformation));
            }

            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }

            return(new QuicTransport(endPointInformation, dispatcher, _appLifetime, _options.IOQueueCount, _logger));
        }
Ejemplo n.º 21
0
#pragma warning disable PUB0001 // Pubternal type in public API
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
#pragma warning restore PUB0001 // Pubternal type in public API
        {
            if (endPointInformation == null)
            {
                throw new ArgumentNullException(nameof(endPointInformation));
            }

            if (endPointInformation.Type != ListenType.IPEndPoint)
            {
                throw new ArgumentException("Only ListenType.IPEndPoint is supported by the Socket Transport. https://go.microsoft.com/fwlink/?linkid=874850", nameof(endPointInformation));
            }

            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }

            return(new SocketTransport(endPointInformation, dispatcher, _appLifetime, _options.IOQueueCount, _trace, _options.MemoryPoolFactory()));
        }
Ejemplo n.º 22
0
#pragma warning disable PUB0001 // Pubternal type in public API
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionDispatcher dispatcher)
#pragma warning restore PUB0001 // Pubternal type in public API
        {
            if (endPointInformation == null)
            {
                throw new ArgumentNullException(nameof(endPointInformation));
            }

            if (endPointInformation.Type != ListenType.IPEndPoint)
            {
                throw new ArgumentException(SocketsStrings.OnlyIPEndPointsSupported, nameof(endPointInformation));
            }

            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }

            return(new SocketTransport(endPointInformation, dispatcher, _appLifetime, _options.IOQueueCount, _trace, _options.MemoryPoolFactory()));
        }
Ejemplo n.º 23
0
        public async Task StartAsync(IConnectionDispatcher dispatcher)
        {
            Exception sendError = null;

            try
            {
                dispatcher.OnConnection(this);

                // Spawn send and receive logic
                Task             receiveTask = DoReceive();
                Task <Exception> sendTask    = DoSend();

                // If the sending task completes then close the receive
                // We don't need to do this in the other direction because the kestrel
                // will trigger the output closing once the input is complete.
                if (await Task.WhenAny(receiveTask, sendTask) == sendTask)
                {
                    // Tell the reader it's being aborted
                    _socket.Dispose();
                }

                // Now wait for both to complete
                await receiveTask;
                sendError = await sendTask;

                // Dispose the socket(should noop if already called)
                _socket.Dispose();
                _receiver.Dispose();
                _sender.Dispose();
                ThreadPool.QueueUserWorkItem(state => ((QuicConnection)state).CancelConnectionClosedToken(), this);
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, $"Unexpected exception in {nameof(QuicConnection)}.{nameof(StartAsync)}.");
            }
            finally
            {
                // Complete the output after disposing the socket
                Output.Complete(sendError);
            }
        }
Ejemplo n.º 24
0
    public KestrelStackTransport(
        KestrelServerWithStack server,
        IEndPointInformation endPointInformation,
        IConnectionDispatcher dispatcher,
        IApplicationLifetime applicationLifetime,
        int ioQueueCount,
        ISocketsTrace trace)
    {
        Debug.Assert(endPointInformation != null);
        Debug.Assert(endPointInformation.Type == ListenType.IPEndPoint);
        Debug.Assert(dispatcher != null);
        Debug.Assert(applicationLifetime != null);
        Debug.Assert(trace != null);

        EndPointInformation = endPointInformation;
        Dispatcher          = dispatcher;
        AppLifetime         = applicationLifetime;
        Trace = trace;

        this.Server = server;
    }
Ejemplo n.º 25
0
        public IServer Build()
        {
            IConnectionDispatcher dispatcher = null;

            if (_maxThreadsNumber != null)
            {
                dispatcher = new ThreadSetAcceptorDispatcher(new ThreadPoolThreadSet(_maxThreadsNumber.Value),
                                                             _newTransportHandler, _transportHandlerFactory);
            }
            else
            {
                dispatcher = new SingleThreadAcceptorDispatcher(_newTransportHandler, _transportHandlerFactory);
            }


            foreach (var acceptor in _acceptors)
            {
                dispatcher.RegisterAcceptor(acceptor);
            }

            return(new SimpleServer(dispatcher, _acceptors));
        }
Ejemplo n.º 26
0
        public async Task StartAsync(IConnectionDispatcher connectionDispatcher)
        {
            try
            {
                connectionDispatcher.OnConnection(this);

                // Spawn send and receive logic
                var receiveTask = DoReceive();
                var sendTask    = DoSend();

                // Now wait for both to complete
                await receiveTask;
                await sendTask;

                _receiver.Dispose();
                _sender.Dispose();
                ThreadPool.QueueUserWorkItem(state => ((SocketConnection)state).CancelConnectionClosedToken(), this);
            }
            catch (Exception ex)
            {
                _trace.LogError(0, ex, $"Unexpected exception in {nameof(SocketConnection)}.{nameof(StartAsync)}.");
            }
        }
Ejemplo n.º 27
0
        internal SocketTransport(
            IEndPointInformation endPointInformation,
            IConnectionDispatcher dispatcher,
            IHostApplicationLifetime applicationLifetime,
            int ioQueueCount,
            ISocketsTrace trace,
            MemoryPool <byte> memoryPool)
        {
            Debug.Assert(endPointInformation != null);
            Debug.Assert(endPointInformation.Type == ListenType.IPEndPoint);
            Debug.Assert(dispatcher != null);
            Debug.Assert(applicationLifetime != null);
            Debug.Assert(trace != null);

            _endPointInformation = endPointInformation;
            _dispatcher          = dispatcher;
            _appLifetime         = applicationLifetime;
            _trace      = trace;
            _memoryPool = memoryPool;

            if (ioQueueCount > 0)
            {
                _numSchedulers = ioQueueCount;
                _schedulers    = new IOQueue[_numSchedulers];

                for (var i = 0; i < _numSchedulers; i++)
                {
                    _schedulers[i] = new IOQueue();
                }
            }
            else
            {
                var directScheduler = new PipeScheduler[] { PipeScheduler.ThreadPool };
                _numSchedulers = directScheduler.Length;
                _schedulers    = directScheduler;
            }
        }
Ejemplo n.º 28
0
 public HermesHost(IConnectionDispatcher messageDispatcher, ILogger <HermesHost> logger)
 {
     _logger            = logger;
     _messageDispatcher = messageDispatcher;
 }
Ejemplo n.º 29
0
 public InMemoryTransport(IConnectionDispatcher dispatcher, IReadOnlyList <InMemoryConnection> connections)
 {
     _dispatcher  = dispatcher;
     _connections = connections;
 }
Ejemplo n.º 30
0
 public TcpConnection(Ethernet ethHeader, IPv4 ipHeader, Network.Header.Tcp tcpHeader, TTransmitter packetTransmitter, IConnectionDispatcher connectionDispatcher)
     : this(ethHeader, ipHeader, tcpHeader, packetTransmitter, PipeScheduler.ThreadPool, PipeScheduler.ThreadPool, MemoryPool <byte> .Shared, connectionDispatcher)
 {
 }