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
listenEndPointIPEndPointThe address and port to listen on. Port 0 lets the system choose one.
backlogintThe maximum number of pending connections the listener queues.
maxConnectionsintThe maximum number of connections served at once; a connection beyond it is accepted and closed at once.
loggerILoggerThe 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
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis 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
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis zero or negative.
Properties
ActiveConnectionCount
Gets the number of client connections being served now.
public int ActiveConnectionCount { get; }
Property Value
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis zero or negative.
IsStarted
Gets a value indicating whether the server is listening.
public bool IsStarted { get; }
Property Value
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis 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
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis zero or negative.
Methods
Dispose()
Stops the server and releases its listener. The server cannot start again.
public void Dispose()
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis zero or negative.
Dispose(bool)
Stops the server and releases its listener.
protected virtual void Dispose(bool disposing)
Parameters
disposingbooltrue when called from Dispose(); false when called from a finalizer, in which case nothing is released.
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis zero or negative.
HandleClientAsync(TcpClient, CancellationToken)
Serves one client connection.
protected abstract Task HandleClientAsync(TcpClient client, CancellationToken cancellationToken)
Parameters
clientTcpClientThe accepted connection.
cancellationTokenCancellationTokenThe 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
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis 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
cancellationTokenCancellationTokenThe 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
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis 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
clientTcpClientThe client connection.
Returns
- IPEndPoint
The remote endpoint, or null.
Exceptions
- ArgumentNullException
listenEndPointis null.- ArgumentOutOfRangeException
backlogormaxConnectionsis zero or negative.