make client.run() block and run from sync
This commit is contained in:
parent
1d6916932b
commit
1b72f94043
2 changed files with 11 additions and 16 deletions
|
@ -1,6 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .mc.proto.play.clientbound import PacketChat
|
from .mc.proto.play.clientbound import PacketChat
|
||||||
|
@ -29,9 +28,6 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
# TODO rework how this is started! Maybe implement client context manager?
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
client = Client(host, port, username=username, password=pwd)
|
client = Client(host, port, username=username, password=pwd)
|
||||||
|
|
||||||
@client.on_packet(PacketChat, ConnectionState.PLAY)
|
@client.on_packet(PacketChat, ConnectionState.PLAY)
|
||||||
|
@ -39,13 +35,7 @@ if __name__ == "__main__":
|
||||||
msg = parse_chat(json.loads(packet.message), color=color)
|
msg = parse_chat(json.loads(packet.message), color=color)
|
||||||
print(f"[{packet.position}] {msg}")
|
print(f"[{packet.position}] {msg}")
|
||||||
|
|
||||||
loop.run_until_complete(client.start())
|
client.run() # will block and start asyncio event loop
|
||||||
|
|
||||||
try:
|
|
||||||
loop.run_until_complete(idle())
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
logging.info("Received SIGINT, stopping...")
|
|
||||||
loop.run_until_complete(client.stop())
|
|
||||||
|
|
||||||
logging.info("Terminated")
|
logging.info("Terminated")
|
||||||
|
|
||||||
|
|
|
@ -120,18 +120,23 @@ class Client:
|
||||||
if restart:
|
if restart:
|
||||||
await self.start()
|
await self.start()
|
||||||
|
|
||||||
async def run(self):
|
def run(self):
|
||||||
await self.start()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
try:
|
loop.run_until_complete(self.start())
|
||||||
|
|
||||||
|
async def idle():
|
||||||
while self._processing: # TODO don't busywait even if it doesn't matter much
|
while self._processing: # TODO don't busywait even if it doesn't matter much
|
||||||
await asyncio.sleep(self.options["poll-timeout"])
|
await asyncio.sleep(self.options["poll-timeout"])
|
||||||
|
|
||||||
|
try:
|
||||||
|
loop.run_forever(idle())
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self._logger.info("Received SIGINT, stopping...")
|
self._logger.info("Received SIGINT, stopping...")
|
||||||
else:
|
else:
|
||||||
self._logger.warning("Client terminating...")
|
self._logger.warning("Client terminating...")
|
||||||
|
|
||||||
await self.stop()
|
loop.run_until_complete(self.stop())
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
self._processing = True
|
self._processing = True
|
||||||
|
@ -243,7 +248,7 @@ class Client:
|
||||||
elif isinstance(packet, PacketKickDisconnect):
|
elif isinstance(packet, PacketKickDisconnect):
|
||||||
self._logger.error("Kicked while in game")
|
self._logger.error("Kicked while in game")
|
||||||
break
|
break
|
||||||
for packet_type in (Packet, packet.__class__): # check both callbacks for base class and instance class
|
for packet_type in (Packet, type(packet)): # check both callbacks for base class and instance class
|
||||||
if packet_type in self._packet_callbacks:
|
if packet_type in self._packet_callbacks:
|
||||||
for cb in self._packet_callbacks[packet_type]:
|
for cb in self._packet_callbacks[packet_type]:
|
||||||
try: # TODO run in executor to not block
|
try: # TODO run in executor to not block
|
||||||
|
|
Loading…
Reference in a new issue