The NetworkEngine which encapsulates all of the peer to peer functionality of the Terrarium application.
Beispiel #1
0
        private void sendAssemblyToPeer(string address, object state)
        {
            const int buffSize     = 4096;
            var       uploadBuffer = new byte[buffSize];
            var       theOrganism  = (TeleportState)state;
            var       assemblyName = ((Species)theOrganism.OrganismState.Species).AssemblyInfo.FullName;

            byte[] assemblyBuf;

            var req = checkToSeeIfPeerHasThisAssembly(address, assemblyName, out assemblyBuf);

            writeAssemblyNameToRequest(req, assemblyBuf);
            var assemblyVersionResponse = getAssemblyVersionResponse(req);

            sendAssemblyIfNecessary(buffSize, state, address, assemblyVersionResponse, uploadBuffer);
            req = sendObjectState(state, address);
            var response = sendRequest(req);

            _engine.WriteProtocolInfo("sendAssemblyToPeer: Organism state sent.");

            // Look for <organismArrived>false</organismArrived>, because that means the organism
            // didn't make it and should be teleported locally
            if (NetworkEngine.GetValueFromContent("<organismArrived>", "</organismArrived>", response) == "false")
            {
                var errorMessage = "The organism didn't arrive on their side for some reason.";
                if (NetworkEngine.GetValueFromContent("<reason>", "</reason>", response) != null)
                {
                    errorMessage = "The organism didn't arrive on the remote peer. " +
                                   NetworkEngine.GetValueFromContent("<reason>", "</reason>", response);
                }
                throw new AbortPeerDiscussionException(errorMessage);
            }

            return;
        }
 internal TeleportWorkItem(NetworkEngine engine, string address, object state, int httpPort, int networkTimeoutMsec, TerrariumLed led)
 {
     this.engine = engine;
     this.address = address;
     this.state = state;
     this.httpPort = httpPort;
     this.peerConnectionLed = led;
     this.networkTimeoutMsec = networkTimeoutMsec;
 }
 internal TeleportWorkItem(NetworkEngine engine, string address, object state, int httpPort,
                           int networkTimeoutMsec, TerrariumLed led)
 {
     _engine = engine;
     _address = address;
     _state = state;
     _httpPort = httpPort;
     _peerConnectionLed = led;
     _networkTimeoutMsec = networkTimeoutMsec;
 }
Beispiel #4
0
 internal TeleportWorkItem(NetworkEngine engine, string address, object state, int httpPort,
                           int networkTimeoutMsec, TerrariumLed led)
 {
     _engine             = engine;
     _address            = address;
     _state              = state;
     _httpPort           = httpPort;
     _peerConnectionLed  = led;
     _networkTimeoutMsec = networkTimeoutMsec;
 }
Beispiel #5
0
        private NameValueCollection getPeerTerrariumInfo(string address)
        {
            address = "http://" + address + ":" + _httpPort + "/version";
            string content;
            var    peerInfo = new NameValueCollection();

            var req = (HttpWebRequest)WebRequest.Create(address);

            req.UserAgent = "Microsoft .NET Terrarium";
            req.Timeout   = _networkTimeoutMsec;

            using (var resp = req.GetResponse())
            {
                using (var reader = new StreamReader(resp.GetResponseStream(), Encoding.ASCII))
                {
                    content = reader.ReadToEnd();
                    reader.Close();
                }
                resp.Close();
            }

            var tempString = NetworkEngine.GetValueFromContent("<major>", "</major>", content);
            var major      = Convert.ToInt32(tempString);

            tempString = NetworkEngine.GetValueFromContent("<minor>", "</minor>", content);
            var minor = Convert.ToInt32(tempString);

            tempString = NetworkEngine.GetValueFromContent("<build>", "</build>", content);
            var build = Convert.ToInt32(tempString);

            var currentChannel = NetworkEngine.GetValueFromContent("<channel>", "</channel>", content);

            peerInfo["major"] = major.ToString();
            peerInfo["minor"] = minor.ToString();
            peerInfo["build"] = build.ToString();

            if (currentChannel != null)
            {
                peerInfo["channel"] = currentChannel;
            }

            return(peerInfo);
        }
