/// <inheritdoc/> public override void StartHostingService() { if (IsHostingServiceRunning) { return; } if (HostingServicePort <= 0) { HostingServicePort = GetAvailablePort(); } else if (!IsPortAvailable(HostingServicePort)) { LogError("Port {0} is in use, cannot start service!", HostingServicePort); return; } if (HostingServiceContentRoots.Count == 0) { throw new Exception( "ContentRoot is not configured; cannot start service. This can usually be fixed by modifying the BuildPath for any new groups and/or building content."); } ConfigureHttpListener(); MyHttpListener.Start(); MyHttpListener.BeginGetContext(HandleRequest, null); Log("Started. Listening on port {0}", HostingServicePort); }
/// <summary> /// Asynchronous callback to handle a client connection request on <see cref="MyHttpListener"/>. This method is /// recursive in that it will call itself immediately after receiving a new incoming request to listen for the /// next connection. /// </summary> /// <param name="ar">Asynchronous result from previous request. Pass null to listen for an initial request</param> /// <exception cref="ArgumentOutOfRangeException">thrown when the request result code is unknown</exception> protected virtual void HandleRequest(IAsyncResult ar) { if (!IsHostingServiceRunning) { return; } var c = MyHttpListener.EndGetContext(ar); MyHttpListener.BeginGetContext(HandleRequest, null); var relativePath = c.Request.RawUrl.Substring(1); var fullPath = FindFileInContentRoots(relativePath); var result = fullPath != null ? ResultCode.Ok : ResultCode.NotFound; var info = fullPath != null ? new FileInfo(fullPath) : null; var size = info != null?info.Length.ToString() : "-"; var remoteAddress = c.Request.RemoteEndPoint != null ? c.Request.RemoteEndPoint.Address : null; var timestamp = DateTime.Now.ToString("o"); Log("{0} - - [{1}] \"{2}\" {3} {4}", remoteAddress, timestamp, fullPath, (int)result, size); switch (result) { case ResultCode.Ok: ReturnFile(c, fullPath); break; case ResultCode.NotFound: Return404(c); break; default: throw new ArgumentOutOfRangeException(); } }
/// <inheritdoc /> public override void StartHostingService() { if (IsHostingServiceRunning) { return; } if (HostingServicePort <= 0) { HostingServicePort = GetAvailablePort(); } else if (!IsPortAvailable(HostingServicePort)) { LogError("Port {0} is in use, cannot start service!", HostingServicePort); return; } if (HostingServiceContentRoots.Count == 0) { throw new Exception( "ContentRoot is not configured; cannot start service. This can usually be fixed by modifying the BuildPath for any new groups and/or building content."); } ConfigureHttpListener(); MyHttpListener.Start(); MyHttpListener.BeginGetContext(HandleRequest, null); Log("Started. Listening on port {0}", HostingServicePort); // if (socketListener == null) { socketListener = new AsyncSocketServer(); } var socketLocalEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), HotfixManager.Instance.ipPort); socketListener.Start(socketLocalEndPoint); }