Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

juggling server connections

Discussion in 'Multiplayer' started by forestjohnson, Jun 29, 2010.

  1. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I was pondering about networking today, I've actually never done any real networking so maybe this gonna be stupid and premature, but I think I understand the basics of it.

    Unity allows you to connect to

    A: Unity Tech's Master Server, which gives you a list of IPs for other users who are hosting their own games that you can in turn connect to.

    B: Any computer that is running a Unity-built app which is set up to connect to the one you are running.

    So, for example, if I built a simple chat program that always connects to IP X, if I put it on the computer that has IP X, start it, and then put it on another computer, the two can chat with eachother.

    If that's the case, can't I host my own "Master Server", if I wanted to? Just publish a game that connects to my home computer by default and run a special version of that game on my home computer all day which saves user's game data and hands out lists of IPs for games. That should work until I move or my house burns down, right? Even if I do move, maybe I put a feature in the game that allows the user to type in a new server IP or looks on a web page for the current server IP.


    The other thing I was wondering about is if it is possible to switch out who the server is and who the clients are within a group of say, 4 computers.

    Say that I am starting a game with 4 people. We all entered a matchmaking queue, and one of us was chosen to be the "server" by the master server. Early on in the game, that person pulls out their ethernet cable, but the other 3 still want to play. Could the 3 lost and confused computers somehow reconnect to eachother, perhaps with the help of the master server?

    The last question is, would it be possible to build some sort of redundancy into the equation so that in a 4 player game, one person is the server, 3 are connected to him, but all 4 have a secondary connection to either one of the 3 clients or a 5th user who is in an entirely different game, and a tertiary connection and so on. That way if one person disconnects, the server-ball gets instantly passed on to someone else and the game goes on.

    The other reasoning behind this is anti-hack. For example if two players are in a matchmaking queue for 1v1, both clients are assigned to a random 3rd player who gets to be the "server", that 3rd player can control which packets to send to each player based on where his units are on the map, meaning that he won't even get packets he could use in a client hack to reveal the opponent. (say that it is a real time strategy game with fog of war)

    Meanwhile the 3rd player doesn't even notice that he is doing this, and if he disconnects, he is instantly replaced by someone else who was selected as a backup when the game started.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Technically, this is possible. You could even use some dynamic dns / IP service. These give you an url and your computer can dynamically update the IP of that url.

    I'm not sure if you ask if this is technically possible or if it can be achieved with reasonable effort?

    Of course it's possible.
    It will be a lot of effort.


    I assume you are more interested in making a game than in solving the technical aspects of peer to peer gaming with clients acting as server.
    If I'm right, you should possibly start with a real, central server. You can use Unity itself, Photon, Smartfox or any other solution for this. Hosting could be the same as the Master Server: at home. But it will of course become a bottleneck pretty soon (keyword: upload bandwidth).

    It leaves room for hacking protection as well and you don't have to shift the "server" responsibilities around.

    The other option to make it easier would be to get rid of the server role in the peer to peer games you create. If every client has all information and every (meaningful) situation is solved on all clients in the same way, you possibly don't need a server.
     
  3. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Hello Forest,

    One thing you really need to consider, is how unhappy most isp's are when you try and run any sort of server off your home connection. Most cable or dsl contracts include language which states doing so is grounds for immediate termination of your account.

    Now hosting a small ftp or ventrilo server on your machine won't likely get you into trouble. But the traffic that a master server generates for a popular game might get you noticed.

    You mentioned changing ip's in your OP. You should do this with your DNS server for a domain your own, or use a service such as DynDns.

    In terms of switching around servers, other than the process of reconstituting the network objects the server instanced, there are some other caveats.

    -Arbitration becomes an issue. Depending on the game each client might have a conflicting view of the game wold. Which of the remaining 3 is correct? This might be easier for a game such as a card game or an RTS where there are discrete and exclusive sets of object ownership that you can simply mirror to your new server.

    -It might just fail altogether. There's no possible way to guarantee that one of the 3 remaining clients is actually capable of hosting. With only three people remaining there is a fair chance that all 3 of them will have a terribad DSL router.
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I think my main question is "Can it be done while utilizing the tools that unity offers, as opposed to, say, starting from the same level as someone programming from scratch."

    I don't really expect to host a popular game from home, I was just thinking about networking for a hypothetical game.

    I think if I try to make this game, I will make it 100% peer to peer, it will share all the information so both clients can determine the exact same outcome based on the exact same set of commands. (although thanks to latency its not that simple but whatever).

    Then, if my game gets popular enough that hacking is a problem, first I will celebrate, then I will worry about implementing the random 3rd party "referee" system.
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Then it's a "yes".
    Unity uses Mono to handle your code and in the end that gives you all the power of DotNet / C#.
     
  6. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I meant as far as the networking things go. Can I do what I'm talking about with nothing but Network.Connect and Netowork.RPC, or would I need to essentially build my own network class out of whatever network stuff is in mono?

    EDIT: TobiasS, can you explain what Photon does? It is a server application that you can run on a server you rented, and it talks back and forth with a bunch of unity / flash / whatever clients according to C# code the user writes?

    Why would you use something like that instead of just using Unity itself? Wouldn't it require you to have two versions of the same code written in different environments?
     
  7. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Networking with built in from Unity need to knows:

    A client can only talk to 1 server at any given time.

    A build of your game can be the server or a client or both.

    When the server goes down, clients will not be smart enough to look for existing players and start a new server, this is done through Network Instansiate, which means you would have to run 4 servers and write code to deal with the disconnection and then figure out who will take over.

    The master server is simply a broadcast list of other games connected to the master as servers.

    Unity by default is not designed to do what you are asking as far as 'smart server', unless you are familiar with server coding on your own, this will not be possible.

    Server failover not available in Unity.

    In short, Unity is built for getting you up and running, takes effort to make it smart enough to handle features you are asking about.
     
  8. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Oh, but it sounds like it is possible with some clever tricks, if you think it all through. So, in my game of 4 players, all 4 could be servers and clients at the same time, but all 4 are clients of the same server. Then when that server goes down, through some careful process, the 3 all become clients of one of the 3. They would probably all have to agree beforehand on the order that they will fall back in, and there might have to be some prevention of hacks/exploits taking advantage of this, should they arise.
     
  9. Tomy125

    Tomy125

    Joined:
    May 2, 2010
    Posts:
    15
    I'm not sure making everyone a server and a client at the same time would work.
    I'd go with storing the current game state somewhere on the clients or in a database and then giving the option to start a new server with the saved game state.
     
  10. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Sure you could probably hash it up a bit to make them all clients / servers with a structure of IP's / ports of the other machines that is passed around through a lobby system. The order would need to be based on latency, the lower the latency the higher on the order of priority for next server in line, game state sync would be an issue though since network view ID's are ONLY maintained as far as I know, through the server that is hosting the game, so I would think you would loose them all, not sure though, haven't bothered to test.