Search Unity

Question Relay with Netcode for entities - how to connect local client

Discussion in 'Relay' started by MarcelArioli, Nov 24, 2022.

  1. MarcelArioli

    MarcelArioli

    Joined:
    May 31, 2014
    Posts:
    28
    Even though it is stated that netcode for entities does not support relay,
    I embedded the netcode package and modified the NetworkStreamReceiveSystem to create the NetworkDriver with RelayParams.

    That seems to work fine, my problem is a different one :

    My setup : 1 server, 2 clients:
    - Host has a ServerWorld and a ClientWorld
    - A build runs a ClientWorld (I currently run the exe on the same machine)

    When my server listens on AnyIpv4 and my built client connects by using the IP provided by relay (which seems to work), how on earth can I connect my local client?
    I cannot wrap my head around it.
    I tried connecting the local client using AnyIpv4 and LoopbackIpv4. I also tried reading the LocalEndPoint from the ServerWorld, which is for example 0.0.0.0:60091 (Port changes everytime).
    But I simply cannot connect the local client.

    I'm under the impression that it should be possible to connect the local ClientWorld to the ServerWorld somewhat directly without sending data first to the relay and then back.
    But I also tried connecting to the relay which it also didn't seem to like (that closed the relay with an error message)

    I think I'm making a thinking issue of some sorts... Any suggestions are greatly appreciated.
     
  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    We are planning to build a sample showing how to use relay with netcode for entities 1.0, but I don't know exactly when that sample will ship. In 1.0 you do not need to modify NetworkStreamReceiveSystem to support relay, you can create a custom driver constructor and set that up (by setting NetworkStreamReceiveSystem.DriverConstructor before world creation - during bootstrap is a convenient place to do it).

    For connecting the client to a server in the same process you have two options. Either go through the relay server for all traffic - which is far from ideal - or use an IPC driver which is the default way to connect to in-proc servers in 1.0.
    Connecting to directly to the server socket will not work since the relay has some additional protocol.

    In order to use an IPC connection for the local connection you need to make sure that the server creates both drivers (a socket driver using relay and an IPC driver). The client has to create a different driver depending on if you are hosting the server or not. There is code dealing with this in RegisterClientDriver in the DefaultDriverConstructor which you can look at for inspiration.
     
    bb8_1 likes this.
  3. MarcelArioli

    MarcelArioli

    Joined:
    May 31, 2014
    Posts:
    28
    Thanks for your answer @timjohansson :)
    It's actually funny, i was writing up an answer to my own question right before you answered XD

    After investing the last 2 weeks, I finally managed to make it work (with entities / netcode version 0.5x) just an hour ago XD

    Did something similar to what you suggest, but probably in an unoptimal/hacky way...

    I did :
    - Embed netcode and transport, so that those modules can be modified
    - Duplicated NetworkStreamReceiveSystem and named it RelayStreamReceiveSystem, so that I can have 2 drivers. (In addition, the RelayStreamReceiveSystem adds RelayNetworkParameters to the NetworkSettings before constructing the driver)
    - Modified CommandSendSystem, GhostSendSystem and RPCSystem, so that they react correctly to both drivers. (that was the tricky part)
    - Like you mentionned, server initializes both drivers, client uses either network or relay driver.
     
    bb8_1 likes this.
  4. gabrieloc

    gabrieloc

    Joined:
    Apr 6, 2014
    Posts:
    49
    Sorry to necro this thread, but I have a related question.

    I'm able to connect one (1) local client just fine, but creating additional connections (ie. server-run bots) is a no-go. I booted up the Netcode sample project (01_BootstrapAndFrontend) and creating additional thin clients with Relay enabled doesn't create additional client connections either. A local only game does create the connections however.

    I kind of understand why this is the case. I can kind of modify my use case as well to not expect a connection per player. I'm just wondering if this makes sense from an architectural POV or if I'm actually doing something wrong. Thoughts, @timjohansson?