Bring your own transport
What this page is for
Section titled “What this page is for”Everything the add-on sends goes through one small layer, the transport. Netcode for GameObjects (NGO) is the transport that ships with the add-on, and it is the supported one. If your game already runs on another netcode, you can write a transport for it. This page shows the interface you implement, the check that tests it, and the template you start from.
The Multiplayer add-on compiles on Unity 6000.0 and 6000.6. Two players were tested on 6000.0 (an editor host and a development player); not yet on 6000.6, over Relay or with IL2CPP.The add-on ships one ready-made transport, for NGO. This page covers the one you write yourself.
When you come here
Section titled “When you come here”- Your game uses Mirror, FishNet, Photon, Steam networking or your own sockets, and you do not want NGO.
- You want to test the session without a network (the add-on’s own tests use an in-memory pair).
The interface
Section titled “The interface”The interface ITransport lives in the namespace MrW.Intrigues.Multiplayer. This is its body, copied from ITransport.cs with the comments left out. The last three members have default bodies, so you override them only when you need them.
public interface ITransport{ bool IsServer { get; } int MaxPacketBytes { get; } bool Start(); bool Send(int connection, byte[] data, int offset, int count); bool Poll(out TransportEvent next); void EndFrame(); void Disconnect(int connection, string reason); void Stop(); string AddressOf(int connection) => null; string IdentityOf(int connection) => string.Empty; bool ApprovedIdentity(int connection, out string identity) { identity = null; return false; }}| Member | What it must do |
|---|---|
IsServer |
True on the host’s transport, false on a client’s. It never changes. |
MaxPacketBytes |
The largest packet Send accepts and delivers whole, at least 287 bytes (a 256-byte slice and its header). |
Start() |
Host: start accepting clients. Client: start connecting. False when it cannot start. Called once. An exception counts as false. |
Send(connection, data, offset, count) |
Queue one packet, copying the bytes. False, and nothing sent, when the connection is not open, the range lies outside data, or the packet is larger than MaxPacketBytes. |
Poll(out next) |
The next event, oldest first. False when none waits. Called until it returns false, once per frame. |
EndFrame() |
Called at the end of every frame, after the frame’s sends. Release what you batch. When MultiplayerSession.Quitting is true, send at once. |
Disconnect(connection, reason) |
Close one connection. Carry reason to the other side if you can. Packets sent before it should still leave first when your netcode can manage it. |
Stop() |
Close everything and stop. Safe to call twice. No packet is sent or reported afterwards. |
AddressOf(connection) |
The remote address without the port, read from your netcode. Null when it cannot tell. Asked only when the Connected event carried no address. |
IdentityOf(connection) |
What your netcode itself knows about who is there, for example ip:1.2.3.4 or steam:7656.... Empty when it cannot tell. |
ApprovedIdentity(connection, out identity) |
True when your own approval step already asked the host’s SeatPolicy.Identify. Leave the default unless you run such a step. |
sources: Runtime/Transport/ITransport.cs, the interface at the end of the file.
The event
Section titled “The event”Poll hands back a TransportEvent. Its constructor is TransportEvent(TransportEventKind kind, int connection, byte[] data, string reason, string address = "").
| Member | Meaning |
|---|---|
Kind |
Connected (1), Data (2) or Disconnected (3). |
Connection |
On the host the client’s number. On a client always TransportConnections.Host, which is 0. |
Data |
The packet of a Data event, a copy the receiver owns. Null otherwise. |
Reason |
The other side’s words of a Disconnected event. Null or empty when none came. |
Address |
Where a Connected client connects from, without the port. Never null. It must come from your netcode, never from anything the client sent. |
Connected is reported once per connection, before any Data. Disconnected is reported at most once, and nothing of that connection follows it.
What the session needs
Section titled “What the session needs”These come from the interface comments and the README.
- One reliable, ordered channel per connection. Every packet arrives whole, once, in the order sent. The session does not resend or reorder. A lost or swapped packet breaks that player’s connection, and the player then joins again.
- Packets up to
MaxPacketBytes. The session cuts its messages into slices that fit and paces them itself. Your transport needs no queue beyond one frame. - Connection numbers. On the host, 1 and up, stable for the life of the connection and never given out again while the transport runs. On a client the host is always 0.
- Main thread only. The session never expects a callback. If your netcode receives on another thread, put what arrives in a queue that
Pollempties. Your transport must not callMultiplayerSession, not even fromPoll. - Copies.
Sendcopies the bytes. EveryDataevent is an array the session may keep. - No exceptions. Report problems with return values and events.
- Keep-alives are optional. The session sends its own, and ends a silent connection after
TimeoutSeconds.
Keep-alives, timeouts, passwords, the seat keys and every check of what a player sends stay the add-on’s job, whatever the transport.
What is guaranteed when your transport throws
Section titled “What is guaranteed when your transport throws”The transport is trusted code. A transport that throws or misbehaves can end or break the session. The add-on guarantees this much: if Start throws, the start fails as if it returned false. If your transport throws while the session stops, ends, kicks, bans or refuses a player, the add-on logs one warning and finishes anyway. Your game is then back to single player, stopping twice does no harm, and you can host or join again at once.
Limits you accept
Section titled “Limits you accept”The add-on does not guard against these.
- An exception in a normal frame’s
Poll,SendorEndFramecosts that frame’s work. - Mixed-up or reused connection numbers can slip one connection’s acknowledgements or keep-alives into another player’s stream.
- An address that is always the same, or always empty, shares or turns off the limits per address and the bans by address for all players.
ApprovedIdentityreturning true with an empty or out-of-date identity skipsSeatPolicy.Identifyand the bans by identity for that join.- A transport that calls
MultiplayerSessionfrom insidePollworks today, but that is outside the contract and may stop working. - Raising
SliceBytesafterUseTransportundoes the fit, and the first large message then breaks the connection. - A
MaxPacketBytesabove 1 MB is checked byTransportConformanceonly up to 1 MB.
The template
Section titled “The template”Samples/TransportTemplate/TransportTemplate.cs is a commented starting point. Its assembly definition references MrW.Intrigues and MrW.Intrigues.Multiplayer and has no define constraint, so it does not need NGO. The file’s own header says it compiles without NGO and does nothing until you replace the stubs. This page did not build it.
It holds three classes in the namespace MrW.Intrigues.Multiplayer.Samples.
| Class | What it is |
|---|---|
TemplateTransport |
An ITransport with stubs. Constructor TemplateTransport(bool isServer, int maxPacketBytes = 16 * 1024). Start throws NotImplementedException on purpose. It has a queue for events and four helpers for your netcode’s callbacks. |
TemplateSession |
StartHost(HostSettings) and StartClient(ClientSettings): the few lines that hand a transport to the settings and start the session. |
TemplateTransportCheck |
A component, Intrigues / Multiplayer / Transport Check (Template), that runs TransportConformance in Play mode and logs the report. |
The helpers on TemplateTransport are the ones your netcode’s callbacks call:
| Helper | Call it from |
|---|---|
int Connected(object handle, string address) |
The host’s “client connected” callback. Gives the connection a new number and reports it. |
void ConnectedToHost(object handle) |
A client’s “connected to the host” callback. Reports connection 0, once. |
void Data(int connection, byte[] data, int offset, int count) |
Your receive callback. Makes a copy for the session. |
void Disconnected(int connection, string reason) |
Your “disconnected” callback. |
The template file also carries notes on how Mirror, FishNet, Photon and Steam networking map onto these members. The file’s author marks them as taken from each netcode’s documentation “as we know it” and says to check your transport’s docs for the version you use. Read them in the file, and check them there.
The steps
Section titled “The steps”- Copy
TransportTemplate.csinto your game and rename the classes. Replace each stub with calls into your netcode. - Check your transport with
TransportConformancebefore you play over it (next section). - Start the session with it. Example, copied from
TemplateSessionin the template, with the stub transport as it is:
public static bool StartHost(HostSettings settings){ if (settings == null) return false; var transport = new TemplateTransport(true); if (!settings.UseTransport(transport)) return false; if (MultiplayerSession.StartHost(settings)) return true; transport.Stop(); return false;}StartClient is the same with new TemplateTransport(false) and ClientSettings. Call UseTransport last, after you set SliceBytes and the other settings. It fits SliceBytes to your transport’s packet size, and a larger SliceBytes set afterwards undoes the fit. See the Settings reference.
Check the transport
Section titled “Check the transport”TransportConformance connects a host side and a client side of your transport and tries what the session relies on. It talks only through ITransport and needs no Intrigue Manager. It makes three clients one after the other, and each must reach the host. Use a free port, and do not run a session at the same time.
| Member | What it does |
|---|---|
TransportConformance(Func<ITransport> createHost, Func<ITransport> createClient, int framesToWait = 600) |
Each function makes a new, unstarted transport. framesToWait is how many frames it waits for something to arrive before a check fails. |
bool Step() |
Runs one frame. Call it once per frame. False once the check is done. |
Report |
The checks so far. Complete once Done is true. |
static TransportReport Run(createHost, createClient, Action betweenFrames = null, int framesToWait = 600, int maxFrames = 100000) |
Runs a whole check without engine frames, for an in-memory pair in a unit test. betweenFrames runs after each frame, where you advance your fake network. |
In Play mode, put the template’s Transport Check (Template) component on an object in an empty scene, after you changed its two functions to make your own transports. Or call Step() once per frame yourself from Update or a coroutine.
The report has one line per check: PASS, FAIL with what went wrong, or NOTE for something worth knowing that is not a failure, such as “no address”. Report.Passed is true when every check ran and none failed. The checks, by the names in the report:
| Group | Checks |
|---|---|
| Setup | Roles and packet size; Start; Connect; Remote address |
| Packets | Small, large and many packets in order, each both ways; Packets at the size limit; Garbage is refused without exceptions |
| Disconnects | Disconnect from the host; Packets sent before a disconnect arrive first; Nothing is delivered after a disconnect; Reconnect; Disconnect from the client |
| End | Stop; Connection numbers; Event order |
The untouched template fails at Start, on purpose. TransportConformance.LargestTestedPacket is 1 MB. A transport that claims more is tested up to that size, and the report notes the claim.
What this does not give you
Section titled “What this does not give you”- No matchmaking, lobby or Relay. That is your netcode’s or your platform’s job, as with NGO.
- The project checks in the editor and the Netcode Session component are for NGO only.
- The README says the add-on was tested over NGO and over its own in-memory transport. A transport you write is tested by you, with the conformance check as the start.
Mistakes and messages
Section titled “Mistakes and messages”| What happens | Why, and what to do |
|---|---|
UseTransport returns false |
The transport is null, its IsServer does not match the settings, or MaxPacketBytes is under 287 bytes (256 plus the header). |
| The first large message breaks the connection | SliceBytes was raised after UseTransport. Call UseTransport last. |
| A player joins again and again | A packet was lost, reordered or refused by Send. Your channel must be reliable and ordered, and Send must keep a packet rather than drop it and return true. |
| Bans by address do nothing | Your transport reports no address, or always the same one. Give the real remote address, or use SeatPolicy.Identify. |
How it connects
Section titled “How it connects”- Settings reference for
SliceBytes,UseTransportand the NGO options. - Security notes for identity, bans and what a relay changes.
- Quick start for the NGO path.
- Limits and what is not verified.