/// <summary> /// Handles requests arriving from a channel. /// </summary> private void OnRequestReceived(TcpServerChannel channel, uint requestId, IServiceRequest request) { if (m_callback != null) { try { IAsyncResult result = m_callback.BeginProcessRequest( channel.GlobalChannelId, channel.EndpointDescription, request, OnProcessRequestComplete, new object[] { channel, requestId, request }); } catch (Exception e) { Utils.Trace(e, "TCPLISTENER - Unexpected error processing request."); } } }
/// <summary> /// Handles a new connection. /// </summary> private void OnAcceptIPv6(IAsyncResult result) { TcpServerChannel channel = null; lock (m_lock) { // check if the socket has been closed. if (m_listeningSocketIPv6 == null) { return; } try { // accept the socket. Socket socket = m_listeningSocketIPv6.EndAccept(result); // create the channel to manage incoming messages. channel = new TcpServerChannel( m_listenerId, this, m_bufferManager, m_quotas, m_serverCertificate, m_descriptions); //channel.ServerCertificateChain = m_serverCertificateChain; // start accepting messages on the channel. channel.Attach(++m_lastChannelId, socket); // save the channel for shutdown and reconnects. m_channels.Add(m_lastChannelId, channel); if (m_callback != null) { channel.SetRequestReceivedCallback(new TcpChannelRequestEventHandler(OnRequestReceived)); } // Utils.Trace("Channel {0} created.", m_lastChannelId); } catch (Exception e) { Utils.Trace(e, "Unexpected error accepting a new connection."); } // go back and wait for the next connection. try { m_listeningSocketIPv6.BeginAccept(OnAcceptIPv6, null); } catch (Exception e) { Utils.Trace(e, "Unexpected error listening for a new IPv6 connection."); } } }
private void Channel_StatusChanged(TcpServerChannel channel, ServiceResult status, bool closed) { ConnectionStatusChanged?.Invoke(this, new ConnectionStatusEventArgs(channel.ReverseConnectionUrl, status, closed)); }
/// <summary> /// Handles a new connection. /// </summary> private void OnAccept(object sender, SocketAsyncEventArgs e) { TcpServerChannel channel = null; lock (m_lock) { Socket listeningSocket = e.UserToken as Socket; if (listeningSocket == null) { Utils.Trace("OnAccept: Listensocket was null." ); e.Dispose(); return; } // check if the accept socket has been created. if (e.AcceptSocket != null && e.SocketError == SocketError.Success) { try { // create the channel to manage incoming messages. channel = new TcpServerChannel( m_listenerId, this, m_bufferManager, m_quotas, m_serverCertificate, m_descriptions); // start accepting messages on the channel. channel.Attach(++m_lastChannelId, e.AcceptSocket); // save the channel for shutdown and reconnects. m_channels.Add(m_lastChannelId, channel); if (m_callback != null) { channel.SetRequestReceivedCallback(new TcpChannelRequestEventHandler(OnRequestReceived)); } } catch (Exception ex) { Utils.Trace(ex, "Unexpected error accepting a new connection."); } } e.Dispose(); // go back and wait for the next connection. try { e = new SocketAsyncEventArgs(); e.Completed += OnAccept; e.UserToken = listeningSocket; listeningSocket.AcceptAsync(e); } catch (Exception ex) { Utils.Trace(ex, "Unexpected error listening for a new connection."); } } }
/// <summary> /// Handles a new connection. /// </summary> private void OnAccept(object sender, SocketAsyncEventArgs e) { TcpListenerChannel channel = null; bool repeatAccept = false; do { repeatAccept = false; lock (m_lock) { Socket listeningSocket = e.UserToken as Socket; if (listeningSocket == null) { Utils.LogError("OnAccept: Listensocket was null."); e.Dispose(); return; } // check if the accept socket has been created. if (e.AcceptSocket != null && e.SocketError == SocketError.Success) { try { if (m_reverseConnectListener) { // create the channel to manage incoming reverse connections. channel = new TcpReverseConnectChannel( m_listenerId, this, m_bufferManager, m_quotas, m_descriptions); } else { // create the channel to manage incoming connections. channel = new TcpServerChannel( m_listenerId, this, m_bufferManager, m_quotas, m_serverCertificate, m_serverCertificateChain, m_descriptions); } if (m_callback != null) { channel.SetRequestReceivedCallback(new TcpChannelRequestEventHandler(OnRequestReceived)); } // get channel id uint channelId = GetNextChannelId(); // start accepting messages on the channel. channel.Attach(channelId, e.AcceptSocket); // save the channel for shutdown and reconnects. m_channels.Add(channelId, channel); } catch (Exception ex) { Utils.LogError(ex, "Unexpected error accepting a new connection."); } } e.Dispose(); if (e.SocketError != SocketError.OperationAborted) { // go back and wait for the next connection. try { e = new SocketAsyncEventArgs(); e.Completed += OnAccept; e.UserToken = listeningSocket; if (!listeningSocket.AcceptAsync(e)) { repeatAccept = true; } } catch (Exception ex) { Utils.LogError(ex, "Unexpected error listening for a new connection."); } } } } while (repeatAccept); }