/// <summary> /// HttpServer Socket Listener /// </summary> public void Listen() { // Create listener and start listening while (m_isStarted) { try { _Bind.RequestContext context = m_replyChannel.ReceiveRequest(); // The context returned by m_httpListener.GetContext(); can be null in case the service was stopped. if (context != null) { WsHttpMessageProcessor processor = new WsHttpMessageProcessor(m_serviceEndpoints); // Try to get a processing thread and process the request m_threadManager.StartNewThread(processor, context); m_ctx.ContextObject = context.m_context.ContextObject; } } catch { m_ctx.ContextObject = null; } } }
/// <summary> /// Listens for Udp request on 239.255.255.250:3702 /// </summary> /// <remarks>On initialization it sends a Discovery Hello message and listens on the Ws-Discovery /// endpoint for a request. When a request arrives it starts a UdpProcess thread that processes the message. /// The number of UdpProcessing threads are limited by the Device.MaxUdpRequestThreads property. /// </remarks> private void Listen() { // Create a duplicate message tester. WsMessageCheck messageCheck = new WsMessageCheck(40); while (!m_requestStop) { try { // If threads ara availble receive next message. If we are waiting on threads let the socket // buffer request until we get a thread. This will work until the reveice buffer is depleted // at which time request will be dropped if (m_threadManager.ThreadsAvailable == true) { RequestContext req = m_replyChannel.ReceiveRequest(); if (req != null) { WsWsaHeader header = req.Message.Header; if (header.MessageID != null && messageCheck.IsDuplicate(header.MessageID, header.From != null ? header.From.Address.AbsoluteUri : "")) { continue; } // Try to get a processing thread and process the request m_threadManager.StartNewThread(new WsUdpMessageProcessor(m_serviceEndpoints, req)); } else { System.Ext.Console.Write("UDP Receive returned 0 bytes"); } } else { System.Ext.Console.Write("Udp service host waiting for a thread..."); m_threadManager.ThreadEvent.WaitOne(); } } catch (SocketException se) { // Since the MF Socket does not have IOControl that would be used to turn off ICMP notifications // for UDP, catch 10054 and try to continue if ((SocketError)se.ErrorCode == SocketError.ConnectionReset) { Thread.Sleep(100); } } catch (Exception e) { System.Ext.Console.Write(e.Message + " " + e.InnerException); } } }
/// <summary> /// HttpServer Socket Listener /// </summary> public void Listen() { // Create listener and start listening int threadCount = 0; m_httpListener.Start(); while (m_requestStop == false) { HttpListenerContext context; try { context = m_httpListener.GetContext(); } catch (SocketException e) { // If request to stop listener flag is set or locking call is interupted return // The error code 10004 comes from the socket exception. if (m_requestStop == true || e.ErrorCode == 10004) { return; } // Throw on any other error System.Ext.Console.Write("Accept failed! Socket Error = " + e.ErrorCode); throw e; } // The context returned by m_httpListener.GetContext(); can be null in case the service was stopped. if (context != null) { WsHttpMessageProcessor processor = new WsHttpMessageProcessor(m_serviceEndpoints, context); if (m_threadManager.ThreadsAvailable == false) { processor.SendError(503, "Service Unavailable"); System.Ext.Console.Write("Http max thread count " + threadCount + " exceeded. Request ignored."); // System.Ext.Console.Write("Sending Service Unavailble to: " + sock.RemoteEndPoint.ToString()); Igor } else { // Try to get a processing thread and process the request m_threadManager.StartNewThread(processor); } } } }
/// <summary> /// HttpServer Socket Listener /// </summary> public void Listen() { // Create listener and start listening while (m_isStarted) { try { _Bind.RequestContext context = m_replyChannel.ReceiveRequest(); // The context returned by m_httpListener.GetContext(); can be null in case the service was stopped. if (context != null) { WsHttpMessageProcessor processor = new WsHttpMessageProcessor(m_serviceEndpoints, context); if (m_threadManager.ThreadsAvailable == false) { WsWsaHeader header = new WsWsaHeader(); context.Reply(WsFault.GenerateFaultResponse(header, WsFaultType.WsaEndpointUnavailable, "Service Unavailable (busy)", context.Version)); System.Ext.Console.Write("Http max thread count exceeded. Request ignored."); } else { // Try to get a processing thread and process the request m_threadManager.StartNewThread(processor); } } } catch { if (!m_isStarted) { break; } } } }
/// <summary> /// Listens for Udp request on 239.255.255.250:3702 /// </summary> /// <remarks>On initialization it sends a Discovery Hello message and listens on the Ws-Discovery /// endpoint for a request. When a request arrives it starts a UdpProcess thread that processes the message. /// The number of UdpProcessing threads are limited by the Device.MaxUdpRequestThreads property. /// </remarks> public void Listen() { // Create a duplicate message tester. WsMessageCheck messageCheck = new WsMessageCheck(40); // Create remote endpoint reference, start listening byte[] buffer = new byte[c_MaxUdpPacketSize]; int size; bool threadPoolDepletedFlag = false; m_servicesRunning = !m_requestStop; while (!m_requestStop) { try { // If threads ara availble receive next message. If we are waiting on threads let the socket // buffer request until we get a thread. This will work until the reveice buffer is depleted // at which time request will be dropped if (m_threadManager.ThreadsAvailable == true) { threadPoolDepletedFlag = false; EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); size = m_udpReceiveClient.ReceiveFrom(buffer, c_MaxUdpPacketSize, SocketFlags.None, ref remoteEP); // If the stack is set to ignore request from this address do so if (this.IgnoreRequestFromThisIP == true && ((IPEndPoint)remoteEP).Address.ToString() == WsNetworkServices.GetLocalIPV4Address()) { continue; } if (size > 0) { byte[] soapMessage = new byte[size]; Array.Copy(buffer, soapMessage, size); System.Ext.Console.Write("UDP Request From: " + remoteEP.ToString()); System.Ext.Console.Write(new String(System.Text.Encoding.UTF8.GetChars(soapMessage))); // Try to get a processing thread and process the request m_threadManager.StartNewThread(new WsUdpMessageProcessor(m_serviceEndpoints, soapMessage, (IPEndPoint)remoteEP, messageCheck)); } else { System.Ext.Console.Write("UDP Receive returned 0 bytes"); } } else { if (threadPoolDepletedFlag == false) { System.Ext.Console.Write("Udp service host waiting for a thread..."); threadPoolDepletedFlag = true; } } } catch (SocketException se) { // Since the MF Socket does not have IOControl that would be used to turn off ICMP notifications // for UDP, catch 10054 and try to continue if (se.ErrorCode == 10054) { continue; } } catch (Exception e) { System.Ext.Console.Write(e.Message + " " + e.InnerException); } Thread.Sleep(100); } }