def _send_control_message(self, player, message: Message): msg = a0070().set([ m009e().set(MESSAGE_CONTROL), m02e6().set(message.to_string()), m034a().set(player.display_name), m0574(), m02fe().set('taserver'), m06de().set('bot') ]) player.send(msg)
def dispatch_client(self): try: while True: # Считать длинну сообщения message_length = yield self._stream.read_bytes(2) # Распоковать length = struct.unpack("!H", message_length)[0] # Считать сообщение message = yield self._stream.read_bytes(length) # Распоковать запрос request = Message.unpack(message=message) # Создать обработчик запросов handler = self._handler(request, self._id, self._storage) # Обработать запрос response = yield handler.process_request() # Вернуть ответ if response: response = Message(response[0], kwargs=response[1]) yield self._stream.write(response.pack()) except iostream.StreamClosedError: pass
def _notification(self): while self._running: try: message_length = yield self._stream.read_bytes(2) length = struct.unpack("!H", message_length)[0] message = yield self._stream.read_bytes(length) request = Message.unpack(message) handler = self.notification_handler(request, self) handler.process_request() except OSError as e: print_(e) self.stop() except Exception as e: print_(e) self.stop()
def test_pack(self): r = Message("LOGIN", kwargs={"username": "******"}) packing = r.pack() self.assertIsInstance(packing, bytes)
def test_unpack(self): message = b"LOGIN\ngAN9cQBYCAAAAHVzZXJuYW1lcQFYBAAAAHRlc3RxAnMu" r = Message.unpack(message) self.assertEqual(r.command, "LOGIN") self.assertIsInstance(r.kwargs, dict) self.assertDictEqual(r.kwargs, {'username': "******"})
def write_msg(self, msg: m.Message): self.transport.write(msg.to_json().encode() + self.delimiter)
def write_msg_no_print(self, msg: m.Message): line = msg.to_json().encode() + self.delimiter self.transport.write(line)
def write_msg(self, msg: m.Message): line = msg.to_json().encode() + self.delimiter print(f"> gm server: Sending \"{line}\"") self.transport.write(line)