Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// IComparable's need to implement Equals().  This checks the
        /// guid's for each socket to see if they are the same.
        /// </summary>
        /// <param name="val">The AsyncSocket to check against.</param>
        /// <returns></returns>
        public override bool Equals(object val)
        {
            AsyncSocket sock = val as AsyncSocket;

            if (sock == null)
            {
                return(false);
            }
            return(this.m_id == sock.m_id);
        }
Exemplo n.º 3
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);
     }
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
        int IComparable.CompareTo(object val)
        {
            if (val == null)
            {
                return(1);
            }

            AsyncSocket sock = val as AsyncSocket;

            if ((object)sock == null)
            {
                throw new ArgumentException("value compared to is not an AsyncSocket", "val");
            }

            return(this.m_id.CompareTo(sock.m_id));
        }
Exemplo n.º 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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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();
                }
            }
        }