public RequestHandle sendRequest(Protocol protocol, LightstreamerRequest request, RequestListener protocolListener, IDictionary <string, string> extraHeaders, Proxy proxy, long tcpConnectTimeout, long tcpReadTimeout) { if (httpProvider == null) { log.Fatal("There is no default HttpProvider, can't connect"); return(null); } RequestHandle connection; try { HttpProvider_HttpRequestListener httpListener = new MyHttpListener(protocolListener, request, sessionThread); connection = httpProvider.createConnection(protocol, request, httpListener, extraHeaders, proxy, tcpConnectTimeout, tcpReadTimeout); } catch (Exception e) { log.Error("Error - " + e.Message + " - " + e.StackTrace); sessionThread.queue(new Task(() => { protocolListener.onBroken(); })); return(null); } if (connection == null) { // we expect that a closed/broken event will be fired soon return(null); } return(new RequestHandleAnonymousInnerClass(this, connection)); }
/// <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); }
/// <inheritdoc/> public override void StopHostingService() { if (!IsHostingServiceRunning) { return; } Log("Stopping"); MyHttpListener.Stop(); }
/// <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); }
static MyGlobalHelper() { markControlService = new FreeHttp.FreeHttpControl.MarkControlService(1000); myHttpListener = new MyHttpListener(); }
public static void SetListner(MyHttpListener listiner) { myhttplistiner = listiner; }