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 ConnectionParameters CreateConnectionParameters(WorkerConfiguration instance) { var tcpParameters = new TcpNetworkParameters { MultiplexLevel = instance.TcpMultiplexLevel }; var raknetParameters = new RakNetNetworkParameters { HeartbeatTimeoutMillis = instance.RaknetHeartbeatTimeoutMillis }; var parameters = new ConnectionParameters { Network = { ConnectionType = instance.LinkProtocol, Tcp = tcpParameters, RakNet = raknetParameters, UseExternalIp = instance.UseExternalIp, }, WorkerType = WorkerTypeUtils.ToWorkerName(instance.WorkerPlatform), BuiltInMetricsReportPeriodMillis = 2000, EnableProtocolLoggingAtStartup = SpatialOS.Configuration.ProtocolLoggingOnStartup, ProtocolLogging = { LogPrefix = SpatialOS.Configuration.ProtocolLogPrefix, MaxLogFiles = 10, MaxLogFileSizeBytes = SpatialOS.Configuration.ProtocolLogMaxFileBytes }, ReceiveQueueCapacity = instance.ReceiveQueueCapacity, SendQueueCapacity = instance.SendQueueCapacity, }; return(parameters); }
protected virtual IAssetLoader <GameObject> InitializeAssetLoader() { var projectName = SpatialOS.Configuration.ProjectName; var deployment = SpatialOS.Deployment; var assemblyName = deployment.HasValue ? deployment.Value.AssemblyName : SpatialOS.Configuration.AssemblyName; // If an assembly name is set, default to streaming from it. The strategy can still be overridden by the command line. AssetDatabaseStrategy defaultStrategy = AssetDatabaseStrategy.Local; if (!string.IsNullOrEmpty(projectName) && !string.IsNullOrEmpty(assemblyName)) { defaultStrategy = AssetDatabaseStrategy.Streaming; } UseLocalPrefabs = SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.UseLocalPrefabs, UseLocalPrefabs); LoadingStrategy = SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.AssetDatabaseStrategy, defaultStrategy); LocalAssetDatabasePath = SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.LocalAssetDatabasePath, LocalAssetDatabasePath); IAssetLoader <GameObject> gameObjectLoader; if (Application.isEditor && UseLocalPrefabs) { gameObjectLoader = new PrefabGameObjectLoader(); } else { switch (LoadingStrategy) { case AssetDatabaseStrategy.Local: var path = Path.GetFullPath(LocalAssetDatabasePath); gameObjectLoader = new GameObjectFromAssetBundleLoader(new LocalAssetBundleLoader(path)); break; case AssetDatabaseStrategy.Streaming: pendingPrepareTemplates = new List <Action>(); var cachePath = Path.Combine(Application.persistentDataPath, "cache" + WorkerTypeUtils.ToWorkerName(SpatialOS.Configuration.WorkerPlatform)); Directory.CreateDirectory(cachePath); var assetBundleDownloader = new AssetBundleDownloader(cachePath, new MachineCache <byte[], AssetBundle>(Path.Combine(cachePath, "assets"), new AssetBundlePersistenceStrategy()), new MachineCache <CacheEntry, CacheEntry>(Path.Combine(cachePath, "asset-metadata"), new AssetMetadataPersistenceStrategy()), new WWWRequest(), this); assetBundleDownloader.GetAssetUrl = GetAssetUrl; var exponentialBackoffRetryAssetLoader = gameObject.GetComponent <ExponentialBackoffRetryAssetLoader>() ?? gameObject.AddComponent <ExponentialBackoffRetryAssetLoader>(); exponentialBackoffRetryAssetLoader.Init(assetBundleDownloader, SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.MaxAssetLoadingRetries, -1), SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.AssetLoadingRetryBackoffMilliseconds, -1)); gameObjectLoader = new GameObjectFromAssetBundleLoader(exponentialBackoffRetryAssetLoader); pendingUrlResolveRequest = CloudAssemblyArtifactResolver.ResolveAssetUrls(this, new WWWRequest(), SpatialOS.Configuration.InfraServiceUrl, projectName, assemblyName, OnAssetBundleNameToUrlMapResolved, OnAssetResolveFailed); break; default: throw new Exception(string.Format("Unknown loading strategy '{0}'", LoadingStrategy)); } } return(gameObjectLoader); }
private void Start() { workerType = WorkerTypeUtils.ToWorkerName(SpatialOS.Configuration.WorkerPlatform); }
/// <summary> /// Constructs a new instance of <c>WorkerConfiguration</c>. /// </summary> /// <param name="data">User-configured data used to configure the worker.</param> /// <param name="commandLineArguments"> /// A list of arguments specified on the command line. /// If null, defaults to /// <example>global::System.Environment.GetCommandLineArgs()</example> /// </param> public WorkerConfiguration(WorkerConfigurationData data, IList <string> commandLineArguments = null) { if (commandLineArguments == null) { commandLineArguments = global::System.Environment.GetCommandLineArgs(); } Debug.LogFormat("Command line {0}", string.Join(" ", commandLineArguments.ToArray())); commandLineDictionary = CommandLineUtil.ParseCommandLineArgs(commandLineArguments); ProjectName = string.Empty; if (Application.isEditor) { // Read ProjectName from the spatialos.json file, if not already specified. // This is only done in Editor mode, as we do not expect spatialos.json to exist outside of dev environment. if (!commandLineDictionary.ContainsKey(CommandLineConfigNames.ProjectName) && !commandLineDictionary.ContainsKey(CommandLineConfigNames.AppName)) { try { ProjectName = ProjectDescriptor.Load().Name; } catch (Exception e) { Debug.LogErrorFormat("Cannot read project name from '{0}'. You will not be able to connect to a deployment. Underlying error: {1}", ProjectDescriptor.ProjectDescriptorPath, e); } } } var defaultWorkerPlatform = data.SpatialOsApplication.WorkerPlatform; #pragma warning disable 618 var workerPlatformString = GetObsoleteAndCurrentCommandLineValue(EditableConfigNames.WorkerType, EditableConfigNames.EngineType, string.Empty); #pragma warning restore 618 if (!string.IsNullOrEmpty(workerPlatformString)) { defaultWorkerPlatform = WorkerTypeUtils.FromWorkerName(workerPlatformString); } var workerName = WorkerTypeUtils.ToWorkerName(data.SpatialOsApplication.WorkerPlatform); appName = GetCommandLineValue(CommandLineConfigNames.AppName, string.Empty); ProjectName = GetCommandLineValue(CommandLineConfigNames.ProjectName, ProjectName); AssemblyName = GetCommandLineValue(EditableConfigNames.AssemblyName, data.SpatialOsApplication.AssemblyName); DeploymentId = GetCommandLineValue(EditableConfigNames.DeploymentId, data.SpatialOsApplication.DeploymentId); DeploymentTag = GetCommandLineValue(EditableConfigNames.DeploymentTag, data.SpatialOsApplication.DeploymentTag); #pragma warning disable 618 WorkerId = GetObsoleteAndCurrentCommandLineValue(EditableConfigNames.WorkerId, EditableConfigNames.EngineId, workerName + Guid.NewGuid()); #pragma warning restore 618 WorkerPlatform = defaultWorkerPlatform; #pragma warning disable 618 EntityCreationLimitPerFrame = GetCommandLineValue(EditableConfigNames.EntityCreationLimitPerFrame, data.Unity.EntityCreationLimitPerFrame); #pragma warning restore 618 InfraServiceUrl = GetCommandLineValue(EditableConfigNames.InfraServiceUrl, data.Debugging.InfraServiceUrl); ReceptionistHost = GetCommandLineValue(EditableConfigNames.ReceptionistHost, data.Networking.ReceptionistHost); ReceptionistPort = GetCommandLineValue(EditableConfigNames.ReceptionistPort, data.Networking.ReceptionistPort); LinkProtocol = GetCommandLineValue(EditableConfigNames.LinkProtocol, data.Networking.LinkProtocol); LocatorHost = GetCommandLineValue(EditableConfigNames.LocatorHost, data.Networking.LocatorHost); LoginToken = GetLoginTokenConfig(data); ProtocolLogPrefix = GetCommandLineValue(EditableConfigNames.ProtocolLogPrefix, data.Debugging.ProtocolLogPrefix); ProtocolLoggingOnStartup = GetCommandLineValue(EditableConfigNames.ProtocolLoggingOnStartup, data.Debugging.ProtocolLoggingOnStartup); ProtocolLogMaxFileBytes = GetCommandLineValue(EditableConfigNames.ProtocolLogMaxFileBytes, data.Debugging.ProtocolLogMaxFileBytes); RaknetHeartbeatTimeoutMillis = GetCommandLineValue(EditableConfigNames.RaknetHeartbeatTimeoutMillis, data.Networking.RaknetHeartbeatTimeoutMillis); ReceiveQueueCapacity = data.Networking.ReceiveQueueCapacity; SendQueueCapacity = data.Networking.SendQueueCapacity; SteamToken = GetCommandLineValue(EditableConfigNames.SteamToken, data.Networking.SteamToken); TcpMultiplexLevel = GetCommandLineValue(EditableConfigNames.TcpMultiplexLevel, data.Networking.TcpMultiplexLevel); UseExternalIp = GetCommandLineValue(CommandLineConfigNames.UseExternalIpForBridge, Defaults.UseExternalIp) == false; // DEV-1120: The flag is flipped for legacy reasons. UseInstrumentation = GetCommandLineValue(EditableConfigNames.UseInstrumentation, data.Debugging.UseInstrumentation); UsePrefabPooling = GetCommandLineValue(EditableConfigNames.UsePrefabPooling, data.Unity.UsePrefabPooling); LogDebugToSpatialOs = GetCommandLineValue(EditableConfigNames.LogDebugToSpatialOs, data.Debugging.LogDebugToSpatialOs); LogAssertToSpatialOs = GetCommandLineValue(EditableConfigNames.LogAssertToSpatialOs, data.Debugging.LogAssertToSpatialOs); LogWarningToSpatialOs = GetCommandLineValue(EditableConfigNames.LogWarningToSpatialOs, data.Debugging.LogWarningToSpatialOs); LogErrorToSpatialOs = GetCommandLineValue(EditableConfigNames.LogErrorToSpatialOs, data.Debugging.LogErrorToSpatialOs); LogExceptionToSpatialOs = GetCommandLineValue(EditableConfigNames.LogExceptionToSpatialOs, data.Debugging.LogExceptionToSpatialOs); if (string.IsNullOrEmpty(ProjectName)) { throw new ArgumentException(string.Format("The ProjectName must be set with {0}, or via command-line argument +{1}", Path.GetFileName(ProjectDescriptor.ProjectDescriptorPath), CommandLineConfigNames.ProjectName)); } if (Application.isEditor == false) { PrintWorkerConfigurationSettings(); } }