Example #1
0
    def _tcp_server_callback(self, sock, client_addr):
        """
        TCPServer callback: detects incoming topic or service connection and passes connection accordingly
    
        @param sock: socket connection
        @type  sock: socket.socket
        @param client_addr: client address
        @type  client_addr: (str, int)
        @raise TransportInitError: If transport cannot be succesfully initialized
        """
        #TODOXXX:rewrite this logic so it is possible to create TCPROSTransport object first, set its protocol,
        #and then use that to do the writing
        try:
            buff_size = 4096  # size of read buffer
            if python3 == 0:
                #initialize read_ros_handshake_header with BytesIO for Python 3 (instead of bytesarray())
                header = read_ros_handshake_header(sock, StringIO(), buff_size)
            else:
                header = read_ros_handshake_header(sock, BytesIO(), buff_size)

            if 'topic' in header:
                err_msg = self.topic_connection_handler(
                    sock, client_addr, header)
            elif 'service' in header:
                err_msg = self.service_connection_handler(
                    sock, client_addr, header)
            else:
                err_msg = 'no topic or service name detected'
            if err_msg:
                # shutdown race condition: nodes that come up and down
                # quickly can receive connections during teardown.

                # We use is_shutdown_requested() because we can get
                # into bad connection states during client shutdown
                # hooks.
                if not rospy.core.is_shutdown_requested():
                    write_ros_handshake_header(sock, {'error': err_msg})
                    raise TransportInitError(
                        "Could not process inbound connection: " + err_msg +
                        str(header))
                else:
                    write_ros_handshake_header(sock,
                                               {'error': 'node shutting down'})
                    return
        except rospy.exceptions.TransportInitError as e:
            logwarn(str(e))
            if sock is not None:
                sock.close()
        except Exception as e:
            # collect stack trace separately in local log file
            if not rospy.core.is_shutdown_requested():
                logwarn("Inbound TCP/IP connection failed: %s", e)
                rospyerr("Inbound TCP/IP connection failed:\n%s",
                         traceback.format_exc(e))
            if sock is not None:
                sock.close()
Example #2
0
 def _tcp_server_callback(self, sock, client_addr):
     """
     TCPServer callback: detects incoming topic or service connection and passes connection accordingly
 
     @param sock: socket connection
     @type  sock: socket.socket
     @param client_addr: client address
     @type  client_addr: (str, int)
     @raise TransportInitError: If transport cannot be succesfully initialized
     """
     #TODOXXX:rewrite this logic so it is possible to create TCPROSTransport object first, set its protocol,
     #and then use that to do the writing
     try:
         buff_size = 4096 # size of read buffer
         if python3 == 0:
             #initialize read_ros_handshake_header with BytesIO for Python 3 (instead of bytesarray())    
             header = read_ros_handshake_header(sock, StringIO(), buff_size)
         else:
             header = read_ros_handshake_header(sock, BytesIO(), buff_size)
         
         if 'topic' in header:
             err_msg = self.topic_connection_handler(sock, client_addr, header)
         elif 'service' in header:
             err_msg = self.service_connection_handler(sock, client_addr, header)
         else:
             err_msg = 'no topic or service name detected'
         if err_msg:
             # shutdown race condition: nodes that come up and down
             # quickly can receive connections during teardown.
             
             # We use is_shutdown_requested() because we can get
             # into bad connection states during client shutdown
             # hooks.
             if not rospy.core.is_shutdown_requested():
                 write_ros_handshake_header(sock, {'error' : err_msg})
                 raise TransportInitError("Could not process inbound connection: "+err_msg+str(header))
             else:
                 write_ros_handshake_header(sock, {'error' : 'node shutting down'})
                 return
     except rospy.exceptions.TransportInitError as e:
         logwarn(str(e))
         if sock is not None:
             sock.close()
     except Exception as e:
         # collect stack trace separately in local log file
         if not rospy.core.is_shutdown_requested():
             logwarn("Inbound TCP/IP connection failed: %s", e)
             rospyerr("Inbound TCP/IP connection failed:\n%s", traceback.format_exc(e))
         if sock is not None:
             sock.close()
Example #3
0
 def write_header(self):
     """Writes the TCPROS header to the active connection."""
     sock = self.socket
     # socket may still be getting spun up, so wait for it to be writable
     fileno = sock.fileno()
     ready = None
     while not ready:
         _, ready, _ = select.select([], [fileno], [])
     logger.debug("[%s]: writing header", self.name)
     sock.setblocking(1)
     self.stat_bytes += write_ros_handshake_header(sock, self.protocol.get_header_fields())
Example #4
0
 def write_header(self):
     """Writes the TCPROS header to the active connection."""
     sock = self.socket
     # socket may still be getting spun up, so wait for it to be writable
     fileno = sock.fileno()
     ready = None
     while not ready:
         _, ready, _ = select.select([], [fileno], [])
     logger.debug("[%s]: writing header", self.name)
     sock.setblocking(1)
     self.stat_bytes += write_ros_handshake_header(
         sock, self.protocol.get_header_fields())
Example #5
0
 def _tcp_server_callback(self, sock, client_addr):
     """
     TCPServer callback: detects incoming topic or service connection and passes connection accordingly
 
     @param sock: socket connection
     @type  sock: socket.socket
     @param client_addr: client address
     @type  client_addr: (str, int)
     @raise TransportInitError: If transport cannot be succesfully initialized
     """
     #TODOXXX:rewrite this logic so it is possible to create TCPROSTransport object first, set its protocol,
     #and then use that to do the writing
     try:
         buff_size = 4096  # size of read buffer
         header = read_ros_handshake_header(sock, cStringIO.StringIO(),
                                            buff_size)
         if 'topic' in header:
             err_msg = self.topic_connection_handler(
                 sock, client_addr, header)
         elif 'service' in header:
             err_msg = self.service_connection_handler(
                 sock, client_addr, header)
         else:
             err_msg = 'no topic or service name detected'
         if err_msg:
             # shutdown race condition: nodes that come up and down quickly can receive connections during teardown
             if not rospy.core.is_shutdown():
                 write_ros_handshake_header(sock, {'error': err_msg})
                 raise TransportInitError(
                     "Could not process inbound connection: " + err_msg +
                     str(header))
             else:
                 write_ros_handshake_header(sock,
                                            {'error': 'node shutting down'})
                 return
     except rospy.exceptions.TransportInitError, e:
         logwarn(str(e))
         if sock is not None:
             sock.close()