Skip to content

fix: Address packet overflow issues in the UTP adapter #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions com.unity.netcode.adapter.utp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Changelog
All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Changed

- Removed 'Maximum Packet Size' configuration field in the inspector. This would cause confusion since the maximum packet size is in effect always the MTU (1400 bytes on most platforms).

### Fixed

- Fixed packet overflow errors when sending payloads too close to the MTU (was mostly visible when using Relay).

## [1.0.0-pre.3] - 2021-10-22

#### Added
Expand Down
29 changes: 5 additions & 24 deletions com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Unity.Netcode
/// </summary>
public interface INetworkStreamDriverConstructor
{
void CreateDriver(UnityTransport transport, out NetworkDriver driver, out NetworkPipeline unreliableSequencedPipeline, out NetworkPipeline reliableSequencedPipeline, out NetworkPipeline reliableSequencedFragmentedPipeline);
void CreateDriver(UnityTransport transport, out NetworkDriver driver, out NetworkPipeline unreliableSequencedPipeline, out NetworkPipeline reliableSequencedFragmentedPipeline);
}

public static class ErrorUtilities
Expand Down Expand Up @@ -78,7 +78,6 @@ private enum State
}

public const int InitialBatchQueueSize = 6 * 1024;
public const int InitialMaxPacketSize = NetworkParameterConstants.MTU;

private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData()
{ Address = "127.0.0.1", Port = 7777 };
Expand All @@ -91,9 +90,6 @@ private enum State
[Tooltip("Which protocol should be selected Relay/Non-Relay")]
[SerializeField] private ProtocolType m_ProtocolType;

[Tooltip("Maximum size in bytes for a given packet")]
[SerializeField] private int m_MaximumPacketSize = InitialMaxPacketSize;

[Tooltip("The maximum amount of packets that can be in the send/recv queues")]
[SerializeField] private int m_MaxPacketQueueSize = 128;

Expand Down Expand Up @@ -146,7 +142,6 @@ public static implicit operator ConnectionAddressData(NetworkEndPoint d) =>
private ulong m_ServerClientId;

private NetworkPipeline m_UnreliableSequencedPipeline;
private NetworkPipeline m_ReliableSequencedPipeline;
private NetworkPipeline m_ReliableSequencedFragmentedPipeline;

public override ulong ServerClientId => m_ServerClientId;
Expand Down Expand Up @@ -199,7 +194,7 @@ public SimulatorUtility.Parameters ClientSimulatorParameters

private void InitDriver()
{
DriverConstructor.CreateDriver(this, out m_Driver, out m_UnreliableSequencedPipeline, out m_ReliableSequencedPipeline, out m_ReliableSequencedFragmentedPipeline);
DriverConstructor.CreateDriver(this, out m_Driver, out m_UnreliableSequencedPipeline, out m_ReliableSequencedFragmentedPipeline);
}

private void DisposeDriver()
Expand All @@ -222,15 +217,7 @@ private NetworkPipeline SelectSendPipeline(NetworkDelivery delivery, int size)

case NetworkDelivery.Reliable:
case NetworkDelivery.ReliableSequenced:
return m_ReliableSequencedPipeline;

case NetworkDelivery.ReliableFragmentedSequenced:
// No need to send on the fragmented pipeline if data is smaller than MTU.
if (size < NetworkParameterConstants.MTU)
{
return m_ReliableSequencedPipeline;
}

return m_ReliableSequencedFragmentedPipeline;

default:
Expand Down Expand Up @@ -607,7 +594,6 @@ public override void Initialize()
{
Debug.Assert(sizeof(ulong) == UnsafeUtility.SizeOf<NetworkConnection>(),
"Netcode connection id size does not match UTP connection id size");
Debug.Assert(m_MaximumPacketSize > 5, "Message buffer size must be greater than 5");

m_NetworkParameters = new List<INetworkParameter>();

Expand All @@ -620,7 +606,7 @@ public override void Initialize()

m_NetworkParameters.Add(new BaselibNetworkParameter()
{
maximumPayloadSize = (uint)m_MaximumPacketSize,
maximumPayloadSize = 2000, // Default value in UTP.
receiveQueueCapacity = m_MaxPacketQueueSize,
sendQueueCapacity = m_MaxPacketQueueSize
});
Expand Down Expand Up @@ -743,7 +729,7 @@ private void SendBatchedMessageAndClearQueue(SendTarget sendTarget, SendQueue se
var payloadSize = sendQueue.Count + 1; // 1 extra byte to tell whether the message is batched or not
if (payloadSize > NetworkParameterConstants.MTU) // If this is bigger than MTU then force it to be sent via the FragmentedReliableSequencedPipeline
{
pipeline = SelectSendPipeline(NetworkDelivery.ReliableFragmentedSequenced, payloadSize);
pipeline = m_ReliableSequencedFragmentedPipeline;
}

var sendBuffer = sendQueue.GetData();
Expand Down Expand Up @@ -805,7 +791,7 @@ public override void Shutdown()
m_ServerClientId = 0;
}

public void CreateDriver(UnityTransport transport, out NetworkDriver driver, out NetworkPipeline unreliableSequencedPipeline, out NetworkPipeline reliableSequencedPipeline, out NetworkPipeline reliableSequencedFragmentedPipeline)
public void CreateDriver(UnityTransport transport, out NetworkDriver driver, out NetworkPipeline unreliableSequencedPipeline, out NetworkPipeline reliableSequencedFragmentedPipeline)
{
var netParams = new NetworkConfigParameter
{
Expand Down Expand Up @@ -839,10 +825,6 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, out
typeof(UnreliableSequencedPipelineStage),
typeof(SimulatorPipelineStage),
typeof(SimulatorPipelineStageInSend));
reliableSequencedPipeline = driver.CreatePipeline(
typeof(ReliableSequencedPipelineStage),
typeof(SimulatorPipelineStage),
typeof(SimulatorPipelineStageInSend));
reliableSequencedFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage),
typeof(ReliableSequencedPipelineStage),
Expand All @@ -853,7 +835,6 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, out
#endif
{
unreliableSequencedPipeline = driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage));
reliableSequencedPipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
reliableSequencedFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage), typeof(ReliableSequencedPipelineStage)
);
Expand Down