@@ -57,7 +57,7 @@ def __init__(
5757 ):
5858 super ().__init__ (bot )
5959
60- def is_connected (self ) -> False :
60+ def is_connected (self ) -> bool :
6161 """Check if the backend is connected to an IRC server.
6262
6363 **Always returns False:** This backend type is never connected.
@@ -378,19 +378,18 @@ def get_connection_kwargs(self) -> Dict:
378378 'local_addr' : self ._source_address ,
379379 }
380380
381- async def _run_forever (self ) -> None :
382- self ._loop = asyncio .get_running_loop ()
383- connection_kwargs = self .get_connection_kwargs ()
384-
385- # register signal handlers
386- for quit_signal in QUIT_SIGNALS :
387- self ._loop .add_signal_handler (quit_signal , self ._signal_quit )
388- for restart_signal in RESTART_SIGNALS :
389- self ._loop .add_signal_handler (restart_signal , self ._signal_restart )
381+ async def _connect_to_server (
382+ self , ** connection_kwargs
383+ ) -> Tuple [
384+ Optional [asyncio .StreamReader ],
385+ Optional [asyncio .StreamWriter ],
386+ ]:
387+ reader : Optional [asyncio .StreamReader ] = None
388+ writer : Optional [asyncio .StreamWriter ] = None
390389
391390 # open connection
392391 try :
393- self . _reader , self . _writer = await asyncio .open_connection (
392+ reader , writer = await asyncio .open_connection (
394393 ** connection_kwargs ,
395394 )
396395
@@ -405,14 +404,12 @@ async def _run_forever(self) -> None:
405404 # tell the bot to quit without restart
406405 self .bot .hasquit = True
407406 self .bot .wantsrestart = False
408- return
409407 except ssl .SSLError as err :
410408 LOGGER .error ('Unable to connect due to an SSL error: %s' , err )
411409 self .log_exception ()
412410 # tell the bot to quit without restart
413411 self .bot .hasquit = True
414412 self .bot .wantsrestart = False
415- return
416413
417414 # Specific connection error (invalid address and timeout)
418415 except socket .gaierror as err :
@@ -430,14 +427,12 @@ async def _run_forever(self) -> None:
430427 # tell the bot to quit without restart
431428 self .bot .hasquit = True
432429 self .bot .wantsrestart = False
433- return
434430 except TimeoutError as err :
435431 LOGGER .error ('Unable to connect due to a timeout: %s' , err )
436432 self .log_exception ()
437433 # tell the bot to quit without restart
438434 self .bot .hasquit = True
439435 self .bot .wantsrestart = False
440- return
441436
442437 # Generic connection error
443438 except ConnectionError as err :
@@ -446,7 +441,6 @@ async def _run_forever(self) -> None:
446441 # tell the bot to quit without restart
447442 self .bot .hasquit = True
448443 self .bot .wantsrestart = False
449- return
450444
451445 # Generic OSError (used for any unspecitic connection error)
452446 except OSError as err :
@@ -461,7 +455,6 @@ async def _run_forever(self) -> None:
461455 # tell the bot to quit without restart
462456 self .bot .hasquit = True
463457 self .bot .wantsrestart = False
464- return
465458
466459 # Unexpected error
467460 except Exception as err :
@@ -475,13 +468,34 @@ async def _run_forever(self) -> None:
475468 # TODO: prevent infinite connection failure loop
476469 self .bot .hasquit = True
477470 self .bot .wantsrestart = False
478- return
479471
480- self ._connected = True
472+ return reader , writer
473+
474+ async def _run_forever (self ) -> None :
475+ self ._loop = asyncio .get_running_loop ()
476+ connection_kwargs = self .get_connection_kwargs ()
477+
478+ # register signal handlers
479+ for quit_signal in QUIT_SIGNALS :
480+ self ._loop .add_signal_handler (quit_signal , self ._signal_quit )
481+ for restart_signal in RESTART_SIGNALS :
482+ self ._loop .add_signal_handler (restart_signal , self ._signal_restart )
481483
484+ # connect to socket
485+ LOGGER .debug ('Attempt connection.' )
486+ self ._reader , self ._writer = await self ._connect_to_server (
487+ ** connection_kwargs
488+ )
489+ if not self ._reader or not self ._writer :
490+ LOGGER .debug ('Connection attempt failed.' )
491+ return
492+
493+ # on socket connection
482494 LOGGER .debug ('Connection registered.' )
495+ self ._connected = True
483496 self .bot .on_connect ()
484497
498+ # read forever
485499 LOGGER .debug ('Waiting for messages...' )
486500 self ._read_task = asyncio .create_task (self .read_forever ())
487501 try :
@@ -491,6 +505,7 @@ async def _run_forever(self) -> None:
491505 else :
492506 LOGGER .debug ('Reader received EOF.' )
493507
508+ # on socket deconnection
494509 self ._connected = False
495510
496511 # cancel timeout tasks
0 commit comments