Пример #1
0
            public bool StopReplay()
            {
                Playing     = false;
                CurrentTime = 0;
                Loaded      = false;
                #region despawn the aircraft
                foreach (uint ThisID in AircraftIDs)
                {
                    Packets.Packet_13_RemoveAirplane ThisRemoveAirplane = new Packets.Packet_13_RemoveAirplane(ThisID);

                    lock (Vehicles.List) Vehicles.List.RemoveAll(x => x.ID == ThisRemoveAirplane.ID);

                    foreach (Client ThisClient in Clients.YSFClients)
                    {
                        Packets.Packet_06_Acknowledgement AcknowledgeLeave = new Packets.Packet_06_Acknowledgement(2, ThisRemoveAirplane.ID);
                        //ThisClient.SendPacketGetPacket(ThisRemoveAirplane, AcknowledgeLeave);
                        ThisClient.SendPacket(ThisRemoveAirplane);
                    }
                    continue;
                }
                #endregion

                return(true);
            }
Пример #2
0
            public bool SendUpdate()
            {
                if (!Loaded)
                {
                    return(false);
                }
                if (Playing)
                {
                    CurrentTime += 0.1;
                }
                #region GetCurrentPackets
                List <Packets.GenericPacket> PacketsToSend =
                    (
                        from ThisPacketEvent in Records
                        where ThisPacketEvent.TimeStamp >= PreviousTime
                        where ThisPacketEvent.TimeStamp < CurrentTime
                        select ThisPacketEvent.Packet
                    ).ToList();
                if (PacketsToSend.Count == 0)
                {
                    return(false);
                }
                List <Packets.Packet_11_FlightData> FlightDataPacketsToSend =
                    (
                        from ThisFlightDataPacket in PacketsToSend
                        where ThisFlightDataPacket.Type == 11
                        select new Packets.Packet_11_FlightData(ThisFlightDataPacket)
                    ).OrderByDescending(x => x.TimeStamp).ToList();
                #endregion

                List <Client> CurrentClients = Clients.LoggedIn;
                foreach (uint ThisACID in AircraftIDs)
                {
                    #region GetFlightDataFromThisAircraft
                    List <Packets.Packet_11_FlightData> ThisAircraftFlightData =
                        (
                            from ThisFlightDataPacket in FlightDataPacketsToSend
                            where ThisFlightDataPacket.ID == ThisACID
                            select ThisFlightDataPacket
                        ).ToList();
                    #endregion
                    if (ThisAircraftFlightData.Count < 1)
                    {
                        continue;
                    }

                    #region GetLatestFlightData
                    Packets.Packet_11_FlightData NewesetFlightData =
                        (from ThisFlightDataPacket in FlightDataPacketsToSend
                         where ThisFlightDataPacket.ID == ThisACID
                         select ThisFlightDataPacket).ToArray().Last();

                    #endregion

                    #region UpdateFlightData
                    foreach (Vehicle ThisVehicle in Vehicles.List.Where(x => x.ID == ThisACID))
                    {
                        ThisVehicle.Update(NewesetFlightData);
                    }
                    #endregion

                    float difference = (float)(NewesetFlightData.TimeStamp - PreviousTime);
                    foreach (Client ThisClient in CurrentClients)
                    {
                        #region SetTimeToNow
                        NewesetFlightData.TimeStamp = (float)((DateTime.Now - OpenYS.TimeStarted).TotalSeconds - (CurrentTime - PreviousTime) + difference);
                        #endregion

                        //Debug.WriteLine(NewesetFlightData.TimeStamp + " Ready to send to " + ThisClient.Username);
                        ThisClient.SendPacket(NewesetFlightData);
                        //Debug.WriteLine(NewesetFlightData.TimeStamp + " Send Flight Data.");
                    }
                    continue;
                }

                foreach (Packets.GenericPacket ThisPacket in PacketsToSend)
                {
                    if (ThisPacket.Type == 11)
                    {
                        continue;
                    }

                    #region Join Data
                    if (ThisPacket.Type == 05)
                    {
                        Packets.Packet_05_EntityJoined ThisJoinData = new Packets.Packet_05_EntityJoined(ThisPacket);

                        #region Create Vehicle
                        Vehicle ThisVehicle = new Vehicle();
                        ThisVehicle.Update(ThisJoinData);
                        //ThisJoinData = ThisVehicle.GetJoinPacket(false);
                        #endregion

                        #region Add Vehicle to Vehicles List
                        Vehicles.List.Add(ThisVehicle);
                        VehiclesHistory.List.Add(ThisVehicle);
                        #endregion

                        foreach (Client ThisClient in CurrentClients)
                        {
                            Packets.Packet_06_Acknowledgement Acknowledgement;
                            if (ThisJoinData.IsAircraft)
                            {
                                Acknowledgement = new Packets.Packet_06_Acknowledgement(0, ThisJoinData.ID);
                            }
                            else
                            {
                                Acknowledgement = new Packets.Packet_06_Acknowledgement(1, 0);
                            }
                            //ThisClient.SendPacketGetPacket(ThisJoinData, Acknowledgement);
                            ThisClient.SendPacket(ThisJoinData);
                        }
                        continue;
                    }
                    #endregion

                    #region Leave Data
                    if (ThisPacket.Type == 13)
                    {
                        Packets.Packet_13_RemoveAirplane ThisRemoveAirplane = new Packets.Packet_13_RemoveAirplane(ThisPacket);

                        lock (Vehicles.List) Vehicles.List.RemoveAll(x => x.ID == ThisRemoveAirplane.ID);

                        foreach (Client ThisClient in CurrentClients)
                        {
                            Packets.Packet_06_Acknowledgement AcknowledgeLeave = new Packets.Packet_06_Acknowledgement(2, ThisRemoveAirplane.ID);
                            //ThisClient.SendPacketGetPacket(ThisRemoveAirplane, AcknowledgeLeave);
                            ThisClient.SendPacket(ThisRemoveAirplane);
                        }
                        continue;
                    }
                    #endregion

                    CurrentClients.SendPacket(ThisPacket);
                }
                if (Playing)
                {
                    PreviousTime = CurrentTime;
                }
                return(true);
            }
