From: Michael Osias <mosias@us.ibm.com>

Fixes mantis 771 - multiple uses of llListen cause duplicate events

Thanks Michael!
0.6.0-stable
Justin Clarke Casey 2008-03-24 19:44:25 +00:00
parent 9117dfc858
commit 45b9114df7
1 changed files with 148 additions and 115 deletions

View File

@ -26,6 +26,7 @@
*/
using System;
using System.Collections;
using System.Collections.Generic;
using libsecondlife;
using Nini.Config;
@ -72,8 +73,8 @@ namespace OpenSim.Region.Environment.Modules
private object ListLock = new object();
private string m_name = "WorldCommModule";
private ListenerManager m_listenerManager;
private Queue<ListenerInfo> m_pending;
private Queue m_pendingQ;
private Queue m_pending;
public WorldCommModule()
{
}
@ -84,7 +85,8 @@ namespace OpenSim.Region.Environment.Modules
m_scene.RegisterModuleInterface<IWorldComm>(this);
m_listenerManager = new ListenerManager();
m_scene.EventManager.OnNewClient += NewClient;
m_pending = new Queue<ListenerInfo>();
m_pendingQ = new Queue();
m_pending = Queue.Synchronized(m_pendingQ);
}
public void PostInitialise()
@ -124,30 +126,32 @@ namespace OpenSim.Region.Environment.Modules
}
public void ListenControl(int handle, int active)
{
if (m_listenerManager != null)
{
if (active == 1)
m_listenerManager.Activate(handle);
else if (active == 0)
m_listenerManager.Dectivate(handle);
}
}
public void ListenRemove(int handle)
{
if (m_listenerManager != null)
{
m_listenerManager.Remove(handle);
}
}
public void DeleteListener(LLUUID itemID)
{
if (m_listenerManager != null)
{
lock (ListLock)
{
m_listenerManager.DeleteListener(itemID);
}
}
}
// This method scans nearby objects and determines if they are listeners,
// and if so if this message fits the filter. If it does, then
// enqueue the message for delivery to the objects listen event handler.
@ -175,6 +179,8 @@ namespace OpenSim.Region.Environment.Modules
m_scene.Entities.TryGetValue(li.GetHostID(), out sPart);
if(sPart != null)
{
// Dont process if this message is from itself!
if (li.GetHostID().ToString().Equals(sourceItemID) ||
sPart.UUID.ToString().Equals(sourceItemID))
@ -192,51 +198,60 @@ namespace OpenSim.Region.Environment.Modules
case ChatTypeEnum.Whisper:
if ((dis < 10) && (dis > -10))
{
if (li.GetChannel() == channel)
{
ListenerInfo isListener = m_listenerManager.IsListenerMatch(
sourceItemID, sPart.UUID, channel, name, msg
);
if (isListener != null)
{
lock (CommListLock)
lock (m_pending.SyncRoot)
{
m_pending.Enqueue(isListener);
}
}
}
}
break;
case ChatTypeEnum.Say:
if ((dis < 30) && (dis > -30))
{
if (li.GetChannel() == channel)
{
ListenerInfo isListener = m_listenerManager.IsListenerMatch(
sourceItemID, sPart.UUID, channel, name, msg
);
if (isListener != null)
{
lock (CommListLock)
lock (m_pending.SyncRoot)
{
m_pending.Enqueue(isListener);
}
}
}
}
break;
case ChatTypeEnum.Shout:
if ((dis < 100) && (dis > -100))
{
if (li.GetChannel() == channel)
{
ListenerInfo isListener = m_listenerManager.IsListenerMatch(
sourceItemID, sPart.UUID, channel, name, msg
);
if (isListener != null)
{
lock (CommListLock)
lock (m_pending.SyncRoot)
{
m_pending.Enqueue(isListener);
}
}
}
}
break;
case ChatTypeEnum.Broadcast:
@ -249,7 +264,7 @@ namespace OpenSim.Region.Environment.Modules
);
if (isListener != null)
{
lock (CommListLock)
lock (m_pending.SyncRoot)
{
m_pending.Enqueue(isListener);
}
@ -260,6 +275,7 @@ namespace OpenSim.Region.Environment.Modules
}
}
}
}
public bool HasMessages()
{
@ -273,9 +289,9 @@ namespace OpenSim.Region.Environment.Modules
{
ListenerInfo li = null;
lock (CommListLock)
lock (m_pending.SyncRoot)
{
li = m_pending.Dequeue();
li = (ListenerInfo) m_pending.Dequeue();
}
return li;
@ -283,12 +299,12 @@ namespace OpenSim.Region.Environment.Modules
public uint PeekNextMessageLocalID()
{
return m_pending.Peek().GetLocalID();
return ((ListenerInfo)m_pending.Peek()).GetLocalID();
}
public LLUUID PeekNextMessageItemID()
{
return m_pending.Peek().GetItemID();
return ((ListenerInfo)m_pending.Peek()).GetItemID();
}
}
@ -298,15 +314,11 @@ namespace OpenSim.Region.Environment.Modules
// localID: local ID of host engine
public class ListenerManager
{
private Dictionary<int, ListenerInfo> m_listeners;
//private Dictionary<int, ListenerInfo> m_listeners;
private Hashtable m_listeners = Hashtable.Synchronized(new Hashtable());
private object ListenersLock = new object();
private int m_MaxListeners = 100;
public ListenerManager()
{
m_listeners = new Dictionary<int, ListenerInfo>();
}
public int AddListener(uint localID, LLUUID itemID, LLUUID hostID, int channel, string name, string id, string msg)
{
if (m_listeners.Count < m_MaxListeners)
@ -321,7 +333,7 @@ namespace OpenSim.Region.Environment.Modules
{
ListenerInfo li = new ListenerInfo(localID, newHandle, itemID, hostID, channel, name, id, msg);
lock (ListenersLock)
lock (m_listeners.SyncRoot)
{
m_listeners.Add(newHandle, li);
}
@ -335,18 +347,31 @@ namespace OpenSim.Region.Environment.Modules
}
public void Remove(int handle)
{
lock (m_listeners.SyncRoot)
{
m_listeners.Remove(handle);
}
}
public void DeleteListener(LLUUID itemID)
{
foreach (ListenerInfo li in m_listeners.Values)
ArrayList removedListeners = new ArrayList();
lock (m_listeners.SyncRoot)
{
IDictionaryEnumerator en = m_listeners.GetEnumerator();
while (en.MoveNext())
{
ListenerInfo li = (ListenerInfo)en.Value;
if (li.GetItemID().Equals(itemID))
{
Remove(li.GetHandle());
return;
removedListeners.Add(li.GetHandle());
}
}
foreach (int handle in removedListeners)
{
m_listeners.Remove(handle);
}
}
}
@ -375,20 +400,23 @@ namespace OpenSim.Region.Environment.Modules
public void Activate(int handle)
{
ListenerInfo li;
if (m_listeners.TryGetValue(handle, out li))
if (m_listeners.ContainsKey(handle))
{
lock (m_listeners.SyncRoot)
{
ListenerInfo li = (ListenerInfo)m_listeners[handle];
li.Activate();
}
}
}
public void Dectivate(int handle)
{
ListenerInfo li;
if (m_listeners.TryGetValue(handle, out li))
if (m_listeners.ContainsKey(handle))
{
ListenerInfo li = (ListenerInfo)m_listeners[handle];
li.Deactivate();
}
}
@ -399,9 +427,13 @@ namespace OpenSim.Region.Environment.Modules
string msg)
{
bool isMatch = true;
foreach (ListenerInfo li in m_listeners.Values)
lock (m_listeners.SyncRoot)
{
IDictionaryEnumerator en = m_listeners.GetEnumerator();
while (en.MoveNext())
{
ListenerInfo li = (ListenerInfo)en.Value;
if (li.GetHostID().Equals(listenerKey))
{
if (li.IsActive())
@ -434,10 +466,11 @@ namespace OpenSim.Region.Environment.Modules
}
}
}
}
return null;
}
public Dictionary<int, ListenerInfo>.ValueCollection GetListeners()
public ICollection GetListeners()
{
return m_listeners.Values;
}