Exemplo n.º 1
0
        private void socketUpdate(int events)
        {
            ScopedTimer.Ping();
            lock (close_mutex)
            {
                if (closed)
                {
                    return;
                }
            }

            if (is_server)
            {
                TcpTransport transport = accept();
                if (transport != null)
                {
                    if (accept_cb == null)
                    {
                        throw new Exception("NULL ACCEPT_CB FTL!");
                    }
                    accept_cb(transport);
                }
            }
            else
            {
                if ((events & POLLIN) != 0 && expecting_read) //POLL IN FLAG
                {
                    if (read_cb != null)
                    {
                        read_cb(this);
                    }
                }

                if ((events & POLLOUT) != 0 && expecting_write)
                {
                    if (write_cb != null)
                    {
                        write_cb(this);
                    }
                }

                if ((events & POLLERR) != 0 || (events & POLLHUP) != 0 || (events & POLLNVAL) != 0)
                {
                    int error = 0;
                    try
                    {
                        error = (int)sock.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
                    }
                    catch (Exception e)
                    {
                        EDB.WriteLine("Failed to get sock options! (error: " + error + ")" + e);
                    }
                    if (error != 0)
                    {
                        EDB.WriteLine("SOCKET ERROR = " + error);
                    }
                    close();
                }
            }
        }
Exemplo n.º 2
0
        private bool onMessageLength(Connection conn, byte[] buffer, int size, bool success)
        {
            ScopedTimer.Ping();
            if (retry_timer != null)
            {
                ROS.timer_manager.RemoveTimer(ref retry_timer);
            }
            if (!success)
            {
                if (connection != null)
                {
                    connection.read(4, onMessageLength);
                }
                return(true);
            }
            if (conn != connection || size != 4)
            {
                return(false);
            }
            int len = BitConverter.ToInt32(buffer, 0);

            if (len > 1000000000)
            {
                EDB.WriteLine("TransportPublisherLink: 1 GB message WTF?!");
                drop();
                return(false);
            }
            connection.read(len, onMessage);
            return(true);
        }
Exemplo n.º 3
0
        public int write(byte[] buffer, int pos, int size)
        {
            ScopedTimer.Ping();
            lock (close_mutex)
            {
                if (closed)
                {
                    return(-1);
                }
            }
            SocketError err;
            //EDB.WriteLine(ByteDumpCondensed(buffer));
            int num_bytes = sock.Send(buffer, pos, size, SocketFlags.None, out err);

            if (num_bytes <= 0)
            {
                if (err == SocketError.TryAgain || err == SocketError.WouldBlock)
                {
                    num_bytes = 0;
                }
                else if (err != SocketError.InProgress && err != SocketError.IsConnected && err != SocketError.Success)
                {
                    close();
                    return(-1);
                }
                else
                {
                    return(0);
                }
            }
            return(num_bytes);
        }
Exemplo n.º 4
0
        public int read(byte[] buffer, int pos, int length)
        {
            ScopedTimer.Ping();
            lock (close_mutex)
            {
                if (closed)
                {
                    return(-1);
                }
            }
            int         num_bytes = 0;
            SocketError err;

            num_bytes = sock.realsocket.Receive(buffer, pos, length, SocketFlags.None, out err);
            if (num_bytes < 0)
            {
                if (err == SocketError.TryAgain || err == SocketError.WouldBlock)
                {
                    num_bytes = 0;
                }
                else if (err != SocketError.InProgress && err != SocketError.IsConnected && err != SocketError.Success)
                {
                    close();
                    num_bytes = -1;
                }
            }
            return(num_bytes);
        }
Exemplo n.º 5
0
 private bool onHeaderReceived(Connection conn, Header header)
 {
     ScopedTimer.Ping();
     if (conn != connection)
     {
         return(false);
     }
     if (!setHeader(header))
     {
         drop();
         return(false);
     }
     if (retry_timer != null)
     {
         ROS.timer_manager.RemoveTimer(ref retry_timer);
     }
     connection.read(4, onMessageLength);
     return(true);
 }
