Skip to content

Commit e92a883

Browse files
Move the interface and document it better
1 parent f764fab commit e92a883

File tree

4 files changed

+94
-33
lines changed

4 files changed

+94
-33
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ 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.
14+
1315
### Fixed
1416

1517
- Fixed issue where the initial client synchronization pre-serialization process was not excluding spawned `NetworkObject` instances that already had pending visibility for the client being synchronized. (#3488)
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.

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@
2626

2727
namespace Unity.Netcode.Transports.UTP
2828
{
29-
/// <summary>
30-
/// Provides an interface that overrides the ability to create your own drivers and pipelines
31-
/// </summary>
32-
public interface INetworkStreamDriverConstructor
33-
{
34-
/// <summary>
35-
/// Creates the internal NetworkDriver
36-
/// </summary>
37-
/// <param name="transport">The owner transport</param>
38-
/// <param name="driver">The driver</param>
39-
/// <param name="unreliableFragmentedPipeline">The UnreliableFragmented NetworkPipeline</param>
40-
/// <param name="unreliableSequencedFragmentedPipeline">The UnreliableSequencedFragmented NetworkPipeline</param>
41-
/// <param name="reliableSequencedPipeline">The ReliableSequenced NetworkPipeline</param>
42-
void CreateDriver(
43-
UnityTransport transport,
44-
out NetworkDriver driver,
45-
out NetworkPipeline unreliableFragmentedPipeline,
46-
out NetworkPipeline unreliableSequencedFragmentedPipeline,
47-
out NetworkPipeline reliableSequencedPipeline);
48-
}
49-
5029
/// <summary>
5130
/// The Netcode for GameObjects NetworkTransport for UnityTransport.
5231
/// Note: This is highly recommended to use over UNet.
@@ -92,15 +71,19 @@ public enum ProtocolType
9271
private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData { Address = "127.0.0.1", Port = 7777, ServerListenAddress = string.Empty };
9372

9473
#pragma warning disable IDE1006 // Naming Styles
95-
9674
/// <summary>
97-
/// The global <see cref="INetworkStreamDriverConstructor"/> implementation
75+
/// An instance of a <see cref="INetworkStreamDriverConstructor"/> implementation. If null,
76+
/// the default driver constructor will be used. Setting it to a non-null value allows
77+
/// controlling how the internal <see cref="NetworkDriver"/> instance is created. See the
78+
/// interface's documentation for details.
9879
/// </summary>
9980
public static INetworkStreamDriverConstructor s_DriverConstructor;
10081
#pragma warning restore IDE1006 // Naming Styles
10182

10283
/// <summary>
103-
/// Returns either the global <see cref="INetworkStreamDriverConstructor"/> implementation or the current <see cref="UnityTransport"/> instance
84+
/// If a custom <see cref="INetworkStreamDriverConstructor"/> implementation is in use (see
85+
/// <see cref="s_DriverConstructor"/>), this returns it. Otherwise it returns the current
86+
/// <see cref="UnityTransport"/> instance, which acts as the default constructor.
10487
/// </summary>
10588
public INetworkStreamDriverConstructor DriverConstructor => s_DriverConstructor ?? this;
10689

@@ -1685,15 +1668,10 @@ public void SetClientSecrets(string serverCommonName, string caCertificate = nul
16851668
m_ClientCaCertificate = caCertificate;
16861669
}
16871670

1688-
/// <summary>
1689-
/// Creates the internal NetworkDriver
1690-
/// </summary>
1691-
/// <param name="transport">The owner transport</param>
1692-
/// <param name="driver">The driver</param>
1693-
/// <param name="unreliableFragmentedPipeline">The UnreliableFragmented NetworkPipeline</param>
1694-
/// <param name="unreliableSequencedFragmentedPipeline">The UnreliableSequencedFragmented NetworkPipeline</param>
1695-
/// <param name="reliableSequencedPipeline">The ReliableSequenced NetworkPipeline</param>
1696-
public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
1671+
/// <inheritdoc cref="INetworkStreamDriverConstructor.CreateDriver"/>
1672+
public void CreateDriver(
1673+
UnityTransport transport,
1674+
out NetworkDriver driver,
16971675
out NetworkPipeline unreliableFragmentedPipeline,
16981676
out NetworkPipeline unreliableSequencedFragmentedPipeline,
16991677
out NetworkPipeline reliableSequencedPipeline)

0 commit comments

Comments
 (0)