private static IEnumerator ConnectToReceptionistAsync(WorkerConnectionParameters parameters, Action <Connection> onConnection) { // Creating a new worker ID per connection attempt. // This is because if the connection fails it will be impossible to reconnect without // restarting the application. string uniqueWorkerId; if (WorkerTypeUtils.FromWorkerName(parameters.ConnectionParameters.WorkerType) == WorkerPlatform.UnityWorker) { // for UnityWorker, the worker ID should be unique as it will be incremented in the command line arguments. uniqueWorkerId = parameters.WorkerId; } else { uniqueWorkerId = string.Format("{0}-{1}", parameters.WorkerId, workerConnectionIndex); workerConnectionIndex++; } var connectionFuture = Worker.Connection.ConnectAsync(parameters.ReceptionistHost, parameters.ReceptionistPort, uniqueWorkerId, parameters.ConnectionParameters); // ReSharper disable once AccessToDisposedClosure var wait = new WaitUntil(() => connectionFuture.Get(ReturnImmediatelyMillis).HasValue); yield return(wait); onConnection(connectionFuture.Get()); connectionFuture.Dispose(); }
private static IEnumerator ConnectToReceptionistAsync(WorkerConnectionParameters parameters, Action <Connection> onConnection) { var connectionFuture = Worker.Connection.ConnectAsync(parameters.ReceptionistHost, parameters.ReceptionistPort, parameters.WorkerId, parameters.ConnectionParameters); // ReSharper disable once AccessToDisposedClosure var wait = new WaitUntil(() => connectionFuture.Get(ReturnImmediatelyMillis).HasValue); yield return(wait); onConnection(connectionFuture.Get()); connectionFuture.Dispose(); }
private static IEnumerator ConnectToLocatorAsync(WorkerConnectionParameters parameters, Deployment deployment, Action <Connection> onConnection) { var locator = CreateLocator(parameters); var connectionFuture = locator.ConnectAsync(deployment.DeploymentName, parameters.ConnectionParameters, OnQueueStatusThunk); // ReSharper disable once AccessToDisposedClosure var wait = new WaitUntil(() => connectionFuture.Get(ReturnImmediatelyMillis).HasValue); yield return(wait); onConnection(connectionFuture.Get()); connectionFuture.Dispose(); locator.Dispose(); }
/// <summary> /// Returns a list of deployments associated with the current ProjectName. /// </summary> /// <returns> /// A list of available deployments. /// </returns> /// <remarks> /// Is it invalid to call this method again before the listReceived callback is invoked. /// </remarks> public static IEnumerator GetDeploymentListAsync(WorkerConnectionParameters parameters, Action <DeploymentList, Action <Deployment> > listReceived, Action <Deployment> handleDeploymentChosen) { var locator = CreateLocator(parameters); var future = locator.GetDeploymentListAsync(); // ReSharper disable once AccessToDisposedClosure var wait = new WaitUntil(() => future.Get(ReturnImmediatelyMillis).HasValue); yield return(wait); listReceived(future.Get(), handleDeploymentChosen); future.Dispose(); locator.Dispose(); }
/// <summary> /// Call this to initiate a connection to SpatialOS, using settings specified in <c>WorkerConfiguration</c>. /// </summary> /// <remarks> /// Is it invalid to call this method if IsConnected is true. /// </remarks> public static IEnumerator ConnectAsync(WorkerConnectionParameters parameters, Deployment?deployment, Action <Connection> onConnection) { if (SpatialOS.Connection != null) { throw new InvalidOperationException("ConnectAsync called while already connected"); } IEnumerator connect; if (deployment.HasValue) { connect = ConnectToLocatorAsync(parameters, deployment.Value, onConnection); } else { connect = ConnectToReceptionistAsync(parameters, onConnection); } yield return(connect); }
private void Start() { PrintClientInfo(); parameters = CreateWorkerConnectionParameters(SpatialOS.Configuration); View = new View(); View.OnDisconnect(SpatialOS.Disconnected); deferredActionDispatcher = new DeferredActionDispatcher(); spatialCommunicator = new SpatialCommunicator(null, View, deferredActionDispatcher); eventHandler = new DispatchEventHandler(this, spatialCommunicator); InitializeEntityPipeline(); // The current MonoBehaviour could be destroyed in response to aborted connection attempts, // causing leaks of unmanaged resources due to in-progress coroutines not running to completion. // Create a new host MonoBehaviour and run on it instead. var starter = gameObject.GetComponent <ConnectionCoroutineHost>() ?? gameObject.AddComponent <ConnectionCoroutineHost>(); starter.StartCoroutine(Connect()); }
private static Locator CreateLocator(WorkerConnectionParameters parameters) { return(new Locator(parameters.LocatorHost, parameters.LocatorParameters)); }