public void Worker()
        {
            Log("Waiting to recieve UDP messages on port {0}", this.clientEndPoint.Port);
            bool gotFirst = false;

            while (this.runThread)
            {
                try
                {
                    comms.Packet packet = comms.Packet.Read(this.socket.Receive(ref this.serverEndPoint));

                    if (!gotFirst)
                    {
                        Log("Received first UDP message from {0}:{1}",
                            this.serverEndPoint.Address.ToString(), this.serverEndPoint.Port);
                        gotFirst = true;
                    }

                    if (packet.Time != null)
                    {
                        this.TimeUpdate = packet.Time;
                    }

                    this.VesselUpdate = packet.Vessel;

                    // If target hasn't changed, don't process as an update
                    if (packet.Target == null || !packet.Target.Equals(this.cachedTarget))
                    {
                        this.cachedTarget = packet.Target;
                        this.TargetUpdate = packet.Target;
                    }

                    var maneuverUpdate = packet.ManeuverList;
                    // If they haven't changed, don't process as an update
                    if (maneuverUpdate != null && maneuverUpdate.Equals(this.cachedManeuvers))
                    {
                        continue;
                    }

                    // They've changed on the other end, update locally
                    this.cachedManeuvers = maneuverUpdate;
                    this.ManeuverUpdate  = maneuverUpdate;
                }
                catch (Exception e)
                {
                    Log("SocketWorker exception: {0} {1}", e.Message, e.StackTrace);
                    if (!(e is System.IO.IOException || e is SocketException))
                    {
                        throw;
                    }
                }
            }
        }
Пример #2
0
        public void UnityWorker()
        {
            // Kill the server if the user exits the current game
            if (HighLogic.LoadedScene == GameScenes.MAINMENU ||
                HighLogic.LoadedScene == GameScenes.CREDITS ||
                HighLogic.LoadedScene == GameScenes.SETTINGS)

            {
                Destroy(this.gameObject);
            }

            var message = this.socketWorker.logMessages.TryPop(null);

            if (message != null)
            {
                this.Log(message);
            }

            message = this.tcpWorker.logMessages.TryPop(null);
            if (message != null)
            {
                this.Log(message);
            }

            if (this.vesselListUpdateRequired)
            {
                if (!this.IsInvoking("SendVesselListUpdate"))
                {
                    this.Invoke("SendVesselListUpdate", 2.0f);
                }
                this.vesselListUpdateRequired = false;
            }

            // Don't send time or flight data when in the VAB, astronaut complex etc.
            if (!HighLogic.LoadedSceneHasPlanetarium)
            {
                //todo send sleep message or something, maybe cancel repeating
                return;
            }

            // No such thing as GameEvents.onQuickLoadOrRevert, so do it ourselves
            if (Planetarium.GetUniversalTime() < this.lastUniversalTime)
            {
                LogDebug("Time went backwards (quickload?).");
                this.vesselListUpdateRequired = true;
            }
            this.lastUniversalTime = Planetarium.GetUniversalTime();

            this.UpdateFromClient();

            try
            {
                var packet = new comms.Packet();
                packet.Time = new comms.Time(Planetarium.GetUniversalTime(),
                                             TimeWarp.CurrentRateIndex, TimeWarp.CurrentRate);

                var vessel = FlightGlobals.ActiveVessel;
                if (vessel != null)
                {
                    packet.Vessel = new comms.Vessel(vessel);

                    if (vessel.patchedConicSolver == null)
                    {
                        packet.ManeuverList = new comms.ManeuverList();
                    }
                    else
                    {
                        packet.ManeuverList = new comms.ManeuverList(vessel.patchedConicSolver.maneuverNodes);
                    }

                    packet.Target = new comms.Target(vessel.targetObject);
                }

                byte[] buffer = packet.Make();
                this.socket.BeginSend(buffer, buffer.Length, this.clientEndPoint,
                                      SendCallback, this.socket);
            }
            catch (System.IO.IOException e)
            {
                LogException(e);
            }
            catch (Exception e)
            {
                LogException(e);
                throw;
            }
        }