Search Unity

Help Wanted EndSend without matching BeginSend

Discussion in 'Unity Transport' started by SimonVigMillard, Jul 29, 2021.

  1. SimonVigMillard

    SimonVigMillard

    Joined:
    Jul 25, 2018
    Posts:
    3
    Hi,

    I don't have too much experience with networking, but I've managed to implement a working solution using the Unity Transport guides found here: https://docs.unity3d.com/Packages/com.unity.transport@0.3/manual/workflow-client-server.html

    However, I'm getting a weird issue, where on some devices only a System.InvalidOperationException is thrown with the message: EndSend without matching BegindSend, as soon as a device joins the game. The error is coming from Unity.Networking.Transport.NetworkDriver.EndSend.
    My code causing the error is here:

    Code (CSharp):
    1. while (_clientInputs.Count > 0)
    2. {
    3.     byte[] inputBytes = _clientInputs.Dequeue().PackToBytes();
    4.  
    5.     NativeArray<byte> bytes = new NativeArray<byte>(inputBytes, Allocator.Temp);
    6.     m_Driver.BeginSend(m_Connection, out DataStreamWriter writer);
    7.     writer.WriteBytes(bytes);
    8.     m_Driver.EndSend(writer);
    9. }
    This code is from the Update loop. Is the issue that I'm using a while loop here? (not sure if I'm not allowed to send more than once per Update). Or am I using the NativeArray wrong? Or is it something else entirely?
    An example of this error occurring is when I host on my PC and try to join with my Android device, then the Android gets this error. If I host on my Android and join with my PC there is no issue. I've also gotten the error with iOS devices trying to join each other, but most devices seem to have no problem.

    I'll be happy to provide more information.
     
  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    386
    You need to check the return value from BeginSend. The send queue is limited and when it is full BeginSend will fail. When it does you cannot call EndSend and you should not try to write to the DataStream.
    The send queue size does not depend on the platform, but how long the OS needs the keep the data you send before making it available to the send queue again does depend on the platform so you can get different behavior based on that.
     
    SimonVigMillard likes this.
unityunity