Esempio n. 1
0
        public void close()
        {
            DisconnectFunc disconnect_cb = null;

            lock (close_mutex)
            {
                if (!closed)
                {
                    closed = true;
                    if (poll_set != null)
                    {
                        poll_set.delSocket(sock);
                    }
                    if (sock.Connected)
                    {
                        sock.Shutdown(SocketShutdown.Both);
                    }
                    sock.Close();
                    sock               = null;
                    disconnect_cb      = this.disconnect_cb;
                    this.disconnect_cb = null;
                    read_cb            = null;
                    write_cb           = null;
                    accept_cb          = null;
                }
            }
            if (disconnect_cb != null)
            {
                disconnect_cb(this);
            }
        }
Esempio n. 2
0
        public bool connect(string host, int port)
        {
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connected_host = host;
            connected_port = port;
            if (!setNonBlocking())
                throw new Exception("Failed to make socket nonblocking");
            setNoDelay(true);
            IPAddress IPA = null;

            if (!IPAddress.TryParse(host, out IPA))
            {
                foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":")))
                {
                    IPA = ipa;
                    break;
                }
                if (IPA == null)
                {
                    close();
                    EDB.WriteLine("Couldn't resolve host name [{0}]", host);
                    return false;
                }
            }

            if (IPA == null)
                return false;

            IPEndPoint ipep = new IPEndPoint(IPA, port);
            LocalEndPoint = ipep;
            DateTime connectionAttempted = DateTime.Now;
            IAsyncResult asyncres;
            lock (this)
                asyncres = sock.BeginConnect(ipep, iar =>
                {
                    lock(this)
                        if (sock != null)
                            try
                            {
                                sock.EndConnect(iar);
                            }
                            catch (Exception e)
                            {
                                EDB.WriteLine(e);
                            }
                }, null);
            bool completed = false;
            while (ROS.ok && !ROS.shutting_down)
            {
#pragma warning disable 665
                if ((completed = asyncres.AsyncWaitHandle.WaitOne(10,false)))
#pragma warning restore 665
                    break;
                if (DateTime.Now.Subtract(connectionAttempted).TotalSeconds >= 3)
                {
                    EDB.WriteLine("TRYING TO CONNECT FOR " + DateTime.Now.Subtract(connectionAttempted).TotalSeconds + "s\t: " + this);
                    if (!asyncres.AsyncWaitHandle.WaitOne(100,true))
                    {
                        sock.Close();
                        sock = null;
                    }
                }
            }
            if (!completed || sock == null || !sock.Connected)
                return false;
            return ROS.ok && initializeSocket();
        }
Esempio n. 3
0
        public bool connect(string host, int port)
        {
            sock           = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connected_host = host;
            connected_port = port;
            if (!setNonBlocking())
            {
                throw new Exception("Failed to make socket nonblocking");
            }
            setNoDelay(true);
            IPAddress IPA = null;

            if (!IPAddress.TryParse(host, out IPA))
            {
                foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":")))
                {
                    IPA = ipa;
                    break;
                }
                if (IPA == null)
                {
                    close();
                    EDB.WriteLine("Couldn't resolve host name [{0}]", host);
                    return(false);
                }
            }

            if (IPA == null)
            {
                return(false);
            }

            IPEndPoint ipep = new IPEndPoint(IPA, port);

            LocalEndPoint = ipep;
            DateTime     connectionAttempted = DateTime.Now;
            IAsyncResult asyncres;

            lock (this)
                asyncres = sock.BeginConnect(ipep, iar =>
                {
                    lock (this)
                        if (sock != null)
                        {
                            try
                            {
                                sock.EndConnect(iar);
                            }
                            catch (Exception e)
                            {
                                EDB.WriteLine(e);
                            }
                        }
                }, null);
            bool completed = false;

            while (ROS.ok && !ROS.shutting_down)
            {
#pragma warning disable 665
                if ((completed = asyncres.AsyncWaitHandle.WaitOne(10, false)))
#pragma warning restore 665
                {
                    break;
                }
                if (DateTime.Now.Subtract(connectionAttempted).TotalSeconds >= 3)
                {
                    EDB.WriteLine("TRYING TO CONNECT FOR " + DateTime.Now.Subtract(connectionAttempted).TotalSeconds + "s\t: " + this);
                    if (!asyncres.AsyncWaitHandle.WaitOne(100, true))
                    {
                        sock.Close();
                        sock = null;
                    }
                }
            }
            if (!completed || sock == null || !sock.Connected)
            {
                return(false);
            }
            return(ROS.ok && initializeSocket());
        }