コード例 #1
0
        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 Worker()
        {
            Log("Waiting to recieve UDP messages");
            bool gotFirst = false;
            while(this.runThread)
            {
                try
                {
                    var buffer = this.socket.Receive(ref this.clientEndPoint);

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

                    var update = comms.ClientPacket.Read(buffer);

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

                    // If maneuvers haven't changed, don't process as an update
                    var maneuverUpdate = update.ManeuverList;
                    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 SocketException || e is System.IO.IOException)
                    {
                        if(this.runThread)
                            System.Threading.Thread.Sleep(100);
                    }
                    else
                        throw;
                }
            }
        }