Noble Connect
Add relays and punchthrough to Mirror or UNet
NobleConnect.Peer Class Reference

The base class for servers and clients More...

Inherits IDisposable.

Classes

class  BridgeState
 

Public Member Functions

 Peer (IceConfig config)
 Create a new peer with the provided configuration More...
 
void PrepareToConnect ()
 Perform initial preparation. You can call this early to save time when actually connecting. More...
 
void FinalizeConnection (IPEndPoint connectedEndPoint)
 Clean up unused routes after connection is established More...
 
IPAddress MapToIPv4 (IPAddress address)
 
void EndSession (IPEndPoint connectedEndPoint)
 Completely end a session. More...
 
void InitializeHosting (int hostPort, Action< string, ushort > onPrepared=null)
 Tell NobleConnect what port you are hosting on and get a relay address for clients to use to connect. More...
 
void InitializeClient (IPEndPoint hostEndPoint, Action< IPEndPoint, IPEndPoint > onReadyToConnect)
 Use the hostEndPoint to establish a route to the host. When the route is established onReadyConnect will be called to tell the application where to connect. More...
 
void CleanUpEverything ()
 Revokes all permissions, channels, and allocations, destroys all bridges, and disposes of the ice controller More...
 
void ConfigureStunController (Stun.Controller stunController)
 Configures a stun controller to pass data through to this peer More...
 
virtual void Update ()
 Keep everything running More...
 
void SetLocalEndPoint (ushort port)
 Set the local end point that the application will connect from / listen on More...
 
void HandleIncomingData (AttributeData data, IPEndPoint source, bool fromRelay)
 Pass the incoming data from the Turn server to the application More...
 
void HandleOutgoingData (IAsyncResult result)
 Pass data from the application to the Turn server More...
 
virtual void SendOutgoing (Bridge bridge, int numBytesToSend, PaddedBuffer buffer)
 Servers receive outgoing data on a socket and pass it to the Turn server as a Channel message. More...
 
Bridge CreateBridge (CandidatePair selectedPair)
 Create a bridge between the provided remote address and the localEndPoint More...
 
void DestroyBridge (Bridge bridge)
 Destroy a bridge and clean up More...
 
void RevokePermissionsChannelsAndAllocation ()
 Clean up all permissions, channels, and allocations More...
 
void DestroyBridges ()
 Destroy all bridges and close their associated sockets More...
 
void Dispose ()
 Dispose of all Bridges. Close all sockets. Stop all timers More...
 

Public Attributes

ConnectionType latestConnectionType = ConnectionType.NONE
 The type of connection that was established. DIRECT, PUNCHTHROUGH, RELAY, or NONE More...
 

Protected Member Functions

virtual void Dispose (bool disposing)
 Dispose of all Bridges. Close all sockets. Stop all timers. More...
 

Protected Attributes

Dictionary< IPEndPoint, BridgebridgeByPeer = new Dictionary<IPEndPoint, Bridge>()
 This is used to look up which local socket to send out on depending on the peer it was received from More...
 
Dictionary< IPEndPoint, BridgebridgeByClientEndPoint = new Dictionary<IPEndPoint, Bridge>()
 This is used to look up a Bridge by the local endpoint that the application connects to. More...
 
IPEndPoint applicationEndPoint
 The local IPv4 end point that the application can receive from and send to. More...
 
IPEndPoint applicationEndPointIPv6
 The local IPv6 end point that the application can receive from and send to. More...
 

Properties

IPEndPoint RelayEndPoint [get]
 This is the address that clients should connect to. It is assigned by the relay server. More...
 

Detailed Description

The base class for servers and clients

The main purpose of this class is to create socket Bridges between the client / server applications and the Turn.Controller.

The application can not connect directly to the Turn server because the Turn.Controller is already connected using the local port that would be required to connect from.

To get around this Bridges are created between the application and the Turn.Controller.

When an application wishes to connect to a host over a relay it instead connects to a local bridge socket. This bridge socket receives outgoing data from the application and sends it to the relay over the Stun.Controller socket. Likewise, incoming application data on the Stun.Controller socket is sent to the application over the bridge socket.

For servers a bridge is created for each peer that wishes to connect. This way even though all of the data comes in from the Turn server, the application receives it as coming from a different local address for each client. In this way the relaying process is completely transparent to the application.

Constructor & Destructor Documentation

◆ Peer()

NobleConnect.Peer.Peer ( IceConfig  config)

Create a new peer with the provided configuration

Parameters
config

Member Function Documentation

◆ CleanUpEverything()

void NobleConnect.Peer.CleanUpEverything ( )

Revokes all permissions, channels, and allocations, destroys all bridges, and disposes of the ice controller

◆ ConfigureStunController()

void NobleConnect.Peer.ConfigureStunController ( Stun.Controller  stunController)

Configures a stun controller to pass data through to this peer

Parameters
stunController

◆ CreateBridge()

Bridge NobleConnect.Peer.CreateBridge ( CandidatePair  selectedPair)

Create a bridge between the provided remote address and the localEndPoint

Parameters
remoteIP
remotePort
Returns

◆ DestroyBridge()

void NobleConnect.Peer.DestroyBridge ( Bridge  bridge)

Destroy a bridge and clean up

Parameters
bridge

◆ DestroyBridges()

void NobleConnect.Peer.DestroyBridges ( )

Destroy all bridges and close their associated sockets

◆ Dispose() [1/2]

void NobleConnect.Peer.Dispose ( )

Dispose of all Bridges. Close all sockets. Stop all timers

◆ Dispose() [2/2]

