예제 #1
0
        /**
         *
         * @param {!Client} client Websocket client that's connected to
         *        the game.
         * @param {!RelayServer} relayserver relayserver the game is
         *        connected to.
         * @param {Game~GameOptions} data Data sent from the game which
         *        includes
         */
        public HFTGame AssignClient(HFTSocket client, HFTRuntimeOptions data)
        {
            // If there are no games make one
            // If multiple games are allowed make one
            // If multiple games are not allowed re-assign
            string newGameId = !String.IsNullOrEmpty(data.id) ? data.id : ("_hft_" + nextGameId_++);
            HFTGame game = new HFTGame(newGameId, this, data);
            // Add it to 'games' immediately because if we remove the old game games would go to 0
            // for a moment and that would trigger this GameGroup getting removed because there'd be no games
            if (masterGame_ == null)
            {
                masterGame_ = game;
            }
            HFTGame oldGame = null;
            // See if there's an old game with the same id then replace it
            games_.TryGetValue(newGameId, out oldGame);
            games_[newGameId] = game;

            if (oldGame != null)
            {
                log_.Info("tell old game to quit");
                oldGame.SendQuit();
                oldGame.Close();
            }

            log_.Info("add game: num games = " + games_.Count);
            game.AssignClient(client, data);

            if (data.master)
            {
                masterGame_ = game;
            }

            return game;
        }
예제 #2
0
 public void AssignAsClientForGame(HFTRuntimeOptions data, HFTSocket client)
 {
     string gameId = String.IsNullOrEmpty(data.gameId) ? "HFTUnity" : data.gameId;
     HFTGameGroup gameGroup = GetGameGroup(gameId, true);
     if (!gameGroup.HasClient())
     {
         ++gameCount_;
     }
     gameGroup.AssignClient(client, data);
 }
예제 #3
0
        public void AssignAsClientForGame(HFTRuntimeOptions data, HFTSocket client)
        {
            string       gameId    = String.IsNullOrEmpty(data.gameId) ? "HFTUnity" : data.gameId;
            HFTGameGroup gameGroup = GetGameGroup(gameId, true);

            if (!gameGroup.HasClient())
            {
                ++gameCount_;
            }
            gameGroup.AssignClient(client, data);
        }
예제 #4
0
        public HFTGame(string id, HFTGameGroup group, HFTRuntimeOptions options)
        {
            id_        = id;
            gameGroup_ = group;
            options_   = options;

            SetGameId();

            log_ = new HFTLog("HFTGame[" + gameId_ + "]");
            log_.Info("created game");
        }
예제 #5
0
        public void Start(HFTRuntimeOptions options, GameObject gameObject)
        {
            m_options = options;
            m_gameObject = gameObject;

            if (options.startServer)
            {
                StartServer();
            }

            StartCheck();
        }
예제 #6
0
        public void Start(HFTRuntimeOptions options, GameObject gameObject)
        {
            m_options    = options;
            m_gameObject = gameObject;

            if (options.startServer)
            {
                StartServer();
            }

            StartCheck();
        }
예제 #7
0
        public HFTConnectionManager(GameObject gameObject, HFTGameOptions options)
        {
            m_gameObject = gameObject;
            m_options    = new HFTRuntimeOptions(options);

            m_server               = new GameServer(m_options, gameObject);
            m_server.OnConnect    += Connected;
            m_server.OnDisconnect += Disconnected;

            m_hftManager          = new HFTManager();
            m_hftManager.OnReady += StartGameServer;
            m_hftManager.OnFail  += FailedToStart;
        }
