Esempio n. 1
0
 public bool delSocket(Socket s)
 {
     lock (socks)
         socks.Remove(s.FD);
     s.Dispose();
     return(true);
 }
Esempio n. 2
0
 public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans)
 {
     s.Info = new SocketInfo { sock = s.FD, func = update_func, transport = trans };
     lock (socks)
         socks.Add(s.FD, s);
     return true;
 }
Esempio n. 3
0
 public bool delSocket(Socket s)
 {
     lock (socks)
         socks.Remove(s.FD);
     s.Dispose();
     return true;
 }
Esempio n. 4
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. 5
0
 public bool delEvents(Socket s, int events)
 {
     if (s != null && s.Info != null)
     {
         s.Info.events &= ~events;
     }
     return(true);
 }
Esempio n. 6
0
 public bool addEvents(Socket s, int events)
 {
     if (s != null && s.Info != null)
     {
         s.Info.events |= events;
     }
     return(true);
 }
Esempio n. 7
0
 public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans)
 {
     s.Info = new SocketInfo {
         sock = s.FD, func = update_func, transport = trans
     };
     lock (socks)
         socks.Add(s.FD, s);
     return(true);
 }
Esempio n. 8
0
 public PollSet()
 {
     localpipeevents[0] = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     localpipeevents[0].Bind(new IPEndPoint(IPAddress.Loopback, 0));
     localpipeevents[1] = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     localpipeevents[1].Connect(localpipeevents[0].LocalEndPoint);
     localpipeevents[0].Connect(localpipeevents[1].LocalEndPoint);
     localpipeevents[0].Blocking = false;
     localpipeevents[1].Blocking = false;
     addSocket(localpipeevents[0], onLocalPipeEvents);
     addEvents(localpipeevents[0].FD, POLLIN);
 }
Esempio n. 9
0
 public PollSet()
 {
     localpipeevents[0] = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     localpipeevents[0].Bind(new IPEndPoint(IPAddress.Loopback, 0));
     localpipeevents[1] = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     localpipeevents[1].Connect(localpipeevents[0].LocalEndPoint);
     localpipeevents[0].Connect(localpipeevents[1].LocalEndPoint);
     localpipeevents[0].Blocking = false;
     localpipeevents[1].Blocking = false;
     addSocket(localpipeevents[0], onLocalPipeEvents);
     addEvents(localpipeevents[0].FD, POLLIN);
 }
Esempio n. 10
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;
            if (!sock.ConnectAsync(new SocketAsyncEventArgs {
                RemoteEndPoint = ipep
            }))
            {
                return(false);
            }

            cached_remote_host = "" + host + ":" + port + " on socket " + sock;


            while (ROS.ok && !sock.Connected)
            {
            }
            if (!ROS.ok || !initializeSocket())
            {
                return(false);
            }
            return(true);
        }
