Search Unity

Unity Multiplayer Server to server communications

Discussion in 'Connected Games' started by tinman, Oct 16, 2015.

  1. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Is there a way (or a hack way) for two UNET server instances to communicate over the network? For example, if one server needed to tell another server that a certain player logged in to his server?
     
  2. GraphXCreations

    GraphXCreations

    Joined:
    Jul 6, 2014
    Posts:
    121
  3. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Thanks! I'm generally aware of these but I thought now that Unity has multiplayer in house to try to keep all of the aspects of multiplayer in my game also Unity built :)
     
  4. Paulohmm

    Paulohmm

    Joined:
    Dec 24, 2012
    Posts:
    18
    I've managed to implement server to server communication using the low level API from UNET. Below contains a brief and really simple explanation of how I did it:

    Basically a created one "master server" (that I called "Relay Server"). His purpose is connect the client to the other server, as well the servers themselves. He receives messages from two distinct ports: one is public and is the port used to communicate with de clients; and the other is a private port wich only de sub servers can use (the intention is that this port can only be accessed from LAN). This server handle login stuff, and forward messages from the clients to the sub servers, and from the servers to the clients.

    The sub servers are basically "just normal clients". They have a list of connected clients with IDs assigned by the relay server, and this ID is used from all the servers to inform what client must receive the message (in the case of a sub server send an message to the client), or what client send an message to that server.
    This ID is also used to determine if the message sender was a client or another server.


    This implementation is a quite complex and designed to work on the way that better suits my project. Took me a whole day just to implement this idea of using the relay to link the servers to the clients, but I am no network engineer nor have much experience with multiplayer related stuff. Depending your needs might be a lot simpler than what I just wrote, so I highly recommend you to wait a bit so the Unity developers, or more experienced users tell you a better way to implement an server to server communication.
    Anyway I hope that somehow my reply help you, and I also apologize for any possible grammar error, since I am not a native speaker of english ;)
     
  5. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    @Paulohmm thank you! Yes it does help a bit. I'm just wondering how the communications don't get "mixed" between the clients-servers and server-servers, because (from what I understand), a HLAPI connection is a LLAPI under the hood. How did you distinguish between the connections of the server-server and server-client?
     
  6. Paulohmm

    Paulohmm

    Joined:
    Dec 24, 2012
    Posts:
    18
    @tinman Yeah I think my post really lacks of a explanation on this subject.

    In my case, the only server that connects with the clients is the relay server, and the sub servers also only connects with the relay server.
    In the sub servers, all the connections are simulated (with the exception to the connection with the relay server). These servers contains only the ID (among other data specific to this server) that identify this client (or server) on the relay.

    The communication process goes like this:

    The connect event happens on the relay and a new network connection is created and an ID is generated to this connection. The ID is used to identify if is a client or a server. The servers IDs are greater than 10000, and client IDs lesser than 10000.
    Soon after this comes the login request (or a register sub server request). If this request succeed a message is sent to the sub servers informing that they need add this connection to their lists of connections.

    When a message is sent (regardless of the sender or intended receiver) the relay server receives it, and based on the hostId and connectionId where it came from, the server determines the sender. From this point the message is modified to contain the ID assigned by relay to this connection, then the message is forwarded to the appropriate receiver.

    For a disconnect, the relay server simply informs the others to clean the data and remove that connection.


    PS.: The way I did, a big part of the HLAPI (such as the handlers) will need some work to operate properly. I am not quite there yet but I think that it's not much complicated.
     
  7. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    @Paulohmm Alright that makes it clearer thank you. When you define a connection ID, for example, server-to-relay server, where is that defined? And how do you then send a message to a specific ID?
     
  8. Paulohmm

    Paulohmm

    Joined:
    Dec 24, 2012
    Posts:
    18
    When a connection is established the ID is created on the relay server and a message is send to every server informing them that a new connection was made with that specific ID. Then, when the servers need send something to a specific connection just use the connection ID

    About the messages: all my messages contains a field for the connection ID. This field is used only by the servers and this field informs the relay who should receive that message (or who sent, in some cases).

    EDIT: the relay server also receives an ID, and all servers know his ID
     
    Last edited: Oct 17, 2015
  9. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    @Paulohmm Ah I see ok that makes sense. Looks like you made quite the system! So I assume when a server needs to send a message to another, he sends a message to the relay server, and the relay server send it to the destination server, and that server only? Thank you far answering all my questions by the way it's very helpful! :)
     
    Paulohmm likes this.
  10. Paulohmm

    Paulohmm

    Joined:
    Dec 24, 2012
    Posts:
    18
    Pretty much it. There is a lot of things happen under the hood to facilitate the communication, but that is the idea.

    I have to implement a few more things an then I will start to look at some of the HLAPI stuff, in order to make work as direct as possible. After that I will try support direct connection between some of the servers, to have a good freedom to use this system in various scenarios.
     
  11. Paulohmm

    Paulohmm

    Joined:
    Dec 24, 2012
    Posts:
    18
    If you decide to follow the same approach, if you want to, feel free to send me PMs, to discuss possible improvements and implementations.
     
  12. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    @Paulohmm Thanks very much I will keep that in mind! :)
     
  13. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    5.3 (which is in beta) contains a NetworkServerSimple class in the HLAPI that can be used for this purpose.
     
    tinman and Paulohmm like this.
  14. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    @seanr That's awesome! Is there any docs where we can read up on it?