async Task Connect() { try { simInfo = CloudAPI.GetInfo(); Status = ConnectionStatus.Connecting; DisconnectReason = null; RunOnUnityThread(() => { ConnectionUI.instance.UpdateStatus(); }); foreach (var timeOut in timeOutSequence) { try { var reader = await API.Connect(simInfo); await ReadResponse(reader); break; } catch (HttpRequestException ex) { // temporary network issue, we'll retry Debug.Log(ex.Message + ", reconnecting after " + timeOut + " seconds"); await Task.Delay(1000 *timeOut); } if (Status == ConnectionStatus.Offline) { Debug.Log("User cancelled connection."); break; } } if (Config.RetryForever) { while (true) { try { var reader = await API.Connect(simInfo); await ReadResponse(reader); break; } catch (CloudAPI.NoSuccessException ex) { Debug.Log(ex.Message + ", reconnecting after " + timeOutSequence[timeOutSequence.Length - 1] + " seconds"); DisconnectReason = ex.Message; await Task.Delay(1000 *timeOutSequence[timeOutSequence.Length - 1]); } } } } catch (CloudAPI.NoSuccessException ex) { // WISE told us it does not like us, so stop reconnecting DisconnectReason = ex.Message; Debug.Log($"WISE backend reported error: {ex.Message}, will not reconnect"); } catch (TaskCanceledException) { Debug.Log("Linking task canceled."); DisconnectReason = "Linking task canceled."; } catch (System.Net.Sockets.SocketException se) { Debug.Log($"Could not reach WISE SSE at {Config.CloudUrl}: {se.Message}"); DisconnectReason = $"Could not reach WISE SSE at {Config.CloudUrl}: {se.Message}"; } catch (Exception ex) { Debug.LogException(ex); } Debug.Log("Giving up reconnecting."); Disconnect(); }
async void LinkTask(SimulatorInfo simInfo) { try { var stream = await API.Connect(simInfo); string line; using var reader = new StreamReader(stream); while (true) { var lineTask = reader.ReadLineAsync(); if (await Task.WhenAny(lineTask, Task.Delay(30000)) != lineTask) { Debug.Log("Took to long to link to cluster, aborting"); return; } line = lineTask.Result; if (line == null) { break; } if (line.StartsWith("data:") && !string.IsNullOrEmpty(line.Substring(6))) { JObject deserialized = JObject.Parse(line.Substring(5)); if (deserialized != null && deserialized.HasValues) { var status = deserialized.GetValue("status"); if (status != null) { switch (status.ToString()) { case "Unrecognized": Application.OpenURL(Config.CloudUrl + "/clusters/link?token=" + simInfo.linkToken); break; case "OK": Debug.Log("appear to be linked!"); Refresh(); linked = true; return; default: return; } } } } } } catch (Exception e) { Debug.LogError("error linking editor instance to wise"); Debug.LogException(e); } finally { Debug.Log("closing api"); API.Disconnect(); } }
async void Refresh() { if (EditorApplication.isPlayingOrWillChangePlaymode) { return; } try { updating = true; ErrorMessage = ""; Simulator.Web.Config.ParseConfigFile(); DatabaseManager.Init(); var csservice = new Simulator.Database.Services.ClientSettingsService(); ClientSettings cls = csservice.GetOrMake(); Config.SimID = cls.simid; if (String.IsNullOrEmpty(Config.CloudUrl)) { ErrorMessage = "Cloud URL not set"; return; } if (String.IsNullOrEmpty(Config.SimID)) { ErrorMessage = "Simulator not linked"; return; } if (API != null) { API.Disconnect(); } API = new CloudAPI(new Uri(Config.CloudUrl), Config.SimID); var simInfo = CloudAPI.GetInfo(); var reader = await API.Connect(simInfo); await API.EnsureConnectSuccess(); var ret = await API.GetLibrary <VehicleDetailData>(); CloudVehicles = ret.ToList(); string[] guids = AssetDatabase.FindAssets("t:Prefab", new[] { "Assets/External/Vehicles" }); LocalVehicles = guids.Select(g => AssetDatabase.GUIDToAssetPath(g)).ToList(); string idOrPath = null; if (DeveloperSimulation.Vehicles != null) // get previously selected thing { // we abuse VehicleData.Id to store the prefab path idOrPath = DeveloperSimulation.Vehicles[0].Id; } if (idOrPath != null) { // find index of previously selected thing in new dataset var vehicleChoices = VehicleChoices; var selected = vehicleChoices.FindIndex(v => v.idOrPath == idOrPath); SetVehicleFromSelectionIndex(vehicleChoices, selected); await UpdateCloudVehicleDetails(); } DeveloperSimulation.NPCs = Config.NPCVehicles.Values.ToArray(); // TODO get from cloud and refresh config.cs LoadExternalAssets() } catch (Exception ex) { Debug.LogException(ex); ErrorMessage = ex.Message; if (ex.InnerException != null) { ErrorMessage += "\n" + ex.InnerException.Message; } API.Disconnect(); } finally { updating = false; Repaint(); } }