Exemplo n.º 6
0
 private bool onMessage(Connection conn, byte[] buffer, int size, bool success)
 {
     ScopedTimer.Ping();
     if (!success || conn == null || conn != connection)
     {
         return(false);
     }
     if (success)
     {
         IRosMessage msg = IRosMessage.generate(parent.msgtype);
         msg.Serialized        = buffer;
         msg.connection_header = getHeader().Values;
         handleMessage(msg, true, false);
     }
     if (success || !connection.transport.getRequiresHeader())
     {
         connection.read(4, onMessageLength);
     }
     return(true);
 }
Exemplo n.º 7
0
 private void writeTransport()
 {
     lock (writing)
     {
         if (dropped)
         {
             return;
         }
         ScopedTimer.Ping();
         bool can_write_more = true;
         while (write_callback != null && can_write_more && !dropped)
         {
             int to_write   = write_size - write_sent;
             int bytes_sent = transport.write(write_buffer, write_sent, to_write);
             if (bytes_sent <= 0)
             {
                 return;
             }
             write_sent += (int)bytes_sent;
             if (bytes_sent < write_size - write_sent)
             {
                 can_write_more = false;
             }
             if (write_sent == write_size && !dropped)
             {
                 lock (write_callback_mutex)
                 {
                     WriteFinishedFunc callback = write_callback;
                     write_callback = null;
                     write_buffer   = null;
                     write_sent     = 0;
                     write_size     = 0;
                     if (!callback(this))
                     {
                         Console.WriteLine("Failed to invoke " + callback.Method.Name);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 8
0
        private void readTransport()
        {
            lock (reading)
            {
                //EDB.WriteLine("READ - "+transport.poll_set);
                if (dropped)
                {
                    return;
                }
                ScopedTimer.Ping();
                ReadFinishedFunc callback;
                lock (read_callback_mutex)
                    callback = read_callback;
                int size;
                while (!dropped && callback != null)
                {
                    ScopedTimer.Ping();
                    int to_read = read_size - read_filled;
                    if (to_read > 0 && read_buffer == null)
                    {
                        throw new Exception("Trying to read " + to_read + " bytes with a null read_buffer!");
                    }
                    if (callback == null)
                    {
                        lock (read_callback_mutex)
                            callback = read_callback;
                    }
                    if (callback == null)
                    {
                        throw new Exception("Cannot determine which read_callback to invoke?!");
                    }
                    if (to_read > 0)
                    {
                        ScopedTimer.Ping();
                        int bytes_read = transport.read(read_buffer, read_filled, to_read);
                        if (dropped)
                        {
                            if (read_callback == null)
                            {
                                transport.disableRead();
                            }
                            break;
                        }
                        if (bytes_read < 0)
                        {
                            ScopedTimer.Ping();
                            read_callback = null;
                            byte[] buffer = read_buffer;
                            read_buffer = null;
                            size        = read_size;
                            read_size   = 0;
                            read_filled = 0;
#if BEGIN_INVOKE
                            callback.BeginInvoke(this, buffer, size, false, readTransportComplete, callback);
#else
                            if (!callback(this, buffer, size, false))
                            {
                                Console.WriteLine("Callbacks invoked by connection errored");
                            }
                            callback = null;
                            lock (read_callback_mutex)
                                if (read_callback == null)
                                {
                                    transport.disableRead();
                                }
#endif
                            break;
                        }
                        lock (read_callback_mutex)
                            callback = read_callback;
                        read_filled += bytes_read;
                    }
                    else
                    {
                        lock (read_callback_mutex)
                            if (read_callback == null)
                            {
                                transport.disableRead();
                            }
                        break;
                    }
                    if (read_filled == read_size && !dropped)
                    {
                        size = read_size;
                        byte[] buffer = read_buffer;
                        read_buffer = null;
                        lock (read_callback_mutex)
                            read_callback = null;
                        read_size = 0;
#if BEGIN_INVOKE
                        callback.BeginInvoke(this, buffer, size, true, readTransportComplete, callback);
#else
                        if (!callback(this, buffer, size, true))
                        {
                            Console.WriteLine("Callbacks invoked by connection errored");
                        }
                        lock (read_callback_mutex)
                            if (read_callback == null)
                            {
                                transport.disableRead();
                            }
#endif
                        callback = null;
                    }
                    else
                    {
                        lock (read_callback_mutex)
                            if (read_callback == null)
                            {
                                transport.disableRead();
                            }
                        break;
                    }
                }
            }
        }