Skip to content

Commit

Permalink
Merge branch 'master' into fix/ainput-only
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/loveletter_cli/session.py
  • Loading branch information
plammens committed Jun 11, 2022
2 parents 187df83 + 2c3a48c commit b6f2ed5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
44 changes: 24 additions & 20 deletions src/loveletter_cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,32 +602,36 @@ class ConnectionErrorOptions(enum.Enum):
RESTART = enum.auto()
QUIT = enum.auto()

connection = None
while connection is None:
while True:
try:
connection = await self.client.connect(*self.server_address)
except (ConnectionError, LogonError) as e:
break
except asyncio.exceptions.TimeoutError:
await aprint("Connection attempt timed out.", end="\n\n")
except (OSError, LogonError) as e:
await aprint("Error while trying to connect to the server:")
await print_exception(e)
choice = await async_ask_valid_input(
"What would you like to do? ("
"RETRY: retry connecting to this server; "
"RESTART: restart Love Letter CLI (go back to username selection); "
"QUIT: quit Love Letter CLI"
")",
choices=ConnectionErrorOptions,
default=ConnectionErrorOptions.RETRY,
)
if choice == ConnectionErrorOptions.RETRY:
continue
elif choice == ConnectionErrorOptions.RESTART:
raise Restart from None
elif choice == ConnectionErrorOptions.QUIT:
sys.exit(1)
else:
assert False

choice = await async_ask_valid_input(
"What would you like to do? ("
"RETRY: retry connecting to this server; "
"RESTART: restart Love Letter CLI (go back to username selection); "
"QUIT: quit Love Letter CLI"
")",
choices=ConnectionErrorOptions,
default=ConnectionErrorOptions.RETRY,
)
if choice == ConnectionErrorOptions.RETRY:
continue
elif choice == ConnectionErrorOptions.RESTART:
raise Restart from None
elif choice == ConnectionErrorOptions.QUIT:
sys.exit(1)
else:
assert False

await aprint("Successfully connected to the server.")
# noinspection PyUnboundLocalVariable
return connection

async def _wait_for_game(self) -> RemoteGameShadowCopy:
Expand Down
11 changes: 9 additions & 2 deletions src/loveletter_multiplayer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __repr__(self):
help_msg="No active connection",
)

async def connect(self, host, port) -> asyncio.Task:
async def connect(self, host, port, *, timeout: float = 5) -> asyncio.Task:
"""
Try connecting to a server and logging on.
Expand All @@ -101,12 +101,19 @@ async def connect(self, host, port) -> asyncio.Task:
for example when the server closes the connection
(see :meth:`LoveletterClient._ServerConnectionManager.manage`).
:param host: IP address of server.
:param port: Port on server to which to connect.
:param timeout: Timeout for the TCP connection attempt.
:raises ConnectionError: if the connection fails at the socket level.
:raises asyncio.exceptions.TimeoutError: if the connection attempt times out.
:raises LogonError: if logon fails.
:return: The connection task described above.
"""
reader, writer = await asyncio.open_connection(host=host, port=port)
reader, writer = await asyncio.wait_for(
asyncio.open_connection(host=host, port=port), timeout=timeout
)
try:
address = writer.get_extra_info("peername")
LOGGER.debug(f"Successfully connected to server address %s", address)
Expand Down

0 comments on commit b6f2ed5

Please sign in to comment.