def parse_messages(sock, mode): buf = b"" partial_msg = [] pos = 0 chunk_length = 0 while True: r = sock.recv(1024) if not r: return buf += r if mode == "1.0": (msgs, buf, pos) = parse_messages_10_from_buf(buf, pos, len(r)) elif mode == "1.1": (msgs, buf, partial_msg, chunk_length) = parse_messages_11_from_buf( buf, partial_msg, chunk_length ) else: raise NotImplementedError( "Unsupported message framing mode {}".format(mode) ) for msg in msgs: logger.debug("Received message: %s", msg) new_mode = yield msg if new_mode is not None and new_mode != mode: logger.debug("Updating parsing mode to %s", new_mode) mode = new_mode pos = 0 del partial_msg[:] chunk_length = 0
def send_msg(self, msg): """Sends a raw byte string to the server :param bytes msg: The byte string to send """ logger.debug("Sending message on session %s", msg) if self.mode == "1.0": self.sock.sendall(msg + b"]]>]]>") elif self.mode == "1.1": self.sock.sendall(frame_message_11(msg))
def parse_messages(sock, mode): buf = b"" while True: r = sock.recv(1024) if not r: return buf += r if mode == "1.0": (msgs, buf) = parse_messages_10_from_buf(buf) elif mode == "1.1": (msgs, buf) = parse_messages_11_from_buf(buf) else: raise NotImplementedError( "Unsupported message framing mode {}".format(mode)) for msg in msgs: logger.debug("Received message: %s", msg) new_mode = yield msg if new_mode is not None and new_mode != mode: logger.debug("Updating parsing mode to %s", new_mode) mode = new_mode