예제 #8
0
        public HFTWebServer(HFTRuntimeOptions options, string[] addresses)
        {
            m_log            = new HFTLog("HFTWebServer");
            m_options        = options;
            m_gamePath       = "/";
            m_webServerUtils = new HFTWebServerUtils(m_gamePath);

            // Touch the HFTWebFileDB
            // We do this be because we want it to get the list
            // of files BEFORE run the server. The server will
            // run in a different thread and HFTWebFileDB will
            // not be able to populate its database from that thread.
            HFTWebFileDB.GetInstance();

            // FIX: sysname and gamename
            string sysName = Environment.MachineName;

            if (sysName.EndsWith(".local"))
            {
                sysName = sysName.Substring(0, sysName.Length - 6);
            }
            string gameName = m_options.name;
            string ping     = Serializer.Serialize(new HFTPing(sysName + ": " + gameName, "HappyFunTimes"));

            m_ping = System.Text.Encoding.UTF8.GetBytes(ping);
            m_log.Info("Ping: " + ping);

            m_liveSettingsStr = "define([], function() { return " + Serializer.Serialize(new LiveSettings()) + "; })\n";
            m_liveSettings    = System.Text.Encoding.UTF8.GetBytes(m_liveSettingsStr);

            if (options.captivePortal || options.installationMode)
            {
                m_captivePortalHandler = new HFTCaptivePortalHandler(m_webServerUtils);
                m_getRouter.Add(m_captivePortalHandler.HandleRequest);
            }

            m_getRouter.Add(HandleRoot);
            m_getRouter.Add(HandleLiveSettings);
            m_getRouter.Add(HandleFile);
            m_getRouter.Add(HandleMissingRoute);
            m_getRouter.Add(HandleNotFound);

            m_postCmdHandlers["happyFunTimesPingForGame"] = HandleCmdPingForGame;
            m_postCmdHandlers["happyFunTimesPing"]        = HandleCmdPing;
            m_postCmdHandlers["happyFunTimesRedir"]       = HandleCmdRedir;
            m_postCmdHandlers["time"] = HandleCmdTime;
            m_postCmdHandlers["quit"] = HandleCmdQuit;

            m_addresses = addresses;
        }
예제 #9
0
        public void AssignClient(HFTSocket client, HFTRuntimeOptions data)
        {
            if (client_ != null)
            {
                log_.Error("this game already has a client!");
                client_.OnMessageEvent -= OnMessage;
                client_.OnCloseEvent   -= OnDisconnect;
                client_.Close();
            }

            client_ = client;

            client.OnMessageEvent += OnMessage;
            client.OnCloseEvent   += OnDisconnect;

            RegisterCmdHandler <object>("client", SendMessageToPlayer);
            RegisterCmdHandler <object>("broadcast", Broadcast);
            RegisterCmdHandler <HFTMessageSwitchGame>("switchGame", SwitchGame);
            RegisterCmdHandler <object>("peer", SendMessageToGame);
            RegisterCmdHandler <object>("bcastToGames", BroadcastToGames);
            RegisterCmdHandler <HFTMessageAddFile>("addFile", AddFile);

            // Tell the game it's id
            var gs = new HFTMessageGameStart();

            gs.id     = id_;
            gs.gameId = ""; //FIX!
            client.Send(new HFTRelayToGameMessage("gamestart", "", gs));

            // start each player
            foreach (var player in players_.Values)
            {
                client.Send(new HFTRelayToGameMessage("start", player.id, null));
            }

            // Not sure why I even have a sendQueue
            // as the game should be running before anyone
            // joins but it seems to be useful for debugging
            // since contollers start and often immediately
            // send a name and color cmd.
            foreach (var pair in sendQueue_.ToArray())
            {
                client.Send(pair.Value);
            }
            sendQueue_.Clear();
        }
예제 #10
0
        public HFTWebServer(HFTRuntimeOptions options, string[] addresses)
        {
            m_log = new HFTLog("HFTWebServer");
            m_options = options;
            m_gamePath = "/";
            m_webServerUtils = new HFTWebServerUtils(m_gamePath);

            // Touch the HFTWebFileDB
            // We do this be because we want it to get the list
            // of files BEFORE run the server. The server will
            // run in a different thread and HFTWebFileDB will
            // not be able to populate its database from that thread.
            HFTWebFileDB.GetInstance();

            // FIX: sysname and gamename
            string sysName = Environment.MachineName;
            if (sysName.EndsWith(".local"))
            {
                sysName = sysName.Substring(0, sysName.Length - 6);
            }
            string gameName = m_options.name;
            string ping = Serializer.Serialize(new HFTPing(sysName + ": " + gameName, "HappyFunTimes"));
            m_ping = System.Text.Encoding.UTF8.GetBytes(ping);
            m_log.Info("Ping: " + ping);

            m_liveSettingsStr = "define([], function() { return " + Serializer.Serialize(new LiveSettings()) + "; })\n";
            m_liveSettings = System.Text.Encoding.UTF8.GetBytes(m_liveSettingsStr);

            if (options.captivePortal || options.installationMode)
            {
                m_captivePortalHandler = new HFTCaptivePortalHandler(m_webServerUtils);
                m_getRouter.Add(m_captivePortalHandler.HandleRequest);
            }

            m_getRouter.Add(HandleRoot);
            m_getRouter.Add(HandleLiveSettings);
            m_getRouter.Add(HandleFile);
            m_getRouter.Add(HandleMissingRoute);
            m_getRouter.Add(HandleNotFound);

            m_addresses = addresses;
        }
