Skip to content

Commit

Permalink
Merge pull request #50 from luar123/reconnect
Browse files Browse the repository at this point in the history
Update on reconnect and catch exception in transaction
  • Loading branch information
happyleavesaoc authored Oct 24, 2022
2 parents bbedfb8 + 1cce28b commit bde2f35
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions snapcast/control/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,30 @@ def _do_connect(self):

def _reconnect_cb(self):
"""Callback to reconnect to the server."""
_LOGGER.info('try reconnect')
@asyncio.coroutine
def try_reconnect():
"""Actual coroutine ro try to reconnect or reschedule."""
try:
yield from self._do_connect()
except IOError:
except OSError:
self._loop.call_later(SERVER_RECONNECT_DELAY,
self._reconnect_cb)
else:
status = yield from self.status()
self.synchronize(status)
self._on_server_connect()
asyncio.ensure_future(try_reconnect())

@asyncio.coroutine
def _transact(self, method, params=None):
"""Wrap requests."""
result, error = yield from self._protocol.request(method, params)
result = error = None
try:
result, error = yield from self._protocol.request(method, params)
except:
_LOGGER.warning('could not send request')
error = 'could not send request'
return result or error

@property
Expand Down Expand Up @@ -303,11 +313,13 @@ def _request(self, method, identifier, key=None, value=None, parameters=None):

def _on_server_connect(self):
"""Handle server connection."""
_LOGGER.info('Server connected')
if self._on_connect_callback_func and callable(self._on_connect_callback_func):
self._on_connect_callback_func()

def _on_server_disconnect(self, exception):
"""Handle server disconnection."""
_LOGGER.info('Server disconnected')
if self._on_disconnect_callback_func and callable(self._on_disconnect_callback_func):
self._on_disconnect_callback_func(exception)
if not self._is_stopped:
Expand Down

0 comments on commit bde2f35

Please sign in to comment.