Example #1
0
        private void Connect()
        {
            m_errorCount = 0;

            if (!m_keepRunning)
                return;
            m_state = ParseState.START;
            m_sock = new AsyncSocket(null, this, m_ssl, false);
            m_sock.Connect(m_addr, m_host);
        }
Example #2
0
        /// <summary>
        /// Shut down the socket, abandoning any outstainding requests
        /// </summary>
        public override void Close()
        {
            lock (m_lock)
            {
                m_keepRunning = false;
                // in case we closed while waiting for connect
                Monitor.Pulse(m_lock);
            }

            if (Connected)
                m_sock.Close();
            m_sock = null;
        }
Example #3
0
 void ISocketEventListener.OnClose(BaseSocket sock)
 {
     m_sock = null;
     lock (m_lock)
     {
         Monitor.Pulse(m_lock);
     }
 }
Example #4
0
 /// <summary>
 /// Called by AsyncSocket when this class is full, and the listening AsyncSocket
 /// socket would like to be restarted when there are slots free.
 /// </summary>
 /// <param name="s">Listening socket</param>
 public void PendingAccept(AsyncSocket s)
 {
     lock (m_lock)
     {
         m_pending.Add(s);
     }
 }
Example #5
0
 /// <summary>
 /// Called by AsyncSocket when a new connection is received on a listen socket.
 /// </summary>
 /// <param name="s">New socket connection</param>
 public void RegisterSocket(AsyncSocket s)
 {
     lock (m_lock)
     {
         if ((m_maxSocks >= 0) && (m_socks.Count >= m_maxSocks))
             throw new InvalidOperationException("Too many sockets: " + m_socks.Count);
         m_socks.Add(s);
     }
 }
Example #6
0
        /// <summary>
        /// Create an outbound socket.
        /// </summary>
        /// <param name="listener">Where to send notifications</param>
        /// <param name="addr">Address to connect to</param>
        /// <param name="SSL">Do SSL3/TLS1 on startup</param>
        /// <param name="hostId">The logical name of the host to connect to, for SSL/TLS purposes.</param>
        /// <returns>Socket that is in the process of connecting</returns>
        public AsyncSocket CreateConnectSocket(ISocketEventListener listener,
            Address              addr,
            bool                 SSL,
            string               hostId)
        {
            AsyncSocket result;

            // Create the socket:
            result = new AsyncSocket(this, listener, SSL, m_synch);
            if (SSL)
                result.LocalCertificate = m_cert;

            // Start the connect process:
            result.Connect(addr, hostId);
            return result;
        }
Example #7
0
 /// <summary>
 /// Create a socket that is listening for inbound connections.
 /// </summary>
 /// <param name="listener">Where to send notifications</param>
 /// <param name="addr">Address to connect to</param>
 /// <param name="backlog">The maximum length of the queue of pending connections</param>
 /// <param name="SSL">Do SSL3/TLS1 on connect</param>
 /// <returns>A socket that is ready for calling RequestAccept()</returns>
 public AsyncSocket CreateListenSocket(ISocketEventListener listener,
     Address              addr,
     int                  backlog,
     bool                 SSL)
 {
     //Debug.Assert(m_maxSocks > 1);
     AsyncSocket result = new AsyncSocket(this, listener, SSL, m_synch);
     if (SSL)
     {
         result.LocalCertificate = m_cert;
         result.RequireClientCert = m_requireClientCert;
     }
     result.Accept(addr, backlog);
     return result;
 }
Example #8
0
        /// <summary>
        /// Called by AsyncSocket when a socket is closed.
        /// </summary>
        /// <param name="s">Closed socket</param>
        public void CleanupSocket(AsyncSocket s)
        {
            lock (m_lock)
            {
                m_socks.Remove(s);

                if (m_pending.Contains(s))
                {
                    m_pending.Remove(s);
                }
                else
                {
                    foreach (AsyncSocket sock in m_pending)
                    {
                        sock.RequestAccept();
                    }
                    m_pending.Clear();
                }
            }
        }
        /// <summary>
        /// Listens for an inbound connection.
        /// </summary>
        public override void Accept()
        {
            AsyncSocket s = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false);
            s.LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as
                System.Security.Cryptography.X509Certificates.X509Certificate2;

            m_sock = s;
            m_sock.Accept(new Address((int)m_listener[Options.PORT]));
            m_sock.RequestAccept();
        }
Example #10
0
        /// <summary>
        /// Start the flow of async accepts.  Flow will continue while
        /// Listener.OnAccept() returns true.  Otherwise, call
        /// RequestAccept() again to continue.
        /// </summary>
        public override void RequestAccept()
        {
            lock (this)
            {
                if (State != SocketState.Listening)
                {
                    throw new InvalidOperationException("Not a listen socket");
                }
            }
            if (m_synch)
            {
                Socket cli;
                try
                {
                    cli = m_sock.Accept();
                }
                catch (SocketException)
                {
                    Debug.WriteLine("A cancel call was sent to the accepting socket.");
                    return;
                }

                AsyncSocket cliCon = new AsyncSocket(m_watcher) {m_sock = cli, m_synch = true};
                AcceptDone(cliCon);
            }
            else
            {
                m_sock.BeginAccept(ExecuteAccept, null);
            }
        }
Example #11
0
 /// <summary>
 /// We got a connection from outside.  Add it to the SocketWatcher.
 /// </summary>
 /// <param name="ar"></param>
 private void ExecuteAccept(IAsyncResult ar)
 {
     Socket cli = m_sock.EndAccept(ar);
     AsyncSocket cliCon = new AsyncSocket(m_watcher) {m_sock = cli};
     AcceptDone(cliCon);
 }
Example #12
0
        private void AcceptDone(AsyncSocket cliCon)
        {
            cliCon.m_addr = m_addr;
            cliCon.Address.IP = ((IPEndPoint) cliCon.m_sock.RemoteEndPoint).Address;
            cliCon.State = SocketState.Connected;

            cliCon.m_stream = new NetworkStream(cliCon.m_sock);
            cliCon.m_server = true;
            cliCon.LocalCertificate = m_cert;
            cliCon.RequireClientCert = m_requireClientCert;

            ISocketEventListener l = m_listener.GetListener(cliCon);
            if (l == null)
            {
                // if the listener returns null, close the socket and
                // quit, instead of asserting.
                cliCon.m_sock.Close();
                RequestAccept();
                return;
            }

            cliCon.m_listener = l;

            try
            {
                if (m_watcher != null)
                    m_watcher.RegisterSocket(cliCon);
            }
            catch (InvalidOperationException)
            {
                // m_watcher out of slots.
                cliCon.AsyncClose();

                // don't set state
                // they really don't need this error, we don't think.
                // Error(e);

                // tell the watcher that when it gets its act together,
                // we'd appreciate it if it would restart the RequestAccept().
                m_watcher.PendingAccept(this);
                return;
            }

            if (m_secureProtocol != SslProtocols.None)
                cliCon.StartTLS();

            if (l.OnAccept(cliCon))
            {
                RequestAccept();
            }
        }