diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs | 382 |
1 files changed, 339 insertions, 43 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs index 13415f8..d4dbfb9 100644 --- a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs +++ b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs | |||
@@ -27,18 +27,13 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections; | 29 | using System.Collections; |
30 | using System.Collections.Specialized; | 30 | using System.Collections.Generic; |
31 | using System.Drawing; | ||
32 | using System.Drawing.Imaging; | ||
33 | using System.Reflection; | 31 | using System.Reflection; |
34 | using System.IO; | 32 | using System.Threading; |
35 | using System.Web; | ||
36 | using log4net; | 33 | using log4net; |
37 | using Nini.Config; | 34 | using Nini.Config; |
38 | using Mono.Addins; | 35 | using Mono.Addins; |
39 | using OpenMetaverse; | 36 | using OpenMetaverse; |
40 | using OpenMetaverse.StructuredData; | ||
41 | using OpenMetaverse.Imaging; | ||
42 | using OpenSim.Framework; | 37 | using OpenSim.Framework; |
43 | using OpenSim.Framework.Servers; | 38 | using OpenSim.Framework.Servers; |
44 | using OpenSim.Framework.Servers.HttpServer; | 39 | using OpenSim.Framework.Servers.HttpServer; |
@@ -47,6 +42,7 @@ using OpenSim.Region.Framework.Scenes; | |||
47 | using OpenSim.Services.Interfaces; | 42 | using OpenSim.Services.Interfaces; |
48 | using Caps = OpenSim.Framework.Capabilities.Caps; | 43 | using Caps = OpenSim.Framework.Capabilities.Caps; |
49 | using OpenSim.Capabilities.Handlers; | 44 | using OpenSim.Capabilities.Handlers; |
45 | using OpenSim.Framework.Monitoring; | ||
50 | 46 | ||
51 | namespace OpenSim.Region.ClientStack.Linden | 47 | namespace OpenSim.Region.ClientStack.Linden |
52 | { | 48 | { |
@@ -54,57 +50,131 @@ namespace OpenSim.Region.ClientStack.Linden | |||
54 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GetTextureModule")] | 50 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GetTextureModule")] |
55 | public class GetTextureModule : INonSharedRegionModule | 51 | public class GetTextureModule : INonSharedRegionModule |
56 | { | 52 | { |
57 | // private static readonly ILog m_log = | 53 | |
58 | // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 54 | struct aPollRequest |
59 | 55 | { | |
56 | public PollServiceTextureEventArgs thepoll; | ||
57 | public UUID reqID; | ||
58 | public Hashtable request; | ||
59 | } | ||
60 | |||
61 | public class aPollResponse | ||
62 | { | ||
63 | public Hashtable response; | ||
64 | public int bytes; | ||
65 | } | ||
66 | |||
67 | |||
68 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
69 | |||
60 | private Scene m_scene; | 70 | private Scene m_scene; |
61 | private IAssetService m_assetService; | ||
62 | 71 | ||
63 | private bool m_Enabled = false; | 72 | private static GetTextureHandler m_getTextureHandler; |
73 | |||
74 | private IAssetService m_assetService = null; | ||
75 | |||
76 | private Dictionary<UUID, string> m_capsDict = new Dictionary<UUID, string>(); | ||
77 | private static Thread[] m_workerThreads = null; | ||
64 | 78 | ||
65 | // TODO: Change this to a config option | 79 | private static OpenMetaverse.BlockingQueue<aPollRequest> m_queue = |
66 | const string REDIRECT_URL = null; | 80 | new OpenMetaverse.BlockingQueue<aPollRequest>(); |
67 | 81 | ||
68 | private string m_URL; | 82 | private Dictionary<UUID,PollServiceTextureEventArgs> m_pollservices = new Dictionary<UUID,PollServiceTextureEventArgs>(); |
69 | 83 | ||
70 | #region ISharedRegionModule Members | 84 | #region ISharedRegionModule Members |
71 | 85 | ||
72 | public void Initialise(IConfigSource source) | 86 | public void Initialise(IConfigSource source) |
73 | { | 87 | { |
74 | IConfig config = source.Configs["ClientStack.LindenCaps"]; | ||
75 | if (config == null) | ||
76 | return; | ||
77 | |||
78 | m_URL = config.GetString("Cap_GetTexture", string.Empty); | ||
79 | // Cap doesn't exist | ||
80 | if (m_URL != string.Empty) | ||
81 | m_Enabled = true; | ||
82 | } | 88 | } |
83 | 89 | ||
84 | public void AddRegion(Scene s) | 90 | public void AddRegion(Scene s) |
85 | { | 91 | { |
86 | if (!m_Enabled) | ||
87 | return; | ||
88 | |||
89 | m_scene = s; | 92 | m_scene = s; |
93 | m_assetService = s.AssetService; | ||
90 | } | 94 | } |
91 | 95 | ||
92 | public void RemoveRegion(Scene s) | 96 | public void RemoveRegion(Scene s) |
93 | { | 97 | { |
94 | if (!m_Enabled) | ||
95 | return; | ||
96 | |||
97 | m_scene.EventManager.OnRegisterCaps -= RegisterCaps; | 98 | m_scene.EventManager.OnRegisterCaps -= RegisterCaps; |
99 | m_scene.EventManager.OnDeregisterCaps -= DeregisterCaps; | ||
100 | m_scene.EventManager.OnThrottleUpdate -= ThrottleUpdate; | ||
98 | m_scene = null; | 101 | m_scene = null; |
99 | } | 102 | } |
100 | 103 | ||
101 | public void RegionLoaded(Scene s) | 104 | public void RegionLoaded(Scene s) |
102 | { | 105 | { |
103 | if (!m_Enabled) | 106 | // We'll reuse the same handler for all requests. |
104 | return; | 107 | m_getTextureHandler = new GetTextureHandler(m_assetService); |
105 | 108 | ||
106 | m_assetService = m_scene.RequestModuleInterface<IAssetService>(); | ||
107 | m_scene.EventManager.OnRegisterCaps += RegisterCaps; | 109 | m_scene.EventManager.OnRegisterCaps += RegisterCaps; |
110 | m_scene.EventManager.OnDeregisterCaps += DeregisterCaps; | ||
111 | m_scene.EventManager.OnThrottleUpdate += ThrottleUpdate; | ||
112 | |||
113 | if (m_workerThreads == null) | ||
114 | { | ||
115 | m_workerThreads = new Thread[2]; | ||
116 | |||
117 | for (uint i = 0; i < 2; i++) | ||
118 | { | ||
119 | m_workerThreads[i] = Watchdog.StartThread(DoTextureRequests, | ||
120 | String.Format("TextureWorkerThread{0}", i), | ||
121 | ThreadPriority.Normal, | ||
122 | false, | ||
123 | false, | ||
124 | null, | ||
125 | int.MaxValue); | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | private int ExtractImageThrottle(byte[] pthrottles) | ||
130 | { | ||
131 | |||
132 | byte[] adjData; | ||
133 | int pos = 0; | ||
134 | |||
135 | if (!BitConverter.IsLittleEndian) | ||
136 | { | ||
137 | byte[] newData = new byte[7 * 4]; | ||
138 | Buffer.BlockCopy(pthrottles, 0, newData, 0, 7 * 4); | ||
139 | |||
140 | for (int i = 0; i < 7; i++) | ||
141 | Array.Reverse(newData, i * 4, 4); | ||
142 | |||
143 | adjData = newData; | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | adjData = pthrottles; | ||
148 | } | ||
149 | |||
150 | // 0.125f converts from bits to bytes | ||
151 | //int resend = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); | ||
152 | //pos += 4; | ||
153 | // int land = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); | ||
154 | //pos += 4; | ||
155 | // int wind = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); | ||
156 | // pos += 4; | ||
157 | // int cloud = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); | ||
158 | // pos += 4; | ||
159 | // int task = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); | ||
160 | // pos += 4; | ||
161 | pos = pos + 20; | ||
162 | int texture = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); //pos += 4; | ||
163 | //int asset = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); | ||
164 | return texture; | ||
165 | } | ||
166 | |||
167 | // Now we know when the throttle is changed by the client in the case of a root agent or by a neighbor region in the case of a child agent. | ||
168 | public void ThrottleUpdate(ScenePresence p) | ||
169 | { | ||
170 | byte[] throttles = p.ControllingClient.GetThrottlesPacked(1); | ||
171 | UUID user = p.UUID; | ||
172 | int imagethrottle = ExtractImageThrottle(throttles); | ||
173 | PollServiceTextureEventArgs args; | ||
174 | if (m_pollservices.TryGetValue(user,out args)) | ||
175 | { | ||
176 | args.UpdateThrottle(imagethrottle); | ||
177 | } | ||
108 | } | 178 | } |
109 | 179 | ||
110 | public void PostInitialise() | 180 | public void PostInitialise() |
@@ -122,24 +192,250 @@ namespace OpenSim.Region.ClientStack.Linden | |||
122 | 192 | ||
123 | #endregion | 193 | #endregion |
124 | 194 | ||
125 | public void RegisterCaps(UUID agentID, Caps caps) | 195 | ~GetTextureModule() |
196 | { | ||
197 | foreach (Thread t in m_workerThreads) | ||
198 | Watchdog.AbortThread(t.ManagedThreadId); | ||
199 | |||
200 | } | ||
201 | |||
202 | private class PollServiceTextureEventArgs : PollServiceEventArgs | ||
126 | { | 203 | { |
127 | UUID capID = UUID.Random(); | 204 | private List<Hashtable> requests = |
205 | new List<Hashtable>(); | ||
206 | private Dictionary<UUID, aPollResponse> responses = | ||
207 | new Dictionary<UUID, aPollResponse>(); | ||
128 | 208 | ||
129 | //caps.RegisterHandler("GetTexture", new StreamHandler("GET", "/CAPS/" + capID, ProcessGetTexture)); | 209 | private Scene m_scene; |
130 | if (m_URL == "localhost") | 210 | private CapsDataThrottler m_throttler = new CapsDataThrottler(100000, 1400000,10000); |
211 | public PollServiceTextureEventArgs(UUID pId, Scene scene) : | ||
212 | base(null, null, null, null, pId, int.MaxValue) | ||
131 | { | 213 | { |
132 | // m_log.DebugFormat("[GETTEXTURE]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName); | 214 | m_scene = scene; |
133 | caps.RegisterHandler( | 215 | // x is request id, y is userid |
134 | "GetTexture", | 216 | HasEvents = (x, y) => |
135 | new GetTextureHandler("/CAPS/" + capID + "/", m_assetService, "GetTexture", agentID.ToString())); | 217 | { |
218 | lock (responses) | ||
219 | { | ||
220 | bool ret = m_throttler.hasEvents(x, responses); | ||
221 | m_throttler.ProcessTime(); | ||
222 | return ret; | ||
223 | |||
224 | } | ||
225 | }; | ||
226 | GetEvents = (x, y) => | ||
227 | { | ||
228 | lock (responses) | ||
229 | { | ||
230 | try | ||
231 | { | ||
232 | return responses[x].response; | ||
233 | } | ||
234 | finally | ||
235 | { | ||
236 | responses.Remove(x); | ||
237 | } | ||
238 | } | ||
239 | }; | ||
240 | // x is request id, y is request data hashtable | ||
241 | Request = (x, y) => | ||
242 | { | ||
243 | aPollRequest reqinfo = new aPollRequest(); | ||
244 | reqinfo.thepoll = this; | ||
245 | reqinfo.reqID = x; | ||
246 | reqinfo.request = y; | ||
247 | |||
248 | m_queue.Enqueue(reqinfo); | ||
249 | }; | ||
250 | |||
251 | // this should never happen except possible on shutdown | ||
252 | NoEvents = (x, y) => | ||
253 | { | ||
254 | /* | ||
255 | lock (requests) | ||
256 | { | ||
257 | Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString()); | ||
258 | requests.Remove(request); | ||
259 | } | ||
260 | */ | ||
261 | Hashtable response = new Hashtable(); | ||
262 | |||
263 | response["int_response_code"] = 500; | ||
264 | response["str_response_string"] = "Script timeout"; | ||
265 | response["content_type"] = "text/plain"; | ||
266 | response["keepalive"] = false; | ||
267 | response["reusecontext"] = false; | ||
268 | |||
269 | return response; | ||
270 | }; | ||
136 | } | 271 | } |
137 | else | 272 | |
273 | public void Process(aPollRequest requestinfo) | ||
274 | { | ||
275 | Hashtable response; | ||
276 | |||
277 | UUID requestID = requestinfo.reqID; | ||
278 | |||
279 | // If the avatar is gone, don't bother to get the texture | ||
280 | if (m_scene.GetScenePresence(Id) == null) | ||
281 | { | ||
282 | response = new Hashtable(); | ||
283 | |||
284 | response["int_response_code"] = 500; | ||
285 | response["str_response_string"] = "Script timeout"; | ||
286 | response["content_type"] = "text/plain"; | ||
287 | response["keepalive"] = false; | ||
288 | response["reusecontext"] = false; | ||
289 | |||
290 | lock (responses) | ||
291 | responses[requestID] = new aPollResponse() {bytes = 0, response = response}; | ||
292 | |||
293 | return; | ||
294 | } | ||
295 | |||
296 | response = m_getTextureHandler.Handle(requestinfo.request); | ||
297 | lock (responses) | ||
298 | { | ||
299 | responses[requestID] = new aPollResponse() | ||
300 | { | ||
301 | bytes = (int) response["int_bytes"], | ||
302 | response = response | ||
303 | }; | ||
304 | |||
305 | } | ||
306 | m_throttler.ProcessTime(); | ||
307 | } | ||
308 | |||
309 | internal void UpdateThrottle(int pimagethrottle) | ||
310 | { | ||
311 | m_throttler.ThrottleBytes = pimagethrottle; | ||
312 | } | ||
313 | } | ||
314 | |||
315 | private void RegisterCaps(UUID agentID, Caps caps) | ||
316 | { | ||
317 | string capUrl = "/CAPS/" + UUID.Random() + "/"; | ||
318 | |||
319 | // Register this as a poll service | ||
320 | PollServiceTextureEventArgs args = new PollServiceTextureEventArgs(agentID, m_scene); | ||
321 | |||
322 | args.Type = PollServiceEventArgs.EventType.Texture; | ||
323 | MainServer.Instance.AddPollServiceHTTPHandler(capUrl, args); | ||
324 | |||
325 | string hostName = m_scene.RegionInfo.ExternalHostName; | ||
326 | uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port; | ||
327 | string protocol = "http"; | ||
328 | |||
329 | if (MainServer.Instance.UseSSL) | ||
330 | { | ||
331 | hostName = MainServer.Instance.SSLCommonName; | ||
332 | port = MainServer.Instance.SSLPort; | ||
333 | protocol = "https"; | ||
334 | } | ||
335 | caps.RegisterHandler("GetTexture", String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, capUrl)); | ||
336 | m_pollservices[agentID] = args; | ||
337 | m_capsDict[agentID] = capUrl; | ||
338 | } | ||
339 | |||
340 | private void DeregisterCaps(UUID agentID, Caps caps) | ||
341 | { | ||
342 | string capUrl; | ||
343 | PollServiceTextureEventArgs args; | ||
344 | if (m_capsDict.TryGetValue(agentID, out capUrl)) | ||
345 | { | ||
346 | MainServer.Instance.RemoveHTTPHandler("", capUrl); | ||
347 | m_capsDict.Remove(agentID); | ||
348 | } | ||
349 | if (m_pollservices.TryGetValue(agentID, out args)) | ||
138 | { | 350 | { |
139 | // m_log.DebugFormat("[GETTEXTURE]: {0} in region {1}", m_URL, m_scene.RegionInfo.RegionName); | 351 | m_pollservices.Remove(agentID); |
140 | caps.RegisterHandler("GetTexture", m_URL); | ||
141 | } | 352 | } |
142 | } | 353 | } |
143 | 354 | ||
355 | private void DoTextureRequests() | ||
356 | { | ||
357 | while (true) | ||
358 | { | ||
359 | aPollRequest poolreq = m_queue.Dequeue(); | ||
360 | |||
361 | poolreq.thepoll.Process(poolreq); | ||
362 | } | ||
363 | } | ||
364 | internal sealed class CapsDataThrottler | ||
365 | { | ||
366 | |||
367 | private volatile int currenttime = 0; | ||
368 | private volatile int lastTimeElapsed = 0; | ||
369 | private volatile int BytesSent = 0; | ||
370 | private int oversizedImages = 0; | ||
371 | public CapsDataThrottler(int pBytes, int max, int min) | ||
372 | { | ||
373 | ThrottleBytes = pBytes; | ||
374 | lastTimeElapsed = Util.EnvironmentTickCount(); | ||
375 | } | ||
376 | public bool hasEvents(UUID key, Dictionary<UUID, GetTextureModule.aPollResponse> responses) | ||
377 | { | ||
378 | PassTime(); | ||
379 | // Note, this is called IN LOCK | ||
380 | bool haskey = responses.ContainsKey(key); | ||
381 | if (!haskey) | ||
382 | { | ||
383 | return false; | ||
384 | } | ||
385 | GetTextureModule.aPollResponse response; | ||
386 | if (responses.TryGetValue(key, out response)) | ||
387 | { | ||
388 | |||
389 | // Normal | ||
390 | if (BytesSent + response.bytes <= ThrottleBytes) | ||
391 | { | ||
392 | BytesSent += response.bytes; | ||
393 | //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + 1000, unlockyn = false }; | ||
394 | //m_actions.Add(timeBasedAction); | ||
395 | return true; | ||
396 | } | ||
397 | // Big textures | ||
398 | else if (response.bytes > ThrottleBytes && oversizedImages <= ((ThrottleBytes % 50000) + 1)) | ||
399 | { | ||
400 | Interlocked.Increment(ref oversizedImages); | ||
401 | BytesSent += response.bytes; | ||
402 | //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + (((response.bytes % ThrottleBytes)+1)*1000) , unlockyn = false }; | ||
403 | //m_actions.Add(timeBasedAction); | ||
404 | return true; | ||
405 | } | ||
406 | else | ||
407 | { | ||
408 | return false; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | return haskey; | ||
413 | } | ||
414 | public void ProcessTime() | ||
415 | { | ||
416 | PassTime(); | ||
417 | } | ||
418 | |||
419 | |||
420 | private void PassTime() | ||
421 | { | ||
422 | currenttime = Util.EnvironmentTickCount(); | ||
423 | int timeElapsed = Util.EnvironmentTickCountSubtract(currenttime, lastTimeElapsed); | ||
424 | //processTimeBasedActions(responses); | ||
425 | if (Util.EnvironmentTickCountSubtract(currenttime, timeElapsed) >= 1000) | ||
426 | { | ||
427 | lastTimeElapsed = Util.EnvironmentTickCount(); | ||
428 | BytesSent -= ThrottleBytes; | ||
429 | if (BytesSent < 0) BytesSent = 0; | ||
430 | if (BytesSent < ThrottleBytes) | ||
431 | { | ||
432 | oversizedImages = 0; | ||
433 | } | ||
434 | } | ||
435 | } | ||
436 | public int ThrottleBytes; | ||
437 | } | ||
144 | } | 438 | } |
439 | |||
440 | |||
145 | } | 441 | } |