コード例 #1
0
        //Ped Packet
        internal void ResendPacket(PedData fullPacket, Client exception, bool pure)
        {
            byte[] full;
            var    basic = new byte[0];

            if (pure)
            {
                full = PacketOptimization.WritePureSync(fullPacket);
                if (fullPacket.NetHandle != null)
                {
                    basic = PacketOptimization.WriteBasicSync(fullPacket.NetHandle.Value, fullPacket.Position);
                }
            }
            else
            {
                full = PacketOptimization.WriteLightSync(fullPacket);
            }

            var msg = Server.CreateMessage();

            if (pure)
            {
                //if (client.Position.DistanceToSquared(fullPacket.Position) > 10000) // 1km
                //{
                //    var lastUpdateReceived = client.LastPacketReceived.Get(exception.handle.Value);

                //    if (lastUpdateReceived != 0 && Program.GetTicks() - lastUpdateReceived <= 1000) continue;
                //    msg.Write((byte)PacketType.BasicSync);
                //    msg.Write(basic.Length);
                //    msg.Write(basic);
                //    Server.SendMessage(msg, client.NetConnection, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.BasicSync);

                //    client.LastPacketReceived.Set(exception.handle.Value, Program.GetTicks());
                //}
                //else
                //{
                msg.Write((byte)PacketType.PedPureSync);
                msg.Write(full.Length);
                msg.Write(full);
                //}
            }
            else
            {
                msg.Write((byte)PacketType.PedLightSync);
                msg.Write(full.Length);
                msg.Write(full);
            }

            List <NetConnection> connectionsNear = new List <NetConnection>();

            foreach (var client in exception.Streamer.GetNearClients())
            {
                if (client.Fake)
                {
                    continue;
                }
                if (client.NetConnection.Status == NetConnectionStatus.Disconnected)
                {
                    continue;
                }
                if (!client.ConnectionConfirmed)
                {
                    continue;
                }
                if (client == exception)
                {
                    continue;
                }

                if (pure)
                {
                    if (client.Position == null)
                    {
                        continue;
                    }

                    connectionsNear.Add(client.NetConnection);
                }
                else
                {
                    connectionsNear.Add(client.NetConnection);
                }
            }

            if (pure)
            {
                Server.SendMessage(msg, connectionsNear, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.PureSync);
            }
            else
            {
                Server.SendMessage(msg, connectionsNear, NetDeliveryMethod.ReliableSequenced, (int)ConnectionChannel.LightSync);
            }


            if (pure)
            {
                var msgBasic = Server.CreateMessage();

                msgBasic.Write((byte)PacketType.BasicSync);
                msgBasic.Write(basic.Length);
                msgBasic.Write(basic);

                long ticks = Program.GetTicks();

                List <NetConnection> connectionsFar = new List <NetConnection>();

                foreach (var client in exception.Streamer.GetFarClients())
                {
                    if (client.Fake)
                    {
                        continue;
                    }
                    if (client.NetConnection.Status == NetConnectionStatus.Disconnected)
                    {
                        continue;
                    }
                    if (!client.ConnectionConfirmed)
                    {
                        continue;
                    }
                    if (client == exception)
                    {
                        continue;
                    }

                    var lastUpdateReceived = client.LastPacketReceived.Get(exception.handle.Value);

                    if (lastUpdateReceived != 0 && ticks - lastUpdateReceived <= 1000)
                    {
                        continue;
                    }

                    connectionsFar.Add(client.NetConnection);
                    client.LastPacketReceived.Set(exception.handle.Value, ticks);
                }

                Server.SendMessage(msgBasic, connectionsFar, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.BasicSync);
            }
        }
