diff options
Diffstat (limited to 'OpenSim/Framework/Servers')
14 files changed, 1596 insertions, 185 deletions
diff --git a/OpenSim/Framework/Servers/BaseOpenSimServer.cs b/OpenSim/Framework/Servers/BaseOpenSimServer.cs index 2c21800..cb47cbf 100644 --- a/OpenSim/Framework/Servers/BaseOpenSimServer.cs +++ b/OpenSim/Framework/Servers/BaseOpenSimServer.cs | |||
@@ -27,7 +27,6 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Diagnostics; | ||
31 | using System.IO; | 30 | using System.IO; |
32 | using System.Reflection; | 31 | using System.Reflection; |
33 | using System.Text; | 32 | using System.Text; |
@@ -99,34 +98,6 @@ namespace OpenSim.Framework.Servers | |||
99 | m_console.Commands.AddCommand("General", false, "shutdown", | 98 | m_console.Commands.AddCommand("General", false, "shutdown", |
100 | "shutdown", | 99 | "shutdown", |
101 | "Quit the application", HandleQuit); | 100 | "Quit the application", HandleQuit); |
102 | |||
103 | m_console.Commands.AddCommand("General", false, "show threads", | ||
104 | "show threads", | ||
105 | "Show thread status", HandleShow); | ||
106 | |||
107 | m_console.Commands.AddCommand("General", false, "show version", | ||
108 | "show version", | ||
109 | "Show server version", HandleShow); | ||
110 | |||
111 | m_console.Commands.AddCommand("General", false, "threads abort", | ||
112 | "threads abort <thread-id>", | ||
113 | "Abort a managed thread. Use \"show threads\" to find possible threads.", HandleThreadsAbort); | ||
114 | |||
115 | m_console.Commands.AddCommand("General", false, "threads show", | ||
116 | "threads show", | ||
117 | "Show thread status. Synonym for \"show threads\"", | ||
118 | (string module, string[] args) => Notice(GetThreadsReport())); | ||
119 | |||
120 | m_console.Commands.AddCommand("General", false, "force gc", | ||
121 | "force gc", | ||
122 | "Manually invoke runtime garbage collection. For debugging purposes", | ||
123 | HandleForceGc); | ||
124 | } | ||
125 | |||
126 | private void HandleForceGc(string module, string[] args) | ||
127 | { | ||
128 | MainConsole.Instance.Output("Manually invoking runtime garbage collection"); | ||
129 | GC.Collect(); | ||
130 | } | 101 | } |
131 | 102 | ||
132 | /// <summary> | 103 | /// <summary> |
@@ -159,54 +130,6 @@ namespace OpenSim.Framework.Servers | |||
159 | } | 130 | } |
160 | 131 | ||
161 | /// <summary> | 132 | /// <summary> |
162 | /// Get a report about the registered threads in this server. | ||
163 | /// </summary> | ||
164 | protected string GetThreadsReport() | ||
165 | { | ||
166 | // This should be a constant field. | ||
167 | string reportFormat = "{0,6} {1,35} {2,16} {3,13} {4,10} {5,30}"; | ||
168 | |||
169 | StringBuilder sb = new StringBuilder(); | ||
170 | Watchdog.ThreadWatchdogInfo[] threads = Watchdog.GetThreadsInfo(); | ||
171 | |||
172 | sb.Append(threads.Length + " threads are being tracked:" + Environment.NewLine); | ||
173 | |||
174 | int timeNow = Environment.TickCount & Int32.MaxValue; | ||
175 | |||
176 | sb.AppendFormat(reportFormat, "ID", "NAME", "LAST UPDATE (MS)", "LIFETIME (MS)", "PRIORITY", "STATE"); | ||
177 | sb.Append(Environment.NewLine); | ||
178 | |||
179 | foreach (Watchdog.ThreadWatchdogInfo twi in threads) | ||
180 | { | ||
181 | Thread t = twi.Thread; | ||
182 | |||
183 | sb.AppendFormat( | ||
184 | reportFormat, | ||
185 | t.ManagedThreadId, | ||
186 | t.Name, | ||
187 | timeNow - twi.LastTick, | ||
188 | timeNow - twi.FirstTick, | ||
189 | t.Priority, | ||
190 | t.ThreadState); | ||
191 | |||
192 | sb.Append("\n"); | ||
193 | } | ||
194 | |||
195 | sb.Append("\n"); | ||
196 | |||
197 | // For some reason mono 2.6.7 returns an empty threads set! Not going to confuse people by reporting | ||
198 | // zero active threads. | ||
199 | int totalThreads = Process.GetCurrentProcess().Threads.Count; | ||
200 | if (totalThreads > 0) | ||
201 | sb.AppendFormat("Total threads active: {0}\n\n", totalThreads); | ||
202 | |||
203 | sb.Append("Main threadpool (excluding script engine pools)\n"); | ||
204 | sb.Append(Util.GetThreadPoolReport()); | ||
205 | |||
206 | return sb.ToString(); | ||
207 | } | ||
208 | |||
209 | /// <summary> | ||
210 | /// Performs initialisation of the scene, such as loading configuration from disk. | 133 | /// Performs initialisation of the scene, such as loading configuration from disk. |
211 | /// </summary> | 134 | /// </summary> |
212 | public virtual void Startup() | 135 | public virtual void Startup() |
@@ -246,50 +169,7 @@ namespace OpenSim.Framework.Servers | |||
246 | private void HandleQuit(string module, string[] args) | 169 | private void HandleQuit(string module, string[] args) |
247 | { | 170 | { |
248 | Shutdown(); | 171 | Shutdown(); |
249 | } | 172 | } |
250 | |||
251 | public override void HandleShow(string module, string[] cmd) | ||
252 | { | ||
253 | base.HandleShow(module, cmd); | ||
254 | |||
255 | List<string> args = new List<string>(cmd); | ||
256 | |||
257 | args.RemoveAt(0); | ||
258 | |||
259 | string[] showParams = args.ToArray(); | ||
260 | |||
261 | switch (showParams[0]) | ||
262 | { | ||
263 | case "threads": | ||
264 | Notice(GetThreadsReport()); | ||
265 | break; | ||
266 | |||
267 | case "version": | ||
268 | Notice(GetVersionText()); | ||
269 | break; | ||
270 | } | ||
271 | } | ||
272 | |||
273 | public virtual void HandleThreadsAbort(string module, string[] cmd) | ||
274 | { | ||
275 | if (cmd.Length != 3) | ||
276 | { | ||
277 | MainConsole.Instance.Output("Usage: threads abort <thread-id>"); | ||
278 | return; | ||
279 | } | ||
280 | |||
281 | int threadId; | ||
282 | if (!int.TryParse(cmd[2], out threadId)) | ||
283 | { | ||
284 | MainConsole.Instance.Output("ERROR: Thread id must be an integer"); | ||
285 | return; | ||
286 | } | ||
287 | |||
288 | if (Watchdog.AbortThread(threadId)) | ||
289 | MainConsole.Instance.OutputFormat("Aborted thread with id {0}", threadId); | ||
290 | else | ||
291 | MainConsole.Instance.OutputFormat("ERROR - Thread with id {0} not found in managed threads", threadId); | ||
292 | } | ||
293 | 173 | ||
294 | public string osSecret { | 174 | public string osSecret { |
295 | // Secret uuid for the simulator | 175 | // Secret uuid for the simulator |
diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index 77fce9e..27af009 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs | |||
@@ -54,6 +54,16 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
54 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 54 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
55 | private HttpServerLogWriter httpserverlog = new HttpServerLogWriter(); | 55 | private HttpServerLogWriter httpserverlog = new HttpServerLogWriter(); |
56 | 56 | ||
57 | |||
58 | /// <summary> | ||
59 | /// This is a pending websocket request before it got an sucessful upgrade response. | ||
60 | /// The consumer must call handler.HandshakeAndUpgrade() to signal to the handler to | ||
61 | /// start the connection and optionally provide an origin authentication method. | ||
62 | /// </summary> | ||
63 | /// <param name="servicepath"></param> | ||
64 | /// <param name="handler"></param> | ||
65 | public delegate void WebSocketRequestDelegate(string servicepath, WebSocketHttpServerHandler handler); | ||
66 | |||
57 | /// <summary> | 67 | /// <summary> |
58 | /// Gets or sets the debug level. | 68 | /// Gets or sets the debug level. |
59 | /// </summary> | 69 | /// </summary> |
@@ -77,6 +87,7 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
77 | // protected HttpListener m_httpListener; | 87 | // protected HttpListener m_httpListener; |
78 | protected CoolHTTPListener m_httpListener2; | 88 | protected CoolHTTPListener m_httpListener2; |
79 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); | 89 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); |
90 | protected Dictionary<string, JsonRPCMethod> jsonRpcHandlers = new Dictionary<string, JsonRPCMethod>(); | ||
80 | protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>(); | 91 | protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>(); |
81 | protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/ | 92 | protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/ |
82 | protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>(); | 93 | protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>(); |
@@ -86,6 +97,9 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
86 | protected Dictionary<string, PollServiceEventArgs> m_pollHandlers = | 97 | protected Dictionary<string, PollServiceEventArgs> m_pollHandlers = |
87 | new Dictionary<string, PollServiceEventArgs>(); | 98 | new Dictionary<string, PollServiceEventArgs>(); |
88 | 99 | ||
100 | protected Dictionary<string, WebSocketRequestDelegate> m_WebSocketHandlers = | ||
101 | new Dictionary<string, WebSocketRequestDelegate>(); | ||
102 | |||
89 | protected uint m_port; | 103 | protected uint m_port; |
90 | protected uint m_sslport; | 104 | protected uint m_sslport; |
91 | protected bool m_ssl; | 105 | protected bool m_ssl; |
@@ -169,6 +183,22 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
169 | } | 183 | } |
170 | } | 184 | } |
171 | 185 | ||
186 | public void AddWebSocketHandler(string servicepath, WebSocketRequestDelegate handler) | ||
187 | { | ||
188 | lock (m_WebSocketHandlers) | ||
189 | { | ||
190 | if (!m_WebSocketHandlers.ContainsKey(servicepath)) | ||
191 | m_WebSocketHandlers.Add(servicepath, handler); | ||
192 | } | ||
193 | } | ||
194 | |||
195 | public void RemoveWebSocketHandler(string servicepath) | ||
196 | { | ||
197 | lock (m_WebSocketHandlers) | ||
198 | if (m_WebSocketHandlers.ContainsKey(servicepath)) | ||
199 | m_WebSocketHandlers.Remove(servicepath); | ||
200 | } | ||
201 | |||
172 | public List<string> GetStreamHandlerKeys() | 202 | public List<string> GetStreamHandlerKeys() |
173 | { | 203 | { |
174 | lock (m_streamHandlers) | 204 | lock (m_streamHandlers) |
@@ -217,6 +247,37 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
217 | return new List<string>(m_rpcHandlers.Keys); | 247 | return new List<string>(m_rpcHandlers.Keys); |
218 | } | 248 | } |
219 | 249 | ||
250 | // JsonRPC | ||
251 | public bool AddJsonRPCHandler(string method, JsonRPCMethod handler) | ||
252 | { | ||
253 | lock(jsonRpcHandlers) | ||
254 | { | ||
255 | jsonRpcHandlers.Add(method, handler); | ||
256 | } | ||
257 | return true; | ||
258 | } | ||
259 | |||
260 | public JsonRPCMethod GetJsonRPCHandler(string method) | ||
261 | { | ||
262 | lock (jsonRpcHandlers) | ||
263 | { | ||
264 | if (jsonRpcHandlers.ContainsKey(method)) | ||
265 | { | ||
266 | return jsonRpcHandlers[method]; | ||
267 | } | ||
268 | else | ||
269 | { | ||
270 | return null; | ||
271 | } | ||
272 | } | ||
273 | } | ||
274 | |||
275 | public List<string> GetJsonRpcHandlerKeys() | ||
276 | { | ||
277 | lock (jsonRpcHandlers) | ||
278 | return new List<string>(jsonRpcHandlers.Keys); | ||
279 | } | ||
280 | |||
220 | public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler) | 281 | public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler) |
221 | { | 282 | { |
222 | //m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName); | 283 | //m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName); |
@@ -378,9 +439,24 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
378 | 439 | ||
379 | public void OnHandleRequestIOThread(IHttpClientContext context, IHttpRequest request) | 440 | public void OnHandleRequestIOThread(IHttpClientContext context, IHttpRequest request) |
380 | { | 441 | { |
442 | |||
381 | OSHttpRequest req = new OSHttpRequest(context, request); | 443 | OSHttpRequest req = new OSHttpRequest(context, request); |
444 | WebSocketRequestDelegate dWebSocketRequestDelegate = null; | ||
445 | lock (m_WebSocketHandlers) | ||
446 | { | ||
447 | if (m_WebSocketHandlers.ContainsKey(req.RawUrl)) | ||
448 | dWebSocketRequestDelegate = m_WebSocketHandlers[req.RawUrl]; | ||
449 | } | ||
450 | if (dWebSocketRequestDelegate != null) | ||
451 | { | ||
452 | dWebSocketRequestDelegate(req.Url.AbsolutePath, new WebSocketHttpServerHandler(req, context, 8192)); | ||
453 | return; | ||
454 | } | ||
455 | |||
382 | OSHttpResponse resp = new OSHttpResponse(new HttpResponse(context, request),context); | 456 | OSHttpResponse resp = new OSHttpResponse(new HttpResponse(context, request),context); |
457 | |||
383 | HandleRequest(req, resp); | 458 | HandleRequest(req, resp); |
459 | |||
384 | 460 | ||
385 | // !!!HACK ALERT!!! | 461 | // !!!HACK ALERT!!! |
386 | // There seems to be a bug in the underlying http code that makes subsequent requests | 462 | // There seems to be a bug in the underlying http code that makes subsequent requests |
@@ -437,7 +513,7 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
437 | // reqnum = String.Format("{0}:{1}",request.RemoteIPEndPoint,request.Headers["opensim-request-id"]); | 513 | // reqnum = String.Format("{0}:{1}",request.RemoteIPEndPoint,request.Headers["opensim-request-id"]); |
438 | //m_log.DebugFormat("[BASE HTTP SERVER]: <{0}> handle request for {1}",reqnum,request.RawUrl); | 514 | //m_log.DebugFormat("[BASE HTTP SERVER]: <{0}> handle request for {1}",reqnum,request.RawUrl); |
439 | 515 | ||
440 | Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", true); | 516 | Culture.SetCurrentCulture(); |
441 | 517 | ||
442 | // // This is the REST agent interface. We require an agent to properly identify | 518 | // // This is the REST agent interface. We require an agent to properly identify |
443 | // // itself. If the REST handler recognizes the prefix it will attempt to | 519 | // // itself. If the REST handler recognizes the prefix it will attempt to |
@@ -469,7 +545,7 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
469 | LogIncomingToStreamHandler(request, requestHandler); | 545 | LogIncomingToStreamHandler(request, requestHandler); |
470 | 546 | ||
471 | response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type. | 547 | response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type. |
472 | 548 | ||
473 | if (requestHandler is IStreamedRequestHandler) | 549 | if (requestHandler is IStreamedRequestHandler) |
474 | { | 550 | { |
475 | IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler; | 551 | IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler; |
@@ -557,10 +633,18 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
557 | 633 | ||
558 | buffer = HandleLLSDRequests(request, response); | 634 | buffer = HandleLLSDRequests(request, response); |
559 | break; | 635 | break; |
636 | |||
637 | case "application/json-rpc": | ||
638 | if (DebugLevel >= 3) | ||
639 | LogIncomingToContentTypeHandler(request); | ||
640 | |||
641 | buffer = HandleJsonRpcRequests(request, response); | ||
642 | break; | ||
560 | 643 | ||
561 | case "text/xml": | 644 | case "text/xml": |
562 | case "application/xml": | 645 | case "application/xml": |
563 | case "application/json": | 646 | case "application/json": |
647 | |||
564 | default: | 648 | default: |
565 | //m_log.Info("[Debug BASE HTTP SERVER]: in default handler"); | 649 | //m_log.Info("[Debug BASE HTTP SERVER]: in default handler"); |
566 | // Point of note.. the DoWeHaveA methods check for an EXACT path | 650 | // Point of note.. the DoWeHaveA methods check for an EXACT path |
@@ -986,6 +1070,93 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
986 | return buffer; | 1070 | return buffer; |
987 | } | 1071 | } |
988 | 1072 | ||
1073 | // JsonRpc (v2.0 only) | ||
1074 | // Batch requests not yet supported | ||
1075 | private byte[] HandleJsonRpcRequests(OSHttpRequest request, OSHttpResponse response) | ||
1076 | { | ||
1077 | Stream requestStream = request.InputStream; | ||
1078 | JsonRpcResponse jsonRpcResponse = new JsonRpcResponse(); | ||
1079 | OSDMap jsonRpcRequest = null; | ||
1080 | |||
1081 | try | ||
1082 | { | ||
1083 | jsonRpcRequest = (OSDMap)OSDParser.DeserializeJson(requestStream); | ||
1084 | } | ||
1085 | catch (LitJson.JsonException e) | ||
1086 | { | ||
1087 | jsonRpcResponse.Error.Code = ErrorCode.InternalError; | ||
1088 | jsonRpcResponse.Error.Message = e.Message; | ||
1089 | } | ||
1090 | |||
1091 | requestStream.Close(); | ||
1092 | |||
1093 | if (jsonRpcRequest != null) | ||
1094 | { | ||
1095 | if (jsonRpcRequest.ContainsKey("jsonrpc") || jsonRpcRequest["jsonrpc"].AsString() == "2.0") | ||
1096 | { | ||
1097 | jsonRpcResponse.JsonRpc = "2.0"; | ||
1098 | |||
1099 | // If we have no id, then it's a "notification" | ||
1100 | if (jsonRpcRequest.ContainsKey("id")) | ||
1101 | { | ||
1102 | jsonRpcResponse.Id = jsonRpcRequest["id"].AsString(); | ||
1103 | } | ||
1104 | |||
1105 | string methodname = jsonRpcRequest["method"]; | ||
1106 | JsonRPCMethod method; | ||
1107 | |||
1108 | if (jsonRpcHandlers.ContainsKey(methodname)) | ||
1109 | { | ||
1110 | lock(jsonRpcHandlers) | ||
1111 | { | ||
1112 | jsonRpcHandlers.TryGetValue(methodname, out method); | ||
1113 | } | ||
1114 | bool res = false; | ||
1115 | try | ||
1116 | { | ||
1117 | res = method(jsonRpcRequest, ref jsonRpcResponse); | ||
1118 | if(!res) | ||
1119 | { | ||
1120 | // The handler sent back an unspecified error | ||
1121 | if(jsonRpcResponse.Error.Code == 0) | ||
1122 | { | ||
1123 | jsonRpcResponse.Error.Code = ErrorCode.InternalError; | ||
1124 | } | ||
1125 | } | ||
1126 | } | ||
1127 | catch (Exception e) | ||
1128 | { | ||
1129 | string ErrorMessage = string.Format("[BASE HTTP SERVER]: Json-Rpc Handler Error method {0} - {1}", methodname, e.Message); | ||
1130 | m_log.Error(ErrorMessage); | ||
1131 | jsonRpcResponse.Error.Code = ErrorCode.InternalError; | ||
1132 | jsonRpcResponse.Error.Message = ErrorMessage; | ||
1133 | } | ||
1134 | } | ||
1135 | else // Error no hanlder defined for requested method | ||
1136 | { | ||
1137 | jsonRpcResponse.Error.Code = ErrorCode.InvalidRequest; | ||
1138 | jsonRpcResponse.Error.Message = string.Format ("No handler defined for {0}", methodname); | ||
1139 | } | ||
1140 | } | ||
1141 | else // not json-rpc 2.0 could be v1 | ||
1142 | { | ||
1143 | jsonRpcResponse.Error.Code = ErrorCode.InvalidRequest; | ||
1144 | jsonRpcResponse.Error.Message = "Must be valid json-rpc 2.0 see: http://www.jsonrpc.org/specification"; | ||
1145 | |||
1146 | if (jsonRpcRequest.ContainsKey("id")) | ||
1147 | jsonRpcResponse.Id = jsonRpcRequest["id"].AsString(); | ||
1148 | } | ||
1149 | } | ||
1150 | |||
1151 | response.KeepAlive = true; | ||
1152 | string responseData = string.Empty; | ||
1153 | |||
1154 | responseData = jsonRpcResponse.Serialize(); | ||
1155 | |||
1156 | byte[] buffer = Encoding.UTF8.GetBytes(responseData); | ||
1157 | return buffer; | ||
1158 | } | ||
1159 | |||
989 | private byte[] HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response) | 1160 | private byte[] HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response) |
990 | { | 1161 | { |
991 | //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request"); | 1162 | //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request"); |
@@ -1283,59 +1454,6 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
1283 | map["login"] = OSD.FromString("false"); | 1454 | map["login"] = OSD.FromString("false"); |
1284 | return map; | 1455 | return map; |
1285 | } | 1456 | } |
1286 | /// <summary> | ||
1287 | /// A specific agent handler was provided. Such a handler is expecetd to have an | ||
1288 | /// intimate, and highly specific relationship with the client. Consequently, | ||
1289 | /// nothing is done here. | ||
1290 | /// </summary> | ||
1291 | /// <param name="handler"></param> | ||
1292 | /// <param name="request"></param> | ||
1293 | /// <param name="response"></param> | ||
1294 | |||
1295 | private bool HandleAgentRequest(IHttpAgentHandler handler, OSHttpRequest request, OSHttpResponse response) | ||
1296 | { | ||
1297 | // In the case of REST, then handler is responsible for ALL aspects of | ||
1298 | // the request/response handling. Nothing is done here, not even encoding. | ||
1299 | |||
1300 | try | ||
1301 | { | ||
1302 | return handler.Handle(request, response); | ||
1303 | } | ||
1304 | catch (Exception e) | ||
1305 | { | ||
1306 | // If the handler did in fact close the stream, then this will blow | ||
1307 | // chunks. So that that doesn't disturb anybody we throw away any | ||
1308 | // and all exceptions raised. We've done our best to release the | ||
1309 | // client. | ||
1310 | try | ||
1311 | { | ||
1312 | m_log.Warn("[HTTP-AGENT]: Error - " + e.Message); | ||
1313 | response.SendChunked = false; | ||
1314 | response.KeepAlive = true; | ||
1315 | response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError; | ||
1316 | //response.OutputStream.Close(); | ||
1317 | try | ||
1318 | { | ||
1319 | response.Send(); | ||
1320 | //response.FreeContext(); | ||
1321 | } | ||
1322 | catch (SocketException f) | ||
1323 | { | ||
1324 | // This has to be here to prevent a Linux/Mono crash | ||
1325 | m_log.Warn( | ||
1326 | String.Format("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux. ", f.Message), f); | ||
1327 | } | ||
1328 | } | ||
1329 | catch(Exception) | ||
1330 | { | ||
1331 | } | ||
1332 | } | ||
1333 | |||
1334 | // Indicate that the request has been "handled" | ||
1335 | |||
1336 | return true; | ||
1337 | |||
1338 | } | ||
1339 | 1457 | ||
1340 | public byte[] HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response) | 1458 | public byte[] HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response) |
1341 | { | 1459 | { |
@@ -1775,6 +1893,8 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
1775 | HTTPDRunning = false; | 1893 | HTTPDRunning = false; |
1776 | try | 1894 | try |
1777 | { | 1895 | { |
1896 | // m_PollServiceManager.Stop(); | ||
1897 | |||
1778 | m_httpListener2.ExceptionThrown -= httpServerException; | 1898 | m_httpListener2.ExceptionThrown -= httpServerException; |
1779 | //m_httpListener2.DisconnectHandler = null; | 1899 | //m_httpListener2.DisconnectHandler = null; |
1780 | 1900 | ||
diff --git a/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs index 0bd3aae..71ca3ff 100644 --- a/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs | |||
@@ -97,6 +97,18 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
97 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler); | 97 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler); |
98 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive); | 98 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive); |
99 | 99 | ||
100 | bool AddJsonRPCHandler(string method, JsonRPCMethod handler); | ||
101 | |||
102 | /// <summary> | ||
103 | /// Websocket HTTP server handlers. | ||
104 | /// </summary> | ||
105 | /// <param name="servicepath"></param> | ||
106 | /// <param name="handler"></param> | ||
107 | void AddWebSocketHandler(string servicepath, BaseHttpServer.WebSocketRequestDelegate handler); | ||
108 | |||
109 | |||
110 | void RemoveWebSocketHandler(string servicepath); | ||
111 | |||
100 | /// <summary> | 112 | /// <summary> |
101 | /// Gets the XML RPC handler for given method name | 113 | /// Gets the XML RPC handler for given method name |
102 | /// </summary> | 114 | /// </summary> |
diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRPCMethod.cs b/OpenSim/Framework/Servers/HttpServer/JsonRPCMethod.cs new file mode 100644 index 0000000..5bab508 --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/JsonRPCMethod.cs | |||
@@ -0,0 +1,34 @@ | |||
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.Net; | ||
29 | using OpenMetaverse.StructuredData; | ||
30 | |||
31 | namespace OpenSim.Framework.Servers.HttpServer | ||
32 | { | ||
33 | public delegate bool JsonRPCMethod(OSDMap jsonRpcRequest, ref JsonRpcResponse response); | ||
34 | } | ||
diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRpcResponse.cs b/OpenSim/Framework/Servers/HttpServer/JsonRpcResponse.cs new file mode 100644 index 0000000..2c50587 --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcResponse.cs | |||
@@ -0,0 +1,150 @@ | |||
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 | using System; | ||
28 | using System.Net; | ||
29 | using OpenMetaverse.StructuredData; | ||
30 | |||
31 | namespace OpenSim.Framework.Servers.HttpServer | ||
32 | { | ||
33 | public sealed class ErrorCode | ||
34 | { | ||
35 | private ErrorCode() {} | ||
36 | |||
37 | public const int ParseError = -32700; | ||
38 | public const int InvalidRequest = -32600; | ||
39 | public const int MethodNotFound = -32601; | ||
40 | public const int InvalidParams = -32602; | ||
41 | public const int InternalError = -32604; | ||
42 | |||
43 | } | ||
44 | |||
45 | public class JsonRpcError | ||
46 | { | ||
47 | internal OSDMap Error = new OSDMap(); | ||
48 | |||
49 | public int Code | ||
50 | { | ||
51 | get | ||
52 | { | ||
53 | if (Error.ContainsKey("code")) | ||
54 | return Error["code"].AsInteger(); | ||
55 | else | ||
56 | return 0; | ||
57 | } | ||
58 | set | ||
59 | { | ||
60 | Error["code"] = OSD.FromInteger(value); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | public string Message | ||
65 | { | ||
66 | get | ||
67 | { | ||
68 | if (Error.ContainsKey("message")) | ||
69 | return Error["message"].AsString(); | ||
70 | else | ||
71 | return null; | ||
72 | } | ||
73 | set | ||
74 | { | ||
75 | Error["message"] = OSD.FromString(value); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | public OSD Data | ||
80 | { | ||
81 | get; set; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | public class JsonRpcResponse | ||
86 | { | ||
87 | public string JsonRpc | ||
88 | { | ||
89 | get | ||
90 | { | ||
91 | return Reply["jsonrpc"].AsString(); | ||
92 | } | ||
93 | set | ||
94 | { | ||
95 | Reply["jsonrpc"] = OSD.FromString(value); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | public string Id | ||
100 | { | ||
101 | get | ||
102 | { | ||
103 | return Reply["id"].AsString(); | ||
104 | } | ||
105 | set | ||
106 | { | ||
107 | Reply["id"] = OSD.FromString(value); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | public OSD Result | ||
112 | { | ||
113 | get; set; | ||
114 | } | ||
115 | |||
116 | public JsonRpcError Error | ||
117 | { | ||
118 | get; set; | ||
119 | } | ||
120 | |||
121 | public OSDMap Reply = new OSDMap(); | ||
122 | |||
123 | public JsonRpcResponse() | ||
124 | { | ||
125 | Error = new JsonRpcError(); | ||
126 | } | ||
127 | |||
128 | public string Serialize() | ||
129 | { | ||
130 | if (Result != null) | ||
131 | Reply["result"] = Result; | ||
132 | |||
133 | if (Error.Code != 0) | ||
134 | { | ||
135 | Reply["error"] = (OSD)Error.Error; | ||
136 | } | ||
137 | |||
138 | string result = string.Empty; | ||
139 | try | ||
140 | { | ||
141 | result = OSDParser.SerializeJsonString(Reply); | ||
142 | } | ||
143 | catch | ||
144 | { | ||
145 | |||
146 | } | ||
147 | return result; | ||
148 | } | ||
149 | } | ||
150 | } | ||
diff --git a/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs index 4be8bf4..07bd48a 100644 --- a/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs +++ b/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs | |||
@@ -50,19 +50,26 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
50 | private uint m_WorkerThreadCount = 0; | 50 | private uint m_WorkerThreadCount = 0; |
51 | private Thread[] m_workerThreads; | 51 | private Thread[] m_workerThreads; |
52 | private PollServiceWorkerThread[] m_PollServiceWorkerThreads; | 52 | private PollServiceWorkerThread[] m_PollServiceWorkerThreads; |
53 | private bool m_running = true; | 53 | private volatile bool m_running = true; |
54 | private int m_pollTimeout; | ||
54 | 55 | ||
55 | public PollServiceRequestManager(BaseHttpServer pSrv, uint pWorkerThreadCount, int pTimeout) | 56 | public PollServiceRequestManager(BaseHttpServer pSrv, uint pWorkerThreadCount, int pTimeout) |
56 | { | 57 | { |
57 | m_server = pSrv; | 58 | m_server = pSrv; |
58 | m_WorkerThreadCount = pWorkerThreadCount; | 59 | m_WorkerThreadCount = pWorkerThreadCount; |
60 | m_pollTimeout = pTimeout; | ||
61 | } | ||
62 | |||
63 | public void Start() | ||
64 | { | ||
65 | m_running = true; | ||
59 | m_workerThreads = new Thread[m_WorkerThreadCount]; | 66 | m_workerThreads = new Thread[m_WorkerThreadCount]; |
60 | m_PollServiceWorkerThreads = new PollServiceWorkerThread[m_WorkerThreadCount]; | 67 | m_PollServiceWorkerThreads = new PollServiceWorkerThread[m_WorkerThreadCount]; |
61 | 68 | ||
62 | //startup worker threads | 69 | //startup worker threads |
63 | for (uint i = 0; i < m_WorkerThreadCount; i++) | 70 | for (uint i = 0; i < m_WorkerThreadCount; i++) |
64 | { | 71 | { |
65 | m_PollServiceWorkerThreads[i] = new PollServiceWorkerThread(m_server, pTimeout); | 72 | m_PollServiceWorkerThreads[i] = new PollServiceWorkerThread(m_server, m_pollTimeout); |
66 | m_PollServiceWorkerThreads[i].ReQueue += ReQueueEvent; | 73 | m_PollServiceWorkerThreads[i].ReQueue += ReQueueEvent; |
67 | 74 | ||
68 | m_workerThreads[i] | 75 | m_workerThreads[i] |
@@ -141,8 +148,10 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
141 | 148 | ||
142 | } | 149 | } |
143 | 150 | ||
144 | ~PollServiceRequestManager() | 151 | public void Stop() |
145 | { | 152 | { |
153 | m_running = false; | ||
154 | |||
146 | foreach (object o in m_requests) | 155 | foreach (object o in m_requests) |
147 | { | 156 | { |
148 | PollServiceHttpRequest req = (PollServiceHttpRequest) o; | 157 | PollServiceHttpRequest req = (PollServiceHttpRequest) o; |
@@ -157,7 +166,6 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
157 | { | 166 | { |
158 | t.Abort(); | 167 | t.Abort(); |
159 | } | 168 | } |
160 | m_running = false; | ||
161 | } | 169 | } |
162 | } | 170 | } |
163 | } | 171 | } |
diff --git a/OpenSim/Framework/Servers/HttpServer/Properties/AssemblyInfo.cs b/OpenSim/Framework/Servers/HttpServer/Properties/AssemblyInfo.cs index 02ecc25..386be2d 100644 --- a/OpenSim/Framework/Servers/HttpServer/Properties/AssemblyInfo.cs +++ b/OpenSim/Framework/Servers/HttpServer/Properties/AssemblyInfo.cs | |||
@@ -29,5 +29,5 @@ using System.Runtime.InteropServices; | |||
29 | // Build Number | 29 | // Build Number |
30 | // Revision | 30 | // Revision |
31 | // | 31 | // |
32 | [assembly: AssemblyVersion("0.7.5.*")] | 32 | [assembly: AssemblyVersion("0.7.6.*")] |
33 | [assembly: AssemblyFileVersion("1.0.0.0")] | 33 | [assembly: AssemblyFileVersion("1.0.0.0")] |
diff --git a/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs b/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs new file mode 100644 index 0000000..cfb1605 --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs | |||
@@ -0,0 +1,1085 @@ | |||
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.IO; | ||
31 | using System.Security.Cryptography; | ||
32 | using System.Text; | ||
33 | using HttpServer; | ||
34 | |||
35 | namespace OpenSim.Framework.Servers.HttpServer | ||
36 | { | ||
37 | // Sealed class. If you're going to unseal it, implement IDisposable. | ||
38 | /// <summary> | ||
39 | /// This class implements websockets. It grabs the network context from C#Webserver and utilizes it directly as a tcp streaming service | ||
40 | /// </summary> | ||
41 | public sealed class WebSocketHttpServerHandler : BaseRequestHandler | ||
42 | { | ||
43 | |||
44 | private class WebSocketState | ||
45 | { | ||
46 | public List<byte> ReceivedBytes; | ||
47 | public int ExpectedBytes; | ||
48 | public WebsocketFrameHeader Header; | ||
49 | public bool FrameComplete; | ||
50 | public WebSocketFrame ContinuationFrame; | ||
51 | } | ||
52 | |||
53 | /// <summary> | ||
54 | /// Binary Data will trigger this event | ||
55 | /// </summary> | ||
56 | public event DataDelegate OnData; | ||
57 | |||
58 | /// <summary> | ||
59 | /// Textual Data will trigger this event | ||
60 | /// </summary> | ||
61 | public event TextDelegate OnText; | ||
62 | |||
63 | /// <summary> | ||
64 | /// A ping request form the other side will trigger this event. | ||
65 | /// This class responds to the ping automatically. You shouldn't send a pong. | ||
66 | /// it's informational. | ||
67 | /// </summary> | ||
68 | public event PingDelegate OnPing; | ||
69 | |||
70 | /// <summary> | ||
71 | /// This is a response to a ping you sent. | ||
72 | /// </summary> | ||
73 | public event PongDelegate OnPong; | ||
74 | |||
75 | /// <summary> | ||
76 | /// This is a regular HTTP Request... This may be removed in the future. | ||
77 | /// </summary> | ||
78 | public event RegularHttpRequestDelegate OnRegularHttpRequest; | ||
79 | |||
80 | /// <summary> | ||
81 | /// When the upgrade from a HTTP request to a Websocket is completed, this will be fired | ||
82 | /// </summary> | ||
83 | public event UpgradeCompletedDelegate OnUpgradeCompleted; | ||
84 | |||
85 | /// <summary> | ||
86 | /// If the upgrade failed, this will be fired | ||
87 | /// </summary> | ||
88 | public event UpgradeFailedDelegate OnUpgradeFailed; | ||
89 | |||
90 | /// <summary> | ||
91 | /// When the websocket is closed, this will be fired. | ||
92 | /// </summary> | ||
93 | public event CloseDelegate OnClose; | ||
94 | |||
95 | /// <summary> | ||
96 | /// Set this delegate to allow your module to validate the origin of the | ||
97 | /// Websocket request. Primary line of defense against cross site scripting | ||
98 | /// </summary> | ||
99 | public ValidateHandshake HandshakeValidateMethodOverride = null; | ||
100 | |||
101 | private OSHttpRequest _request; | ||
102 | private HTTPNetworkContext _networkContext; | ||
103 | private IHttpClientContext _clientContext; | ||
104 | |||
105 | private int _pingtime = 0; | ||
106 | private byte[] _buffer; | ||
107 | private int _bufferPosition; | ||
108 | private int _bufferLength; | ||
109 | private bool _closing; | ||
110 | private bool _upgraded; | ||
111 | |||
112 | private const string HandshakeAcceptText = | ||
113 | "HTTP/1.1 101 Switching Protocols\r\n" + | ||
114 | "upgrade: websocket\r\n" + | ||
115 | "Connection: Upgrade\r\n" + | ||
116 | "sec-websocket-accept: {0}\r\n\r\n";// + | ||
117 | //"{1}"; | ||
118 | |||
119 | private const string HandshakeDeclineText = | ||
120 | "HTTP/1.1 {0} {1}\r\n" + | ||
121 | "Connection: close\r\n\r\n"; | ||
122 | |||
123 | /// <summary> | ||
124 | /// Mysterious constant defined in RFC6455 to append to the client provided security key | ||
125 | /// </summary> | ||
126 | private const string WebsocketHandshakeAcceptHashConstant = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | ||
127 | |||
128 | public WebSocketHttpServerHandler(OSHttpRequest preq, IHttpClientContext pContext, int bufferlen) | ||
129 | : base(preq.HttpMethod, preq.Url.OriginalString) | ||
130 | { | ||
131 | _request = preq; | ||
132 | _networkContext = pContext.GiveMeTheNetworkStreamIKnowWhatImDoing(); | ||
133 | _clientContext = pContext; | ||
134 | _bufferLength = bufferlen; | ||
135 | _buffer = new byte[_bufferLength]; | ||
136 | } | ||
137 | |||
138 | // Sealed class implments destructor and an internal dispose method. complies with C# unmanaged resource best practices. | ||
139 | ~WebSocketHttpServerHandler() | ||
140 | { | ||
141 | Dispose(); | ||
142 | |||
143 | } | ||
144 | |||
145 | /// <summary> | ||
146 | /// Sets the length of the stream buffer | ||
147 | /// </summary> | ||
148 | /// <param name="pChunk">Byte length.</param> | ||
149 | public void SetChunksize(int pChunk) | ||
150 | { | ||
151 | if (!_upgraded) | ||
152 | { | ||
153 | _buffer = new byte[pChunk]; | ||
154 | } | ||
155 | else | ||
156 | { | ||
157 | throw new InvalidOperationException("You must set the chunksize before the connection is upgraded"); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /// <summary> | ||
162 | /// This is the famous nagle. | ||
163 | /// </summary> | ||
164 | public bool NoDelay_TCP_Nagle | ||
165 | { | ||
166 | get | ||
167 | { | ||
168 | if (_networkContext != null && _networkContext.Socket != null) | ||
169 | { | ||
170 | return _networkContext.Socket.NoDelay; | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | throw new InvalidOperationException("The socket has been shutdown"); | ||
175 | } | ||
176 | } | ||
177 | set | ||
178 | { | ||
179 | if (_networkContext != null && _networkContext.Socket != null) | ||
180 | _networkContext.Socket.NoDelay = value; | ||
181 | else | ||
182 | { | ||
183 | throw new InvalidOperationException("The socket has been shutdown"); | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | |||
188 | /// <summary> | ||
189 | /// This triggers the websocket to start the upgrade process... | ||
190 | /// This is a Generalized Networking 'common sense' helper method. Some people expect to call Start() instead | ||
191 | /// of the more context appropriate HandshakeAndUpgrade() | ||
192 | /// </summary> | ||
193 | public void Start() | ||
194 | { | ||
195 | HandshakeAndUpgrade(); | ||
196 | } | ||
197 | |||
198 | /// <summary> | ||
199 | /// This triggers the websocket start the upgrade process | ||
200 | /// </summary> | ||
201 | public void HandshakeAndUpgrade() | ||
202 | { | ||
203 | string webOrigin = string.Empty; | ||
204 | string websocketKey = string.Empty; | ||
205 | string acceptKey = string.Empty; | ||
206 | string accepthost = string.Empty; | ||
207 | if (!string.IsNullOrEmpty(_request.Headers["origin"])) | ||
208 | webOrigin = _request.Headers["origin"]; | ||
209 | |||
210 | if (!string.IsNullOrEmpty(_request.Headers["sec-websocket-key"])) | ||
211 | websocketKey = _request.Headers["sec-websocket-key"]; | ||
212 | |||
213 | if (!string.IsNullOrEmpty(_request.Headers["host"])) | ||
214 | accepthost = _request.Headers["host"]; | ||
215 | |||
216 | if (string.IsNullOrEmpty(_request.Headers["upgrade"])) | ||
217 | { | ||
218 | FailUpgrade(OSHttpStatusCode.ClientErrorBadRequest, "no upgrade request submitted"); | ||
219 | } | ||
220 | |||
221 | string connectionheader = _request.Headers["upgrade"]; | ||
222 | if (connectionheader.ToLower() != "websocket") | ||
223 | { | ||
224 | FailUpgrade(OSHttpStatusCode.ClientErrorBadRequest, "no connection upgrade request submitted"); | ||
225 | } | ||
226 | |||
227 | // If the object consumer provided a method to validate the origin, we should call it and give the client a success or fail. | ||
228 | // If not.. we should accept any. The assumption here is that there would be no Websocket handlers registered in baseHTTPServer unless | ||
229 | // Something asked for it... | ||
230 | if (HandshakeValidateMethodOverride != null) | ||
231 | { | ||
232 | if (HandshakeValidateMethodOverride(webOrigin, websocketKey, accepthost)) | ||
233 | { | ||
234 | acceptKey = GenerateAcceptKey(websocketKey); | ||
235 | string rawaccept = string.Format(HandshakeAcceptText, acceptKey); | ||
236 | SendUpgradeSuccess(rawaccept); | ||
237 | |||
238 | } | ||
239 | else | ||
240 | { | ||
241 | FailUpgrade(OSHttpStatusCode.ClientErrorForbidden, "Origin Validation Failed"); | ||
242 | } | ||
243 | } | ||
244 | else | ||
245 | { | ||
246 | acceptKey = GenerateAcceptKey(websocketKey); | ||
247 | string rawaccept = string.Format(HandshakeAcceptText, acceptKey); | ||
248 | SendUpgradeSuccess(rawaccept); | ||
249 | } | ||
250 | } | ||
251 | |||
252 | /// <summary> | ||
253 | /// Generates a handshake response key string based on the client's | ||
254 | /// provided key to prove to the client that we're allowing the Websocket | ||
255 | /// upgrade of our own free will and we were not coerced into doing it. | ||
256 | /// </summary> | ||
257 | /// <param name="key">Client provided security key</param> | ||
258 | /// <returns></returns> | ||
259 | private static string GenerateAcceptKey(string key) | ||
260 | { | ||
261 | if (string.IsNullOrEmpty(key)) | ||
262 | return string.Empty; | ||
263 | |||
264 | string acceptkey = key + WebsocketHandshakeAcceptHashConstant; | ||
265 | |||
266 | SHA1 hashobj = SHA1.Create(); | ||
267 | string ret = Convert.ToBase64String(hashobj.ComputeHash(Encoding.UTF8.GetBytes(acceptkey))); | ||
268 | hashobj.Clear(); | ||
269 | |||
270 | return ret; | ||
271 | } | ||
272 | |||
273 | /// <summary> | ||
274 | /// Informs the otherside that we accepted their upgrade request | ||
275 | /// </summary> | ||
276 | /// <param name="pHandshakeResponse">The HTTP 1.1 101 response that says Yay \o/ </param> | ||
277 | private void SendUpgradeSuccess(string pHandshakeResponse) | ||
278 | { | ||
279 | // Create a new websocket state so we can keep track of data in between network reads. | ||
280 | WebSocketState socketState = new WebSocketState() { ReceivedBytes = new List<byte>(), Header = WebsocketFrameHeader.HeaderDefault(), FrameComplete = true}; | ||
281 | |||
282 | byte[] bhandshakeResponse = Encoding.UTF8.GetBytes(pHandshakeResponse); | ||
283 | try | ||
284 | { | ||
285 | |||
286 | // Begin reading the TCP stream before writing the Upgrade success message to the other side of the stream. | ||
287 | _networkContext.Stream.BeginRead(_buffer, 0, _bufferLength, OnReceive, socketState); | ||
288 | |||
289 | // Write the upgrade handshake success message | ||
290 | _networkContext.Stream.Write(bhandshakeResponse, 0, bhandshakeResponse.Length); | ||
291 | _networkContext.Stream.Flush(); | ||
292 | _upgraded = true; | ||
293 | UpgradeCompletedDelegate d = OnUpgradeCompleted; | ||
294 | if (d != null) | ||
295 | d(this, new UpgradeCompletedEventArgs()); | ||
296 | } | ||
297 | catch (IOException fail) | ||
298 | { | ||
299 | Close(string.Empty); | ||
300 | } | ||
301 | catch (ObjectDisposedException fail) | ||
302 | { | ||
303 | Close(string.Empty); | ||
304 | } | ||
305 | |||
306 | } | ||
307 | |||
308 | /// <summary> | ||
309 | /// The server has decided not to allow the upgrade to a websocket for some reason. The Http 1.1 response that says Nay >:( | ||
310 | /// </summary> | ||
311 | /// <param name="pCode">HTTP Status reflecting the reason why</param> | ||
312 | /// <param name="pMessage">Textual reason for the upgrade fail</param> | ||
313 | private void FailUpgrade(OSHttpStatusCode pCode, string pMessage ) | ||
314 | { | ||
315 | string handshakeResponse = string.Format(HandshakeDeclineText, (int)pCode, pMessage.Replace("\n", string.Empty).Replace("\r", string.Empty)); | ||
316 | byte[] bhandshakeResponse = Encoding.UTF8.GetBytes(handshakeResponse); | ||
317 | _networkContext.Stream.Write(bhandshakeResponse, 0, bhandshakeResponse.Length); | ||
318 | _networkContext.Stream.Flush(); | ||
319 | _networkContext.Stream.Dispose(); | ||
320 | |||
321 | UpgradeFailedDelegate d = OnUpgradeFailed; | ||
322 | if (d != null) | ||
323 | d(this,new UpgradeFailedEventArgs()); | ||
324 | } | ||
325 | |||
326 | |||
327 | /// <summary> | ||
328 | /// This is our ugly Async OnReceive event handler. | ||
329 | /// This chunks the input stream based on the length of the provided buffer and processes out | ||
330 | /// as many frames as it can. It then moves the unprocessed data to the beginning of the buffer. | ||
331 | /// </summary> | ||
332 | /// <param name="ar">Our Async State from beginread</param> | ||
333 | private void OnReceive(IAsyncResult ar) | ||
334 | { | ||
335 | WebSocketState _socketState = ar.AsyncState as WebSocketState; | ||
336 | try | ||
337 | { | ||
338 | int bytesRead = _networkContext.Stream.EndRead(ar); | ||
339 | if (bytesRead == 0) | ||
340 | { | ||
341 | // Do Disconnect | ||
342 | _networkContext.Stream.Dispose(); | ||
343 | _networkContext = null; | ||
344 | return; | ||
345 | } | ||
346 | _bufferPosition += bytesRead; | ||
347 | |||
348 | if (_bufferPosition > _bufferLength) | ||
349 | { | ||
350 | // Message too big for chunksize.. not sure how this happened... | ||
351 | //Close(string.Empty); | ||
352 | } | ||
353 | |||
354 | int offset = 0; | ||
355 | bool headerread = true; | ||
356 | int headerforwardposition = 0; | ||
357 | while (headerread && offset < bytesRead) | ||
358 | { | ||
359 | if (_socketState.FrameComplete) | ||
360 | { | ||
361 | WebsocketFrameHeader pheader = WebsocketFrameHeader.ZeroHeader; | ||
362 | |||
363 | headerread = WebSocketReader.TryReadHeader(_buffer, offset, _bufferPosition - offset, out pheader, | ||
364 | out headerforwardposition); | ||
365 | offset += headerforwardposition; | ||
366 | |||
367 | if (headerread) | ||
368 | { | ||
369 | _socketState.FrameComplete = false; | ||
370 | |||
371 | if (pheader.PayloadLen > 0) | ||
372 | { | ||
373 | if ((int) pheader.PayloadLen > _bufferPosition - offset) | ||
374 | { | ||
375 | byte[] writebytes = new byte[_bufferPosition - offset]; | ||
376 | |||
377 | Buffer.BlockCopy(_buffer, offset, writebytes, 0, (int) _bufferPosition - offset); | ||
378 | _socketState.ExpectedBytes = (int) pheader.PayloadLen; | ||
379 | _socketState.ReceivedBytes.AddRange(writebytes); | ||
380 | _socketState.Header = pheader; // We need to add the header so that we can unmask it | ||
381 | offset += (int) _bufferPosition - offset; | ||
382 | } | ||
383 | else | ||
384 | { | ||
385 | byte[] writebytes = new byte[pheader.PayloadLen]; | ||
386 | Buffer.BlockCopy(_buffer, offset, writebytes, 0, (int) pheader.PayloadLen); | ||
387 | WebSocketReader.Mask(pheader.Mask, writebytes); | ||
388 | pheader.IsMasked = false; | ||
389 | _socketState.FrameComplete = true; | ||
390 | _socketState.ReceivedBytes.AddRange(writebytes); | ||
391 | _socketState.Header = pheader; | ||
392 | offset += (int) pheader.PayloadLen; | ||
393 | } | ||
394 | } | ||
395 | else | ||
396 | { | ||
397 | pheader.Mask = 0; | ||
398 | _socketState.FrameComplete = true; | ||
399 | _socketState.Header = pheader; | ||
400 | } | ||
401 | |||
402 | |||
403 | |||
404 | if (_socketState.FrameComplete) | ||
405 | { | ||
406 | ProcessFrame(_socketState); | ||
407 | _socketState.Header.SetDefault(); | ||
408 | _socketState.ReceivedBytes.Clear(); | ||
409 | _socketState.ExpectedBytes = 0; | ||
410 | |||
411 | } | ||
412 | |||
413 | } | ||
414 | } | ||
415 | else | ||
416 | { | ||
417 | WebsocketFrameHeader frameHeader = _socketState.Header; | ||
418 | int bytesleft = _socketState.ExpectedBytes - _socketState.ReceivedBytes.Count; | ||
419 | |||
420 | if (bytesleft > _bufferPosition) | ||
421 | { | ||
422 | byte[] writebytes = new byte[_bufferPosition]; | ||
423 | |||
424 | Buffer.BlockCopy(_buffer, offset, writebytes, 0, (int) _bufferPosition); | ||
425 | _socketState.ReceivedBytes.AddRange(writebytes); | ||
426 | _socketState.Header = frameHeader; // We need to add the header so that we can unmask it | ||
427 | offset += (int) _bufferPosition; | ||
428 | } | ||
429 | else | ||
430 | { | ||
431 | byte[] writebytes = new byte[_bufferPosition]; | ||
432 | Buffer.BlockCopy(_buffer, offset, writebytes, 0, (int) _bufferPosition); | ||
433 | _socketState.FrameComplete = true; | ||
434 | _socketState.ReceivedBytes.AddRange(writebytes); | ||
435 | _socketState.Header = frameHeader; | ||
436 | offset += (int) _bufferPosition; | ||
437 | } | ||
438 | if (_socketState.FrameComplete) | ||
439 | { | ||
440 | ProcessFrame(_socketState); | ||
441 | _socketState.Header.SetDefault(); | ||
442 | _socketState.ReceivedBytes.Clear(); | ||
443 | _socketState.ExpectedBytes = 0; | ||
444 | // do some processing | ||
445 | } | ||
446 | |||
447 | } | ||
448 | } | ||
449 | if (offset > 0) | ||
450 | { | ||
451 | // If the buffer is maxed out.. we can just move the cursor. Nothing to move to the beginning. | ||
452 | if (offset <_buffer.Length) | ||
453 | Buffer.BlockCopy(_buffer, offset, _buffer, 0, _bufferPosition - offset); | ||
454 | _bufferPosition -= offset; | ||
455 | } | ||
456 | if (_networkContext.Stream != null && _networkContext.Stream.CanRead && !_closing) | ||
457 | { | ||
458 | _networkContext.Stream.BeginRead(_buffer, _bufferPosition, _bufferLength - _bufferPosition, OnReceive, | ||
459 | _socketState); | ||
460 | } | ||
461 | else | ||
462 | { | ||
463 | // We can't read the stream anymore... | ||
464 | } | ||
465 | |||
466 | } | ||
467 | catch (IOException fail) | ||
468 | { | ||
469 | Close(string.Empty); | ||
470 | } | ||
471 | catch (ObjectDisposedException fail) | ||
472 | { | ||
473 | Close(string.Empty); | ||
474 | } | ||
475 | } | ||
476 | |||
477 | /// <summary> | ||
478 | /// Sends a string to the other side | ||
479 | /// </summary> | ||
480 | /// <param name="message">the string message that is to be sent</param> | ||
481 | public void SendMessage(string message) | ||
482 | { | ||
483 | byte[] messagedata = Encoding.UTF8.GetBytes(message); | ||
484 | WebSocketFrame textMessageFrame = new WebSocketFrame() { Header = WebsocketFrameHeader.HeaderDefault(), WebSocketPayload = messagedata }; | ||
485 | textMessageFrame.Header.Opcode = WebSocketReader.OpCode.Text; | ||
486 | textMessageFrame.Header.IsEnd = true; | ||
487 | SendSocket(textMessageFrame.ToBytes()); | ||
488 | |||
489 | } | ||
490 | |||
491 | public void SendData(byte[] data) | ||
492 | { | ||
493 | WebSocketFrame dataMessageFrame = new WebSocketFrame() { Header = WebsocketFrameHeader.HeaderDefault(), WebSocketPayload = data}; | ||
494 | dataMessageFrame.Header.IsEnd = true; | ||
495 | dataMessageFrame.Header.Opcode = WebSocketReader.OpCode.Binary; | ||
496 | SendSocket(dataMessageFrame.ToBytes()); | ||
497 | |||
498 | } | ||
499 | |||
500 | /// <summary> | ||
501 | /// Writes raw bytes to the websocket. Unframed data will cause disconnection | ||
502 | /// </summary> | ||
503 | /// <param name="data"></param> | ||
504 | private void SendSocket(byte[] data) | ||
505 | { | ||
506 | if (!_closing) | ||
507 | { | ||
508 | try | ||
509 | { | ||
510 | |||
511 | _networkContext.Stream.Write(data, 0, data.Length); | ||
512 | } | ||
513 | catch (IOException) | ||
514 | { | ||
515 | |||
516 | } | ||
517 | } | ||
518 | } | ||
519 | |||
520 | /// <summary> | ||
521 | /// Sends a Ping check to the other side. The other side SHOULD respond as soon as possible with a pong frame. This interleaves with incoming fragmented frames. | ||
522 | /// </summary> | ||
523 | public void SendPingCheck() | ||
524 | { | ||
525 | WebSocketFrame pingFrame = new WebSocketFrame() { Header = WebsocketFrameHeader.HeaderDefault(), WebSocketPayload = new byte[0] }; | ||
526 | pingFrame.Header.Opcode = WebSocketReader.OpCode.Ping; | ||
527 | pingFrame.Header.IsEnd = true; | ||
528 | _pingtime = Util.EnvironmentTickCount(); | ||
529 | SendSocket(pingFrame.ToBytes()); | ||
530 | } | ||
531 | |||
532 | /// <summary> | ||
533 | /// Closes the websocket connection. Sends a close message to the other side if it hasn't already done so. | ||
534 | /// </summary> | ||
535 | /// <param name="message"></param> | ||
536 | public void Close(string message) | ||
537 | { | ||
538 | if (_networkContext.Stream != null) | ||
539 | { | ||
540 | if (_networkContext.Stream.CanWrite) | ||
541 | { | ||
542 | byte[] messagedata = Encoding.UTF8.GetBytes(message); | ||
543 | WebSocketFrame closeResponseFrame = new WebSocketFrame() | ||
544 | { | ||
545 | Header = WebsocketFrameHeader.HeaderDefault(), | ||
546 | WebSocketPayload = messagedata | ||
547 | }; | ||
548 | closeResponseFrame.Header.Opcode = WebSocketReader.OpCode.Close; | ||
549 | closeResponseFrame.Header.PayloadLen = (ulong) messagedata.Length; | ||
550 | closeResponseFrame.Header.IsEnd = true; | ||
551 | SendSocket(closeResponseFrame.ToBytes()); | ||
552 | } | ||
553 | } | ||
554 | CloseDelegate closeD = OnClose; | ||
555 | if (closeD != null) | ||
556 | { | ||
557 | closeD(this, new CloseEventArgs()); | ||
558 | } | ||
559 | |||
560 | _closing = true; | ||
561 | } | ||
562 | |||
563 | /// <summary> | ||
564 | /// Processes a websocket frame and triggers consumer events | ||
565 | /// </summary> | ||
566 | /// <param name="psocketState">We need to modify the websocket state here depending on the frame</param> | ||
567 | private void ProcessFrame(WebSocketState psocketState) | ||
568 | { | ||
569 | if (psocketState.Header.IsMasked) | ||
570 | { | ||
571 | byte[] unmask = psocketState.ReceivedBytes.ToArray(); | ||
572 | WebSocketReader.Mask(psocketState.Header.Mask, unmask); | ||
573 | psocketState.ReceivedBytes = new List<byte>(unmask); | ||
574 | } | ||
575 | |||
576 | switch (psocketState.Header.Opcode) | ||
577 | { | ||
578 | case WebSocketReader.OpCode.Ping: | ||
579 | PingDelegate pingD = OnPing; | ||
580 | if (pingD != null) | ||
581 | { | ||
582 | pingD(this, new PingEventArgs()); | ||
583 | } | ||
584 | |||
585 | WebSocketFrame pongFrame = new WebSocketFrame(){Header = WebsocketFrameHeader.HeaderDefault(),WebSocketPayload = new byte[0]}; | ||
586 | pongFrame.Header.Opcode = WebSocketReader.OpCode.Pong; | ||
587 | pongFrame.Header.IsEnd = true; | ||
588 | SendSocket(pongFrame.ToBytes()); | ||
589 | break; | ||
590 | case WebSocketReader.OpCode.Pong: | ||
591 | |||
592 | PongDelegate pongD = OnPong; | ||
593 | if (pongD != null) | ||
594 | { | ||
595 | pongD(this, new PongEventArgs(){PingResponseMS = Util.EnvironmentTickCountSubtract(Util.EnvironmentTickCount(),_pingtime)}); | ||
596 | } | ||
597 | break; | ||
598 | case WebSocketReader.OpCode.Binary: | ||
599 | if (!psocketState.Header.IsEnd) // Not done, so we need to store this and wait for the end frame. | ||
600 | { | ||
601 | psocketState.ContinuationFrame = new WebSocketFrame | ||
602 | { | ||
603 | Header = psocketState.Header, | ||
604 | WebSocketPayload = | ||
605 | psocketState.ReceivedBytes.ToArray() | ||
606 | }; | ||
607 | } | ||
608 | else | ||
609 | { | ||
610 | // Send Done Event! | ||
611 | DataDelegate dataD = OnData; | ||
612 | if (dataD != null) | ||
613 | { | ||
614 | dataD(this,new WebsocketDataEventArgs(){Data = psocketState.ReceivedBytes.ToArray()}); | ||
615 | } | ||
616 | } | ||
617 | break; | ||
618 | case WebSocketReader.OpCode.Text: | ||
619 | if (!psocketState.Header.IsEnd) // Not done, so we need to store this and wait for the end frame. | ||
620 | { | ||
621 | psocketState.ContinuationFrame = new WebSocketFrame | ||
622 | { | ||
623 | Header = psocketState.Header, | ||
624 | WebSocketPayload = | ||
625 | psocketState.ReceivedBytes.ToArray() | ||
626 | }; | ||
627 | } | ||
628 | else | ||
629 | { | ||
630 | TextDelegate textD = OnText; | ||
631 | if (textD != null) | ||
632 | { | ||
633 | textD(this, new WebsocketTextEventArgs() { Data = Encoding.UTF8.GetString(psocketState.ReceivedBytes.ToArray()) }); | ||
634 | } | ||
635 | |||
636 | // Send Done Event! | ||
637 | } | ||
638 | break; | ||
639 | case WebSocketReader.OpCode.Continue: // Continuation. Multiple frames worth of data for one message. Only valid when not using Control Opcodes | ||
640 | //Console.WriteLine("currhead " + psocketState.Header.IsEnd); | ||
641 | //Console.WriteLine("Continuation! " + psocketState.ContinuationFrame.Header.IsEnd); | ||
642 | byte[] combineddata = new byte[psocketState.ReceivedBytes.Count+psocketState.ContinuationFrame.WebSocketPayload.Length]; | ||
643 | byte[] newdata = psocketState.ReceivedBytes.ToArray(); | ||
644 | Buffer.BlockCopy(psocketState.ContinuationFrame.WebSocketPayload, 0, combineddata, 0, psocketState.ContinuationFrame.WebSocketPayload.Length); | ||
645 | Buffer.BlockCopy(newdata, 0, combineddata, | ||
646 | psocketState.ContinuationFrame.WebSocketPayload.Length, newdata.Length); | ||
647 | psocketState.ContinuationFrame.WebSocketPayload = combineddata; | ||
648 | psocketState.Header.PayloadLen = (ulong)combineddata.Length; | ||
649 | if (psocketState.Header.IsEnd) | ||
650 | { | ||
651 | if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Text) | ||
652 | { | ||
653 | // Send Done event | ||
654 | TextDelegate textD = OnText; | ||
655 | if (textD != null) | ||
656 | { | ||
657 | textD(this, new WebsocketTextEventArgs() { Data = Encoding.UTF8.GetString(combineddata) }); | ||
658 | } | ||
659 | } | ||
660 | else if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Binary) | ||
661 | { | ||
662 | // Send Done event | ||
663 | DataDelegate dataD = OnData; | ||
664 | if (dataD != null) | ||
665 | { | ||
666 | dataD(this, new WebsocketDataEventArgs() { Data = combineddata }); | ||
667 | } | ||
668 | } | ||
669 | else | ||
670 | { | ||
671 | // protocol violation | ||
672 | } | ||
673 | psocketState.ContinuationFrame = null; | ||
674 | } | ||
675 | break; | ||
676 | case WebSocketReader.OpCode.Close: | ||
677 | Close(string.Empty); | ||
678 | |||
679 | break; | ||
680 | |||
681 | } | ||
682 | psocketState.Header.SetDefault(); | ||
683 | psocketState.ReceivedBytes.Clear(); | ||
684 | psocketState.ExpectedBytes = 0; | ||
685 | } | ||
686 | public void Dispose() | ||
687 | { | ||
688 | if (_networkContext != null && _networkContext.Stream != null) | ||
689 | { | ||
690 | if (_networkContext.Stream.CanWrite) | ||
691 | _networkContext.Stream.Flush(); | ||
692 | _networkContext.Stream.Close(); | ||
693 | _networkContext.Stream.Dispose(); | ||
694 | _networkContext.Stream = null; | ||
695 | } | ||
696 | |||
697 | if (_request != null && _request.InputStream != null) | ||
698 | { | ||
699 | _request.InputStream.Close(); | ||
700 | _request.InputStream.Dispose(); | ||
701 | _request = null; | ||
702 | } | ||
703 | |||
704 | if (_clientContext != null) | ||
705 | { | ||
706 | _clientContext.Close(); | ||
707 | _clientContext = null; | ||
708 | } | ||
709 | } | ||
710 | } | ||
711 | |||
712 | /// <summary> | ||
713 | /// Reads a byte stream and returns Websocket frames. | ||
714 | /// </summary> | ||
715 | public class WebSocketReader | ||
716 | { | ||
717 | /// <summary> | ||
718 | /// Bit to determine if the frame read on the stream is the last frame in a sequence of fragmented frames | ||
719 | /// </summary> | ||
720 | private const byte EndBit = 0x80; | ||
721 | |||
722 | /// <summary> | ||
723 | /// These are the Frame Opcodes | ||
724 | /// </summary> | ||
725 | public enum OpCode | ||
726 | { | ||
727 | // Data Opcodes | ||
728 | Continue = 0x0, | ||
729 | Text = 0x1, | ||
730 | Binary = 0x2, | ||
731 | |||
732 | // Control flow Opcodes | ||
733 | Close = 0x8, | ||
734 | Ping = 0x9, | ||
735 | Pong = 0xA | ||
736 | } | ||
737 | |||
738 | /// <summary> | ||
739 | /// Masks and Unmasks data using the frame mask. Mask is applied per octal | ||
740 | /// Note: Frames from clients MUST be masked | ||
741 | /// Note: Frames from servers MUST NOT be masked | ||
742 | /// </summary> | ||
743 | /// <param name="pMask">Int representing 32 bytes of mask data. Mask is applied per octal</param> | ||
744 | /// <param name="pBuffer"></param> | ||
745 | public static void Mask(int pMask, byte[] pBuffer) | ||
746 | { | ||
747 | byte[] maskKey = BitConverter.GetBytes(pMask); | ||
748 | int currentMaskIndex = 0; | ||
749 | for (int i = 0; i < pBuffer.Length; i++) | ||
750 | { | ||
751 | pBuffer[i] = (byte)(pBuffer[i] ^ maskKey[currentMaskIndex]); | ||
752 | if (currentMaskIndex == 3) | ||
753 | { | ||
754 | currentMaskIndex = 0; | ||
755 | } | ||
756 | else | ||
757 | { | ||
758 | currentMaskIndex++; | ||
759 | |||
760 | } | ||
761 | |||
762 | } | ||
763 | } | ||
764 | |||
765 | /// <summary> | ||
766 | /// Attempts to read a header off the provided buffer. Returns true, exports a WebSocketFrameheader, | ||
767 | /// and an int to move the buffer forward when it reads a header. False when it can't read a header | ||
768 | /// </summary> | ||
769 | /// <param name="pBuffer">Bytes read from the stream</param> | ||
770 | /// <param name="pOffset">Starting place in the stream to begin trying to read from</param> | ||
771 | /// <param name="length">Lenth in the stream to try and read from. Provided for cases where the | ||
772 | /// buffer's length is larger then the data in it</param> | ||
773 | /// <param name="oHeader">Outputs the read WebSocket frame header</param> | ||
774 | /// <param name="moveBuffer">Informs the calling stream to move the buffer forward</param> | ||
775 | /// <returns>True if it got a header, False if it didn't get a header</returns> | ||
776 | public static bool TryReadHeader(byte[] pBuffer, int pOffset, int length, out WebsocketFrameHeader oHeader, | ||
777 | out int moveBuffer) | ||
778 | { | ||
779 | oHeader = WebsocketFrameHeader.ZeroHeader; | ||
780 | int minumheadersize = 2; | ||
781 | if (length > pBuffer.Length - pOffset) | ||
782 | throw new ArgumentOutOfRangeException("The Length specified was larger the byte array supplied"); | ||
783 | if (length < minumheadersize) | ||
784 | { | ||
785 | moveBuffer = 0; | ||
786 | return false; | ||
787 | } | ||
788 | |||
789 | byte nibble1 = (byte)(pBuffer[pOffset] & 0xF0); //FIN/RSV1/RSV2/RSV3 | ||
790 | byte nibble2 = (byte)(pBuffer[pOffset] & 0x0F); // Opcode block | ||
791 | |||
792 | oHeader = new WebsocketFrameHeader(); | ||
793 | oHeader.SetDefault(); | ||
794 | |||
795 | if ((nibble1 & WebSocketReader.EndBit) == WebSocketReader.EndBit) | ||
796 | { | ||
797 | oHeader.IsEnd = true; | ||
798 | } | ||
799 | else | ||
800 | { | ||
801 | oHeader.IsEnd = false; | ||
802 | } | ||
803 | //Opcode | ||
804 | oHeader.Opcode = (WebSocketReader.OpCode)nibble2; | ||
805 | //Mask | ||
806 | oHeader.IsMasked = Convert.ToBoolean((pBuffer[pOffset + 1] & 0x80) >> 7); | ||
807 | |||
808 | // Payload length | ||
809 | oHeader.PayloadLen = (byte)(pBuffer[pOffset + 1] & 0x7F); | ||
810 | |||
811 | int index = 2; // LargerPayload length starts at byte 3 | ||
812 | |||
813 | switch (oHeader.PayloadLen) | ||
814 | { | ||
815 | case 126: | ||
816 | minumheadersize += 2; | ||
817 | if (length < minumheadersize) | ||
818 | { | ||
819 | moveBuffer = 0; | ||
820 | return false; | ||
821 | } | ||
822 | Array.Reverse(pBuffer, pOffset + index, 2); // two bytes | ||
823 | oHeader.PayloadLen = BitConverter.ToUInt16(pBuffer, pOffset + index); | ||
824 | index += 2; | ||
825 | break; | ||
826 | case 127: // we got more this is a bigger frame | ||
827 | // 8 bytes - uint64 - most significant bit 0 network byte order | ||
828 | minumheadersize += 8; | ||
829 | if (length < minumheadersize) | ||
830 | { | ||
831 | moveBuffer = 0; | ||
832 | return false; | ||
833 | } | ||
834 | Array.Reverse(pBuffer, pOffset + index, 8); | ||
835 | oHeader.PayloadLen = BitConverter.ToUInt64(pBuffer, pOffset + index); | ||
836 | index += 8; | ||
837 | break; | ||
838 | |||
839 | } | ||
840 | //oHeader.PayloadLeft = oHeader.PayloadLen; // Start the count in case it's chunked over the network. This is different then frame fragmentation | ||
841 | if (oHeader.IsMasked) | ||
842 | { | ||
843 | minumheadersize += 4; | ||
844 | if (length < minumheadersize) | ||
845 | { | ||
846 | moveBuffer = 0; | ||
847 | return false; | ||
848 | } | ||
849 | oHeader.Mask = BitConverter.ToInt32(pBuffer, pOffset + index); | ||
850 | index += 4; | ||
851 | } | ||
852 | moveBuffer = index; | ||
853 | return true; | ||
854 | |||
855 | } | ||
856 | } | ||
857 | |||
858 | /// <summary> | ||
859 | /// RFC6455 Websocket Frame | ||
860 | /// </summary> | ||
861 | public class WebSocketFrame | ||
862 | { | ||
863 | /* | ||
864 | * RFC6455 | ||
865 | nib 0 1 2 3 4 5 6 7 | ||
866 | byt 0 1 2 3 | ||
867 | dec 0 1 2 3 | ||
868 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
869 | +-+-+-+-+-------+-+-------------+-------------------------------+ | ||
870 | |F|R|R|R| opcode|M| Payload len | Extended payload length | | ||
871 | |I|S|S|S| (4) |A| (7) | (16/64) + | ||
872 | |N|V|V|V| |S| | (if payload len==126/127) | | ||
873 | | |1|2|3| |K| | + | ||
874 | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | ||
875 | | Extended payload length continued, if payload len == 127 | | ||
876 | + - - - - - - - - - - - - - - - +-------------------------------+ | ||
877 | | |Masking-key, if MASK set to 1 | | ||
878 | +-------------------------------+-------------------------------+ | ||
879 | | Masking-key (continued) | Payload Data | | ||
880 | +-------------------------------- - - - - - - - - - - - - - - - + | ||
881 | : Payload Data continued ... : | ||
882 | + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | ||
883 | | Payload Data continued ... | | ||
884 | +---------------------------------------------------------------+ | ||
885 | |||
886 | * When reading these, the frames are possibly fragmented and interleaved with control frames | ||
887 | * the fragmented frames are not interleaved with data frames. Just control frames | ||
888 | */ | ||
889 | public static readonly WebSocketFrame DefaultFrame = new WebSocketFrame(){Header = new WebsocketFrameHeader(),WebSocketPayload = new byte[0]}; | ||
890 | public WebsocketFrameHeader Header; | ||
891 | public byte[] WebSocketPayload; | ||
892 | |||
893 | public byte[] ToBytes() | ||
894 | { | ||
895 | Header.PayloadLen = (ulong)WebSocketPayload.Length; | ||
896 | return Header.ToBytes(WebSocketPayload); | ||
897 | } | ||
898 | |||
899 | } | ||
900 | |||
901 | public struct WebsocketFrameHeader | ||
902 | { | ||
903 | //public byte CurrentMaskIndex; | ||
904 | /// <summary> | ||
905 | /// The last frame in a sequence of fragmented frames or the one and only frame for this message. | ||
906 | /// </summary> | ||
907 | public bool IsEnd; | ||
908 | |||
909 | /// <summary> | ||
910 | /// Returns whether the payload data is masked or not. Data from Clients MUST be masked, Data from Servers MUST NOT be masked | ||
911 | /// </summary> | ||
912 | public bool IsMasked; | ||
913 | |||
914 | /// <summary> | ||
915 | /// A set of cryptologically sound random bytes XoR-ed against the payload octally. Looped | ||
916 | /// </summary> | ||
917 | public int Mask; | ||
918 | /* | ||
919 | byt 0 1 2 3 | ||
920 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
921 | +---------------+---------------+---------------+---------------+ | ||
922 | | Octal 1 | Octal 2 | Octal 3 | Octal 4 | | ||
923 | +---------------+---------------+---------------+---------------+ | ||
924 | */ | ||
925 | |||
926 | |||
927 | public WebSocketReader.OpCode Opcode; | ||
928 | |||
929 | public UInt64 PayloadLen; | ||
930 | //public UInt64 PayloadLeft; | ||
931 | // Payload is X + Y | ||
932 | //public UInt64 ExtensionDataLength; | ||
933 | //public UInt64 ApplicationDataLength; | ||
934 | public static readonly WebsocketFrameHeader ZeroHeader = WebsocketFrameHeader.HeaderDefault(); | ||
935 | |||
936 | public void SetDefault() | ||
937 | { | ||
938 | |||
939 | //CurrentMaskIndex = 0; | ||
940 | IsEnd = true; | ||
941 | IsMasked = true; | ||
942 | Mask = 0; | ||
943 | Opcode = WebSocketReader.OpCode.Close; | ||
944 | // PayloadLeft = 0; | ||
945 | PayloadLen = 0; | ||
946 | // ExtensionDataLength = 0; | ||
947 | // ApplicationDataLength = 0; | ||
948 | |||
949 | } | ||
950 | |||
951 | /// <summary> | ||
952 | /// Returns a byte array representing the Frame header | ||
953 | /// </summary> | ||
954 | /// <param name="payload">This is the frame data payload. The header describes the size of the payload. | ||
955 | /// If payload is null, a Zero sized payload is assumed</param> | ||
956 | /// <returns>Returns a byte array representing the frame header</returns> | ||
957 | public byte[] ToBytes(byte[] payload) | ||
958 | { | ||
959 | List<byte> result = new List<byte>(); | ||
960 | |||
961 | // Squeeze in our opcode and our ending bit. | ||
962 | result.Add((byte)((byte)Opcode | (IsEnd?0x80:0x00) )); | ||
963 | |||
964 | // Again with the three different byte interpretations of size.. | ||
965 | |||
966 | //bytesize | ||
967 | if (PayloadLen <= 125) | ||
968 | { | ||
969 | result.Add((byte) PayloadLen); | ||
970 | } //Uint16 | ||
971 | else if (PayloadLen <= ushort.MaxValue) | ||
972 | { | ||
973 | result.Add(126); | ||
974 | byte[] payloadLengthByte = BitConverter.GetBytes(Convert.ToUInt16(PayloadLen)); | ||
975 | Array.Reverse(payloadLengthByte); | ||
976 | result.AddRange(payloadLengthByte); | ||
977 | } //UInt64 | ||
978 | else | ||
979 | { | ||
980 | result.Add(127); | ||
981 | byte[] payloadLengthByte = BitConverter.GetBytes(PayloadLen); | ||
982 | Array.Reverse(payloadLengthByte); | ||
983 | result.AddRange(payloadLengthByte); | ||
984 | } | ||
985 | |||
986 | // Only add a payload if it's not null | ||
987 | if (payload != null) | ||
988 | { | ||
989 | result.AddRange(payload); | ||
990 | } | ||
991 | return result.ToArray(); | ||
992 | } | ||
993 | |||
994 | /// <summary> | ||
995 | /// A Helper method to define the defaults | ||
996 | /// </summary> | ||
997 | /// <returns></returns> | ||
998 | |||
999 | public static WebsocketFrameHeader HeaderDefault() | ||
1000 | { | ||
1001 | return new WebsocketFrameHeader | ||
1002 | { | ||
1003 | //CurrentMaskIndex = 0, | ||
1004 | IsEnd = false, | ||
1005 | IsMasked = true, | ||
1006 | Mask = 0, | ||
1007 | Opcode = WebSocketReader.OpCode.Close, | ||
1008 | //PayloadLeft = 0, | ||
1009 | PayloadLen = 0, | ||
1010 | // ExtensionDataLength = 0, | ||
1011 | // ApplicationDataLength = 0 | ||
1012 | }; | ||
1013 | } | ||
1014 | } | ||
1015 | |||
1016 | public delegate void DataDelegate(object sender, WebsocketDataEventArgs data); | ||
1017 | |||
1018 | public delegate void TextDelegate(object sender, WebsocketTextEventArgs text); | ||
1019 | |||
1020 | public delegate void PingDelegate(object sender, PingEventArgs pingdata); | ||
1021 | |||
1022 | public delegate void PongDelegate(object sender, PongEventArgs pongdata); | ||
1023 | |||
1024 | public delegate void RegularHttpRequestDelegate(object sender, RegularHttpRequestEvnetArgs request); | ||
1025 | |||
1026 | public delegate void UpgradeCompletedDelegate(object sender, UpgradeCompletedEventArgs completeddata); | ||
1027 | |||
1028 | public delegate void UpgradeFailedDelegate(object sender, UpgradeFailedEventArgs faileddata); | ||
1029 | |||
1030 | public delegate void CloseDelegate(object sender, CloseEventArgs closedata); | ||
1031 | |||
1032 | public delegate bool ValidateHandshake(string pWebOrigin, string pWebSocketKey, string pHost); | ||
1033 | |||
1034 | |||
1035 | public class WebsocketDataEventArgs : EventArgs | ||
1036 | { | ||
1037 | public byte[] Data; | ||
1038 | } | ||
1039 | |||
1040 | public class WebsocketTextEventArgs : EventArgs | ||
1041 | { | ||
1042 | public string Data; | ||
1043 | } | ||
1044 | |||
1045 | public class PingEventArgs : EventArgs | ||
1046 | { | ||
1047 | /// <summary> | ||
1048 | /// The ping event can arbitrarily contain data | ||
1049 | /// </summary> | ||
1050 | public byte[] Data; | ||
1051 | } | ||
1052 | |||
1053 | public class PongEventArgs : EventArgs | ||
1054 | { | ||
1055 | /// <summary> | ||
1056 | /// The pong event can arbitrarily contain data | ||
1057 | /// </summary> | ||
1058 | public byte[] Data; | ||
1059 | |||
1060 | public int PingResponseMS; | ||
1061 | |||
1062 | } | ||
1063 | |||
1064 | public class RegularHttpRequestEvnetArgs : EventArgs | ||
1065 | { | ||
1066 | |||
1067 | } | ||
1068 | |||
1069 | public class UpgradeCompletedEventArgs : EventArgs | ||
1070 | { | ||
1071 | |||
1072 | } | ||
1073 | |||
1074 | public class UpgradeFailedEventArgs : EventArgs | ||
1075 | { | ||
1076 | |||
1077 | } | ||
1078 | |||
1079 | public class CloseEventArgs : EventArgs | ||
1080 | { | ||
1081 | |||
1082 | } | ||
1083 | |||
1084 | |||
1085 | } | ||
diff --git a/OpenSim/Framework/Servers/MainServer.cs b/OpenSim/Framework/Servers/MainServer.cs index ae7d515..293887f 100644 --- a/OpenSim/Framework/Servers/MainServer.cs +++ b/OpenSim/Framework/Servers/MainServer.cs | |||
@@ -230,6 +230,10 @@ namespace OpenSim.Framework.Servers | |||
230 | List<String> poll = httpServer.GetPollServiceHandlerKeys(); | 230 | List<String> poll = httpServer.GetPollServiceHandlerKeys(); |
231 | foreach (String s in httpServer.GetHTTPHandlerKeys()) | 231 | foreach (String s in httpServer.GetHTTPHandlerKeys()) |
232 | handlers.AppendFormat("\t{0} {1}\n", s, (poll.Contains(s) ? "(poll service)" : string.Empty)); | 232 | handlers.AppendFormat("\t{0} {1}\n", s, (poll.Contains(s) ? "(poll service)" : string.Empty)); |
233 | |||
234 | handlers.AppendFormat("* JSONRPC:\n"); | ||
235 | foreach (String s in httpServer.GetJsonRpcHandlerKeys()) | ||
236 | handlers.AppendFormat("\t{0}\n", s); | ||
233 | 237 | ||
234 | // handlers.AppendFormat("* Agent:\n"); | 238 | // handlers.AppendFormat("* Agent:\n"); |
235 | // foreach (String s in httpServer.GetAgentHandlerKeys()) | 239 | // foreach (String s in httpServer.GetAgentHandlerKeys()) |
diff --git a/OpenSim/Framework/Servers/Properties/AssemblyInfo.cs b/OpenSim/Framework/Servers/Properties/AssemblyInfo.cs index 021f63c..792c62e 100644 --- a/OpenSim/Framework/Servers/Properties/AssemblyInfo.cs +++ b/OpenSim/Framework/Servers/Properties/AssemblyInfo.cs | |||
@@ -29,5 +29,5 @@ using System.Runtime.InteropServices; | |||
29 | // Build Number | 29 | // Build Number |
30 | // Revision | 30 | // Revision |
31 | // | 31 | // |
32 | [assembly: AssemblyVersion("0.7.5.*")] | 32 | [assembly: AssemblyVersion("0.7.6.*")] |
33 | [assembly: AssemblyFileVersion("1.0.0.0")] | 33 | [assembly: AssemblyFileVersion("1.0.0.0")] |
diff --git a/OpenSim/Framework/Servers/ServerBase.cs b/OpenSim/Framework/Servers/ServerBase.cs index 9eb2281..65ccd10 100644 --- a/OpenSim/Framework/Servers/ServerBase.cs +++ b/OpenSim/Framework/Servers/ServerBase.cs | |||
@@ -27,16 +27,19 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Diagnostics; | ||
30 | using System.IO; | 31 | using System.IO; |
31 | using System.Reflection; | 32 | using System.Reflection; |
32 | using System.Text; | 33 | using System.Text; |
33 | using System.Text.RegularExpressions; | 34 | using System.Text.RegularExpressions; |
35 | using System.Threading; | ||
34 | using log4net; | 36 | using log4net; |
35 | using log4net.Appender; | 37 | using log4net.Appender; |
36 | using log4net.Core; | 38 | using log4net.Core; |
37 | using log4net.Repository; | 39 | using log4net.Repository; |
38 | using Nini.Config; | 40 | using Nini.Config; |
39 | using OpenSim.Framework.Console; | 41 | using OpenSim.Framework.Console; |
42 | using OpenSim.Framework.Monitoring; | ||
40 | 43 | ||
41 | namespace OpenSim.Framework.Servers | 44 | namespace OpenSim.Framework.Servers |
42 | { | 45 | { |
@@ -168,6 +171,9 @@ namespace OpenSim.Framework.Servers | |||
168 | "General", false, "show info", "show info", "Show general information about the server", HandleShow); | 171 | "General", false, "show info", "show info", "Show general information about the server", HandleShow); |
169 | 172 | ||
170 | m_console.Commands.AddCommand( | 173 | m_console.Commands.AddCommand( |
174 | "General", false, "show version", "show version", "Show server version", HandleShow); | ||
175 | |||
176 | m_console.Commands.AddCommand( | ||
171 | "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow); | 177 | "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow); |
172 | 178 | ||
173 | m_console.Commands.AddCommand( | 179 | m_console.Commands.AddCommand( |
@@ -206,6 +212,34 @@ namespace OpenSim.Framework.Servers | |||
206 | "General", false, "command-script", | 212 | "General", false, "command-script", |
207 | "command-script <script>", | 213 | "command-script <script>", |
208 | "Run a command script from file", HandleScript); | 214 | "Run a command script from file", HandleScript); |
215 | |||
216 | m_console.Commands.AddCommand( | ||
217 | "General", false, "show threads", | ||
218 | "show threads", | ||
219 | "Show thread status", HandleShow); | ||
220 | |||
221 | m_console.Commands.AddCommand( | ||
222 | "General", false, "threads abort", | ||
223 | "threads abort <thread-id>", | ||
224 | "Abort a managed thread. Use \"show threads\" to find possible threads.", HandleThreadsAbort); | ||
225 | |||
226 | m_console.Commands.AddCommand( | ||
227 | "General", false, "threads show", | ||
228 | "threads show", | ||
229 | "Show thread status. Synonym for \"show threads\"", | ||
230 | (string module, string[] args) => Notice(GetThreadsReport())); | ||
231 | |||
232 | m_console.Commands.AddCommand( | ||
233 | "General", false, "force gc", | ||
234 | "force gc", | ||
235 | "Manually invoke runtime garbage collection. For debugging purposes", | ||
236 | HandleForceGc); | ||
237 | } | ||
238 | |||
239 | private void HandleForceGc(string module, string[] args) | ||
240 | { | ||
241 | Notice("Manually invoking runtime garbage collection"); | ||
242 | GC.Collect(); | ||
209 | } | 243 | } |
210 | 244 | ||
211 | public virtual void HandleShow(string module, string[] cmd) | 245 | public virtual void HandleShow(string module, string[] cmd) |
@@ -222,9 +256,17 @@ namespace OpenSim.Framework.Servers | |||
222 | ShowInfo(); | 256 | ShowInfo(); |
223 | break; | 257 | break; |
224 | 258 | ||
259 | case "version": | ||
260 | Notice(GetVersionText()); | ||
261 | break; | ||
262 | |||
225 | case "uptime": | 263 | case "uptime": |
226 | Notice(GetUptimeReport()); | 264 | Notice(GetUptimeReport()); |
227 | break; | 265 | break; |
266 | |||
267 | case "threads": | ||
268 | Notice(GetThreadsReport()); | ||
269 | break; | ||
228 | } | 270 | } |
229 | } | 271 | } |
230 | 272 | ||
@@ -537,6 +579,75 @@ namespace OpenSim.Framework.Servers | |||
537 | } | 579 | } |
538 | 580 | ||
539 | /// <summary> | 581 | /// <summary> |
582 | /// Get a report about the registered threads in this server. | ||
583 | /// </summary> | ||
584 | protected string GetThreadsReport() | ||
585 | { | ||
586 | // This should be a constant field. | ||
587 | string reportFormat = "{0,6} {1,35} {2,16} {3,13} {4,10} {5,30}"; | ||
588 | |||
589 | StringBuilder sb = new StringBuilder(); | ||
590 | Watchdog.ThreadWatchdogInfo[] threads = Watchdog.GetThreadsInfo(); | ||
591 | |||
592 | sb.Append(threads.Length + " threads are being tracked:" + Environment.NewLine); | ||
593 | |||
594 | int timeNow = Environment.TickCount & Int32.MaxValue; | ||
595 | |||
596 | sb.AppendFormat(reportFormat, "ID", "NAME", "LAST UPDATE (MS)", "LIFETIME (MS)", "PRIORITY", "STATE"); | ||
597 | sb.Append(Environment.NewLine); | ||
598 | |||
599 | foreach (Watchdog.ThreadWatchdogInfo twi in threads) | ||
600 | { | ||
601 | Thread t = twi.Thread; | ||
602 | |||
603 | sb.AppendFormat( | ||
604 | reportFormat, | ||
605 | t.ManagedThreadId, | ||
606 | t.Name, | ||
607 | timeNow - twi.LastTick, | ||
608 | timeNow - twi.FirstTick, | ||
609 | t.Priority, | ||
610 | t.ThreadState); | ||
611 | |||
612 | sb.Append("\n"); | ||
613 | } | ||
614 | |||
615 | sb.Append("\n"); | ||
616 | |||
617 | // For some reason mono 2.6.7 returns an empty threads set! Not going to confuse people by reporting | ||
618 | // zero active threads. | ||
619 | int totalThreads = Process.GetCurrentProcess().Threads.Count; | ||
620 | if (totalThreads > 0) | ||
621 | sb.AppendFormat("Total threads active: {0}\n\n", totalThreads); | ||
622 | |||
623 | sb.Append("Main threadpool (excluding script engine pools)\n"); | ||
624 | sb.Append(Util.GetThreadPoolReport()); | ||
625 | |||
626 | return sb.ToString(); | ||
627 | } | ||
628 | |||
629 | public virtual void HandleThreadsAbort(string module, string[] cmd) | ||
630 | { | ||
631 | if (cmd.Length != 3) | ||
632 | { | ||
633 | MainConsole.Instance.Output("Usage: threads abort <thread-id>"); | ||
634 | return; | ||
635 | } | ||
636 | |||
637 | int threadId; | ||
638 | if (!int.TryParse(cmd[2], out threadId)) | ||
639 | { | ||
640 | MainConsole.Instance.Output("ERROR: Thread id must be an integer"); | ||
641 | return; | ||
642 | } | ||
643 | |||
644 | if (Watchdog.AbortThread(threadId)) | ||
645 | MainConsole.Instance.OutputFormat("Aborted thread with id {0}", threadId); | ||
646 | else | ||
647 | MainConsole.Instance.OutputFormat("ERROR - Thread with id {0} not found in managed threads", threadId); | ||
648 | } | ||
649 | |||
650 | /// <summary> | ||
540 | /// Console output is only possible if a console has been established. | 651 | /// Console output is only possible if a console has been established. |
541 | /// That is something that cannot be determined within this class. So | 652 | /// That is something that cannot be determined within this class. So |
542 | /// all attempts to use the console MUST be verified. | 653 | /// all attempts to use the console MUST be verified. |
diff --git a/OpenSim/Framework/Servers/Tests/OSHttpTests.cs b/OpenSim/Framework/Servers/Tests/OSHttpTests.cs index 4c2f586..deae45c 100644 --- a/OpenSim/Framework/Servers/Tests/OSHttpTests.cs +++ b/OpenSim/Framework/Servers/Tests/OSHttpTests.cs | |||
@@ -35,11 +35,12 @@ using HttpServer; | |||
35 | using HttpServer.FormDecoders; | 35 | using HttpServer.FormDecoders; |
36 | using NUnit.Framework; | 36 | using NUnit.Framework; |
37 | using OpenSim.Framework.Servers.HttpServer; | 37 | using OpenSim.Framework.Servers.HttpServer; |
38 | using OpenSim.Tests.Common; | ||
38 | 39 | ||
39 | namespace OpenSim.Framework.Servers.Tests | 40 | namespace OpenSim.Framework.Servers.Tests |
40 | { | 41 | { |
41 | [TestFixture] | 42 | [TestFixture] |
42 | public class OSHttpTests | 43 | public class OSHttpTests : OpenSimTestCase |
43 | { | 44 | { |
44 | // we need an IHttpClientContext for our tests | 45 | // we need an IHttpClientContext for our tests |
45 | public class TestHttpClientContext: IHttpClientContext | 46 | public class TestHttpClientContext: IHttpClientContext |
@@ -69,6 +70,11 @@ namespace OpenSim.Framework.Servers.Tests | |||
69 | public void Close() { } | 70 | public void Close() { } |
70 | public bool EndWhenDone { get { return false;} set { return;}} | 71 | public bool EndWhenDone { get { return false;} set { return;}} |
71 | 72 | ||
73 | public HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing() | ||
74 | { | ||
75 | return new HTTPNetworkContext(); | ||
76 | } | ||
77 | |||
72 | public event EventHandler<DisconnectedEventArgs> Disconnected = delegate { }; | 78 | public event EventHandler<DisconnectedEventArgs> Disconnected = delegate { }; |
73 | /// <summary> | 79 | /// <summary> |
74 | /// A request have been received in the context. | 80 | /// A request have been received in the context. |
diff --git a/OpenSim/Framework/Servers/Tests/VersionInfoTests.cs b/OpenSim/Framework/Servers/Tests/VersionInfoTests.cs index 49e5061..480f2bb 100644 --- a/OpenSim/Framework/Servers/Tests/VersionInfoTests.cs +++ b/OpenSim/Framework/Servers/Tests/VersionInfoTests.cs | |||
@@ -29,11 +29,12 @@ using System; | |||
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Text; | 30 | using System.Text; |
31 | using NUnit.Framework; | 31 | using NUnit.Framework; |
32 | using OpenSim.Tests.Common; | ||
32 | 33 | ||
33 | namespace OpenSim.Framework.Servers.Tests | 34 | namespace OpenSim.Framework.Servers.Tests |
34 | { | 35 | { |
35 | [TestFixture] | 36 | [TestFixture] |
36 | public class VersionInfoTests | 37 | public class VersionInfoTests : OpenSimTestCase |
37 | { | 38 | { |
38 | [Test] | 39 | [Test] |
39 | public void TestVersionLength() | 40 | public void TestVersionLength() |
diff --git a/OpenSim/Framework/Servers/VersionInfo.cs b/OpenSim/Framework/Servers/VersionInfo.cs index bb094ed..737c14d 100644 --- a/OpenSim/Framework/Servers/VersionInfo.cs +++ b/OpenSim/Framework/Servers/VersionInfo.cs | |||
@@ -29,7 +29,7 @@ namespace OpenSim | |||
29 | { | 29 | { |
30 | public class VersionInfo | 30 | public class VersionInfo |
31 | { | 31 | { |
32 | private const string VERSION_NUMBER = "0.7.5CM"; | 32 | private const string VERSION_NUMBER = "0.7.6CM"; |
33 | private const Flavour VERSION_FLAVOUR = Flavour.Dev; | 33 | private const Flavour VERSION_FLAVOUR = Flavour.Dev; |
34 | 34 | ||
35 | public enum Flavour | 35 | public enum Flavour |