Search Unity

Unity based dedicated server? No graphics, console/terminal instead?

Discussion in 'Scripting' started by Steel_Arm, Sep 19, 2017.

  1. Steel_Arm

    Steel_Arm

    Joined:
    Apr 9, 2013
    Posts:
    22
    My goal: to make a Unity based dedicated multiplayer server, with minimum bloat, such as turning off the renderer, and spawning a console window for text output.

    My progress thus far: Running a unity application with the command line arguments "-batchmode -nographics" will effectively prevent Unity from opening a window (this works both with the editor, and a compiled Unity application). To spawn a console window, I am using unmanaged code on a "manager" GameObject as follows:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Runtime.InteropServices;
    6. using UnityEngine;
    7.  
    8. public class GameManager : MonoBehaviour {
    9.     [DllImport("kernel32.dll")]
    10.     private static extern bool AllocConsole();
    11.     private static extern IntPtr GetStdHandle(UInt32 nStdHandle);
    12.     private static extern void SetStdHandle(UInt32 nStdHandle, IntPtr handle);
    13.     private const UInt32 StdOutputHandle = 0xFFFFFFF5;
    14.     private TextWriter writer;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         DontDestroyOnLoad(this);
    19.         StartConsole();
    20.         WriteLine("Console Ready");
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.      
    26.     }
    27.  
    28.     void StartConsole() {
    29.         AllocConsole();
    30.         // stdout's handle seems to always be equal to 7
    31.         IntPtr defaultStdout = new IntPtr(7);
    32.         IntPtr currentStdout = GetStdHandle(StdOutputHandle);
    33.         if (currentStdout != defaultStdout)
    34.             // reset stdout
    35.             SetStdHandle(StdOutputHandle, defaultStdout);
    36.         // reopen stdout
    37.         writer = new StreamWriter(Console.OpenStandardOutput())
    38.         { AutoFlush = true };
    39.         Console.SetOut(writer);
    40.     }
    41.  
    42.     void WriteLine(string line) {
    43.         writer.WriteLine(line);
    44.     }
    45. }
    When I run this scene in the editor, a console window is created, but then Unity crashes. Same with a standalone compiled application. It crashes before displaying the text "Console Ready".

    1) Does anyone have any ideas or tips on what I may be doing wrong with this?

    2) Is there perhaps a different (possibly better) approach? Aside from networking, I still need some form of scene management and physics for the server to properly do game logic. It would be great if I could use just the parts of Unity that are essential to running a server, for maximum efficiency.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Ehhhh.... just select "headless" in the Linux build options and it doesn't include any of the graphics stuff in your server. In mine I wrote a separate logging system that outputs independent of the player.log, it is for server status information, etc. If you want to watch it in real time, that is what "tail -f" is for.

    If you're trying to make your server a Windows or Mac application, you're already giving up on your "minimum bloat" requirement.
     
    JoeStrout and Steel_Arm like this.
  3. Steel_Arm

    Steel_Arm

    Joined:
    Apr 9, 2013
    Posts:
    22
    I will give that a shot. I have Linux Mint on Oracle Virtualbox. Still, a Windows solution is good for maximizing user friendliness for the user base, making the product more readily available?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What? No. The server app is for running on a server. I don't have any actual facts to back this up, but I'd bet a pizza there are far more Linux servers than Windows servers on the internet, as well as more people comfortable using the former than the latter.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That depends on what your user base is. Back when we were playing Minecraft in the office, it was very convenient to be able to boot a server on our normal workstation, and then connect over LAN. If your game's server is something a gang of friends can boot and join over IP/LAN, it's a lot more convenient if it can run on any OS.

    If it's a dedicated server setup (like having your own TF2 server with a custom ruleset), you can assume people will have a dedicated box/cloud server to run that on, and supporting non-linux setups becomes a lot less important.
     
    JoeStrout likes this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Fully agree, but in this case, I would also suggest your server have a UI (and quite likely, be the very same app as your main game — with simply a "host a game" mode).