Skip to content

Commit

Permalink
feat: client protocol: Add 1s latency before close connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gamecss committed May 8, 2024
1 parent 03767fb commit 395ab58
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/pyfsd/protocol/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ async def realfunc(
return decorator


def kill_after_1sec(kill_func: Callable) -> None:
"""Kill the client after 1 second by kill_func."""
async def kill() -> None:
await asleep(1)
kill_func()
task_keeper.add(create_task(kill()))


class ClientProtocol(LineProtocol):
"""PyFSD client protocol.
Expand Down Expand Up @@ -172,7 +180,7 @@ async def timeout_killer() -> None:
pass
else:
self.send_line(b"# Timeout")
self.transport.close()
kill_after_1sec(self.transport.close)

if hasattr(self, "timeout_killer_task"):
self.timeout_killer_task.cancel()
Expand Down Expand Up @@ -220,7 +228,7 @@ def send_error(self, errno: int, env: bytes = b"", fatal: bool = False) -> None:
),
)
if fatal:
self.transport.close()
kill_after_1sec(self.transport.close)

def send_motd(self) -> None:
"""Send motd to client."""
Expand Down Expand Up @@ -475,7 +483,7 @@ async def handle_add_client(
def handle_remove_client(self, _: Tuple[bytes, ...]) -> HandleResult:
"""Handle remove client request."""
assert self.client is not None
self.transport.close()
kill_after_1sec(self.transport.close)
return True, True

@check_packet(17)
Expand Down Expand Up @@ -890,7 +898,7 @@ def handle_kill(self, packet: Tuple[bytes, ...]) -> HandleResult:
callsign_kill,
make_packet(FSDClientCommand.KILL + b"SERVER", callsign_kill, reason),
)
self.factory.clients[callsign_kill].transport.close()
kill_after_1sec(self.factory.clients[callsign_kill].transport.close)
return True, True

def line_received(self, line: bytes) -> None:
Expand Down

0 comments on commit 395ab58

Please sign in to comment.