123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
-
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using OpenSim.Framework.Servers;
- using Mono.Addins;
- using log4net;
- using Nini.Config;
- using OpenSim.Region.Framework.Interfaces;
- using OpenSim.Region.Framework.Scenes;
- using OpenSim.Framework.Servers.HttpServer;
- namespace OpenSim.Region.OptionalModules.WebSocketEchoModule
- {
-
- [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "WebSocketEchoModule")]
- public class WebSocketEchoModule : ISharedRegionModule
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private bool enabled;
- public string Name { get { return "WebSocketEchoModule"; } }
- public Type ReplaceableInterface { get { return null; } }
- private HashSet<WebSocketHttpServerHandler> _activeHandlers = new HashSet<WebSocketHttpServerHandler>();
- public void Initialise(IConfigSource pConfig)
- {
- enabled = (pConfig.Configs["WebSocketEcho"] != null);
- }
-
-
-
- public void PostInitialise()
- {
- if (enabled)
- MainServer.Instance.AddWebSocketHandler("/echo", WebSocketHandlerCallback);
- }
-
- public void WebSocketHandlerCallback(string path, WebSocketHttpServerHandler handler)
- {
- SubscribeToEvents(handler);
- handler.SetChunksize(8192);
- handler.NoDelay_TCP_Nagle = true;
- handler.HandshakeAndUpgrade();
- }
-
- public void SubscribeToEvents(WebSocketHttpServerHandler handler)
- {
- handler.OnClose += HandlerOnOnClose;
- handler.OnText += HandlerOnOnText;
- handler.OnUpgradeCompleted += HandlerOnOnUpgradeCompleted;
- handler.OnData += HandlerOnOnData;
- handler.OnPong += HandlerOnOnPong;
- }
- public void UnSubscribeToEvents(WebSocketHttpServerHandler handler)
- {
- handler.OnClose -= HandlerOnOnClose;
- handler.OnText -= HandlerOnOnText;
- handler.OnUpgradeCompleted -= HandlerOnOnUpgradeCompleted;
- handler.OnData -= HandlerOnOnData;
- handler.OnPong -= HandlerOnOnPong;
- }
- private void HandlerOnOnPong(object sender, PongEventArgs pongdata)
- {
- m_log.Info("[WebSocketEchoModule]: Got a pong.. ping time: " + pongdata.PingResponseMS);
- }
- private void HandlerOnOnData(object sender, WebsocketDataEventArgs data)
- {
- WebSocketHttpServerHandler obj = sender as WebSocketHttpServerHandler;
- obj.SendData(data.Data);
- m_log.Info("[WebSocketEchoModule]: We received a bunch of ugly non-printable bytes");
- obj.SendPingCheck();
- }
-
- private void HandlerOnOnUpgradeCompleted(object sender, UpgradeCompletedEventArgs completeddata)
- {
- WebSocketHttpServerHandler obj = sender as WebSocketHttpServerHandler;
- _activeHandlers.Add(obj);
- }
- private void HandlerOnOnText(object sender, WebsocketTextEventArgs text)
- {
- WebSocketHttpServerHandler obj = sender as WebSocketHttpServerHandler;
- obj.SendMessage(text.Data);
- m_log.Info("[WebSocketEchoModule]: We received this: " + text.Data);
- }
-
- private void HandlerOnOnClose(object sender, CloseEventArgs closedata)
- {
- WebSocketHttpServerHandler obj = sender as WebSocketHttpServerHandler;
- UnSubscribeToEvents(obj);
- lock (_activeHandlers)
- _activeHandlers.Remove(obj);
- obj.Dispose();
- }
-
-
- public void Close()
- {
- if (!enabled)
- return;
-
-
-
- WebSocketHttpServerHandler[] items = new WebSocketHttpServerHandler[_activeHandlers.Count];
- _activeHandlers.CopyTo(items);
- for (int i = 0; i < items.Length; i++)
- {
- items[i].Close(string.Empty);
- items[i].Dispose();
- }
- _activeHandlers.Clear();
- MainServer.Instance.RemoveWebSocketHandler("/echo");
- }
- public void AddRegion(Scene scene)
- {
- }
- public void RemoveRegion(Scene scene)
- {
- }
- public void RegionLoaded(Scene scene)
- {
- }
- }
- }
|