Skip to content

Commit f70eab1

Browse files
feat: Make INetworkStreamDriverConstructor useful (#3501)
This PR addresses MTTB-1331 by making it simpler to use a custom driver constructor without breaking things. `INetworkStreamDriverConstructor` is a mechanism that was added a long time ago that allows users to override the creation of the `NetworkDriver` that `UnityTransport` is using. As the name might hint at, it was pulled directly out of N4E and was never properly integrated into `UnityTransport`. For example, before this PR, a user implementing a custom driver constructor would need to recreate the settings and pipelines manually if they wanted to mimic the default behavior of `UnityTransport`, which required reading the code to figure out how we are settings things up. In the case of pipelines, it's not even possible to do that in all cases because one of the pipelines used when the multiplayer tools package is installed is internal. This PR addresses this by making both the default settings and default pipeline configurations available through the new `GetDefaultNetworkSettings` and `GetDefaultPipelineConfigurations` methods. A user can use these methods to reuse or extend the configuration `UnityTransport` uses by default. The (new and improved) documentation of the driver constructor interface provides an example of how to do so. As a bonus, this centralizes all the `NetworkSettings` in one place which makes the code a tiny bit more organized. ## Changelog - Added: Added methods `GetDefaultNetworkSettings` and `GetDefaultPipelineConfigurations` to `UnityTransport`. These can be used to retrieve the default settings and pipeline stages that are used by `UnityTransport`. This is useful when providing a custom driver constructor through `UnityTransport.s_DriverConstructor`, since it allows reusing or tuning the existing configuration instead of trying to recreate it. This means a transport with a custom driver can now easily benefit from most of the features of `UnityTransport`, like integration with the Network Simulator and Network Profiler from the multiplayer tools package. ## Testing and Documentation - No tests have been added. - Includes edits to existing public API documentation. ## Backport I don't plan on backporting this, unless the team feels strongly otherwise. --------- Co-authored-by: Noel Stephens <[email protected]>
1 parent a06c9d8 commit f70eab1

File tree

4 files changed

+290
-213
lines changed

4 files changed

+290
-213
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added methods `GetDefaultNetworkSettings` and `GetDefaultPipelineConfigurations` to `UnityTransport`. These can be used to retrieve the default settings and pipeline stages that are used by `UnityTransport`. This is useful when providing a custom driver constructor through `UnityTransport.s_DriverConstructor`, since it allows reusing or tuning the existing configuration instead of trying to recreate it. This means a transport with a custom driver can now easily benefit from most of the features of `UnityTransport`, like integration with the Network Simulator and Network Profiler from the multiplayer tools package. (#3501)
1314
- Added mappings between `ClientId` and `TransportId`. (#3516)
1415
- Added `NetworkPrefabInstanceHandlerWithData<T>`, a variant of `INetworkPrefabInstanceHandler` that provides access to custom instantiation data directly within the `Instantiate()` method. (#3430)
1516

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Unity.Networking.Transport;
2+
3+
namespace Unity.Netcode.Transports.UTP
4+
{
5+
/// <summary>
6+
/// <para>
7+
/// This interface allows one to override the creation of the <see cref="NetworkDriver"/> object
8+
/// that will be used under the hood by <see cref="UnityTransport"/>. This can be useful when
9+
/// implementing a custom <see cref="INetworkInterface"/> or to add custom pipeline stages to
10+
/// the default pipelines.
11+
/// </para>
12+
/// <para>
13+
/// To use a custom driver constructor, set <see cref="UnityTransport.s_DriverConstructor"/> to
14+
/// an instance of an implementation of this interface. This must be done before calling
15+
/// <see cref="UnityTransport.StartClient"/> or <see cref="UnityTransport.StartServer"/>.
16+
/// </para>
17+
/// </summary>
18+
/// <example>
19+
/// <para>
20+
/// This example implements a custom driver constructor that uses the IPC network interface from
21+
/// the Unity Transport package. This network interface is used for intra-process communications
22+
/// which can be useful for implementing a single-player version of a game. Since the example is
23+
/// also preserving all the default settings and pipelines, you'd also benefit from all the
24+
/// existing features of the transport, like integration with the Network Profiler.
25+
/// </para>
26+
/// <code>
27+
/// public class IPCDriverConstructor : INetworkStreamDriverConstructor
28+
/// {
29+
/// public void CreateDriver(
30+
/// UnityTransport transport,
31+
/// out NetworkDriver driver,
32+
/// out NetworkPipeline unreliableFragmentedPipeline,
33+
/// out NetworkPipeline unreliableSequencedFragmentedPipeline,
34+
/// out NetworkPipeline reliableSequencedPipeline)
35+
/// {
36+
/// var settings = transport.GetDefaultNetworkSettings();
37+
/// driver = NetworkDriver.Create(new IPCNetworkInterface(), settings);
38+
///
39+
/// transport.GetDefaultPipelineConfigurations(
40+
/// out var unreliableFragmentedPipelineStages,
41+
/// out var unreliableSequencedFragmentedPipelineStages,
42+
/// out var reliableSequencedPipelineStages);
43+
///
44+
/// unreliableFragmentedPipeline = driver.CreatePipeline(unreliableFragmentedPipelineStages);
45+
/// unreliableSequencedFragmentedPipeline = driver.CreatePipeline(unreliableSequencedFragmentedPipelineStages);
46+
/// reliableSequencedPipeline = driver.CreatePipeline(reliableSequencedPipelineStages);
47+
/// }
48+
/// }
49+
/// </code>
50+
/// </example>
51+
public interface INetworkStreamDriverConstructor
52+
{
53+
/// <summary>
54+
/// Creates the <see cref="NetworkDriver"/> instance to be used by the transport.
55+
/// </summary>
56+
/// <param name="transport">The transport for which the driver is created.</param>
57+
/// <param name="driver">The newly-created <see cref="NetworkDriver"/>.</param>
58+
/// <param name="unreliableFragmentedPipeline">
59+
/// The driver's pipeline on which to send unreliable traffic. This pipeline must also
60+
/// support fragmentation (payloads larger than the MTU).
61+
/// </param>
62+
/// <param name="unreliableSequencedFragmentedPipeline">
63+
/// The driver's pipeline on which to send unreliable but sequenced traffic. Traffic sent
64+
/// on this pipeline must be delivered in the right order, although packet loss is okay.
65+
/// This pipeline must also support fragmentation (payloads larger than the MTU).
66+
/// </param>
67+
/// <param name="reliableSequencedPipeline">
68+
/// The driver's pipeline on which to send reliable traffic. This pipeline must ensure that
69+
/// all of its traffic is delivered, and in the correct order too. There is no need for that
70+
/// pipeline to support fragmentation (<see cref="UnityTransport"/> will handle that).
71+
/// </param>
72+
void CreateDriver(
73+
UnityTransport transport,
74+
out NetworkDriver driver,
75+
out NetworkPipeline unreliableFragmentedPipeline,
76+
out NetworkPipeline unreliableSequencedFragmentedPipeline,
77+
out NetworkPipeline reliableSequencedPipeline);
78+
}
79+
}

com.unity.netcode.gameobjects/Runtime/Transports/UTP/INetworkStreamDriverConstructor.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)