/// <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>
 /// 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);
     }
 }
        /// <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;
        }
 /// <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);
     }
 }
Beispiel #5
0
 public void OnClose(BaseSocket sock)
 {
     m_sock = null;
     lock (m_lock)
     {
         Monitor.Pulse(m_lock);
     }
 }
 /// <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;
 }
Beispiel #7
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);
        }
Beispiel #8
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;
        }
        private void Server()
        {
            SocketWatcher s_w = new SocketWatcher(20);

            //s_w.RequireClientCert = true;

            X509Certificate2 c2;
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);
            X509Certificate2Collection cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", true);
            if (cert.Count == 0)
            {
                c2 = new X509Certificate2("../../localhost-cert.p12", "test");
                store.Add(c2);
            }
            else
            {
                c2 = cert[0];
            }
            Assert.IsTrue(c2.HasPrivateKey);
            Assert.IsNotNull(c2.PrivateKey);
            Assert.AreEqual(typeof(X509Certificate2), c2.GetType());

            cert = store.Certificates.Find(X509FindType.FindByThumbprint, c2.GetCertHashString(), false);
            c2 = cert[0];
            Assert.AreEqual(typeof(X509Certificate2), c2.GetType());
            Assert.IsTrue(c2.HasPrivateKey);
            Assert.IsNotNull(c2.PrivateKey);
            store.Close();
            s_w.LocalCertificate = c2;
            s_w.Synchronous = true;

            m_listen = s_w.CreateListenSocket(this, a, true);
            lock(start)
            {
                Monitor.Pulse(start);
            }

            try
            {
                m_listen.RequestAccept();
            }
            catch (Exception ex)
            {
                lock (done)
                {
                    succeeded = false;
                    errorMessage = ex.Message;
                    Monitor.Pulse(done);
                }
            }
        }
Beispiel #10
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();
            }
        }
Beispiel #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 = (Socket) m_sock.EndAccept(ar);
     AsyncSocket cliCon = new AsyncSocket(m_watcher);
     cliCon.m_sock = cli;
     AcceptDone(cliCon);
 }
Beispiel #12
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);
                cliCon.m_sock = cli;
                cliCon.m_synch = true;
                AcceptDone(cliCon);
            }
            else
            {
                m_sock.BeginAccept(new AsyncCallback(ExecuteAccept), null);
            }
        }
        /// <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();
        }