Class MoongateTCPClient
Represents a connected TCP client with async send/receive loops, middleware processing, lifecycle events, and recent byte history.
public sealed class MoongateTCPClient : IAsyncDisposable, IDisposable
- Inheritance
-
MoongateTCPClient
- Implements
- Inherited Members
Constructors
MoongateTCPClient(Socket, IEnumerable<INetMiddleware>?, int, int)
Creates a client wrapper for an accepted socket.
public MoongateTCPClient(Socket socket, IEnumerable<INetMiddleware>? middlewares = null, int receiveBufferSize = 8192, int historyBufferCapacity = 65536)
Parameters
socketSocketConnected socket.
middlewaresIEnumerable<INetMiddleware>Optional middleware list.
receiveBufferSizeintReceive chunk size in bytes.
historyBufferCapacityintMax number of received bytes to keep in history.
Properties
AvailableBytes
Gets the number of bytes currently available in the receive circular buffer.
public int AvailableBytes { get; }
Property Value
IsConnected
True when the underlying socket is connected and client not closed.
public bool IsConnected { get; }
Property Value
IsReceiveBufferFull
Gets whether the receive circular buffer is full.
public bool IsReceiveBufferFull { get; }
Property Value
ReceiveBufferSize
Receives payload chunk size in bytes.
public int ReceiveBufferSize { get; }
Property Value
RemoteEndPoint
Client remote endpoint, when connected.
public EndPoint? RemoteEndPoint { get; }
Property Value
SessionId
Unique session identifier for this client connection.
public long SessionId { get; }
Property Value
Methods
AddMiddleware(INetMiddleware)
Adds a middleware component to this client pipeline.
public MoongateTCPClient AddMiddleware(INetMiddleware middleware)
Parameters
middlewareINetMiddlewareMiddleware to register.
Returns
- MoongateTCPClient
The current client instance.
CloseAsync()
Closes the client connection and raises disconnect event once.
public Task CloseAsync()
Returns
ConnectAsync(IPEndPoint, IEnumerable<INetMiddleware>?, CancellationToken)
Creates an outbound client and connects to the specified endpoint.
public static Task<MoongateTCPClient> ConnectAsync(IPEndPoint endPoint, IEnumerable<INetMiddleware>? middlewares = null, CancellationToken cancellationToken = default)
Parameters
endPointIPEndPointEndpoint to connect to.
middlewaresIEnumerable<INetMiddleware>Optional middleware list.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<MoongateTCPClient>
A connected MoongateTCPClient instance.
ConsumeBytes(int)
Consumes bytes from the front of the receive circular buffer.
public int ConsumeBytes(int count)
Parameters
countintRequested number of bytes to consume.
Returns
- int
The number of bytes actually consumed.
ContainsMiddleware<TMiddleware>()
Checks whether this client pipeline contains at least one middleware instance of the specified type.
public bool ContainsMiddleware<TMiddleware>() where TMiddleware : INetMiddleware
Returns
- bool
truewhen at least one instance is registered; otherwisefalse.
Type Parameters
TMiddlewareMiddleware type to check.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
GetRecentReceivedBytes()
Returns a snapshot of recent received bytes from the circular history buffer.
public byte[] GetRecentReceivedBytes()
Returns
- byte[]
PeekData(int)
Peeks at data in the receive circular buffer without consuming it.
public byte[] PeekData(int count = 0)
Parameters
countintNumber of bytes to peek. Use 0 to read all available bytes.
Returns
- byte[]
Array containing the requested bytes.
RemoveMiddleware<TMiddleware>()
Removes all middleware components of the specified type from this client pipeline.
public bool RemoveMiddleware<TMiddleware>() where TMiddleware : INetMiddleware
Returns
- bool
truewhen at least one middleware instance was removed; otherwisefalse.
Type Parameters
TMiddlewareMiddleware type to remove.
SendAsync(ReadOnlyMemory<byte>, CancellationToken)
Sends a payload to the connected socket.
public Task SendAsync(ReadOnlyMemory<byte> payload, CancellationToken cancellationToken)
Parameters
payloadReadOnlyMemory<byte>Payload bytes to send.
cancellationTokenCancellationTokenCancellation token.
Returns
StartAsync(CancellationToken)
Starts the receive loop and raises connect event.
public Task StartAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenExternal cancellation token.
Returns
Events
OnConnected
Raised when the client is fully connected and receive loop starts.
public event EventHandler<MoongateTCPClientEventArgs>? OnConnected
Event Type
OnDataReceived
Raised when data is received (after middleware pipeline).
public event EventHandler<MoongateTCPDataReceivedEventArgs>? OnDataReceived
Event Type
OnDisconnected
Raised when the client is disconnected.
public event EventHandler<MoongateTCPClientEventArgs>? OnDisconnected
Event Type
OnException
Raised when receive/send loops throw an exception.
public event EventHandler<MoongateTCPExceptionEventArgs>? OnException