예제 #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();
            }
        }
예제 #2
0
 private void Connect()
 {
     if (!m_keepRunning)
     {
         return;
     }
     m_state = ParseState.START;
     m_sock  = new AsyncSocket(null, this, m_ssl, false);
     m_sock.Connect(m_addr, m_host);
 }
예제 #3
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);
        }
예제 #4
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);
     }
 }
예제 #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;
        }
예제 #6
0
        /// <summary>
        /// Shut down the socket, abandoning any outstainding requests
        /// </summary>
        public override void Close()
        {
            m_keepRunning = false;
            lock (m_queue)
            {
                m_queue.Clear();
                Monitor.Pulse(m_queue);
            }

            if (Connected)
            {
                m_sock.Close();
            }
            m_sock = null;
        }
예제 #7
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));
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
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();
                }
            }
        }
예제 #11
0
        /// <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();
        }
예제 #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);
            }
        }
예제 #13
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);
 }
예제 #14
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;
        }
예제 #15
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);
     }
 }
예제 #16
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;
        }
예제 #17
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;
 }
예제 #18
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);
        }
예제 #19
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();
                }
            }
        }
예제 #20
0
        /// <summary>
        /// Shut down the socket, abandoning any outstainding requests
        /// </summary>
        public override void Close()
        {
            m_keepRunning = false;
            lock (m_queue)
            {
                m_queue.Clear();
                Monitor.Pulse(m_queue);
            }

            if (Connected)
                m_sock.Close();
            m_sock = null;
        }
예제 #21
0
 void ISocketEventListener.OnClose(BaseSocket sock)
 {
     m_sock = null;
     lock (m_lock)
     {
         Monitor.Pulse(m_lock);
     }
 }
예제 #22
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);
     }
 }
예제 #23
0
        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);
                }
            }
        }
예제 #24
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();
            }
        }