Esempio n. 11
0
        private bool setKeepAlive(Socket sock, ulong time, ulong interval, ulong count)
        {
            try
            {
                // resulting structure
                byte[] SIO_KEEPALIVE_VALS = new byte[3 * bytesperlong];

                // array to hold input values
                ulong[] input = new ulong[4];

                // put input arguments in input array
                if (time == 0 || interval == 0) // enable disable keep-alive
                {
                    input[0] = (0UL);           // off
                }
                else
                {
                    input[0] = (1UL);  // on
                }
                input[1] = (time);     // time millis
                input[2] = (interval); // interval millis
                input[3] = count;
                // pack input into byte struct
                for (int i = 0; i < input.Length; i++)
                {
                    SIO_KEEPALIVE_VALS[i * bytesperlong + 3] =
                        (byte)(input[i] >> ((bytesperlong - 1) * bitsperbyte) & 0xff);
                    SIO_KEEPALIVE_VALS[i * bytesperlong + 2] =
                        (byte)(input[i] >> ((bytesperlong - 2) * bitsperbyte) & 0xff);
                    SIO_KEEPALIVE_VALS[i * bytesperlong + 1] =
                        (byte)(input[i] >> ((bytesperlong - 3) * bitsperbyte) & 0xff);
                    SIO_KEEPALIVE_VALS[i * bytesperlong + 0] =
                        (byte)(input[i] >> ((bytesperlong - 4) * bitsperbyte) & 0xff);
                }
                // create bytestruct for result (bytes pending on server socket)
                byte[] result = BitConverter.GetBytes(0);
                // write SIO_VALS to Socket IOControl
                sock.IOControl(IOControlCode.KeepAliveValues, SIO_KEEPALIVE_VALS, result);

                ByteDump(result);
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 12
0
        public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans)
        {
            SocketInfo info = new SocketInfo {
                sock = s.FD, func = update_func, transport = trans
            };

            lock (socket_info_mutex)
            {
                if (socket_info.ContainsKey(info.sock))
                {
                    return(false);
                }
                socket_info.Add(info.sock, info);
                sockets_changed = true;
            }
            signal();
            return(true);
        }
Esempio n. 13
0
 public bool delSocket(Socket s)
 {
     lock (socket_info_mutex)
     {
         uint fd = s.FD;
         if (!socket_info.ContainsKey(fd))
         {
             return(false);
         }
         socket_info.Remove(fd);
         lock (just_deleted_mutex)
         {
             just_deleted.Add(s);
         }
         sockets_changed = true;
     }
     signal();
     return(true);
 }
Esempio n. 14
0
        public bool listen(int port, int backlog, AcceptCallback accept_cb)
        {
            is_server      = true;
            this.accept_cb = accept_cb;

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            setNonBlocking();
            sock.Bind(new IPEndPoint(IPAddress.Any, port));
            server_port = ((IPEndPoint)sock.LocalEndPoint).Port;
            sock.Listen(backlog);
            if (!initializeSocket())
            {
                return(false);
            }
            if ((flags & (int)Flags.SYNCHRONOUS) == 0)
            {
                enableRead();
            }
            return(true);
        }
Esempio n. 15
0
        public TcpTransport accept()
        {
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();

            if (sock == null || !sock.AcceptAsync(args))
            {
                return(null);
            }
            if (args.AcceptSocket == null)
            {
                EDB.WriteLine("NOTHING TO ACCEPT SO RETURNING NULL!");
                return(null);
            }
            Socket       acc       = new Socket(args.AcceptSocket);
            TcpTransport transport = new TcpTransport(poll_set, flags);

            if (!transport.setSocket(acc))
            {
                throw new Exception("FAILED TO ADD SOCKET TO TRANSPORT ZOMG!");
            }
            return(transport);
        }
Esempio n. 16
0
 private bool setSocket(Socket s)
 {
     sock = s;
     return initializeSocket();
 }
Esempio n. 17
0
 public TcpTransport accept()
 {
     SocketAsyncEventArgs args = new SocketAsyncEventArgs();
     if (sock == null || !sock.AcceptAsync(args))
         return null;
     if (args.AcceptSocket == null)
     {
         EDB.WriteLine("NOTHING TO ACCEPT SO RETURNING NULL!");
         return null;
     }
     Socket acc = new Socket(args.AcceptSocket);
     TcpTransport transport = new TcpTransport(poll_set, flags);
     if (!transport.setSocket(acc))
     {
         throw new Exception("FAILED TO ADD SOCKET TO TRANSPORT ZOMG!");
     }
     return transport;
 }
Esempio n. 18
0
        public bool listen(int port, int backlog, AcceptCallback accept_cb)
        {
            is_server = true;
            this.accept_cb = accept_cb;

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            setNonBlocking();
            sock.Bind(new IPEndPoint(IPAddress.Any, port));
            server_port = ((IPEndPoint) sock.LocalEndPoint).Port;
            sock.Listen(backlog);
            if (!initializeSocket())
                return false;
            if ((flags & (int)Flags.SYNCHRONOUS) == 0)
                enableRead();
            return true;
        }
Esempio n. 19
0
        private bool setKeepAlive(Socket sock, ulong time, ulong interval, ulong count)
        {
            try
            {
                // resulting structure
                byte[] SIO_KEEPALIVE_VALS = new byte[3*bytesperlong];

                // array to hold input values
                ulong[] input = new ulong[4];

                // put input arguments in input array
                if (time == 0 || interval == 0) // enable disable keep-alive
                    input[0] = (0UL); // off
                else
                    input[0] = (1UL); // on

                input[1] = (time); // time millis
                input[2] = (interval); // interval millis
                input[3] = count;
                // pack input into byte struct
                for (int i = 0; i < input.Length; i++)
                {
                    SIO_KEEPALIVE_VALS[i*bytesperlong + 3] =
                        (byte) (input[i] >> ((bytesperlong - 1)*bitsperbyte) & 0xff);
                    SIO_KEEPALIVE_VALS[i*bytesperlong + 2] =
                        (byte) (input[i] >> ((bytesperlong - 2)*bitsperbyte) & 0xff);
                    SIO_KEEPALIVE_VALS[i*bytesperlong + 1] =
                        (byte) (input[i] >> ((bytesperlong - 3)*bitsperbyte) & 0xff);
                    SIO_KEEPALIVE_VALS[i*bytesperlong + 0] =
                        (byte) (input[i] >> ((bytesperlong - 4)*bitsperbyte) & 0xff);
                }
                // create bytestruct for result (bytes pending on server socket)
                byte[] result = BitConverter.GetBytes(0);
                // write SIO_VALS to Socket IOControl
                sock.IOControl(IOControlCode.KeepAliveValues, SIO_KEEPALIVE_VALS, result);

                ByteDump(result);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
Esempio n. 20
0
        public void update(int poll_timeout)
        {
            createNativePollSet();
            int udfscount = ufds.Count;

            //int ret = 0;
            for (int i = 0; i < ufds.Count; i++)
            {
                Socket sock = Socket.Get(ufds[i].sock);
                if (sock == null || !sock.Connected)
                {
                    ufds[i].revents |= POLLHUP;
                }
                else if (sock == null || sock.SafePoll(poll_timeout, SelectMode.SelectError))
                {
                    ufds[i].revents |= POLLERR;
                }
                else
                {
                    if (sock != null && sock.SafePoll(poll_timeout, SelectMode.SelectWrite))
                    {
                        ufds[i].revents |= POLLOUT;
                    }
                    if (sock != null && sock.SafePoll(poll_timeout, SelectMode.SelectRead))
                    {
                        ufds[i].revents |= POLLIN;
                    }
                }
            }
            if (udfscount == 0)
            {
                return;
            }
            for (int i = 0; i < udfscount; i++)
            {
                if (ufds[i].revents == 0)
                {
                    continue;
                }

                SocketUpdateFunc func = null;
                int events            = 0;
                lock (socket_info_mutex)
                {
                    if (!socket_info.ContainsKey(ufds[i].sock))
                    {
                        continue;
                    }
                    SocketInfo info = socket_info[ufds[i].sock];
                    func   = info.func;
                    events = info.events;
                }

                int revents = ufds[i].revents;

                if (func != null &&
                    ((events & revents) != 0 || (revents & POLLERR) != 0 || (revents & POLLHUP) != 0 ||
                     (revents & POLLNVAL) != 0))
                {
                    bool skip = false;
                    if ((revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
                    {
                        lock (just_deleted_mutex)
                        {
                            if (just_deleted.Contains(Socket.Get(ufds[i].sock)))
                            {
                                skip = true;
                            }
                        }
                    }

                    if (!skip)
                    {
                        func(revents & (events | POLLERR | POLLHUP | POLLNVAL));
                    }
                }

                ufds[i].revents = 0;
            }

            lock (just_deleted_mutex)
            {
                just_deleted.Clear();
            }
        }
Esempio n. 21
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. 22
0
 public bool delSocket(Socket s)
 {
     lock (socket_info_mutex)
     {
         uint fd = s.FD;
         if (!socket_info.ContainsKey(fd))
             return false;
         socket_info.Remove(fd);
         lock (just_deleted_mutex)
         {
             just_deleted.Add(s);
         }
         sockets_changed = true;
     }
     signal();
     return true;
 }
Esempio n. 23
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. 24
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. 25
0
 private bool setSocket(Socket s)
 {
     sock = s;
     return(initializeSocket());
 }
Esempio n. 26
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. 27
0
 public bool addSocket(Socket s, SocketUpdateFunc update_func)
 {
     return(addSocket(s, update_func, null));
 }
Esempio n. 28
0
 public bool delEvents(Socket s, int events)
 {
     if (s != null && s.Info != null)
         s.Info.events &= ~events;
     return true;
 }
Esempio n. 29
0
 public bool addEvents(Socket s, int events)
 {
     if (s != null && s.Info != null)
         s.Info.events |= events;
     return true;
 }
Esempio n. 30
0
 public bool addSocket(Socket s, SocketUpdateFunc update_func)
 {
     return addSocket(s, update_func, null);
 }
Esempio n. 31
0
 public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans)
 {
     SocketInfo info = new SocketInfo {sock = s.FD, func = update_func, transport = trans};
     lock (socket_info_mutex)
     {
         if (socket_info.ContainsKey(info.sock))
             return false;
         socket_info.Add(info.sock, info);
         sockets_changed = true;
     }
     signal();
     return true;
 }
Esempio n. 32
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());
        }