virtual void NobleConnect.Peer.Dispose ( bool  disposing)
protectedvirtual

Dispose of all Bridges. Close all sockets. Stop all timers.

Parameters
disposing

◆ EndSession()

void NobleConnect.Peer.EndSession ( IPEndPoint  connectedEndPoint)

Completely end a session.

The permissions are revoked that were used to send the sip invite and answer Channels are revoked for all candidate pairs Binding refreshes are cancelled for all candidate pairs

Parameters
connectedEndPoint

◆ FinalizeConnection()

void NobleConnect.Peer.FinalizeConnection ( IPEndPoint  connectedEndPoint)

Clean up unused routes after connection is established

The permissions are revoked that were used to send the sip invite and answer Channels are revoked for all unused candidate pairs Binding refreshes are cancelled for all unused candidate pairs

Parameters
connectedEndPoint

◆ HandleIncomingData()

void NobleConnect.Peer.HandleIncomingData ( AttributeData  data,
IPEndPoint  source,
bool  fromRelay 
)

Pass the incoming data from the Turn server to the application

When the Turn controller receives relayed data it arrives here. The data is then passed over the bridge to the application.

Parameters
data
source

◆ HandleOutgoingData()

void NobleConnect.Peer.HandleOutgoingData ( IAsyncResult  result)

Pass data from the application to the Turn server

This is called whenever the bridge socket receives data from the application. The data is immediately sent out to the relay server.

Parameters
result

◆ InitializeClient()

void NobleConnect.Peer.InitializeClient ( IPEndPoint  hostEndPoint,
Action< IPEndPoint, IPEndPoint >  onReadyToConnect 
)

Use the hostEndPoint to establish a route to the host. When the route is established onReadyConnect will be called to tell the application where to connect.

Parameters
hostEndPointThe host's relay address.
onReadyToConnectAn action to be called when a route is established

◆ InitializeHosting()

void NobleConnect.Peer.InitializeHosting ( int  hostPort,
Action< string, ushort >  onPrepared = null 
)

Tell NobleConnect what port you are hosting on and get a relay address for clients to use to connect.

Parameters
hostPortThe port the application is hosting on
onPreparedAn action to be called when the relay address has been received

◆ MapToIPv4()

IPAddress NobleConnect.Peer.MapToIPv4 ( IPAddress  address)

◆ PrepareToConnect()

void NobleConnect.Peer.PrepareToConnect ( )

Perform initial preparation. You can call this early to save time when actually connecting.

Gathers local and reflexive addresses and allocates relays.

◆ RevokePermissionsChannelsAndAllocation()

void NobleConnect.Peer.RevokePermissionsChannelsAndAllocation ( )

Clean up all permissions, channels, and allocations

◆ SendOutgoing()

virtual void NobleConnect.Peer.SendOutgoing ( Bridge  bridge,
int  numBytesToSend,
PaddedBuffer  buffer 
)
virtual

Servers receive outgoing data on a socket and pass it to the Turn server as a Channel message.

Parameters
bridge
numBytesToSend

◆ SetLocalEndPoint()

void NobleConnect.Peer.SetLocalEndPoint ( ushort  port)

Set the local end point that the application will connect from / listen on

Bridges will send data coming from this end point out to the relay. Bridges will send data received from the relay to this end point.

Parameters
port

◆ Update()

virtual void NobleConnect.Peer.Update ( )
virtual

Keep everything running

Member Data Documentation

◆ applicationEndPoint

IPEndPoint NobleConnect.Peer.applicationEndPoint
protected

The local IPv4 end point that the application can receive from and send to.

This is set by hosts when starting hosting since the application port is usually known at that point. This is set by clients when they send the first outgoing package and the bridge socket receives it. Each bridge holds a socket that communicates with this end point. Data received from this end point will be sent out to the relay. Data received from the relay will be sent to this end point.

◆ applicationEndPointIPv6

IPEndPoint NobleConnect.Peer.applicationEndPointIPv6
protected

The local IPv6 end point that the application can receive from and send to.

This is set by hosts when starting hosting since the application port is usually known at that point. This is set by clients when they send the first outgoing package and the bridge socket receives it. Each bridge holds a socket that communicates with this end point. Data received from this end point will be sent out to the relay. Data received from the relay will be sent to this end point if it exists.

◆ bridgeByClientEndPoint

Dictionary<IPEndPoint, Bridge> NobleConnect.Peer.bridgeByClientEndPoint = new Dictionary<IPEndPoint, Bridge>()
protected

This is used to look up a Bridge by the local endpoint that the application connects to.

This is useful for destroying a Bridge when the application signals that it is done with it.

◆ bridgeByPeer

Dictionary<IPEndPoint, Bridge> NobleConnect.Peer.bridgeByPeer = new Dictionary<IPEndPoint, Bridge>()
protected

This is used to look up which local socket to send out on depending on the peer it was received from

◆ latestConnectionType

ConnectionType NobleConnect.Peer.latestConnectionType = ConnectionType.NONE

The type of connection that was established. DIRECT, PUNCHTHROUGH, RELAY, or NONE

Property Documentation

◆ RelayEndPoint

IPEndPoint NobleConnect.Peer.RelayEndPoint
get

This is the address that clients should connect to. It is assigned by the relay server.

Note that this is not the host's actual IP address, but one assigned to the host by the relay server. When clients connect to this address, Noble Connect will find the best possible connection and use it. This means that the client may actually end up connecting to an address on the local network, or an address on the router, or an address on the relay. But you don't need to worry about any of that, it is all handled for you internally.