Table of Contents

Class TcpProxyServerBase

Namespace
VpnHood.Net.Proxies
Assembly
VpnHood.Net.Proxies.dll

Provides the listener, the connection limit and the lifetime of a TCP proxy server; a derived class serves each connection.

public abstract class TcpProxyServerBase : IDisposable
Inheritance
TcpProxyServerBase
Implements
Derived
Inherited Members

Remarks

The server accepts connections from Start() until Stop() or Dispose(), and passes each to HandleClientAsync(TcpClient, CancellationToken) on its own task. It can start again after it stops, but not after it is disposed. Start(), Stop() and Dispose() may be called from any thread.

Constructors

TcpProxyServerBase(IPEndPoint, int, int, ILogger?)

Provides the listener, the connection limit and the lifetime of a TCP proxy server; a derived class serves each connection.

protected TcpProxyServerBase(IPEndPoint listenEndPoint, int backlog, int maxConnections, ILogger? logger)

Parameters

listenEndPoint IPEndPoint

The address and port to listen on. Port 0 lets the system choose one.

backlog int

The maximum number of pending connections the listener queues.

maxConnections int

The maximum number of connections served at once; a connection beyond it is accepted and closed at once.

logger ILogger

The logger for the server's events, or null for none.

Remarks

The server accepts connections from Start() until Stop() or Dispose(), and passes each to HandleClientAsync(TcpClient, CancellationToken) on its own task. It can start again after it stops, but not after it is disposed. Start(), Stop() and Dispose() may be called from any thread.

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

Fields

Logger

The logger the server writes to; a logger that discards everything when none was given.

protected readonly ILogger Logger

Field Value

ILogger

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

Properties

ActiveConnectionCount

Gets the number of client connections being served now.

public int ActiveConnectionCount { get; }

Property Value

int

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

IsStarted

Gets a value indicating whether the server is listening.

public bool IsStarted { get; }

Property Value

bool

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

ListenerEndPoint

Gets the address and port the server listens on. Once the server has started, it carries the port the system chose when the configured port is 0.

public IPEndPoint ListenerEndPoint { get; }

Property Value

IPEndPoint

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

Methods

Dispose()

Stops the server and releases its listener. The server cannot start again.

public void Dispose()

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

Dispose(bool)

Stops the server and releases its listener.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true when called from Dispose(); false when called from a finalizer, in which case nothing is released.

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

HandleClientAsync(TcpClient, CancellationToken)

Serves one client connection.

protected abstract Task HandleClientAsync(TcpClient client, CancellationToken cancellationToken)

Parameters

client TcpClient

The accepted connection.

cancellationToken CancellationToken

The token that is canceled when the server stops. An exception that a callback registered on it throws is logged, and the stop goes on.

Returns

Task

A task that represents the asynchronous operation. It completes when the connection is done.

Remarks

It runs on its own task for each accepted connection. When the returned task completes, the base class disposes client, and it logs any exception the task throws.

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

RunAsync(CancellationToken)

Starts the server, keeps it running until cancellationToken is canceled, then stops it.

public Task RunAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The token that stops the server. With the default, which is never canceled, the task never completes, even after Stop() or Dispose().

Returns

Task

A task that represents the asynchronous operation. It completes when the server has stopped, and does not fault when the token is canceled.

Exceptions

ObjectDisposedException

The server is disposed.

SocketException

The listener cannot use its address and port, for example because another socket holds them.

Start()

Starts listening for connections. Calling it on a server that is listening does nothing.

public void Start()

Exceptions

ObjectDisposedException

The server is disposed.

SocketException

The listener cannot use its address and port, for example because another socket holds them.

Stop()

Stops listening and cancels the token of every connection being served, without waiting for them to end. Calling it on a server that is not listening does nothing.

public void Stop()

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.

TryGetRemoteEndPoint(TcpClient)

Gets a client's remote address and port, or null when they cannot be read, as on a connection that was already reset.

protected static IPEndPoint? TryGetRemoteEndPoint(TcpClient client)

Parameters

client TcpClient

The client connection.

Returns

IPEndPoint

The remote endpoint, or null.

Exceptions

ArgumentNullException

listenEndPoint is null.

ArgumentOutOfRangeException

backlog or maxConnections is zero or negative.