コード例 #1
0
        public void StartServer()
        {
            m_log.Info("Start Server");
            m_listening = true;

            // Where should this be checked?
            string controllerPath = "/" + m_options.controllerFilename;

            if (!HFTWebFileDB.GetInstance().FileExists(controllerPath))
            {
                throw new System.ArgumentException(
                          "\"Assets/WebPlayerTemplates/HappyFunTimes" + controllerPath + "\" does not exist. Did you forget to set \"controllerFilename\" in your \"PlayerSpawner\" or \"PlayerConnector\"?");
            }

            #if UNITY_STANDALONE_OSX
            // TODO make 2 classes, one for running internal server, one for external?
            if (m_options.startExternalServer)
            {
                StartExternalServer(true);
                return;
            }
            #endif

            List <string> addresses = new List <string>();
            addresses.Add("http://[::0]:" + m_options.serverPort);
            #if UNITY_STANDALONE_WIN
            addresses.Add("http://0.0.0.0:" + m_options.serverPort);
            #endif

            if (m_options.installationMode)
            {
                addresses.Add("http://[::0]:80");
                #if UNITY_STANDALONE_WIN
                addresses.Add("http://0.0.0.0:80");
                #endif
            }
            else
            {
                var hftOptions = new HFTSite.Options();
                //hftOptions.port = ??
                HFTUtil.SetIfNotNullOrEmpty(m_options.rendezvousUrl, ref hftOptions.rendezvousUrl);
                HFTUtil.SetIfNotNullOrEmpty(m_options.serverPort, ref hftOptions.port);
                m_hftSite = m_gameObject.AddComponent <HFTSite>();
                m_hftSite.Init(hftOptions);
            }

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

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

            if (m_options.dns || m_options.installationMode)
            {
                m_dnsRunner = new HFTDnsRunner();
                m_dnsRunner.Start(ipv4Address, ipv6Address, 53);
            }
        }
コード例 #2
0
        // This runs continously, every second it checks for new addresses
        // If they're new it starts the informers. In pumps the informers
        // until they're finished (success or failure)
        IEnumerator CheckAddresses()
        {
            for (;;)
            {
                // I'm not sure what to do here. The original node.js version
                // had advantages here. One is it's naturally multi-threaded
                // so this takes zero time from the main thread.
                //
                // The other is the game wasn't running in node it was
                // running in either the browser or unity so again no
                // effect on the game.
                //
                // I could move this to another thread but the reason this
                // exists is that students would move their machine to another
                // network, stop and start unity but that wouldn't start and stop
                // node.js so without this polling node.js would have kept on
                // the old network.
                //
                // Now that this is in Unity it's more likely the user
                // will start and stop Unity so no reason to poll.
                //
                // For now I'm just keeping this code in here but
                // it only looks for new addresses once.
                bool haveNewAddresses = false;

                if (once_ || forever_)
                {
                    once_ = false;
                    string[] addresses       = HFTIpUtils.GetLocalIPAddresses();
                    string   newAddressesStr = String.Join(", ", addresses);
                    if (!newAddressesStr.Equals(oldAddressesStr_))
                    {
                        oldAddressesStr_ = newAddressesStr;
                        var data = new InformData(addresses, options_.port);
                        var json = Serializer.Serialize(data);
                        log_.Info("sending: " + json);
                        addressBytes_    = System.Text.Encoding.UTF8.GetBytes(json);
                        haveNewAddresses = true;
                    }
                }

                foreach (Informer informer in informers_)
                {
                    informer.Inform(haveNewAddresses, this, addressBytes_, oldAddressesStr_);
                }

                yield return(new WaitForSeconds(1.0f));
            }
        }
コード例 #3
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);
        }