Table of Contents

Class MoongateTCPClient

Namespace
Moongate.Network.Client
Assembly
Moongate.Network.dll

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

socket Socket

Connected socket.

middlewares IEnumerable<INetMiddleware>

Optional middleware list.

receiveBufferSize int

Receive chunk size in bytes.

historyBufferCapacity int

Max 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

int

IsConnected

True when the underlying socket is connected and client not closed.

public bool IsConnected { get; }

Property Value

bool

IsReceiveBufferFull

Gets whether the receive circular buffer is full.

public bool IsReceiveBufferFull { get; }

Property Value

bool

ReceiveBufferSize

Receives payload chunk size in bytes.

public int ReceiveBufferSize { get; }

Property Value

int

RemoteEndPoint

Client remote endpoint, when connected.

public EndPoint? RemoteEndPoint { get; }

Property Value

EndPoint

SessionId

Unique session identifier for this client connection.

public long SessionId { get; }

Property Value

long

Methods

AddMiddleware(INetMiddleware)

Adds a middleware component to this client pipeline.

public MoongateTCPClient AddMiddleware(INetMiddleware middleware)

Parameters

middleware INetMiddleware

Middleware to register.

Returns

MoongateTCPClient

The current client instance.

CloseAsync()

Closes the client connection and raises disconnect event once.

public Task CloseAsync()

Returns

Task

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

endPoint IPEndPoint

Endpoint to connect to.

middlewares IEnumerable<INetMiddleware>

Optional middleware list.

cancellationToken CancellationToken

Cancellation 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

count int

Requested 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

true when at least one instance is registered; otherwise false.

Type Parameters

TMiddleware

Middleware 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

count int

Number 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

true when at least one middleware instance was removed; otherwise false.

Type Parameters

TMiddleware

Middleware type to remove.

SendAsync(ReadOnlyMemory<byte>, CancellationToken)

Sends a payload to the connected socket.

public Task SendAsync(ReadOnlyMemory<byte> payload, CancellationToken cancellationToken)

Parameters

payload ReadOnlyMemory<byte>

Payload bytes to send.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

StartAsync(CancellationToken)

Starts the receive loop and raises connect event.

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

External cancellation token.

Returns

Task

Events

OnConnected

Raised when the client is fully connected and receive loop starts.

public event EventHandler<MoongateTCPClientEventArgs>? OnConnected

Event Type

EventHandler<MoongateTCPClientEventArgs>

OnDataReceived

Raised when data is received (after middleware pipeline).

public event EventHandler<MoongateTCPDataReceivedEventArgs>? OnDataReceived

Event Type

EventHandler<MoongateTCPDataReceivedEventArgs>

OnDisconnected

Raised when the client is disconnected.

public event EventHandler<MoongateTCPClientEventArgs>? OnDisconnected

Event Type

EventHandler<MoongateTCPClientEventArgs>

OnException

Raised when receive/send loops throw an exception.

public event EventHandler<MoongateTCPExceptionEventArgs>? OnException

Event Type

EventHandler<MoongateTCPExceptionEventArgs>