Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Very simple custom NetworkTransport (proxy to UnityTransport) not working

Discussion in 'Netcode for GameObjects' started by BSimonSweet, Nov 20, 2023.

  1. BSimonSweet

    BSimonSweet

    Joined:
    Aug 17, 2022
    Posts:
    63
    Hello !

    I'm doing some experiments with
    NetworkTransport
    , and what I did to start is a simple proxy class, where every methods call to the
    UnityTransport
    .

    I then add the component to the GameObject with NetworkManager, and set that component in the
    Network Transport
    field. I also have a child GameObject with the UnityTransport component and set that reference to my proxy component.

    But it does not work ... I can start the NetworkManager as a Host and Server (
    .StartHost()
    and
    .StartServer()
    returns
    true
    ) , but if I try to connect as a Client, nothing happens. The
    .StartClient()
    method returns
    true
    , but on the server, there is no sign of the client and on the client the NetworkManager says that it is connected but nothing is sync.

    I'm not sure what is wrong, as I just made a very simple proxy class and everything call back to
    UnityTransport
    methods ... There no errors or warnings in the Console.

    Here is the code of my class and a screen of the setup in the Inspector, just in case :

    Code (CSharp):
    1. using System;
    2. using GLUnity.Network;
    3. using Unity.Netcode;
    4. using Unity.Netcode.Transports.UTP;
    5. using UnityEngine;
    6.  
    7. public class TestTransport : NetworkTransport, ITransportProxy
    8. {
    9.     // INSPECTOR
    10.  
    11.     [SerializeField]
    12.     private UnityTransport transport;
    13.  
    14.     // ACCESSORS
    15.  
    16.     public override ulong ServerClientId => transport.ServerClientId;
    17.  
    18.     // INTERFACES
    19.  
    20.     public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
    21.         => transport.Send(clientId, payload, networkDelivery);
    22.  
    23.     public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
    24.         => transport.PollEvent(out clientId, out payload, out receiveTime);
    25.  
    26.     public override bool StartClient()
    27.         => transport.StartClient();
    28.  
    29.     public override bool StartServer()
    30.         => transport.StartServer();
    31.  
    32.     public override void DisconnectRemoteClient(ulong clientId)
    33.         => transport.DisconnectRemoteClient(clientId);
    34.  
    35.     public override void DisconnectLocalClient()
    36.         => transport.DisconnectLocalClient();
    37.  
    38.     public override ulong GetCurrentRtt(ulong clientId)
    39.         => transport.GetCurrentRtt(clientId);
    40.  
    41.     public override void Shutdown()
    42.         => transport.Shutdown();
    43.  
    44.     public override void Initialize(NetworkManager networkManager = null)
    45.         => transport.Initialize(networkManager);
    46.  
    47.     // this is from my interface ITransportProxy
    48.     public void SetConnectionData(string ip4, ushort port, string bindIP4 = null)
    49.         => transport.SetConnectionData(ip4, port, bindIP4);
    50. }
    upload_2023-11-20_18-5-0.png
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    Did you just try deriving directly from UnityTransport and add whatever you need to that?
    It might provide you with better results.
     
  3. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    416
    UnityTransport
    is event-based and uses
    InvokeOnTransportEvent
    to notify NGO of transport events, instead of being polled for new events through
    PollEvent
    . I'm guessing that this is what is causing issues here. NGO will attach its own event handler to the
    NetworkTransport
    that is configured for the
    NetworkManager
    (here that's your proxy) but not to the proxied transport. So the latter ends up calling a non-existing event handler and NGO is never notified that (say) a new connection was established.

    This should be easy to fix by assigning the event handler manually at initialization (not tested though):
    Code (CSharp):
    1. public override void Initialize(NetworkManager networkManager = null)
    2. {
    3.     transport.OnTransportEvent = OnTransportEvent;
    4.     transport.Initialize(networkManager);
    5. }
     
    BSimonSweet likes this.
  4. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    Or just do what Simon says...he would know better than I for sure. ;)
     
  5. BSimonSweet

    BSimonSweet

    Joined:
    Aug 17, 2022
    Posts:
    63
    Ok great ! That works !

    Just need to do that
    transport.OnTransportEvent += InvokeOnTransportEvent;
    , because
    OnTransport
    is a C#
    event
    .