Beispiel #1
0
    public void ScanHost()
    {
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());

        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                string address    = ip.ToString();
                string subAddress = address.Remove(address.LastIndexOf('.'));

                //if (!LocalAddresses.Contains(address))
                //{
                LocalAddresses.Add(address);
                //}

                if (!LocalSubAddresses.Contains(subAddress))
                {
                    LocalSubAddresses.Add(subAddress);
                }
            }
        }
    }
Beispiel #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.");
        }