コード例 #2
0
ファイル: SyncSender.cs プロジェクト: Sofunny17/GTANetwork
        internal static void MainLoop()
        {
            bool lastPedData       = false;
            int  lastLightSyncSent = 0;

            while (true)
            {
                if (!Main.IsOnServer())
                {
                    Thread.Sleep(100);
                    continue;
                }

                object lastPacket;
                lock (SyncCollector.Lock)
                {
                    lastPacket = SyncCollector.LastSyncPacket;
                    SyncCollector.LastSyncPacket = null;
                }

                if (lastPacket == null)
                {
                    continue;
                }
                try
                {
                    if (lastPacket is PedData)
                    {
                        var bin = PacketOptimization.WritePureSync((PedData)lastPacket);

                        var msg = Main.Client.CreateMessage();
                        msg.Write((byte)PacketType.PedPureSync);
                        msg.Write(bin.Length);
                        msg.Write(bin);

                        try
                        {
                            Main.Client.SendMessage(msg, NetDeliveryMethod.UnreliableSequenced,
                                                    (int)ConnectionChannel.PureSync);
                        }
                        catch (Exception ex)
                        {
                            Util.Util.SafeNotify("FAILED TO SEND DATA: " + ex.Message);
                            LogManager.LogException(ex, "SENDPLAYERDATA");
                        }

                        if (!lastPedData || Environment.TickCount - lastLightSyncSent > LIGHT_SYNC_RATE)
                        {
                            lastLightSyncSent = Environment.TickCount;

                            LogManager.DebugLog("SENDING LIGHT VEHICLE SYNC");

                            var lightBin = PacketOptimization.WriteLightSync((PedData)lastPacket);

                            var lightMsg = Main.Client.CreateMessage();
                            lightMsg.Write((byte)PacketType.PedLightSync);
                            lightMsg.Write(lightBin.Length);
                            lightMsg.Write(lightBin);
                            try
                            {
                                Main.Client.SendMessage(lightMsg, NetDeliveryMethod.ReliableSequenced,
                                                        (int)ConnectionChannel.LightSync);
                            }
                            catch (Exception ex)
                            {
                                Util.Util.SafeNotify("FAILED TO SEND LIGHT DATA: " + ex.Message);
                                LogManager.LogException(ex, "SENDPLAYERDATA");
                            }

                            Main._bytesSent += lightBin.Length;
                            Main._messagesSent++;
                        }

                        lastPedData = true;

                        lock (Main._averagePacketSize)
                        {
                            Main._averagePacketSize.Add(bin.Length);
                            if (Main._averagePacketSize.Count > 10)
                            {
                                Main._averagePacketSize.RemoveAt(0);
                            }
                        }

                        Main._bytesSent += bin.Length;
                        Main._messagesSent++;
                    }
                    else
                    {
                        var bin = PacketOptimization.WritePureSync((VehicleData)lastPacket);

                        var msg = Main.Client.CreateMessage();
                        msg.Write((byte)PacketType.VehiclePureSync);
                        msg.Write(bin.Length);
                        msg.Write(bin);
                        try
                        {
                            Main.Client.SendMessage(msg, NetDeliveryMethod.UnreliableSequenced,
                                                    (int)ConnectionChannel.PureSync);
                        }
                        catch (Exception ex)
                        {
                            Util.Util.SafeNotify("FAILED TO SEND DATA: " + ex.Message);
                            LogManager.LogException(ex, "SENDPLAYERDATA");
                        }

                        if (lastPedData || Environment.TickCount - lastLightSyncSent > LIGHT_SYNC_RATE)
                        {
                            lastLightSyncSent = Environment.TickCount;

                            LogManager.DebugLog("SENDING LIGHT VEHICLE SYNC");

                            var lightBin = PacketOptimization.WriteLightSync((VehicleData)lastPacket);

                            var lightMsg = Main.Client.CreateMessage();
                            lightMsg.Write((byte)PacketType.VehicleLightSync);
                            lightMsg.Write(lightBin.Length);
                            lightMsg.Write(lightBin);
                            try
                            {
                                Main.Client.SendMessage(lightMsg, NetDeliveryMethod.ReliableSequenced,
                                                        (int)ConnectionChannel.LightSync);
                            }
                            catch (Exception ex)
                            {
                                Util.Util.SafeNotify("FAILED TO SEND LIGHT DATA: " + ex.Message);
                                LogManager.LogException(ex, "SENDPLAYERDATA");
                            }

                            Main._bytesSent += lightBin.Length;
                            Main._messagesSent++;
                        }

                        lastPedData = false;

                        lock (Main._averagePacketSize)
                        {
                            Main._averagePacketSize.Add(bin.Length);
                            if (Main._averagePacketSize.Count > 10)
                            {
                                Main._averagePacketSize.RemoveAt(0);
                            }
                        }

                        Main._bytesSent += bin.Length;
                        Main._messagesSent++;
                    }
                }
                catch (Exception ex)
                {
                    LogManager.LogException(ex, "SYNCSENDER");
                }

                LogManager.DebugLog("END SYNC SEND");

                Thread.Sleep(PURE_SYNC_RATE);
            }
        }