Skip to content

Commit

Permalink
Speed up client shutdown by un-sleeping the sync task
Browse files Browse the repository at this point in the history
The sync task now also un-sleeps whenever ReceivedItems or LocationInfo
are received.
  • Loading branch information
Mysteryem committed Feb 3, 2025
1 parent 72c42cb commit d568a8c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions worlds/tww/TWWClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,23 @@ async def dolphin_sync_task(ctx: TWWContext) -> None:
:param ctx: The Wind Waker client context.
"""
logger.info("Starting Dolphin connector. Use /dolphin for status information.")
sleep_time = 0.0
while not ctx.exit_event.is_set():
if sleep_time > 0.0:
try:
# ctx.watcher_event gets set when receiving ReceivedItems or LocationInfo, or when shutting down.
await asyncio.wait_for(ctx.watcher_event.wait(), sleep_time)
except asyncio.TimeoutError:
pass
sleep_time = 0.0
ctx.watcher_event.clear()

try:
if dolphin_memory_engine.is_hooked() and ctx.dolphin_status == CONNECTION_CONNECTED_STATUS:
if not check_ingame():
# Reset the give item array while not in the game.
dolphin_memory_engine.write_bytes(GIVE_ITEM_ARRAY_ADDR, bytes([0xFF] * ctx.len_give_item_array))
await asyncio.sleep(0.1)
sleep_time = 0.1
continue
if ctx.slot is not None:
if "DeathLink" in ctx.tags:
Expand All @@ -658,7 +668,7 @@ async def dolphin_sync_task(ctx: TWWContext) -> None:
ctx.auth = read_string(SLOT_NAME_ADDR, 0x40)
if ctx.awaiting_rom:
await ctx.server_auth()
await asyncio.sleep(0.1)
sleep_time = 0.1
else:
if ctx.dolphin_status == CONNECTION_CONNECTED_STATUS:
logger.info("Connection to Dolphin lost, reconnecting...")
Expand All @@ -670,7 +680,7 @@ async def dolphin_sync_task(ctx: TWWContext) -> None:
logger.info(CONNECTION_REFUSED_GAME_STATUS)
ctx.dolphin_status = CONNECTION_REFUSED_GAME_STATUS
dolphin_memory_engine.un_hook()
await asyncio.sleep(5)
sleep_time = 5
else:
logger.info(CONNECTION_CONNECTED_STATUS)
ctx.dolphin_status = CONNECTION_CONNECTED_STATUS
Expand All @@ -679,15 +689,15 @@ async def dolphin_sync_task(ctx: TWWContext) -> None:
logger.info("Connection to Dolphin failed, attempting again in 5 seconds...")
ctx.dolphin_status = CONNECTION_LOST_STATUS
await ctx.disconnect()
await asyncio.sleep(5)
sleep_time = 5
continue
except Exception:
dolphin_memory_engine.un_hook()
logger.info("Connection to Dolphin failed, attempting again in 5 seconds...")
logger.error(traceback.format_exc())
ctx.dolphin_status = CONNECTION_LOST_STATUS
await ctx.disconnect()
await asyncio.sleep(5)
sleep_time = 5
continue


Expand All @@ -711,12 +721,14 @@ async def _main(connect: Optional[str], password: Optional[str]) -> None:
ctx.dolphin_sync_task = asyncio.create_task(dolphin_sync_task(ctx), name="DolphinSync")

await ctx.exit_event.wait()
# Wake the sync task, if it is currently sleeping, so it can start shutting down when it sees that the
# exit_event is set.
ctx.watcher_event.set()
ctx.server_address = None

await ctx.shutdown()

if ctx.dolphin_sync_task:
await asyncio.sleep(3)
await ctx.dolphin_sync_task

import colorama
Expand Down

0 comments on commit d568a8c

Please sign in to comment.