Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [1.4.0] - 2023-06-28

### New features
* Added a new `GetUnsafeReadOnlyPtr` method to `DataStreamReader`. This is meant as an escape hatch for use cases that require passing stream readers outside of a job (this not possible with `DataStreamReader` directly since the safety system will not allow it). As the name implies, this throws away many safety guarantees and is thus not recommended to use unless absolutely required.

### Changes
* `NetworkDriver.GetRelayConnectionStatus` will now return the new enum value `RelayConnectionStatus.NotUsingRelay` when called on a `NetworkDriver` that has not been configured to use Unity Relay. The previous behavior was to throw an exception.
* It is now possible to configure the maximum message size that the transport will send through a new `maxMessageSize` parameter in `NetworkSettings.WithNetworkConfigParameters`. This is useful for environments where network equipment mishandles larger packets (like some mobile networks or VPNs). The value excludes IP and UDP headers, but includes headers added by the transport itself (e.g. reliability headers). The default value is 1400. Note that it is recommended that both client and server be configured to use the same value.
  • Loading branch information
Unity Technologies committed Jun 28, 2023
1 parent a9cfd65 commit 57436fa
Show file tree
Hide file tree
Showing 28 changed files with 447 additions and 185 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change log

## [1.4.0] - 2023-06-28

### New features
* Added a new `GetUnsafeReadOnlyPtr` method to `DataStreamReader`. This is meant as an escape hatch for use cases that require passing stream readers outside of a job (this not possible with `DataStreamReader` directly since the safety system will not allow it). As the name implies, this throws away many safety guarantees and is thus not recommended to use unless absolutely required.

### Changes
* `NetworkDriver.GetRelayConnectionStatus` will now return the new enum value `RelayConnectionStatus.NotUsingRelay` when called on a `NetworkDriver` that has not been configured to use Unity Relay. The previous behavior was to throw an exception.
* It is now possible to configure the maximum message size that the transport will send through a new `maxMessageSize` parameter in `NetworkSettings.WithNetworkConfigParameters`. This is useful for environments where network equipment mishandles larger packets (like some mobile networks or VPNs). The value excludes IP and UDP headers, but includes headers added by the transport itself (e.g. reliability headers). The default value is 1400. Note that it is recommended that both client and server be configured to use the same value.

