IEnumerator PingClock() { m_www = new WWW(m_url, m_timeCmdBytes, m_headers); double sendTime = HFTUtil.GetJSTimeInSeconds(); yield return(m_www); double receiveTime = HFTUtil.GetJSTimeInSeconds(); string err = m_www.error; string result = m_www.text; m_www = null; if (!String.IsNullOrEmpty(err)) { m_log.Tell("error: " + err + ", result:" + result); } else { HFTTimePing ping = JsonUtility.FromJson <HFTTimePing>(result); if (ping != null && ping.time > 0.0) { double duration = receiveTime - sendTime; double serverTime = ping.time + duration / 2; s_timeOffsetSeconds = serverTime - receiveTime; } } }
IEnumerator PingClock() { using (var www = new UnityWebRequest(m_url, UnityWebRequest.kHttpVerbPOST)) { www.uploadHandler = new UploadHandlerRaw(m_timeCmdBytes); www.uploadHandler.contentType = "application/json"; www.downloadHandler = new DownloadHandlerBuffer(); www.SetRequestHeader("Content-Type", "application/json"); double sendTime = HFTUtil.GetJSTimeInSeconds(); yield return(www.SendWebRequest()); double receiveTime = HFTUtil.GetJSTimeInSeconds(); string err = www.error; string result = www.downloadHandler.text; if (www.isHttpError || www.isNetworkError && !String.IsNullOrEmpty(err)) { m_log.Tell("error: " + err + ", result:" + result); } else { HFTTimePing ping = JsonUtility.FromJson <HFTTimePing>(result); if (ping != null && ping.time > 0.0) { double duration = receiveTime - sendTime; double serverTime = ping.time + duration / 2; s_timeOffsetSeconds = serverTime - receiveTime; } } } }
void HandleCmdTime(HttpListenerRequest req, HttpListenerResponse res) { double seconds = HFTUtil.GetJSTimeInSeconds(); string timeStr = Serializer.Serialize(new HFTTimePing(seconds)); byte[] time = System.Text.Encoding.UTF8.GetBytes(timeStr); m_webServerUtils.SendJsonBytes(res, time); }
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); } }
bool FileExistsAndSame(string filename, string shaFilename, string sha256) { if (System.IO.File.Exists(filename)) { if (System.IO.File.Exists(shaFilename)) { string oldSha = HFTUtil.ReadText(shaFilename); return(sha256.Equals(oldSha)); } } return(false); }
bool SendCaptivePortalHTML(HttpListenerRequest req, HttpListenerResponse res, string sessionId, string path) { //var fullPath = path.normalize(path.join(this.options.baseDir, opt_path)); byte[] content = null; if (!m_webServerUtils.GetGameFile(path, out content)) { res.StatusCode = (int)HttpStatusCode.NotFound; return(true); } string str = System.Text.Encoding.UTF8.GetString(content); Dictionary <string, string> subs = new Dictionary <string, string>(); subs["startUrl"] = GetBaseUrl(req) + m_firstPath + "?sessionId=" + sessionId; subs["sessionId"] = sessionId; str = HFTUtil.ReplaceParamsFlat(str, subs); m_log.Info("SCPH: Sending " + path); return(m_webServerUtils.SendContent(res, path, str)); }
public bool GetFile(string path, out byte[] content) { path = path.Replace('\\', '/'); if (path.StartsWith("/")) { path = path.Substring(1); } if (isEditor_) { string livePath = Path.Combine(dataPath_, path); try { content = HFTUtil.ReadBytes(livePath); return(true); } catch (System.Exception) { } } return(files_.TryGetValue(path, out content)); }
void StartExternalServer(bool admin) { TextAsset serverData = LoadTextAsset("HFTOSXServer"); TextAsset shaData = LoadTextAsset("HFTOSXServer.sha256"); if (serverData == null || shaData == null) { return; } string serverPath = Application.persistentDataPath + "/hft-server"; string shaPath = Application.persistentDataPath + "/hft-server.sha"; if (!FileExistsAndSame(serverPath, shaPath, shaData.text)) { HFTUtil.WriteBytes(serverPath, serverData.bytes); HFTUtil.WriteText(shaPath, shaData.text); m_log.Info("wrote new webserver: " + serverPath + ", size: " + serverData.bytes.Length); } string cmdString = @"-e ' set myFile to quoted form of ""%(serverPath)s"" do shell script ""chmod -R 700 "" & myFile do shell script myFile %(admin)s '"; Dictionary <string, string> dict = new Dictionary <string, string>(); dict["serverPath"] = serverPath; dict["admin"] = admin ? "with administrator privileges" : ""; string script = HFTUtil.ReplaceParamsFlat(cmdString, dict); System.Diagnostics.Process p = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo psi = p.StartInfo; psi.EnvironmentVariables.Add("HFT_ARGS", Serializer.Serialize(m_options)); psi.UseShellExecute = false; psi.FileName = "/usr/bin/osascript"; psi.Arguments = script; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) => { if (m_listening && !String.IsNullOrEmpty(e.Data)) { m_log.Tell("webserver: stderr: " + e.Data); } }); p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) => { if (m_listening && !String.IsNullOrEmpty(e.Data)) { m_log.Info("webserver: stdout: " + e.Data); } }); p.Start(); p.BeginOutputReadLine(); p.BeginErrorReadLine(); if (p.WaitForExit(2000)) { // If it exited there was an error m_log.Tell("error starting webserver: exit code = " + p.ExitCode); } m_webServerProcess = p; }