Пример #3
0
        public bool Disconnect(string Reason)
        {
            lock (this)
            {
                if (IsDisconnecting())
                {
                    return(false);
                }
                if (IsDisconnected())
                {
                    return(false);
                }

                //Console.WriteLine(ConsoleColor.Red, Utilities.GetSourceCodePosition(1));
                if (IsFakeClient())
                {
                    return(true);
                }
                if (IsDisconnecting())
                {
                    Log.Warning("Disconnect() Called for Client: " + Username + ". Disconnecting Flag is Currently ON, A duplicate has occured and will be ignored.");
                    return(false);
                }
                if (IsDisconnected())
                {
                    Log.Warning("Disconnect() Called for Client: " + Username + ". Disconnect Flag is Currently ON, A duplicate has occured and will be ignored.");
                    return(false);
                }
                try
                {
                    if (YSFClient.Socket.Connected ||
                        (!YSFClient.Socket.Poll(1000, SelectMode.SelectRead) && YSFClient.Socket.Available != 0))
                    {
                        //The socket still exists!
                        //DO NOT DISCONNECT!

                        Log.Warning("Client " + Username + " Socket told to disconnect when it should not be disconnected? (Data Still Available?)");
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.ToString());
                }
                Log.Warning("Disconnect() Called for Client: " + Username + ". Disconnect Flag is Currently OFF, Setting to ON. (This is completely normal...)");
                Log.Warning("Disconnect() Reason: " + Reason);
                SetDisconnecting();
                #region Despawn Vehicle
                if (Vehicle != Vehicles.NoVehicle)
                {
                    //Need to tell all the other clients the vehicle is removed!
                    Packets.Packet_13_RemoveAirplane  RemoveAirplane   = new Packets.Packet_13_RemoveAirplane(Vehicle.ID);
                    Packets.Packet_06_Acknowledgement AcknowledgeLeave = new Packets.Packet_06_Acknowledgement(2, RemoveAirplane.ID);
                    foreach (Client OtherClient in Clients.AllClients.Exclude(this))
                    {
                        OtherClient.SendPacket(RemoveAirplane);
                        //OtherClient.GetPacket(AcknowledgeLeave);
                    }
                    if (Vehicle.VirtualCarrierObject_ID != 0)
                    {
                        Packets.Packet_19_RemoveGround RemoveVCO = new Packets.Packet_19_RemoveGround(Vehicle.VirtualCarrierObject_ID);
                        Clients.AllClients.Exclude(this).SendPacket(RemoveVCO);
                    }
                    Vehicles.List.RemoveAll(x => x == Vehicle);
                }
                #endregion
                Clients.RemoveClient(this); //remove self from the client list.
                #region Inform Players
                if ((this.Username == "nameless" & !this.ClientType.HasFlag(Client.FlagsClientType.YSFlightClient)) | this.IsBot())
                {
                    if (this.Username == "nameless")
                    {
                        if (Settings.Loading.BotPingMessages)
                        {
                            Console.WriteLine(ConsoleColor.Magenta, "Server was pinged by an unknown service.");
                        }
                    }
                    else if (this.IsBot())
                    {
                        if (Settings.Loading.BotPingMessages)
                        {
                            Console.WriteLine(ConsoleColor.Magenta, "Server was pinged by a serverlist.");
                        }
                    }
                }
                else
                {
                    foreach (Client OtherClient in Clients.AllClients.Exclude(this).ToArray())
                    {
                        OtherClient.SendMessage("&c" + Username + " left the server.");
                    }
                }
                #endregion
                #region Kill Client.
                try
                {
                    //lock (YSFClient.PacketWaiters)
                    //{
                    //    foreach (Connection.PacketWaiter ThisPacketWaiter in YSFClient.PacketWaiters.ToArray())
                    //    {
                    //        //force all listener threads to invalidate and cancel.
                    //        ThisPacketWaiter.Signal.Set();
                    //        ThisPacketWaiter.Signal.Dispose();
                    //    }
                    //}

                    //YSFClient.Disconnect();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                #endregion
                #region Kill Server.
                try
                {
                    //YSFServer.ReceiveThread.Abort();
                    //foreach (Connection.PacketWaiter ThisPacketWaiter in YSFServer.PacketWaiters.ToArray())
                    //{
                    //    //force all listener threads to invalidate and cancel.
                    //    ThisPacketWaiter.Signal.Set();
                    //    ThisPacketWaiter.Signal.Dispose();
                    //}

                    //YSFServer.Disconnect();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                #endregion
                //YSFClient.PacketLog.Log();
                StateConnection = Client.FlagsStateConnection.Disconnected;
                return(true);
            }
        }
Пример #4
0
        public static bool MicroTick()
        {
            #region DoWeather
            if (AdvancedWeatherOptions._Weather_Variable)
            {
                Packets.Packet_33_Weather TempWeather = AdvancedWeatherOptions.GetUpdatedWeather();
                TempWeather.Control_BlackOut       = false;
                TempWeather.Control_Collisions     = false;
                TempWeather.Control_LandEverywhere = false;
                Clients.AllClients.SendPacket(TempWeather);
            }
            #endregion
            #region DoHeartbeatMonitor
            Clients.DoHeartbeatMonitor();
            #endregion
            #region DoOnTickUpdateEvent
            lock (Events_OnMicroTickUpdate)
            {
                foreach (OnMicroTickUpdateEvent ThisMethod in Events_OnMicroTickUpdate)
                {
                    ThisMethod();
                }
            }
            #endregion

            #region CountThreads
            Thread[] _ThreadsArray;
            lock (Threads.List) _ThreadsArray = Threads.List.ToArray();
            foreach (Thread ThisThread in _ThreadsArray)
            {
                if (!ThisThread.IsAlive)
                {
                    //Log.Error("Thread killed as part of maintenance: " + ThisThread.Name);
                    Threads.List.RemoveAll(x => x == ThisThread);
                }
            }
            #endregion
            #region ManageVehicles
            Vehicle[] _VehiclesArray;
            lock (Vehicles.List) _VehiclesArray = Vehicles.List.ToArray();
            foreach (Vehicle ThisVehicle in _VehiclesArray)
            {
                if (Clients.Flying.Where(x => x.Vehicle == ThisVehicle).Count() < 1
                    & !ThisVehicle.IsVirtualVehicle)
                {
                    Vehicles.List.RemoveAll(x => x == ThisVehicle);
                    Packets.Packet_13_RemoveAirplane RemoveAirplane = new Packets.Packet_13_RemoveAirplane(ThisVehicle.ID);
                    foreach (Client OtherClient in Clients.AllClients)
                    {
                        Packets.Packet_06_Acknowledgement AcknowledgeLeave = new Packets.Packet_06_Acknowledgement(2, RemoveAirplane.ID);
                        //OtherClient.SendPacketGetPacket(RemoveAirplane, AcknowledgeLeave);
                        OtherClient.SendPacket(RemoveAirplane);
                    }
                    Console.WriteLine("&3Found a lost Vehicle... Cleaned it up!");
                    continue;
                }
                if (ThisVehicle.Invincible)
                {
                    Clients.LoggedIn.SendPacket(new Packets.Packet_30_AirCommand(ThisVehicle.ID, "STRENGTH", "255"));
                }
            }
            #endregion
            return(true);
        }