/// <summary>
        /// Deinitialization method cleaning all the created objects
        /// </summary>
        public async Task Deinitialize()
        {
            //Client should wait for master to break the connection
            if (IsClient)
            {
                while (Client.Connection.ConnectedPeersCount > 0)
                {
                    await Task.Delay(20);
                }
            }
            StopConnection();

            var type = Type;
            var clearObjectsAction = new Action(() =>
            {
                ClearNetworkObjects(type);
            });

            ThreadingUtilities.DispatchToMainThread(clearObjectsAction);

            LocalAddresses.Clear();
            MasterAddresses.Clear();
            Type = ClusterNodeType.NotClusterNode;
            CurrentSimulation = null;
            Log.Info("Deinitialized the Simulation Network data.");
        }
示例#2
0
        /// <summary>
        /// Initialization method for setting up the simulation network
        /// </summary>
        /// <param name="simulationId">This machine simulation id</param>
        /// <param name="clusterData">Cluster data for this simulation</param>
        /// <param name="settings">Settings used in this simulation</param>
        public void Initialize(string simulationId, ClusterData clusterData, NetworkSettings settings)
        {
            ClusterData = clusterData;
            Settings    = settings;

            //Check what cluster node type is this machine in the simulation
            if (ClusterData.Instances.Length <= 1)
            {
                Type = ClusterNodeType.NotClusterNode;
            }
            else
            {
                //Set this machine type
                var instancesData = ClusterData.Instances.Where(instance => instance.SimId == simulationId).ToArray();
                var resultsCount  = instancesData.Length;
                if (resultsCount == 0)
                {
                    throw new ArgumentException(
                              $"Invalid cluster settings. Could not find instance data for the simulation id '{simulationId}'.");
                }
                if (resultsCount > 1)
                {
                    throw new ArgumentException(
                              $"Invalid cluster settings. Found multiple instance data for the simulation id '{simulationId}'.");
                }
                var thisInstanceData = instancesData[0];
                Type            = thisInstanceData.IsMaster ? ClusterNodeType.Master : ClusterNodeType.Client;
                LocalIdentifier = thisInstanceData.SimId;
                foreach (var ip in thisInstanceData.Ip)
                {
                    LocalAddresses.Add(new IPEndPoint(IPAddress.Parse(ip), Settings.ConnectionPort));
                }

                //Setup master addresses
                var masterInstanceData = ClusterData.Instances.Where(instance => instance.IsMaster).ToArray();
                resultsCount = masterInstanceData.Length;
                if (resultsCount == 0)
                {
                    throw new ArgumentException($"Invalid cluster settings. Could not find master instance data.");
                }
                if (resultsCount > 1)
                {
                    throw new ArgumentException($"Invalid cluster settings. Found multiple master instance data.");
                }
                MasterIdentifier = masterInstanceData[0].SimId;
                foreach (var ip in masterInstanceData[0].Ip)
                {
                    MasterAddresses.Add(new IPEndPoint(IPAddress.Parse(ip), Settings.ConnectionPort));
                }
            }

            //Initialize network objects
            var mainThreadDispatcher = Object.FindObjectOfType <MainThreadDispatcher>();

            if (mainThreadDispatcher == null)
            {
                var dispatcher = new GameObject("MainThreadDispatcher");
                Object.DontDestroyOnLoad(dispatcher);
                dispatcher.AddComponent <MainThreadDispatcher>();
            }
            if (Type == ClusterNodeType.Master)
            {
                var masterGameObject = new GameObject("MasterManager");
                Object.DontDestroyOnLoad(masterGameObject);
                Master = masterGameObject.AddComponent <MasterManager>();
                Master.SetSettings(Settings);
                var clientsIdentifiers = ClusterData.Instances.Where(instanceData => !instanceData.IsMaster)
                                         .Select(instanceData => instanceData.SimId).ToList();
                Master.StartConnection(clientsIdentifiers);
            }
            else if (Type == ClusterNodeType.Client)
            {
                var clientGameObject = new GameObject("ClientManager");
                Object.DontDestroyOnLoad(clientGameObject);
                Client = clientGameObject.AddComponent <ClientManager>();
                clientGameObject.AddComponent <MainThreadDispatcher>();
                Client.SetSettings(Settings);
                Client.StartConnection();
                Client.TryConnectToMaster();
            }
            Log.Info("Initialized the Simulation Network data.");
        }