## [1.3.4] - 2023-04-27

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion Documentation~/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The full documentation for the package can be found on the [Unity multiplayer do

This version of `com.unity.transport` is compatible with the following Unity versions and platforms:

* 2020.1.2 and later.
* Unity 2021.3 and 2022.2 (and later 2022 versions).
* All platforms supported by Unity are supported, except WebGL.

## Document revision history
Expand Down
12 changes: 12 additions & 0 deletions Runtime/DataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,18 @@ public unsafe ushort ReadPackedFixedStringDelta(byte* data, int maxLength, byte*
return (ushort)length;
}

/// <summary>
/// Get a pointer to the stream's data. Note that the pointer always points at the
/// beginning of the data, no matter how much was read from the stream.
/// </summary>
/// <remarks>Performs a job safety check for read-only access.</remarks>
/// <returns>A pointer to the stream's data.</returns>
public unsafe void* GetUnsafeReadOnlyPtr()
{
CheckRead();
return m_bufferPtr;
}

[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckRead()
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/IPCManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal unsafe struct IPCData
{
[FieldOffset(0)] public int from;
[FieldOffset(4)] public int length;
[FieldOffset(8)] public fixed byte data[NetworkParameterConstants.MTU];
[FieldOffset(8)] public fixed byte data[NetworkParameterConstants.MaxPacketBufferSize];
}

private NativeMultiQueue<IPCData> m_IPCQueue;
Expand Down
2 changes: 1 addition & 1 deletion Runtime/IPCNetworkInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public unsafe void Execute()

while (true)
{
var size = NetworkParameterConstants.MTU;
var size = NetworkParameterConstants.MaxPacketBufferSize;
var ptr = receiver.AllocateMemory(ref size);
if (ptr == IntPtr.Zero)
return;
Expand Down
14 changes: 9 additions & 5 deletions Runtime/NetworkDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Unity.Networking.Transport
public unsafe struct QueuedSendMessage
{
/// <summary>Content of the message.</summary>
public fixed byte Data[NetworkParameterConstants.MTU];
public fixed byte Data[NetworkParameterConstants.MaxPacketBufferSize];
/// <summary>Destination endpoint for the message.</summary>
public NetworkInterfaceEndPoint Dest;
/// <summary>Length of the message's content.</summary>
Expand Down Expand Up @@ -54,6 +54,7 @@ public Concurrent ToConcurrent()
m_PipelineProcessor = m_PipelineProcessor.ToConcurrent(),
m_DefaultHeaderFlags = m_DefaultHeaderFlags,
m_ConcurrentParallelSendQueue = m_ParallelSendQueue.AsParallelWriter(),
m_MaxMessageSize = m_NetworkParams.config.maxMessageSize,
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_ThreadIndex = 0,
m_PendingBeginSend = m_PendingBeginSend
Expand All @@ -74,6 +75,7 @@ private Concurrent ToConcurrentSendOnly()
m_PipelineProcessor = m_PipelineProcessor.ToConcurrent(),
m_DefaultHeaderFlags = m_DefaultHeaderFlags,
m_ConcurrentParallelSendQueue = m_ParallelSendQueue.AsParallelWriter(),
m_MaxMessageSize = m_NetworkParams.config.maxMessageSize,
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_ThreadIndex = 0,
m_PendingBeginSend = m_PendingBeginSend
Expand Down Expand Up @@ -202,14 +204,14 @@ public unsafe int BeginSend(NetworkPipeline pipe, NetworkConnection id,
// fragmented pipelines have an explicity capacity, and we want users to be able to
// rely on this configured value.
var payloadCapacity = pipelinePayloadCapacity == 0
? NetworkParameterConstants.MTU - protocolOverhead - pipelineHeader
? m_MaxMessageSize - protocolOverhead - pipelineHeader
: pipelinePayloadCapacity;

// Total capacity is the full size of the buffer we'll allocate. Without an explicit
// pipeline payload capacity, this is the MTU. Otherwise it's the pipeline payload
// capacity plus whatever overhead we need to transmit the packet.
var totalCapacity = pipelinePayloadCapacity == 0
? NetworkParameterConstants.MTU
? m_MaxMessageSize
: pipelinePayloadCapacity + protocolOverhead + pipelineHeader;

// Check if we can accomodate the user's required payload size.
Expand All @@ -227,7 +229,7 @@ public unsafe int BeginSend(NetworkPipeline pipe, NetworkConnection id,
}

var sendHandle = default(NetworkInterfaceSendHandle);
if (totalCapacity > NetworkParameterConstants.MTU)
if (totalCapacity > m_MaxMessageSize)
{
sendHandle.data = (IntPtr)UnsafeUtility.Malloc(totalCapacity, 8, Allocator.Temp);
sendHandle.capacity = totalCapacity;
Expand Down Expand Up @@ -370,7 +372,7 @@ internal unsafe int CompleteSend(NetworkConnection sendConnection, NetworkInterf
{
var ret = 0;
NetworkInterfaceSendHandle originalHandle = sendHandle;
if ((ret = m_NetworkSendInterface.BeginSendMessage.Ptr.Invoke(out sendHandle, m_NetworkSendInterface.UserData, NetworkParameterConstants.MTU)) != 0)
if ((ret = m_NetworkSendInterface.BeginSendMessage.Ptr.Invoke(out sendHandle, m_NetworkSendInterface.UserData, NetworkParameterConstants.MaxPacketBufferSize)) != 0)
{
return ret;
}
Expand Down Expand Up @@ -417,6 +419,8 @@ public NetworkConnection.State GetConnectionState(NetworkConnection id)
internal UdpCHeader.HeaderFlags m_DefaultHeaderFlags;
internal NativeQueue<QueuedSendMessage>.ParallelWriter m_ConcurrentParallelSendQueue;

internal int m_MaxMessageSize;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
[NativeSetThreadIndex] internal int m_ThreadIndex;
[NativeDisableParallelForRestriction] internal NativeArray<int> m_PendingBeginSend;
Expand Down
Loading

0 comments on commit 57436fa

Please sign in to comment.