Search Unity

Feedback Simple client and server example improvement

Discussion in 'Unity Transport' started by MariuszKowalczyk, Mar 24, 2023.

  1. MariuszKowalczyk

    MariuszKowalczyk

    Joined:
    Nov 29, 2011
    Posts:
    301
    I think you can edit the simple example from the documentation to inform the users that they have to make sure the event queue is empty before calling the ScheduleUpdate().Complete() again. Otherwise there will be an error:

    To break the example you can edit the ClientBehaviour.cs as follows:

    Code (CSharp):
    1. void Update()
    2. {
    3.     m_Driver.ScheduleUpdate().Complete();
    4.  
    5.     if (!m_Connection.IsCreated)
    6.     {
    7.         return;
    8.     }
    9.  
    10.     //added code
    11.     if (m_Connection.IsCreated && m_Connection.GetState(m_Driver) == NetworkConnection.State.Connected)
    12.     {
    13.         m_Driver.Disconnect(m_Connection);
    14.         m_Connection = default;
    15.     }
    16.     //
    17.  
    18.     (...)
    To fix the example again, you empty the queue at the very end of Update() in ClientBehaviour.cs:
    Code (CSharp):
    1. while (m_Driver.PopEvent(out _, out _) != NetworkEvent.Type.Empty);
    It is all because the client decided to disconnect while there were some events sent from the server in the queue. Then in the event loop the connection was set to default and the events were not popped.

    It sounds like an artificial example, but I experienced this problem when working on my code. I think the situation may be even worse if someone is sending state updates every frame.
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Thanks for the feedback! We'll reword the documentation to make it clearer that if the user must close the connection client-side before having processed all events, they still must empty the rest of the queue.
     
    MariuszKowalczyk likes this.