예제 #11
0
        /// <summary>
        /// Constructor for GameServer
        /// </summary>
        /// <param name="options">The objects</param>
        /// <param name="gameObject">gameObject that will process messages from HappyFunTimes</param>
        public GameServer(HFTRuntimeOptions options, GameObject gameObject)
        {
            m_options      = options;
            HFTLog.debug   = options.debug;
            m_gameObject   = gameObject;
            m_players      = new Dictionary <string, NetPlayer>();
            m_sendQueue    = new List <String>();
            m_deserializer = new Deserializer();
            m_handlers     = new Dictionary <string, CmdEventHandler>();

            m_eventProcessor = m_gameObject.AddComponent <HFTEventProcessor>();
            HFTGlobalEventEmitter.GetInstance().Setup(m_eventProcessor);

            m_msgHandlers.Add("update", UpdatePlayer);
            m_msgHandlers.Add("upgame", UpdateGame);
            m_msgHandlers.Add("start", StartPlayer);
            m_msgHandlers.Add("gamestart", StartGame);
            m_msgHandlers.Add("remove", RemovePlayer);
            m_msgHandlers.Add("system", DoSysCommand);
            m_msgHandlers.Add("log", LogMessage);
        }
예제 #12
0
        /**
         *
         * @param {!Client} client Websocket client that's connected to
         *        the game.
         * @param {!RelayServer} relayserver relayserver the game is
         *        connected to.
         * @param {Game~GameOptions} data Data sent from the game which
         *        includes
         */
        public HFTGame AssignClient(HFTSocket client, HFTRuntimeOptions data)
        {
            // If there are no games make one
            // If multiple games are allowed make one
            // If multiple games are not allowed re-assign
            string  newGameId = !String.IsNullOrEmpty(data.id) ? data.id : ("_hft_" + nextGameId_++);
            HFTGame game      = new HFTGame(newGameId, this, data);

            // Add it to 'games' immediately because if we remove the old game games would go to 0
            // for a moment and that would trigger this GameGroup getting removed because there'd be no games
            if (masterGame_ == null)
            {
                masterGame_ = game;
            }
            HFTGame oldGame = null;

            // See if there's an old game with the same id then replace it
            games_.TryGetValue(newGameId, out oldGame);
            games_[newGameId] = game;

            if (oldGame != null)
            {
                log_.Info("tell old game to quit");
                oldGame.SendQuit();
                oldGame.Close();
            }

            log_.Info("add game: num games = " + games_.Count);
            game.AssignClient(client, data);

            if (data.master)
            {
                masterGame_ = game;
            }

            return(game);
        }
예제 #13
0
        void Awake()
        {
            m_connectToServerOnStart = enabled;
            m_options = new HFTRuntimeOptions(happyfuntimesOptions);

            m_server = new GameServer(m_options, gameObject);
            m_server.OnConnect += Connected;
            m_server.OnDisconnect += Disconnected;

            m_hftManager = new HFTManager();
            m_hftManager.OnReady += StartGameServer;
            m_hftManager.OnFail  += FailedToStart;

            m_playerManager = new HFTPlayerManager(m_server, gameObject, players.Length, timeoutForDisconnectedPlayersToReconnect, GetPlayer);
        }
예제 #14
0
        public static int Main(string[] args)
        {
            HFTRuntimeOptions m_options;
            HFTArgParser p = HFTArgParser.GetInstance();
            string argStr = "";
            if (p.TryGet<string> ("hft-args", ref argStr))
            {
                Deserializer d = new Deserializer();
                m_options = d.Deserialize<HFTRuntimeOptions>(argStr);
            }
            else
            {
                m_options = new HFTRuntimeOptions ();
            }
            if (!HFTArgsToFields.Apply("hft", m_options))
            {
                System.Console.WriteLine("bad args!");
                return 1;
            }

            //using (System.IO.StreamWriter writer = new System.IO.StreamWriter(System.IO.File.Open("/Users/gregg/temp/hft-server.log", System.IO.FileMode.Create)))
            //{
            //    writer.WriteLine(System.DateTime.Now.ToString());
            //    writer.WriteLine(Serializer.Serialize(m_options, false, true));
            //}

            List<string> addresses = new List<string>();
            addresses.Add("http://[::0]:18679");
            //            addresses.Add("http://0.0.0.0:18679");

            if (m_options.installationMode)
            {
                addresses.Add("http://[::0]:80");
                //                addresses.Add("http://0.0.0.0:80");
            }

            // Do I want this option ever?
            // Good: Need from editor to test instalation mode in editor
            // Bad: If game is hacked and serve any folder. But if game
            // is hack you can probably own machine anyway.
            if (!String.IsNullOrEmpty(m_options.dataPath))
            {
                HFTWebFileDB.GetInstance().DataPath = m_options.dataPath;
            }

            string ipv4Address = String.IsNullOrEmpty(m_options.ipv4DnsAddress) ? HFTIpUtils.GetLocalIPv4Address() : m_options.ipv4DnsAddress;
            string ipv6Address = String.IsNullOrEmpty(m_options.ipv6DnsAddress) ? HFTIpUtils.GetLocalIPv6Address() : m_options.ipv6DnsAddress;

            HFTWebServer webServer = new HFTWebServer(m_options, addresses.ToArray());
            webServer.Start();

            if (m_options.dns || m_options.installationMode)
            {
                HFTDnsRunner dnsRunner = new HFTDnsRunner();
                dnsRunner.Start(ipv4Address, ipv6Address, 53);
            }

            // There's no HFTSite because were in installationMode which means there's no internet

            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

            return 0;
        }
