/// <summary> /// Start listening on the given host IP Address, on the given /// port. /// </summary> /// <param name="hostIP">The host's IP address to bind to.</param> /// <param name="port">The port to bind on.</param> public void Start(string hostIP, int port) { HttpTraceHelper.ExceptionCaught.Level = TraceLevel.Verbose; _listener = new HttpWebListener(); _listener.Start(port); // Put this in a loop if we want more listeners in the pool var stateObject = new HttpApplication(); _listener.BeginGetRequest(_getRequestCB, stateObject); }
/// <summary> /// This method receives requests from other peers and maps the URI /// namespace or resource name to an actual namespace handler. If one /// exists then the namespace handler is invoked to process the request. /// If not some default processing is used. /// </summary> /// <param name="asyncResult">The Async object for the request.</param> public void GetRequestCallback(IAsyncResult asyncResult) { try { OnBeforeProcessRequest(); var stateObject = asyncResult.AsyncState as HttpApplication; stateObject.HttpRequest = _listener.EndGetRequest(asyncResult); if (stateObject.HttpRequest.Method == "GET") { var ns = stateObject.HttpRequest.RequestUri.Segments[1]; if (_nsModules.Contains(ns)) { var nsHandler = (IHttpNamespaceHandler)_nsModules[ns]; nsHandler.ProcessRequest(stateObject); } else { HandleUnsupportedNamespace(stateObject); } } else if (stateObject.HttpRequest.Method == "POST") { if (stateObject.HttpRequest.ContentLength <= 0) { var message = "<html><body>Bad Request - POST with no data"; message += "</body></html>"; HandleBadRequest(stateObject, message); } else { stateObject.RequestStream = stateObject.HttpRequest.GetRequestStream(); try { stateObject.RequestStream.BeginRead(stateObject.ReadBuffer, 0, stateObject.ReadBuffer.Length, _readCB, stateObject); } catch (Exception exception) { #if DEBUG if (HttpTraceHelper.ExceptionCaught.TraceVerbose) { HttpTraceHelper.WriteLine("Caught exception:" + exception); } #endif } } } else { HandleUnsupportedNamespace(stateObject); } // Queue up another _listener var newStateObject = new HttpApplication(); _listener.BeginGetRequest(_getRequestCB, newStateObject); } catch (Exception ex) { #if DEBUG if (HttpTraceHelper.ExceptionCaught.TraceVerbose) { HttpTraceHelper.WriteLine("Caught exception:" + ex); } #endif var stateObject = asyncResult.AsyncState as HttpApplication; stateObject.Reset(); _listener.BeginGetRequest(_getRequestCB, stateObject); } finally { OnAfterProcessRequest(); } }
/// <summary> /// Start listening on the given host IP Address, on the given /// port. /// </summary> /// <param name="hostIP">The host's IP address to bind to.</param> /// <param name="port">The port to bind on.</param> public void Start(string hostIP, int port) { HttpTraceHelper.ExceptionCaught.Level = TraceLevel.Verbose; //TraceHelper.Api.Level = TraceLevel.Verbose; //TraceHelper.ExceptionThrown.Level = TraceLevel.Verbose; //TraceHelper.InternalLog.Level = TraceLevel.Verbose; //TraceHelper.Protocol.Level = TraceLevel.Verbose; //TraceHelper.Socket.Level = TraceLevel.Verbose; listener = new HttpWebListener(); listener.Start(port); // Put this in a loop if we want more listeners in the pool HttpApplication stateObject = new HttpApplication(); listener.BeginGetRequest(getRequestCB, stateObject); }