Class NetworkHost
Manages a network socket and all network communication between peers.
Inheritance
Namespace: SuperNet.Unity.Core
Assembly: cs.temp.dll.dll
Syntax
public sealed class NetworkHost : MonoBehaviour, IHostListener, IPeerListener
Constructors
NetworkHost()
Declaration
public NetworkHost()
Fields
AutoConnect
Remote address to connect to on startup or empty to disable.
Declaration
public string AutoConnect
Field Value
| Type | Description |
|---|---|
| System.String |
AutoRegister
Automatically register all peers to the network.
Declaration
public bool AutoRegister
Field Value
| Type | Description |
|---|---|
| System.Boolean |
AutoStartup
Should the host start listening on startup.
Declaration
public bool AutoStartup
Field Value
| Type | Description |
|---|---|
| System.Boolean |
HostConfiguration
Host configuration values.
Declaration
public HostConfig HostConfiguration
Field Value
| Type | Description |
|---|---|
| HostConfig |
MaxConnections
The maximum number of concurrent network connections to support.
Declaration
public int MaxConnections
Field Value
| Type | Description |
|---|---|
| System.Int32 |
PeerConfiguration
Peer configuration values.
Declaration
public PeerConfig PeerConfiguration
Field Value
| Type | Description |
|---|---|
| PeerConfig |
PersistAcrossScenes
Do not destroy the host when loading a new Scene.
Declaration
public bool PersistAcrossScenes
Field Value
| Type | Description |
|---|---|
| System.Boolean |
Properties
Allocator
Host allocator or an empty allocator if not listening.
Declaration
public Allocator Allocator { get; }
Property Value
| Type | Description |
|---|---|
| Allocator |
HostEvents
Host events for this host.
Declaration
public HostEvents HostEvents { get; }
Property Value
| Type | Description |
|---|---|
| HostEvents |
Listening
True if host is active and listening.
Declaration
public bool Listening { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
PeerEvents
Peer events for all peers.
Declaration
public PeerEvents PeerEvents { get; }
Property Value
| Type | Description |
|---|---|
| PeerEvents |
Methods
Connect(IPEndPoint, IPeerListener, IWritable, Boolean)
Create a local peer and start connecting to an active remote host.
Declaration
public Peer Connect(IPEndPoint remote, IPeerListener listener = null, IWritable message = null, bool rethrow = false)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | Remote address to connect to. |
| IPeerListener | listener | Peer listener to use or null for none. |
| IWritable | message | Connect message to use. |
| System.Boolean | rethrow | Rethrow any exceptions on failure. |
Returns
| Type | Description |
|---|---|
| Peer | Local peer that attempts to connect or null on failure. |
Connect(String)
Create a local peer and start connecting to an active remote host.
Declaration
public void Connect(string address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | address | Address to connect to. |
Dispose()
Instantly dispose all resources held by this host and connected peers.
Declaration
public void Dispose()
GetBindAddress()
Get address this host is listening on or loopback if none.
This is usually 0.0.0.0 with the listen port.
Declaration
public IPEndPoint GetBindAddress()
Returns
| Type | Description |
|---|---|
| IPEndPoint | A valid address. |
GetHost()
Get netcode host or null if not listening.
Declaration
public Host GetHost()
Returns
| Type | Description |
|---|---|
| Host | Netcode host. |
GetLocalAddress()
Get LAN address for this host.
An example is 192.168.1.10 with the listen port.
Declaration
public IPEndPoint GetLocalAddress()
Returns
| Type | Description |
|---|---|
| IPEndPoint | Local address. |
GetLoopbackAddress()
Get loopback address for this host.
This is usually 127.0.0.1 with the listen port.
Declaration
public IPEndPoint GetLoopbackAddress()
Returns
| Type | Description |
|---|---|
| IPEndPoint | Loopback address. |
GetPeers()
Get all peers on this host.
Declaration
public IReadOnlyList<Peer> GetPeers()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IReadOnlyList<Peer> | Array of all peers on this host. |
SendAll(IMessage, Peer[])
Send a global message to all connected peers.
Declaration
public void SendAll(IMessage message, params Peer[] exclude)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessage | message | Message to send. |
| Peer[] | exclude | Peers to exclude. |
SendBroadcast(Int32, IWritable)
Send an unconnected message to all machines on the local network.
Declaration
public void SendBroadcast(int port, IWritable message)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | port | Network port to send to. |
| IWritable | message | Message to send. |
SendUnconnected(IPEndPoint, IWritable)
Send an unconnected message to a remote host.
Declaration
public void SendUnconnected(IPEndPoint remote, IWritable message)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | Remote address to send to. |
| IWritable | message | Message to send. |
SetListener(IPEndPoint, IPeerListener)
Set listener for a specific peer.
Declaration
public bool SetListener(IPEndPoint remote, IPeerListener listener)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | Peer remote address. |
| IPeerListener | listener | Listener to replace with. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if replaced, false if peer doesn't exist. |
Shutdown()
Gracefully disconnect all peers and perform a shutdown.
Declaration
public void Shutdown()
Startup(Boolean)
Start listening.
Declaration
public bool Startup(bool rethrow = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | rethrow | Rethrow any exceptions on failure. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True on success, false on failure. |
Explicit Interface Implementations
IHostListener.OnHostException(IPEndPoint, Exception)
Declaration
void IHostListener.OnHostException(IPEndPoint remote, Exception exception)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | |
| Exception | exception |
IHostListener.OnHostReceiveBroadcast(IPEndPoint, Reader)
Declaration
void IHostListener.OnHostReceiveBroadcast(IPEndPoint remote, Reader message)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | |
| Reader | message |
IHostListener.OnHostReceiveRequest(ConnectionRequest, Reader)
Declaration
void IHostListener.OnHostReceiveRequest(ConnectionRequest request, Reader message)
Parameters
| Type | Name | Description |
|---|---|---|
| ConnectionRequest | request | |
| Reader | message |
IHostListener.OnHostReceiveSocket(IPEndPoint, Byte[], Int32)
Declaration
void IHostListener.OnHostReceiveSocket(IPEndPoint remote, byte[] buffer, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | |
| System.Byte[] | buffer | |
| System.Int32 | length |
IHostListener.OnHostReceiveUnconnected(IPEndPoint, Reader)
Declaration
void IHostListener.OnHostReceiveUnconnected(IPEndPoint remote, Reader message)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | remote | |
| Reader | message |
IHostListener.OnHostShutdown()
Declaration
void IHostListener.OnHostShutdown()
IPeerListener.OnPeerConnect(Peer)
Declaration
void IPeerListener.OnPeerConnect(Peer peer)
Parameters
| Type | Name | Description |
|---|---|---|
| Peer | peer |
IPeerListener.OnPeerDisconnect(Peer, Reader, DisconnectReason, Exception)
Declaration
void IPeerListener.OnPeerDisconnect(Peer peer, Reader message, DisconnectReason reason, Exception exception)
Parameters
| Type | Name | Description |
|---|---|---|
| Peer | peer | |
| Reader | message | |
| DisconnectReason | reason | |
| Exception | exception |
IPeerListener.OnPeerException(Peer, Exception)
Declaration
void IPeerListener.OnPeerException(Peer peer, Exception exception)
Parameters
| Type | Name | Description |
|---|---|---|
| Peer | peer | |
| Exception | exception |
IPeerListener.OnPeerReceive(Peer, Reader, MessageReceived)
Declaration
void IPeerListener.OnPeerReceive(Peer peer, Reader reader, MessageReceived info)
Parameters
| Type | Name | Description |
|---|---|---|
| Peer | peer | |
| Reader | reader | |
| MessageReceived | info |
IPeerListener.OnPeerUpdateRTT(Peer, UInt16)
Declaration
void IPeerListener.OnPeerUpdateRTT(Peer peer, ushort rtt)
Parameters
| Type | Name | Description |
|---|---|---|
| Peer | peer | |
| System.UInt16 | rtt |