Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Resolved Send fragmented pipeline as Async Erorr

Discussion in 'Unity Transport' started by Farshadfarzan368, Jun 29, 2023.

  1. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
    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<byte> 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:<SendProfileComplate>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.<SendProfileComplate>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
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    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.
     
    Farshadfarzan368 likes this.