Skip to content

Commit

Permalink
Automatic reconnection and less noise on errors
Browse files Browse the repository at this point in the history
Only show one error message when connection failed *on demand*,
otherwise resort to a status message. Also try to reconnect every 15s
silently, including on startup and when running the command. This can be
aborted by running the "Disconnect" command.

Closes #16.
  • Loading branch information
FichteFoll committed Jan 10, 2018
1 parent 2f326cb commit 391ff75
Showing 1 changed file with 55 additions and 31 deletions.
86 changes: 55 additions & 31 deletions drp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
SETTINGS_FILE = 'DiscordRichPresence.sublime-settings'
settings = {}
DISCORD_CLIENT_ID = '389368374645227520'
RECONNECT_DELAY = 15000

logger = logging.getLogger(__name__)

last_file = ''
last_edit = 0
ipc = None
is_connecting = False

start_time = time.time()

Expand Down Expand Up @@ -138,12 +140,18 @@ def handle_activity(view, is_write=False):
try:
ipc.set_activity(act)
except OSError as e:
sublime.error_message("[DiscordRP] Sending activity failed."
"\n\nYou have been disconnected from your Discord instance."
" Run 'Discord Rich Presence: Connect to Discord'"
" after you restarted your Discord client."
"\n\nError: {}".format(e))
disconnect()
handle_error(e)


def handle_error(exc, retry=True):
sublime.active_window().status_message("[DiscordRP] Sending activity failed")
logger.error("Sending activity failed. Error: %s", exc)
disconnect()

if retry:
global is_connecting
is_connecting = True
sublime.set_timeout_async(connect_background, 0)


def get_project_name(window, current_file):
Expand Down Expand Up @@ -185,41 +193,45 @@ def is_view_active(view):
return active_view.buffer_id() == view.buffer_id()
return False


class DRPListener(sublime_plugin.EventListener):

def on_post_save_async(self, view):
handle_activity(view, is_write=True)

def on_modified_async(self, view):
if is_view_active(view):
handle_activity(view)


def connect():
def connect(silent=False, retry=True):
global ipc
if ipc:
logger.error("Already connected")
return
return True

try:
ipc = discord_ipc.DiscordIpcClient.for_platform(DISCORD_CLIENT_ID)
except OSError:
sublime.error_message("[DiscordRP] Unable to connect to Discord."
"\n\nPlease verify that it is running."
" Run 'Discord Rich Presence: Connect to Discord'"
" to try again.")
if silent:
logger.info("Unable to connect to Discord client")
else:
sublime.error_message("[DiscordRP] Unable to connect to Discord client."
"\n\nPlease verify that it is running."
" Run 'Discord Rich Presence: Connect to Discord'"
" to try again.")
if retry:
global is_connecting
is_connecting = True
sublime.set_timeout_async(connect_background, RECONNECT_DELAY)
return

try:
ipc.set_activity(base_activity())
except OSError as e:
sublime.error_message("[DiscordRP] Sending activity failed."
"\n\nYou have been disconnected from your Discord instance."
" Run 'Discord Rich Presence: Connect to Discord'"
" after you restarted your Discord client."
"\n\nError: {}".format(e))
disconnect()
handle_error(e, retry=retry)
return

return True


def connect_background():
if not is_connecting:
logger.warning("Automatic connection retry aborted")
return

logger.info("Trying to reconnect to Discord client...")
if not connect(silent=True, retry=False):
sublime.set_timeout_async(connect_background, RECONNECT_DELAY)


def disconnect():
Expand All @@ -237,6 +249,16 @@ def disconnect():
ipc = None


class DRPListener(sublime_plugin.EventListener):

def on_post_save_async(self, view):
handle_activity(view, is_write=True)

def on_modified_async(self, view):
if is_view_active(view):
handle_activity(view)


class DiscordrpConnectCommand(sublime_plugin.ApplicationCommand):

def is_enabled(self):
Expand All @@ -259,17 +281,19 @@ def run(self):
class DiscordrpDisconnectCommand(sublime_plugin.ApplicationCommand):

def is_enabled(self):
return bool(ipc)
return bool(ipc) or is_connecting

def run(self):
global is_connecting
disconnect()
is_connecting = False


def plugin_loaded():
global settings
settings = sublime.load_settings(SETTINGS_FILE)
if settings.get('connect_on_startup'):
connect()
connect(silent=True)


def plugin_unloaded():
Expand Down

0 comments on commit 391ff75

Please sign in to comment.