def _receive_message(self): # Reads expected message length, reading # MESSAGE_LENGTH_DESCRIPTOR_LENGTH bytes msg_length = '' while len(msg_length) < self.MESSAGE_LENGTH_DESCRIPTOR_LENGTH: chunk = self.sock.recv( self.MESSAGE_LENGTH_DESCRIPTOR_LENGTH - len(msg_length)) if len(chunk) == 0: raise ConnectionException("Server has closed the connection.") msg_length += chunk msg_length = msg_length.strip() msg_length = int(msg_length) # Reads msg_length bytes msg = '' while len(msg) < msg_length: chunk = self.sock.recv(msg_length - len(msg)) if len(chunk) == 0: raise ConnectionException("Server has closed the connection.") msg += chunk msg = unpack(msg) if msg.name != 'KEEP_ALIVE': # self.logger.debug(u"Received message %r", msg) pass return msg
def _receive_message(self): # Reads expected message length, reading # MESSAGE_LENGTH_DESCRIPTOR_LENGTH bytes msg_length = '' while len(msg_length) < self.MESSAGE_LENGTH_DESCRIPTOR_LENGTH: chunk = self.sock.recv(self.MESSAGE_LENGTH_DESCRIPTOR_LENGTH - len(msg_length)) if len(chunk) == 0: raise ConnectionException("Server has closed the connection.") msg_length += chunk msg_length = msg_length.strip() msg_length = int(msg_length) # Reads msg_length bytes msg = '' while len(msg) < msg_length: chunk = self.sock.recv(msg_length - len(msg)) if len(chunk) == 0: raise ConnectionException("Server has closed the connection.") msg += chunk msg = unpack(msg) if msg.name != 'KEEP_ALIVE': # self.logger.debug(u"Received message %r", msg) pass return msg
def _receive_message(self): """Listens on socket for MSG_LENGTH and MSG_DATA and returns a Message object. Please note that potentially raise BadMessageLengthException or MsgUnpackingException. Who calls receive_message should catch such exceptions. """ msg_length = \ self.sock.recv(self.message_length_descriptor_length).strip() if not len(msg_length): raise BrokenPipeException() try: msg_length = int(msg_length) except ValueError: raise ProtocolException() msg = self.sock.recv(msg_length) if not len(msg): raise BrokenPipeException() return unpack(msg)