示例#1
0
    def _on_read(self, future):
        """ Appends bytes read from socket to a buffer. Once the full packet
        has been read the parser is invoked and the buffers is cleared. The
        parsed packet is then passed to the suitable method or dropped if the
        packet type in unknown. """

        try:
            line = future.result()
            self.__buffer = self.__buffer + line
        except StreamClosedError as stream_ex:
            self.log.error(stream_ex)
            return

        hdr = HEADER.parse(self.__buffer)

        if len(self.__buffer) < hdr.length:
            remaining = hdr.length - len(self.__buffer)
            future = self.stream.read_bytes(remaining)
            future.add_done_callback(self._on_read)
            return

        try:
            self._trigger_message(hdr)
        except Exception as ex:
            self.log.exception(ex)
            self.stream.close()

        if not self.stream.closed():
            self._wait()
示例#2
0
    def _on_read(self, line):
        """ Appends bytes read from socket to a buffer. Once the full packet
        has been read the parser is invoked and the buffers is cleared. The
        parsed packet is then passed to the suitable method or dropped if the
        packet type in unknown. """

        self.__buffer = self.__buffer + line
        hdr = HEADER.parse(self.__buffer)

        if len(self.__buffer) < hdr.length:
            remaining = hdr.length - len(self.__buffer)
            self.stream.read_bytes(remaining, self._on_read)
            return

        try:
            self._trigger_message(hdr)
        except Exception as ex:
            LOG.exception(ex)
            self.stream.close()

        if not self.stream.closed():
            self._wait()