OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ClientManager.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using System.Net;
32 using OpenMetaverse;
33 using OpenMetaverse.Packets;
34 
35 namespace OpenSim.Framework
36 {
41  public class ClientManager
42  {
45  private Dictionary<UUID, IClientAPI> m_dict1;
48  private Dictionary<IPEndPoint, IClientAPI> m_dict2;
51  private IClientAPI[] m_array;
53  private object m_syncRoot = new object();
54 
56  public int Count { get { return m_dict1.Count; } }
57 
61  public ClientManager()
62  {
63  m_dict1 = new Dictionary<UUID, IClientAPI>();
64  m_dict2 = new Dictionary<IPEndPoint, IClientAPI>();
65  m_array = new IClientAPI[0];
66  }
67 
75  public bool Add(IClientAPI value)
76  {
77  lock (m_syncRoot)
78  {
79  if (m_dict1.ContainsKey(value.AgentId) || m_dict2.ContainsKey(value.RemoteEndPoint))
80  return false;
81 
82  m_dict1[value.AgentId] = value;
83  m_dict2[value.RemoteEndPoint] = value;
84 
85  IClientAPI[] oldArray = m_array;
86  int oldLength = oldArray.Length;
87 
88  IClientAPI[] newArray = new IClientAPI[oldLength + 1];
89  for (int i = 0; i < oldLength; i++)
90  newArray[i] = oldArray[i];
91  newArray[oldLength] = value;
92 
93  m_array = newArray;
94  }
95 
96  return true;
97  }
98 
105  public bool Remove(UUID key)
106  {
107  lock (m_syncRoot)
108  {
109  IClientAPI value;
110  if (m_dict1.TryGetValue(key, out value))
111  {
112  m_dict1.Remove(key);
113  m_dict2.Remove(value.RemoteEndPoint);
114 
115  IClientAPI[] oldArray = m_array;
116  int oldLength = oldArray.Length;
117 
118  IClientAPI[] newArray = new IClientAPI[oldLength - 1];
119  int j = 0;
120  for (int i = 0; i < oldLength; i++)
121  {
122  if (oldArray[i] != value)
123  newArray[j++] = oldArray[i];
124  }
125 
126  m_array = newArray;
127  return true;
128  }
129  }
130 
131  return false;
132  }
133 
137  public void Clear()
138  {
139  lock (m_syncRoot)
140  {
141  m_dict1.Clear();
142  m_dict2.Clear();
143  m_array = new IClientAPI[0];
144  }
145  }
146 
152  public bool ContainsKey(UUID key)
153  {
154  return m_dict1.ContainsKey(key);
155  }
156 
162  public bool ContainsKey(IPEndPoint key)
163  {
164  return m_dict2.ContainsKey(key);
165  }
166 
173  public bool TryGetValue(UUID key, out IClientAPI value)
174  {
175  try { return m_dict1.TryGetValue(key, out value); }
176  catch (Exception)
177  {
178  value = null;
179  return false;
180  }
181  }
182 
189  public bool TryGetValue(IPEndPoint key, out IClientAPI value)
190  {
191  try { return m_dict2.TryGetValue(key, out value); }
192  catch (Exception)
193  {
194  value = null;
195  return false;
196  }
197  }
198 
204  public void ForEach(Action<IClientAPI> action)
205  {
206  IClientAPI[] localArray = m_array;
207  Parallel.For(0, localArray.Length,
208  delegate(int i)
209  { action(localArray[i]); }
210  );
211  }
212 
218  public void ForEachSync(Action<IClientAPI> action)
219  {
220  IClientAPI[] localArray = m_array;
221  for (int i = 0; i < localArray.Length; i++)
222  action(localArray[i]);
223  }
224  }
225 }
bool ContainsKey(UUID key)
Checks if a UUID is in the collection
bool Add(IClientAPI value)
Add a client reference to the collection if it does not already exist
void ForEach(Action< IClientAPI > action)
Performs a given task in parallel for each of the elements in the collection
bool Remove(UUID key)
Remove a client from the collection
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
Definition: ICM_Api.cs:31
bool TryGetValue(IPEndPoint key, out IClientAPI value)
Attempts to fetch a value out of the collection
bool ContainsKey(IPEndPoint key)
Checks if an endpoint is in the collection
ClientManager()
Default constructor
Maps from client AgentID and RemoteEndPoint values to IClientAPI references for all of the connected ...
void ForEachSync(Action< IClientAPI > action)
Performs a given task synchronously for each of the elements in the collection
void Clear()
Resets the client collection
bool TryGetValue(UUID key, out IClientAPI value)
Attempts to fetch a value out of the collection