Beispiel #6
0
        /// <summary>
        ///  Stops the game and determines if the final tick of data should be serialized.
        /// </summary>
        /// <param name="serializeState">Controls if the state is serialized.</param>
        public void StopGame(Boolean serializeState)
        {
            // This needs to be the first call in stop game because if it fails, the game needs to be able
            // to continue.  In addition, it may jump the engine ahead a few phases, and if the engine isn't
            // in the normal running state this could cause things to fail since the engine phases are expecting
            // things to be in a normal state.
            if (serializeState)
            {
                this.serializeState(_currentStateFileName);
            }

            if (_usingNetwork && _networkEngine != null)
            {
                _networkEngine.ShutdownNetwork();
                _networkEngine = null;
                _usingNetwork = false;
            }

            // Shut down populationData after serializing since serializing
            // may advance to the end of the tick
            if (_populationData != null)
            {
                _populationData.Close();
                _populationData = null;
            }

            AppMgr.DestroyScheduler();
            _pac.Close();
            _ledIndicators = null;
            _engine = null;
        }
Beispiel #7
0
        /// <summary>
        ///  Constructs a new game engine.
        /// </summary>
        /// <param name="dataPath">The path to save game directory.</param>
        /// <param name="useNetwork">Controls the usage of the network engine.</param>
        /// <param name="deserializeState">Controls if the state is deserialized or not.</param>
        /// <param name="fileName">The path to the state file.</param>
        /// <param name="reportData">Determines if data should be reported.</param>
        /// <param name="leds">Provides a listing of game leds that can be used.</param>
        /// <param name="trackLastRun">Controls whether the PAC keeps track of the last run creature for blacklisting.</param>
        private GameEngine(string dataPath, bool useNetwork, bool deserializeState, string fileName, bool reportData,
            TerrariumLed[] leds, bool trackLastRun)
        {
            _ledIndicators = leds;
            _currentStateFileName = fileName;

            // test to make sure we're not violating any constraints by current
            // physics settings in the engine.
            EngineSettings.EngineSettingsAsserts();

            // Calculate quanta and worldsize if we haven't done so yet
            if (_reloadSettings)
                CalculateWorldSize();

            _pac = new PrivateAssemblyCache(dataPath, fileName, true, trackLastRun);

            // Reset the appdomain policy since we changed the location of the organism dlls
            // This must be done before any animals are loaded in any way.  Make sure this call stays
            // as soon as possible
            AppDomain.CurrentDomain.SetAppDomainPolicy(SecurityUtils.MakePolicyLevel(_pac.AssemblyDirectory));

            _usingNetwork = useNetwork;
            _populationData = new PopulationData(reportData, leds[(int) LedIndicators.ReportWebService]);

            // Should only happen if an exception prevented a previous attempt to start a game
            if (AppMgr.CurrentScheduler != null)
            {
                AppMgr.DestroyScheduler();
            }

            // Create a scheduler that manages giving the creatures timeslices
            _scheduler = AppMgr.CreateSameDomainScheduler(this);
            _scheduler.Quantum = _organismQuanta;

            if (useNetwork)
            {
                // Required to start up the network listeners
                _networkEngine = new NetworkEngine();
            }

            WorldState currentState;
            Boolean successfulDeserialization = false;
            if (deserializeState && File.Exists(fileName))
            {
                try
                {
                    if (_pac.LastRun.Length != 0)
                    {
                        // The process was killed while an organism was being run, blacklist it
                        // Since this potentially means the animal hung the game.
                        _pac.BlacklistAssemblies(new string[] {_pac.LastRun});
                    }
                    this.deserializeState(fileName);
                    currentState = CurrentVector.State;
                    _scheduler.CurrentState = currentState;
                    _scheduler.CompleteOrganismDeserialization();
                    successfulDeserialization = true;
                }
                catch (Exception e)
                {
                    ErrorLog.LogHandledException(e);
                }
            }

            if (successfulDeserialization) return;

            // Set up initial world state
            currentState = new WorldState(GridWidth, GridHeight);
            currentState.TickNumber = 0;
            currentState.StateGuid = Guid.NewGuid();
            currentState.Teleporter = new Teleporter(AnimalCount/EngineSettings.NumberOfAnimalsPerTeleporter);
            currentState.MakeImmutable();

            WorldVector vector = new WorldVector(currentState);
            CurrentVector = vector;
            _scheduler.CurrentState = currentState;
        }
 public OrganismsNamespaceHandler(NetworkEngine engine)
 {
     _engine = engine;
 }
 public OrganismsNamespaceHandler(NetworkEngine engine)
 {
     _engine = engine;
 }