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.
- Listed in Networking and servers
- Inherits or implements
ISocket,IDisposable - Documented in Sockets and connections
Constructors
| Member | What 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
| Member | What it is |
|---|---|
private Dictionary<int, Connection> connectionMap | Connections by transport connection ID. |
private List<Connection> connections | All live connections. |
private int connectionStart | Rotating start index so each frame's send begins with a different connection. |
private static Logger logger | Logger for this class. |
private IPacketReceiver packetReceiver | Server or client packet receiver; null until the socket is serving or connected. |
Properties
| Member | What 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
| Member | What it is |
|---|---|
event ConnectionEventHandler ConnectionCreated | Raised when a connection is added. |
event ConnectionEventHandler ConnectionDestroyed | Raised after a connection is removed and disposed. |
static event Action EndOfReceival | Static one-shot event raised after a receive pass. Handlers are cleared after it fires. |
event Action<ISocket> SocketDestroyed | Raised when the socket is disposed. |
Methods
| Member | What 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
| Member | What 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. |