From b3f876b9a354d336abc6fb8a2566c00aa30112ef Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Fri, 10 Mar 2023 15:43:40 +0000 Subject: [PATCH] cosmetics --- .../ClientStack/Linden/UDP/LLUDPClient.cs | 68 +++++--------- .../Linden/UDP/LLUDPServerCommands.cs | 93 ++++++++++--------- .../ClientStack/Linden/UDP/OpenSimUDPBase.cs | 62 +------------ 3 files changed, 76 insertions(+), 147 deletions(-) diff --git a/OpenSim/Region/ClientStack/Linden/UDP/LLUDPClient.cs b/OpenSim/Region/ClientStack/Linden/UDP/LLUDPClient.cs index 0a4614bc56..74960f4399 100644 --- a/OpenSim/Region/ClientStack/Linden/UDP/LLUDPClient.cs +++ b/OpenSim/Region/ClientStack/Linden/UDP/LLUDPClient.cs @@ -26,14 +26,11 @@ */ using System; -using System.Collections.Generic; using System.Net; using System.Threading; using log4net; using OpenSim.Framework; -using OpenSim.Framework.Monitoring; using OpenMetaverse; -using OpenMetaverse.Packets; using TokenBucket = OpenSim.Region.ClientStack.LindenUDP.TokenBucket; @@ -81,24 +78,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP /// Controls whether information is logged about each outbound packet immediately before it is sent. For debug purposes. /// /// Any level above 0 will turn on logging. - public int ThrottleDebugLevel - { - get - { - return m_throttleDebugLevel; - } - - set - { - m_throttleDebugLevel = value; -/* - m_throttleClient.DebugLevel = m_throttleDebugLevel; - foreach (TokenBucket tb in m_throttleCategories) - tb.DebugLevel = m_throttleDebugLevel; - */ - } - } - private int m_throttleDebugLevel; + public int ThrottleDebugLevel { get; set; } /// Fired when updated networking stats are produced for this client public event PacketStats OnPacketStats; @@ -115,13 +95,13 @@ namespace OpenSim.Region.ClientStack.LindenUDP /// Circuit code that this client is connected on public readonly uint CircuitCode; /// Sequence numbers of packets we've received (for duplicate checking) - public IncomingPacketHistoryCollection PacketArchive = new IncomingPacketHistoryCollection(1024); + public IncomingPacketHistoryCollection PacketArchive = new(1024); /// Packets we have sent that need to be ACKed by the client - public UnackedPacketCollection NeedAcks = new UnackedPacketCollection(); + public UnackedPacketCollection NeedAcks = new(); /// ACKs that are queued up, waiting to be sent to the client - public DoubleLocklessQueue PendingAcks = new DoubleLocklessQueue(); + public DoubleLocklessQueue PendingAcks = new(); public int AckStalls; @@ -167,7 +147,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP private double m_nextOnQueueEmpty = 0; /// Throttle bucket for this agent's connection - private AdaptiveTokenBucket m_throttleClient; + private readonly AdaptiveTokenBucket m_throttleClient; public AdaptiveTokenBucket FlowThrottle { get { return m_throttleClient; } @@ -176,22 +156,22 @@ namespace OpenSim.Region.ClientStack.LindenUDP /// Throttle buckets for each packet category private readonly TokenBucket[] m_throttleCategories; /// Outgoing queues for throttled packets - private DoubleLocklessQueue[] m_packetOutboxes = new DoubleLocklessQueue[THROTTLE_CATEGORY_COUNT]; + private readonly DoubleLocklessQueue[] m_packetOutboxes = new DoubleLocklessQueue[THROTTLE_CATEGORY_COUNT]; /// A container that can hold one packet for each outbox, used to store /// dequeued packets that are being held for throttling - private OutgoingPacket[] m_nextPackets = new OutgoingPacket[THROTTLE_CATEGORY_COUNT]; + private readonly OutgoingPacket[] m_nextPackets = new OutgoingPacket[THROTTLE_CATEGORY_COUNT]; /// A reference to the LLUDPServer that is managing this client private readonly LLUDPServer m_udpServer; /// Caches packed throttle information private byte[] m_packedThrottles; - private int m_defaultRTO = 1000; // 1sec is the recommendation in the RFC - private int m_maxRTO = 3000; - private int m_minRTO = 250; + private readonly int m_defaultRTO = 1000; // 1sec is the recommendation in the RFC + private readonly int m_maxRTO = 3000; + private readonly int m_minRTO = 250; - private float m_burstTime; - private int m_maxRate; + private readonly float m_burstTime; + private readonly int m_maxRate; public double m_lastStartpingTimeMS; public int m_pingMS; @@ -208,7 +188,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP } } - private ClientInfo m_info = new ClientInfo(); + private readonly ClientInfo m_info = new(); /// /// Default constructor @@ -575,7 +555,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP /// True if any packets were sent, otherwise false public bool DequeueOutgoing() { -// if (m_deliverPackets == false) return false; + //if (m_deliverPackets == false) return false; OutgoingPacket packet; DoubleLocklessQueue queue; @@ -586,9 +566,9 @@ namespace OpenSim.Region.ClientStack.LindenUDP // do resends packet = m_nextPackets[0]; - if (packet != null) + if (packet is not null) { - if (packet.Buffer != null) + if (packet.Buffer is not null) { if (m_throttleCategories[0].RemoveTokens(packet.Buffer.DataLength)) { @@ -604,13 +584,13 @@ namespace OpenSim.Region.ClientStack.LindenUDP else { queue = m_packetOutboxes[0]; - if (queue != null) + if (queue is not null) { if(queue.Dequeue(out packet)) { // A packet was pulled off the queue. See if we have // enough tokens in the bucket to send it out - if (packet.Buffer != null) + if (packet.Buffer is not null) { if (m_throttleCategories[0].RemoveTokens(packet.Buffer.DataLength)) { @@ -643,9 +623,9 @@ namespace OpenSim.Region.ClientStack.LindenUDP //queueDebugOutput += m_packetOutboxes[i].Count + " "; // Serious debug business packet = m_nextPackets[i]; - if (packet != null) + if (packet is not null) { - if(packet.Buffer == null) + if(packet.Buffer is null) { if (m_packetOutboxes[i].Count < 5) emptyCategories |= CategoryToFlag(i); @@ -671,7 +651,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP queue = m_packetOutboxes[i]; if(queue.Dequeue(out packet)) { - if (packet.Buffer == null) + if (packet.Buffer is null) { // packet canceled elsewhere (by a ack for example) if (queue.Count < 5) @@ -735,7 +715,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP /// Throttle categories to fire the callback for private void BeginFireQueueEmpty(ThrottleOutPacketTypeFlags categories) { - if (!QueueEmptyRunning && HasUpdates(categories) && OnQueueEmpty != null) + if (!QueueEmptyRunning && HasUpdates(categories) && OnQueueEmpty is not null) { double start = Util.GetTimeStampMS(); if (start < m_nextOnQueueEmpty) @@ -782,7 +762,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP public void FireQueueEmpty(object o) { QueueEmpty callback = OnQueueEmpty; - if (callback != null) + if (callback is not null) { ThrottleOutPacketTypeFlags categories = (ThrottleOutPacketTypeFlags)o; try { callback(categories); } @@ -836,7 +816,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP public class DoubleLocklessQueue : OpenSim.Framework.LocklessQueue { - OpenSim.Framework.LocklessQueue highQueue = new OpenSim.Framework.LocklessQueue(); + readonly OpenSim.Framework.LocklessQueue highQueue = new(); public override int Count { diff --git a/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServerCommands.cs b/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServerCommands.cs index a297f541f6..62c1e27940 100755 --- a/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServerCommands.cs +++ b/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServerCommands.cs @@ -37,8 +37,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP { public class LLUDPServerCommands { - private ICommandConsole m_console; - private LLUDPServer m_udpServer; + private readonly ICommandConsole m_console; + private readonly LLUDPServer m_udpServer; public LLUDPServerCommands(ICommandConsole console, LLUDPServer udpServer) { @@ -219,11 +219,12 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleShowServerThrottlesCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; m_console.Output("Throttles for {0}", m_udpServer.Scene.Name); - ConsoleDisplayList cdl = new ConsoleDisplayList(); + ConsoleDisplayList cdl = new(); cdl.AddRow("Adaptive throttles", m_udpServer.ThrottleRates.AdaptiveThrottlesEnabled); long maxSceneDripRate = (long)m_udpServer.Throttle.MaxDripRate; @@ -243,8 +244,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP private string GetServerThrottlesReport(LLUDPServer udpServer) { - StringBuilder report = new StringBuilder(); - + StringBuilder report = new(); report.AppendFormat( "{0,7} {1,8} {2,7} {3,7} {4,7} {5,7} {6,9} {7,7}\n", "Total", @@ -301,8 +301,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP return; } - int level; - if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, args[4], out level)) + if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, args[4], out int level)) return; string firstName = args[5]; @@ -336,8 +335,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP return; } - int level; - if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, args[4], out level)) + if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, args[4], out int level)) return; string firstName = null; @@ -391,8 +389,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP if (param == "adaptive") { - bool newValue; - if (!ConsoleUtil.TryParseConsoleBool(MainConsole.Instance, rawValue, out newValue)) + if (!ConsoleUtil.TryParseConsoleBool(MainConsole.Instance, rawValue, out bool newValue)) return; m_udpServer.Scene.ForEachScenePresence(sp => @@ -405,15 +402,14 @@ namespace OpenSim.Region.ClientStack.LindenUDP LLUDPClient udpClient = ((LLClientView)sp.ControllingClient).UDPClient; udpClient.FlowThrottle.AdaptiveEnabled = newValue; - // udpClient.FlowThrottle.MaxDripRate = 0; - // udpClient.FlowThrottle.AdjustedDripRate = 0; + //udpClient.FlowThrottle.MaxDripRate = 0; + //udpClient.FlowThrottle.AdjustedDripRate = 0; } }); } else if (param == "request") { - int newValue; - if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, rawValue, out newValue)) + if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, rawValue, out int newValue)) return; int newCurrentThrottleKbps = newValue * 1000 / 8; @@ -433,8 +429,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP } else if (param == "max") { - int newValue; - if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, rawValue, out newValue)) + if (!ConsoleUtil.TryParseConsoleInt(MainConsole.Instance, rawValue, out int newValue)) return; int newThrottleMaxKbps = newValue * 1000 / 8; @@ -488,7 +483,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP LLUDPClient udpClient = ((LLClientView)sp.ControllingClient).UDPClient; - ConsoleDisplayList cdl = new ConsoleDisplayList(); + ConsoleDisplayList cdl = new(); cdl.AddRow("adaptive", udpClient.FlowThrottle.AdaptiveEnabled); cdl.AddRow("current", string.Format("{0} kbps", udpClient.FlowThrottle.DripRate * 8 / 1000)); cdl.AddRow("request", string.Format("{0} kbps", udpClient.FlowThrottle.RequestedDripRate * 8 / 1000)); @@ -501,11 +496,12 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleGetCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; m_console.Output("Debug settings for {0}", m_udpServer.Scene.Name); - ConsoleDisplayList cdl = new ConsoleDisplayList(); + ConsoleDisplayList cdl = new(); long maxSceneDripRate = (long)m_udpServer.Throttle.MaxDripRate; cdl.AddRow( @@ -612,14 +608,15 @@ namespace OpenSim.Region.ClientStack.LindenUDP */ private void HandlePacketCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; bool setAsDefaultLevel = false; bool setAll = false; OptionSet optionSet = new OptionSet() - .Add("default", o => setAsDefaultLevel = (o != null)) - .Add("all", o => setAll = (o != null)); + .Add("default", o => setAsDefaultLevel = (o is not null)) + .Add("all", o => setAll = (o is not null)); List filteredArgs = optionSet.Parse(args); string name = null; @@ -639,8 +636,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP if (filteredArgs.Count > 3) { - int newDebug; - if (int.TryParse(filteredArgs[3], out newDebug)) + if (int.TryParse(filteredArgs[3], out int newDebug)) { if (setAsDefaultLevel || setAll) { @@ -653,28 +649,28 @@ namespace OpenSim.Region.ClientStack.LindenUDP if (setAll) { m_udpServer.Scene.ForEachScenePresence(sp => - { - MainConsole.Instance.Output( - "Packet debug for {0} ({1}) set to {2} in {3}", - sp.Name, sp.IsChildAgent ? "child" : "root", newDebug, m_udpServer.Scene.Name); + { + MainConsole.Instance.Output( + "Packet debug for {0} ({1}) set to {2} in {3}", + sp.Name, sp.IsChildAgent ? "child" : "root", newDebug, m_udpServer.Scene.Name); - sp.ControllingClient.DebugPacketLevel = newDebug; - }); + sp.ControllingClient.DebugPacketLevel = newDebug; + }); } } else { m_udpServer.Scene.ForEachScenePresence(sp => - { - if (name == null || sp.Name == name) - { - MainConsole.Instance.Output( - "Packet debug for {0} ({1}) set to {2} in {3}", - sp.Name, sp.IsChildAgent ? "child" : "root", newDebug, m_udpServer.Scene.Name); + { + if (name is null || sp.Name == name) + { + MainConsole.Instance.Output( + "Packet debug for {0} ({1}) set to {2} in {3}", + sp.Name, sp.IsChildAgent ? "child" : "root", newDebug, m_udpServer.Scene.Name); - sp.ControllingClient.DebugPacketLevel = newDebug; - } - }); + sp.ControllingClient.DebugPacketLevel = newDebug; + } + }); } } else @@ -686,7 +682,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleDropCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; if (args.Length != 6) @@ -759,7 +756,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleStopCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; if (args.Length != 4) @@ -779,7 +777,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleAgentUpdateCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; m_udpServer.DiscardInboundAgentUpdates = !m_udpServer.DiscardInboundAgentUpdates; @@ -790,7 +789,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleStatusCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; MainConsole.Instance.Output( @@ -805,7 +805,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP private void HandleOqreCommand(string module, string[] args) { - if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_udpServer.Scene) + if (SceneManager.Instance.CurrentScene is not null && + SceneManager.Instance.CurrentScene != m_udpServer.Scene) return; if (args.Length != 4) diff --git a/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs b/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs index aba5d48f09..b1a37b971a 100644 --- a/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs +++ b/OpenSim/Region/ClientStack/Linden/UDP/OpenSimUDPBase.cs @@ -104,56 +104,6 @@ namespace OpenMetaverse get { return m_udpPort; } } - #region PacketDropDebugging - /// - /// For debugging purposes only... parameters for a simplified - /// model of packet loss with bursts, overall drop rate should - /// be roughly 1 - m_dropLengthProbability / (m_dropProbabiliy + m_dropLengthProbability) - /// which is about 1% for parameters 0.0015 and 0.15 - /// - private double m_dropProbability = 0.0030; - private double m_dropLengthProbability = 0.15; - private bool m_dropState = false; - - /// - /// For debugging purposes only... parameters to control the time - /// duration over which packet loss bursts can occur, if no packets - /// have been sent for m_dropResetTicks milliseconds, then reset the - /// state of the packet dropper to its default. - /// - private int m_dropLastTick = 0; - private int m_dropResetTicks = 500; - - /// - /// Debugging code used to simulate dropped packets with bursts - /// - private bool DropOutgoingPacket() - { - double rnum = Random.Shared.NextDouble(); - - // if the connection has been idle for awhile (more than m_dropResetTicks) then - // reset the state to the default state, don't continue a burst - int curtick = Util.EnvironmentTickCount(); - if (Util.EnvironmentTickCountSubtract(curtick, m_dropLastTick) > m_dropResetTicks) - m_dropState = false; - - m_dropLastTick = curtick; - - // if we are dropping packets, then the probability of dropping - // this packet is the probability that we stay in the burst - if (m_dropState) - { - m_dropState = (rnum < (1.0 - m_dropLengthProbability)) ? true : false; - } - else - { - m_dropState = (rnum < m_dropProbability) ? true : false; - } - - return m_dropState; - } - #endregion PacketDropDebugging - /// /// Default constructor /// @@ -172,7 +122,7 @@ namespace OpenMetaverse ~OpenSimUDPBase() { - if(m_udpSocket !=null) + if(m_udpSocket is not null) try { m_udpSocket.Close(); } catch { } } @@ -235,12 +185,8 @@ namespace OpenMetaverse const int SIO_UDP_CONNRESET = -1744830452; - IPEndPoint ipep = new IPEndPoint(m_localBindAddress, m_udpPort); - m_udpSocket = new Socket( - AddressFamily.InterNetwork, - SocketType.Dgram, - ProtocolType.Udp); + m_udpSocket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { @@ -278,6 +224,7 @@ namespace OpenMetaverse if (recvBufferSize != 0) m_udpSocket.ReceiveBufferSize = recvBufferSize; + IPEndPoint ipep = new(m_localBindAddress, m_udpPort); m_udpSocket.Bind(ipep); if (m_udpPort == 0) @@ -423,6 +370,7 @@ namespace OpenMetaverse UdpReceives, se.ErrorCode), se); } + catch(ObjectDisposedException) { } catch (Exception e) { m_log.Error( @@ -438,7 +386,7 @@ namespace OpenMetaverse public void SyncSend(UDPPacketBuffer buf) { - if(buf.RemoteEndPoint == null) + if(buf.RemoteEndPoint is null) return; // already expired try {