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