Beispiel #1
0
        private Connection(TransportSocket clientSocket, IPAddress endpointInterface, int bufferSize)
        {
            Id = Guid.NewGuid().ToString().Substring(0, 8);

            ClientSocket      = clientSocket;
            EndpointInterface = endpointInterface;
            BufferSize        = bufferSize;
        }
Beispiel #2
0
        public static Connection From(TransportSocket clientSocket, IPAddress endpointInterface, int bufferSize)
        {
            if (clientSocket == null)
            {
                throw new ArgumentException($"Parameter `{nameof(clientSocket)}` cannot be `null`.");
            }

            return(new Connection(clientSocket, endpointInterface, bufferSize));
        }
Beispiel #3
0
        public static async Task RunSequentialAsync(TransportSocket clientSocket, TransportSocket endpointSocket, int bufferSize, CancellationToken cancellationToken = default)
        {
            var pumpUp = Task.Run(async() =>
            {
                var bufferUp = MemoryPool <byte> .Shared.Rent(bufferSize).Memory;
                while (true)
                {
                    var readFromClient = await clientSocket.ReceiveAsync(bufferUp, cancellationToken);

                    if (readFromClient == 0)
                    {
                        try
                        {
                            if (endpointSocket.Connected)
                            {
                                endpointSocket.Disconnect(false);
                            }
                        } catch (SocketException) {}
                        return;
                    }

                    var data             = bufferUp.Slice(0, readFromClient);
                    var pushedToEndpoint = await endpointSocket.SendAsync(data, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                }
            });

            var pumpDown = Task.Run(async() =>
            {
                var bufferDown = MemoryPool <byte> .Shared.Rent(bufferSize).Memory;
                while (true)
                {
                    var readFromEndpoint = await endpointSocket.ReceiveAsync(bufferDown, cancellationToken);

                    if (readFromEndpoint == 0)
                    {
                        try
                        {
                            if (clientSocket.Connected)
                            {
                                clientSocket.Disconnect(false);
                            }
                        } catch (SocketException) {}
                        return;
                    }

                    var data           = bufferDown.Slice(0, readFromEndpoint);
                    var pushedToClient = await clientSocket.SendAsync(data, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                }
            });

            await Task.WhenAll(pumpUp, pumpDown);
        }