Esempio n. 1
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. 2
0
        public bool connect(string host, int port)
        {
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connected_host = host;
            connected_port = port;

            setNonBlocking();

            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;
            ManualResetEvent connectDone = new ManualResetEvent(false);

            sock.BeginConnect(ipep, (iar) =>
            {
                try
                {
                    sock.EndConnect(iar);
                    connectDone.Set();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }, null);
            bool completed = false;
            while (ROS.ok && !ROS.shutting_down)
            {
#pragma warning disable 665
                if ((completed = connectDone.WaitOne(10)))
#pragma warning restore 665
                    break;
            }
            if (!completed || !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());
        }
Esempio n. 4
0
        public bool connect(string host, int port)
        {
            sock           = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connected_host = host;
            connected_port = port;

            setNonBlocking();

            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;
            ManualResetEvent connectDone = new ManualResetEvent(false);

            sock.BeginConnect(ipep, iar =>
            {
                try
                {
                    sock.EndConnect(iar);
                    connectDone.Set();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }, null);
            bool completed = false;

            while (ROS.ok && !ROS.shutting_down)
            {
#pragma warning disable 665
                if ((completed = connectDone.WaitOne(10)))
#pragma warning restore 665
                {
                    break;
                }
            }
            if (!completed || !sock.Connected)
            {
                return(false);
            }
            return(ROS.ok && initializeSocket());
        }