Skip to main content

Socket

A class in Root.Township, Alta.Networking.

The game's network socket over a transport: holds all connections, pumps receiving and sending, and sends messages to many players at once. The live one is Socket.Current.

Constructors​

MemberWhat it is
Socket(ITransport transport, int port, bool isServer, ConnectionChannel[] channels, ConnectionChannel defaultChannel)Creates a Socket from transport, port, isServer, channels and defaultChannel.

Fields​

5 private and internal fields
MemberWhat it is
private Dictionary<int, Connection> connectionMapConnections by transport connection ID.
private List<Connection> connectionsAll live connections.
private int connectionStartRotating start index so each frame's send begins with a different connection.
private static Logger loggerLogger for this class.
private IPacketReceiver packetReceiverServer or client packet receiver; null until the socket is serving or connected.

Properties​

MemberWhat it is
int ConnectionCount { get; }Number of open connections.
IEnumerable<Connection> Connections { get; }All open connections.
static ISocket Current { get; set; }The socket in use. Set when a socket is created, or to FakeSocket.Instance by SetFake.
ConnectionChannel DefaultChannel { get; set; }Channel new connections use by default.
bool IsServer { get; private set; }True if this socket was created as the server.
int OutgoingQueueSize { get; set; }Transport's outgoing queue size, refreshed on each UpdateSend.
ITransportSocket TransportSocket { get; private set; }The underlying transport server or client.
protected ConnectionChannel[] Channels { get; private set; }All channels set up for each connection.
protected ITransport Transport { get; private set; }The transport this socket runs on.

Events​

MemberWhat it is
event ConnectionEventHandler ConnectionCreatedRaised when a connection is added.
event ConnectionEventHandler ConnectionDestroyedRaised after a connection is removed and disposed.
static event Action EndOfReceivalStatic one-shot event raised after a receive pass. Handlers are cleared after it fires.
event Action<ISocket> SocketDestroyedRaised when the socket is disposed.

Methods​

MemberWhat it is
Connection CreateConnection(string ipAddress, int port)Client: connects to an address and port. Server: wraps an incoming transport connection. Returns null on failure.
Connection CreateConnection(ITransportConnection transportConnection, string ipAddress)Client: connects to an address and port. Server: wraps an incoming transport connection. Returns null on failure.
void DestroyConnection(Connection connection, bool isFlushing = false)Removes and disposes a connection, optionally flushing what's queued first.
void Dispose()Disposes every connection, terminates the transport and raises SocketDestroyed.
Connection GetConnection(int identifier)Finds a connection by its transport id, or null.
bool SendToAll(ConnectionChannel channel, MessageType type, SerializeConnectionMethod method)Serializes a message once and sends it to every connection. False if any send failed.
bool SendToPlayers(ConnectionChannel channel, MessageType type, SerializeConnectionMethod method, IEnumerable<IPlayer> players)Serializes a message once and sends it to each listed player's connection. False if any send failed.
static void SetFake()Points Current at the fake socket, for running without a network.
void UpdateReceive()Processes queued messages on each connection, receives new packets, then fires EndOfReceival.
void UpdateSend()Flushes each connection's packet managers (rotating which goes first each frame) and processes the transport's outgoing queue.
3 private and internal methods
MemberWhat it is
private void OnConnectionCreated(Connection connection)Raises ConnectionCreated.
private void OnConnectionDestroyed(Connection connection)Raises ConnectionDestroyed.
private void OnTransportSocketError(AltaNetworkError error)Server: counts each transport error by type in the NetworkManager error counters.