Exemplo n.º 1
0
 def _parse_message(self):
     if self._size is None:
         if len(self._data) >= 4:
             header_data = self._data[:4]
             self._data = self._data[4:]
             header_value = deserialize_uint32(header_data)
             if header_value == 0xFFFFFFFF:
                 # keep-alive request
                 self._conn.write(serialize_uint32(0xFFFFFFFE))
                 return True
             elif header_value == 0xFFFFFFFE:
                 # keep-alive response
                 return True
             self._size = header_value
             if self._size > self._max_message_size:
                 template = ("received message size %s is greater than " +
                             "maximum message size %s")
                 error_msg = template % (self._size, self._max_message_size)
                 error = MessageTooLargeError(error_msg)
                 self.close()
                 safe_callback(self._error_callback, error)
                 return False
     if self._size is not None:
         if len(self._data) >= self._size:
             message = self._data[:self._size]
             self._data = self._data[self._size:]
             self._size = None
             safe_callback(self._message_callback, message)
             return True
     return False
Exemplo n.º 2
0
class TCPReader(object) :
    def __init__(self, socket, events, receive_callback) :
        self._socket = socket
        self._events = events
        self._receive_callback = receive_callback
        self._receiving = False
        self._kickstarting = False
        self._read_event_registered = False

    def start_receive(self) :
        assert not self._receiving
        self._receiving = True
        assert not self._read_event_registered
        self._kickstart()

    def stop_receive(self) :
        assert self._receiving
        self._receiving = False
        self._unregister_read_event()

    def cleanup(self) :
        if self._receiving :
            self.stop_receive()

    def _kickstart(self) :
        if self._kickstarting :
            return
        self._kickstarting = True
        def callback() :
            self._kickstarting = False
            if not self._receiving :
                return
            self._perform_read()
        self._events.io_loop().add_callback(callback)

    def _perform_read(self) :
        data_list = []
        close_received = False
        try :
            while True :
                data = self._socket.recv(65536)
                if not data :
                    close_received = True
                    break
                data_list.append(data)
        except socket.error, e :
            if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN) :
                self._events.notify_error(e)
                return
            self._register_read_event()
        data = "".join(data_list)
        if data :
            safe_callback(self._receive_callback, data)
            if not self._receiving :
                return
        if close_received :
            safe_callback(self._receive_callback, "")
            if self._receiving :
                self.stop_receive()
Exemplo n.º 3
0
 def _read_handler(self, fd, events) :
     unused(fd)
     if events & self._io_loop.READ :
         n = self._max_receive_size
         while self._socket is not None :
             try :
                 data, from_end_point = self._socket.recvfrom(n)
             except socket.error, e :
                 if is_blocking_error(e) :
                     break
             safe_callback(self._receive_callback, data, from_end_point)
Exemplo n.º 4
0
 def _on_data_received(self, data):
     if len(data) == 0:
         # eof received
         self.close()
         error = RemoteClosedError("remote end closed")
         safe_callback(self._error_callback, error)
         return
     self._data += data
     while self._conn is not None:
         if not self._parse_message():
             break
Exemplo n.º 5
0
 def _ready_for_accept(self):
     while self._accepting:
         try:
             client_socket, client_address = self._socket.accept()
         except socket.error, e:
             if e.args[0] in [errno.EWOULDBLOCK, errno.EAGAIN]:
                 return
             else:
                 safe_callback(self._error_callback, e)
                 return
         socket_set_blocking(client_socket, False)
         safe_callback(self._accept_callback, client_socket, client_address)
Exemplo n.º 6
0
 def _on_keepalive_timer(self):
     assert self._conn is not None
     assert self._keepalive_time is not None
     self._keepalive_timer = None
     self._keepalive_timer = self._io_loop.add_timeout(
         time.time() + self._keepalive_time, self._on_keepalive_timer)
     self._keepalive_attempts += 1
     self._conn.write(serialize_uint32(0xFFFFFFFF))
     if self._keepalive_attempts > self._keepalive_max_attempts:
         error_msg = "keepalive failed"
         error = KeepaliveFailedError(error_msg)
         self.close()
         safe_callback(self._error_callback, error)
Exemplo n.º 7
0
 def _event_handler(self, fd, events) :
     unused(fd)
     if events & self._io_loop.ERROR :
         error = socket_error(self._socket)
         assert error is not None
         safe_callback(self._error_callback, error)
         return
     if events & self._io_loop.READ :
         if self._read_callback is not None :
             safe_callback(self._read_callback)
     if events & self._io_loop.WRITE :
         if self._write_callback is not None :
             safe_callback(self._write_callback)
     if self._activity_callback is not None :
         safe_callback(self._activity_callback)
Exemplo n.º 8
0
 def _event_handler(self, fd, events) :
     unused(fd)
     if self._socket is None :
         return
     self._io_loop.remove_handler(self._socket.fileno())
     self._handler_added = False
     if events & self._io_loop.ERROR :
         error = socket_error(self._socket)
         assert error is not None
         self._close_socket()
         safe_callback(self._error_callback, error)
         return
     if events & self._io_loop.WRITE :
         error = socket_error(self._socket)
         if error is not None :
             self._close_socket()
             safe_callback(self._error_callback, error)
             return
         sock = self._socket
         self._socket = None
         safe_callback(self._connect_callback, sock)
Exemplo n.º 9
0
 def notify_error(self, error) :
     if self._error_callback is not None :
         safe_callback(self._error_callback, error)
Exemplo n.º 10
0
 def _on_error(self, error) :
     self.close()
     safe_callback(self._error_callback, error)
Exemplo n.º 11
0
                if e.args[0] in [errno.EWOULDBLOCK, errno.EAGAIN] :
                    self._queue.append(data)
                    return
                self._error_occurred = True
                self._events.notify_error(e)
                return
        self._unregister_write_event()
        if self._shutdown_requested :
            try :
                self._socket.shutdown(socket.SHUT_WR)
            except socket.error, e :
                self._error_occurred = True
                self._events.notify_error(e)
                return
        if self._queue_emptied_callback is not None :
            safe_callback(self._queue_emptied_callback)


#
# TCPReader
#

class TCPReader(object) :
    def __init__(self, socket, events, receive_callback) :
        self._socket = socket
        self._events = events
        self._receive_callback = receive_callback
        self._receiving = False
        self._kickstarting = False
        self._read_event_registered = False
Exemplo n.º 12
0
 def notify_error() :
     if self._socket is None :
         return
     self._close_socket()
     safe_callback(self._error_callback, e)
Exemplo n.º 13
0
 def _on_error(self, error):
     # TCPConnection is already closed
     self.disable_keepalive()
     self._conn = None
     safe_callback(self._error_callback, error)