예제 #15
0
        public static int Main(string[] args)
        {
            HFTRuntimeOptions m_options;
            HFTArgParser      p      = HFTArgParser.GetInstance();
            string            argStr = "";

            if (p.TryGet <string> ("hft-args", ref argStr))
            {
                Deserializer d = new Deserializer();
                m_options = d.Deserialize <HFTRuntimeOptions>(argStr);
            }
            else
            {
                m_options = new HFTRuntimeOptions();
            }
            if (!HFTArgsToFields.Apply("hft", m_options))
            {
                System.Console.WriteLine("bad args!");
                return(1);
            }

            HFTLog.debug = m_options.debug;

            //using (System.IO.StreamWriter writer = new System.IO.StreamWriter(System.IO.File.Open("/Users/gregg/temp/hft-server.log", System.IO.FileMode.Create)))
            //{
            //    writer.WriteLine(System.DateTime.Now.ToString());
            //    writer.WriteLine(Serializer.Serialize(m_options, false, true));
            //}

            List <string> addresses = new List <string>();

            addresses.Add("http://[::0]:" + m_options.serverPort);
            //            addresses.Add("http://0.0.0.0:18679");

            if (m_options.installationMode)
            {
                addresses.Add("http://[::0]:80");
                //                addresses.Add("http://0.0.0.0:80");
            }

            // Do I want this option ever?
            // Good: Need from editor to test instalation mode in editor
            // Bad: If game is hacked and serve any folder. But if game
            // is hack you can probably own machine anyway.
            if (!String.IsNullOrEmpty(m_options.dataPath))
            {
                HFTWebFileDB.GetInstance().DataPath = m_options.dataPath;
            }

            string ipv4Address = String.IsNullOrEmpty(m_options.ipv4DnsAddress) ? HFTIpUtils.GetLocalIPv4Address() : m_options.ipv4DnsAddress;
            string ipv6Address = String.IsNullOrEmpty(m_options.ipv6DnsAddress) ? HFTIpUtils.GetLocalIPv6Address() : m_options.ipv6DnsAddress;

            HFTWebServer webServer = new HFTWebServer(m_options, addresses.ToArray());

            webServer.Start();

            if (m_options.dns || m_options.installationMode)
            {
                HFTDnsRunner dnsRunner = new HFTDnsRunner();
                dnsRunner.Start(ipv4Address, ipv6Address, 53);
            }

            // There's no HFTSite because were in installationMode which means there's no internet


            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

            return(0);
        }
예제 #16
0
        void Awake()
        {
            m_connectToServerOnStart = enabled;
            m_options = new HFTRuntimeOptions(happyfuntimesOptions);

            m_server = new GameServer(m_options, gameObject);
            m_server.OnConnect += Connected;
            m_server.OnDisconnect += Disconnected;

            m_hftManager = new HFTManager();
            m_hftManager.OnReady += StartGameServer;
            m_hftManager.OnFail += FailedToStart;

            if (maxPlayers > 0)
            {
                int timeoutForDisconnectedPlayerToReconnect = 0;
                m_playerManager = new HFTPlayerManager(m_server, gameObject, maxPlayers, timeoutForDisconnectedPlayerToReconnect, GetPrefab);
            }
            else
            {
                m_server.OnPlayerConnect += StartNewPlayer;
            }
        }
예제 #17
0
 void AssignAsServerForGame(HFTRuntimeOptions data)  // ???????????????????
 {
     client_.OnMessageEvent -= HandleMessage;
     client_.OnCloseEvent   -= HandleDisconnect;
     gameManager_.AssignAsClientForGame(data, client_);
 }