Send fragmented pipeline as Async Erorr

hi
in reliblePipeline worked;
in fragmentedPipeline no work.

my setting

var NetWorkSetting = new NetworkSettings();
NetWorkSetting.WithFragmentationStageParameters(1000000);
m_serverDrive = NetworkDriver.Create(NetWorkSetting);
_fragmentedPipeline = m_serverDrive.CreatePipeline(typeof(FragmentationPipelineStage), typeof(ReliableSequencedPipelineStage));

my code

public async void SendProfileComplate( NativeArray nb, NetworkConnection nc)
{
await Task.Run(() =>
{

if (m_serverDrive.BeginSend(_fragmentedPipeline, nc, out var senddata) == 0)
{
senddata.WriteInt(bn.Length);
senddata.WriteBytes(nb);
m_serverDrive.EndSend(senddata);
nb.Dispose();
bn = null;

}
});
}
erorr
ssertion failed on expression: ‘gCurrentManagedTempMem != NULL’
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Unity.Networking.Transport.NetworkDriver/Concurrent:BeginSend (Unity.Networking.Transport.NetworkPipeline,Unity.Networking.Transport.NetworkConnection,Unity.Networking.Transport.DataStreamWriter&,int) (at Library/PackageCache/com.unity.transport@1.3.4/Runtime/NetworkDriver.cs:232)
Unity.Networking.Transport.NetworkDriver:BeginSend (Unity.Networking.Transport.NetworkPipeline,Unity.Networking.Transport.NetworkConnection,Unity.Networking.Transport.DataStreamWriter&,int) (at Library/PackageCache/com.unity.transport@1.3.4/Runtime/NetworkDriver.cs:1222)
ServerCode:SendFragmentedDatatoClient (Netmsg_Min,Unity.Networking.Transport.NetworkConnection) (at Assets/Scripts/ServerCode.cs:2811)
Mongo/<>c__DisplayClass42_0:b__0 () (at Assets/Scripts/Mongo.cs:1158)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

erorr 2

ArgumentException: Could not allocate native memory. If this allocation was made from a managed thread outside of a job, you must use Allocator.Persistent or Allocator.TempJob.
Unity.Networking.Transport.NetworkDriver+Concurrent.BeginSend (Unity.Networking.Transport.NetworkPipeline pipe, Unity.Networking.Transport.NetworkConnection id, Unity.Networking.Transport.DataStreamWriter& writer, System.Int32 requiredPayloadSize) (at Library/PackageCache/com.unity.transport@1.3.4/Runtime/NetworkDriver.cs:232)
Unity.Networking.Transport.NetworkDriver.BeginSend (Unity.Networking.Transport.NetworkPipeline pipe, Unity.Networking.Transport.NetworkConnection id, Unity.Networking.Transport.DataStreamWriter& writer, System.Int32 requiredPayloadSize) (at Library/PackageCache/com.unity.transport@1.3.4/Runtime/NetworkDriver.cs:1222)
ServerCode.SendFragmentedDatatoClient (Netmsg_Min msg, Unity.Networking.Transport.NetworkConnection nc) (at Assets/Scripts/ServerCode.cs:2811)
Mongo+<>c__DisplayClass42_0.b__0 () (at Assets/Scripts/Mongo.cs:1158)
System.Threading.Tasks.Task.InnerInvoke () (at <4a4789deb75f446a81a24a1a00bdd3f9>:0)
System.Threading.Tasks.Task.Execute () (at <4a4789deb75f446a81a24a1a00bdd3f9>:0)
— End o

Sending a fragmented message requires making a temporary allocation of memory with the Allocator.Temp allocator. As the second error indicates, this allocator is not available in managed threads outside of a job. Here your Task.Run call ends up executing the send on a non-job thread. In general I’d recommend against trying to use Unity Transport with regular C# threads as this is not something we test, and as you see we sometimes rely on APIs that are only available on the main thread or in jobs.

If you absolutely must perform the send on a different thread, you can use Unity’s job system to do that. Unity Transport was written to work with that. But unless you’ve measured the send as being a bottleneck, I’d leave it on the main thread.

1 Like