From be2ad79e52efb5eb543057e8e73fa601d0b91c87 Mon Sep 17 00:00:00 2001 From: Jeff Ames Date: Thu, 20 Dec 2007 05:43:02 +0000 Subject: Added patch from Johan. First attempt to solve the LibSL.Packet GC problem. Works with LibSL rev>1532 --- OpenSim/Region/ClientStack/UDPServer.cs | 65 ++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'OpenSim/Region/ClientStack/UDPServer.cs') diff --git a/OpenSim/Region/ClientStack/UDPServer.cs b/OpenSim/Region/ClientStack/UDPServer.cs index f91e5e2..5cad041 100644 --- a/OpenSim/Region/ClientStack/UDPServer.cs +++ b/OpenSim/Region/ClientStack/UDPServer.cs @@ -26,6 +26,7 @@ * */ using System; +using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; @@ -36,6 +37,68 @@ using OpenSim.Framework.Console; namespace OpenSim.Region.ClientStack { + public sealed class PacketPool + { + // Set up a thread-safe singleton pattern + static PacketPool() + { + } + + static readonly PacketPool instance = new PacketPool(); + + public static PacketPool Instance + { + get + { + return instance; + } + } + + private Hashtable pool = new Hashtable(); + + public Packet GetPacket(PacketType type) { + Packet packet = null; + + lock(pool) + { + if(pool[type] == null || ((Stack) pool[type]).Count == 0) + { + // Creating a new packet if we cannot reuse an old package + packet = Packet.BuildPacket(type); + } + else + { + // Recycle old packages + packet=(Packet) ((Stack) pool[type]).Pop(); + } + } + + return packet; + } + + public Packet GetPacket(byte[] bytes, ref int packetEnd, byte[] zeroBuffer) { + Packet packet = GetPacket(Packet.GetType(bytes, packetEnd, zeroBuffer)); + + int i = 0; + packet.FromBytes(bytes, ref i, ref packetEnd, zeroBuffer); + return packet; + } + + public void ReturnPacket(Packet packet) { + lock(pool) + { + PacketType type=packet.Type; + + if(pool[type] == null) + { + pool[type] = new Stack(); + } + + ((Stack) pool[type]).Push(packet); + } + } + } + public class UDPServer : ClientStackNetworkHandler { protected Dictionary clientCircuits = new Dictionary(); @@ -194,7 +257,7 @@ namespace OpenSim.Region.ClientStack try { - packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer); + packet = PacketPool.Instance.GetPacket(RecvBuffer, ref packetEnd, ZeroBuffer); } catch(Exception) { -- cgit v1.1