Search Unity

To Have An Authoritative Server

Discussion in 'Multiplayer' started by atmuc, Jun 1, 2016.

  1. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    I want to share my custom unet architecture that i made for my multiplayer network games. i will write all details here. i will update documentation regarding your questions. I did this to have an authoritative server.

    InfraArchitecture.jpg

    Glossary

    Windows Server :
    A computer with Windows Server operation system (OS)
    IIS : Internet Information Services. It is an web application container that comes with Windows Operating System.
    Windows Service : A background application running on Windows OS
    Game : A unity build that contains both server and client code. Server code will run on Windows Standalone Build. It will be run as Instance on Game Server. Client part will be run client devices like phones.
    Game Server : A computer that hosts unity game server parts.
    MVC : Model View Controller framework. It is used for GUI for web pages, desktop applications etc.
    Web Service (WS) : Communication services that generally uses xml as data format.
    Rest Services : Communication services that generally uses JSON as data format.
    WCF : Windows Communication Framework. It is used to build communication services as many different protocols(ws, rest, tcp, etc)
    File Repository : A folder that contains files to process.
    Instance : A running copy of an application. In this case it is Unity Game Windows Build.
    CCU : Concurrent user


    System Parts

    Control Center

    It manages all user, game, session, instance, versioning processes.

    Center File Repository :
    It contains all game file versions to deploy it to game servers.
    System Database : It is used to save all user, game, room, session data.
    Admin Pages : It is an web application. It supplies pages to add/remove game, server definition and see some statistics about session, room counts.
    Web Services : They are used to upload new game version files to Control Center Repository. All web services uses https to have secure transport and uses username/password for authentication/authorization.
    Rest Services : Because they are Unity friendly it is used by Unity game server and game clients. All rest services uses https to have secure transport and uses username/password for authentication/authorization.

    Game Server Node
    It hosts all Unity Game Server parts. For this system there should be several nodes. It depends on CCU count. A server node can host several games and several server node can host a single game. It depends on your design.

    Agent : It is a windows service that always runs. It starts and kills instances. There is a single agent per server. It has a WCF host to publish Web Services. Control center uses these services to control agents. Agents can collect statistics from instances.
    Agent File Repository : It contains unity game build files. There should be different build versions like one for Android devices one for iOS devices because you cannot release new game version on both store at the same time. Agent will run xxx.exe file in this repository to start an instance.
    Unity Game Instance : Running version of a game's Windows Build executable. In my design an instance can handle several rooms and sessions. In many case you can do it. If your game levels are complex you should run an instance per level to handle specific level. For turn based games like chess, it is easy to use single instance to handle several rooms.

    Admin Console : It is web pages to manage this system.

    Game Version Uploader : After you build a new version you can upload new files to Control Center via this application.

    Unity Game Client : It is build of your game for several platforms like Andoid, iOS, Window Universal etc. Thanks to UNET, a build contains both client and server code. If you have server specific code like hard coded passwords you can disable this code by using Unity platform defines.

    Coding

    This system uses UNET. You write your code for both server and client side in the same NetworkBehaviour classes. You use Rpc methods to call client side from serverside. You use Command methods to call server side from client side. Both calls are one way you cannot get return value. So you can accept them as triggers. You can use syncvars to sync values on both side. Even you can use the same NetworkBehaviour class on both server and client side, These classes are isolated. So you must trigger other side or sync variables. Trigger calls received in order and syncvars are synced at the end of the update method. So i prefer triggers to sync values and states. There should be a player prefab on the scene to manage network process. I prefer having single player object per client. For a chess game there are 2 player prefabs on the server side. If you use Unity's standard network classes like network manager, you will get 2 player prefab on each client. I use my custom network manager to have single player prefab on each client. This single player prefab manages game. If your game is not an MMO, single player will be enough to manage a network game. I use server side player prefabs to manage authoritative processes. I use client side player prefabs to manage GUI processes. In that way client devices become dummy terminal. I set room classes on the server side. When a player make a move, it sends a command to server and server process this. Then server calls rpc on all client prefabs that saved in the room class.

    Sequence

    Registration
    New clients calls RegisterDevice rest service. Control Center keeps all devices as a user in the System Database. Service generate a passcode and returns it to the device on registration call. Device uses this passcode for further service calls. System uses device's unique id and some hardware information to define it unique.

    Joining Room
    Client calls JoinRoom rest service for a specific game. When a registered device calls a service function for a game, system creates a game registration for this device automatically. Control Center checks Game Server Nodes and their loads. If there is no Unity Game Instance for this game or current one if full, it creates a new Unity Game Instance by calling Agent. JoinRoom call returns IP, Port address and created session passcode.

    Creating New Unity Game Instance (UGI)
    Control Center(CC) calls Agent's Web Service to create a new UGI. CC sends executable file location on the disk and send executable arguments. UGI uses arguments for setting port, persistence location, log file location. Each instance uses different log file that has name as instance id. Each instance has different socket listener port. Agent uses this port to communicate with the instance. Because Agent is an windows service written by pure .net, i cannot communicate with an Unity Windows Build. UGI starts a SocketListener and accepts Agent's commands via this socket connection. When the computer or the Agent restart, Agent gets all instances information from Control Center and it starts these UGIs.
     
    Last edited: Jun 3, 2016
  2. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    Glossary and system parts added.
     
    Last edited: Jun 2, 2016
  3. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    Coding added.
     
  4. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    No question so far?
     
  5. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    this part is added;

    Sequence

    Registration
    New clients calls RegisterDevice rest service. Control Center keeps all devices as a user in the System Database. Service generate a passcode and returns it to the device on registration call. Device uses this passcode for further service calls. System uses device's unique id and some hardware information to define it unique.

    Joining Room
    Client calls JoinRoom rest service for a specific game. When a registered device calls a service function for a game, system creates a game registration for this device automatically. Control Center checks Game Server Nodes and their loads. If there is no Unity Game Instance for this game or current one if full, it creates a new Unity Game Instance by calling Agent. JoinRoom call returns IP, Port address and created session passcode.

    Creating New Unity Game Instance (UGI)
    Control Center(CC) calls Agent's Web Service to create a new UGI. CC sends executable file location on the disk and send executable arguments. UGI uses arguments for setting port, persistence location, log file location. Each instance uses different log file that has name as instance id. Each instance has different socket listener port. Agent uses this port to communicate with the instance. Because Agent is an windows service written by pure .net, i cannot communicate with an Unity Windows Build. UGI starts a SocketListener and accepts Agent's commands via this socket connection. When the computer or the Agent restart, Agent gets all instances information from Control Center and it starts these UGIs.
     
    Miscellaneous likes this.
  6. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    I completed this doc. If you want more detail i can add it.
     
    Miscellaneous likes this.
  7. Burglebutt

    Burglebutt

    Joined:
    Jul 24, 2014
    Posts:
    51
    This is very interesting! I am kind of a noob with the web stuff but I really want to learn how to make an authoritative server. Thanks for sharing some info about it!
     
  8. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    it looks multiplayer server library and simulation server will be delayed like uGui. so i started to use GameSparks. i guess 1-2 years later i will be able to use Unity multiplayer system.
     
  9. Squalol

    Squalol

    Joined:
    Jan 27, 2015
    Posts:
    7
    I've been looking at gamespark, and the only example I see is https://docs.gamesparks.com/tutorials/real-time-services/real-time-game-example.html which works exactly like Photon Unity Networking, its not authoritative, every players sents its info directly to the others :/ Is there any other tutorial elsewhere that im missing? Ty in advance!
     
  10. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    PUN is a matured realtime messaging cloud system. when using PUN you make a device master (authority), you send messages through Photons regional relay services. Photon also give you a matchmaking system. to make photon it authoritative you should use photon server. that means you have to have a server and manage this server and Photon server service. there is no persistence support on both cloud and server system. you use c# to code server side logic. you can install this server in your local and develop your game.

    in gamesparks it is full cloud system. it has persistence (mongo database), server side coding (cloud code), matchmaking, leaderboards. it's realtime system is new. it is not old as Photon. Because it is full cloud system you have to have an internet connection to develop your game. You use javascript for cloud code, so you do not have functions like linq. you can find some linq libraries for javascript. because you don't compile your code you catch many errors on runtime. on the other hand you can add properties to your objects in easy. Mongo is a nonsql object oriented database.

    Nowadays Unity multiplayer has just a relay server and matchmaking service. relay server sends messages from one device to another. Advantage of UNET is; you code in one place, you use c#, you can use unity physics engine. There is no persistence and server components now.

    if you are a single developer or a small team and need persistence and serverside coding GameSparks fits you.
     
    Squalol likes this.
  11. AlienMe

    AlienMe

    Joined:
    Sep 16, 2014
    Posts:
    93
    @atmuc, that's a cool system you have there...

    How many Unity Game Instances can you fit in one EC2 server? What EC2 instance type are you using?
     
  12. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    Hi @AlienMe, it depends on how much cpu and ram does your instance uses. dynamic allocation should be part of this system. you should monitor spu and ram, then decide to open a new EC2 for new instances.
     
  13. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    I used GameSparks for 5 months. It is nice in general. Because i have to learn javascript, mongo and cannot debug in ease i decided to complete my infrastructure. I will be sharing further details here.
     
  14. GameSparksDave

    GameSparksDave

    Joined:
    Nov 25, 2016
    Posts:
    2
    Hi there!

    Thanks for the well thought out system plan, its very informative and clear.

    Besides your observations above, do you have any other feedback on your experience with Gamesparks? We are always looking for insightfull feedback from experienced users to help us improve our systems and services.

    Thanks,
    -Dave
     
  15. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    Hi Dev, Gamesparks is very good product. For now, i quit using Gamesparks because;
    1. I am a single developer. when i develop code for both server and cliendside, i have to use c# and javascript. there is no c# functions like Linq. I am very experienced c# developer. so i choose what i can use the best.
    2. I have debug problems. i cannot debug scheduled events. everytime i change my code debug, breakpoints changes.
    3. Gamesparks IDE is not as powerful as Visual Studio.
    4. Because using javascript there is no build process so there is no syntax check.
    5. it requires internet connection to develop.
    Those are not big problems. Because i have chance to use c# well i prefer Visual Studio + c# + sql server. GameSparks fits wide range of developers that develops both turnbase and realtime games. So i make my infrastructure to able to use UNET, GameSparks, Photon. After i complete and test my project i may go on with GameSparks.
     
  16. RentireGames

    RentireGames

    Joined:
    Jan 4, 2017
    Posts:
    2
    Hello atmuc, I have examined the infrastructure of your approach, and it really coordinates with my fast-paced game idea. Do you think of sharing it on github to give us developers an opportunity to improve and build up?
     
  17. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,315
  18. RentireGames

    RentireGames

    Joined:
    Jan 4, 2017
    Posts:
    2
    I will check it out
    Thanks Damien-Delmarie.
     
  19. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    i suggest it too. it was paid and i thought it will be an supported product so i bought it and canceled my project. now i see it is free. i hope the developer will continue to support it.