Table of Contents

Class ServerAdapter

Namespace
BSGameLib.Networking
Assembly
BSGameLib.Networking.dll

The server side of the network. It accepts client connections, sends packets to them and runs the handlers for the packets they send.

public class ServerAdapter : IServerAdapter, INetworkAdapter
Inheritance
ServerAdapter
Implements
Inherited Members

Constructors

ServerAdapter(ILogger, IDataPacketManager, NetworkAdapterConfiguration)

Creates a server adapter that listens with the QUIC settings in config.

public ServerAdapter(ILogger logger, IDataPacketManager packetManager, NetworkAdapterConfiguration config)

Parameters

logger ILogger
packetManager IDataPacketManager
config NetworkAdapterConfiguration

Properties

HandlerRunner

What each received packet's handler is run through, or null to run it where it arrives.

public Action<Type, Action> HandlerRunner { get; set; }

Property Value

Action<Type, Action>

Remarks

Packets arrive on the transport's threads, one handler per connection at once, beside whatever thread runs the game's own loop. A game whose world is to be touched by one thing at a time sets this to something that takes its lock, runs the handler and lets go; one that wants handlers on its loop's thread sets it to something that queues them. It is given the packet's type, or null for one this side does not know, so that it can say which packet it is running: for timing them, say.

PacketManager

The packet manager that serializes and handles this adapter's packets.

public IDataPacketManager PacketManager { get; }

Property Value

IDataPacketManager

Methods

Disconnect(INetConnection, string)

Closes the connection to client. reason is sent to the client.

public void Disconnect(INetConnection client, string reason = "")

Parameters

client INetConnection
reason string

GetStatistics()

Traffic since the server started, and each connected client's, with its round-trip time.

public NetworkStatistics GetStatistics()

Returns

NetworkStatistics

SendReliableThenDisconnect<T>(INetConnection, T, string)

Delivers one last packet and only then closes the connection -- a kick reason, a shutdown notice, anything the peer needs in order to explain itself to its user.

public void SendReliableThenDisconnect<T>(INetConnection client, T packet, string reason = "") where T : IDataPacket

Parameters

client INetConnection
packet T
reason string

Type Parameters

T

Remarks

Needed because SendReliable<T>(INetConnection, T) is fire-and-forget: sending and then calling Disconnect(INetConnection, string) races, and the close normally wins, so the packet never reaches the wire. This waits for the write, with a timeout so an unresponsive peer cannot stall the disconnect.

SendReliable<T>(INetConnection, T)

Sends packet to client reliably and in order.

public void SendReliable<T>(INetConnection client, T packet) where T : IDataPacket

Parameters

client INetConnection
packet T

Type Parameters

T

Remarks

The size is checked before the packet is queued, so a packet too large for a frame throws here.

SendUnreliableSequenced<T>(INetConnection, T)

Sends packet to client without delivery guarantees. A packet that arrives after a newer one is dropped.

public void SendUnreliableSequenced<T>(INetConnection client, T packet) where T : IDataPacket

Parameters

client INetConnection
packet T

Type Parameters

T

SendUnreliable<T>(INetConnection, T)

Sends packet to client without delivery or order guarantees.

public void SendUnreliable<T>(INetConnection client, T packet) where T : IDataPacket

Parameters

client INetConnection
packet T

Type Parameters

T

Start()

Starts listening for clients on the configured port.

public void Start()

Exceptions

InvalidOperationException

The configuration has no server certificate.

Stop()

Stops listening and closes every connection.

public void Stop()

Events

Connected

Raised when a client connects.

public event EventHandler<ClientEventArgs> Connected

Event Type

EventHandler<ClientEventArgs>

DataReceived

Raised for each frame received from any client, before its packet is handled.

public event EventHandler<DataReceivedEventArgs> DataReceived

Event Type

EventHandler<DataReceivedEventArgs>

Disconnected

Raised when a client's connection closes.

public event EventHandler<ClientEventArgs> Disconnected

Event Type

EventHandler<ClientEventArgs>

ForciblyDisconnected

Raised when the server closes a connection itself, through Disconnect(INetConnection, string) or SendReliableThenDisconnect<T>(INetConnection, T, string), just before it does.

public event EventHandler<ClientEventArgs> ForciblyDisconnected

Event Type

EventHandler<ClientEventArgs>