Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback Simple client and server example improvement

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

  1. MariuszKowalczyk


    Nov 29, 2011
    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();
    5.     if (!m_Connection.IsCreated)
    6.     {
    7.         return;
    8.     }
    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.     //
    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


    Unity Technologies

    Jul 19, 2021
    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.