From a3f9b0727951886f2620ad7fc930f9ff64031958 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 17 Mar 2017 23:33:49 +0100 Subject: [PATCH] callbacks: add "connected" and "failed" callback More preparations for asynchronous connect. --- src/callbacks.h | 13 +++++++++++++ src/main.c | 45 ++++++++++++++++++++++++++++----------------- src/mpdclient.c | 4 ++++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/callbacks.h b/src/callbacks.h index c4f1cf9..6c315e4 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -26,6 +26,19 @@ struct mpdclient; +/** + * A connection to MPD has been established. + */ +void +mpdclient_connected_callback(void); + +/** + * An attempt to connect to MPD has failed. + * mpdclient_error_callback() has been called already. + */ +void +mpdclient_failed_callback(void); + /** * The connection to MPD was lost. If this was due to an error, then * mpdclient_error_callback() has already been called. diff --git a/src/main.c b/src/main.c index ec051f4..12bee3d 100644 --- a/src/main.c +++ b/src/main.c @@ -263,14 +263,26 @@ timer_reconnect(gcc_unused gpointer data) doupdate(); mpdclient_disconnect(mpd); - if (!mpdclient_connect(mpd, options.host, options.port, - options.timeout_ms, - options.password)) { - /* try again in 5 seconds */ - reconnect_source_id = g_timeout_add(5000, - timer_reconnect, NULL); - return FALSE; - } + mpdclient_connect(mpd, options.host, options.port, + options.timeout_ms, + options.password); + + return FALSE; +} + +static void +check_reconnect(void) +{ + if (mpdclient_is_dead(mpd) && reconnect_source_id == 0) + /* reconnect when the connection is lost */ + reconnect_source_id = g_timeout_add(1000, timer_reconnect, + NULL); +} + +void +mpdclient_connected_callback(void) +{ + assert(reconnect_source_id == 0); #ifndef NCMPC_MINI /* quit if mpd is pre 0.14 - song id not supported by mpd */ @@ -287,7 +299,7 @@ timer_reconnect(gcc_unused gpointer data) /* try again after 30 seconds */ reconnect_source_id = g_timeout_add(30000, timer_reconnect, NULL); - return FALSE; + return; } #endif @@ -300,17 +312,16 @@ timer_reconnect(gcc_unused gpointer data) do_mpd_update(); auto_update_timer(); - - return FALSE; } -static void -check_reconnect(void) +void +mpdclient_failed_callback(void) { - if (mpdclient_is_dead(mpd) && reconnect_source_id == 0) - /* reconnect when the connection is lost */ - reconnect_source_id = g_timeout_add(1000, timer_reconnect, - NULL); + assert(reconnect_source_id == 0); + + /* try again in 5 seconds */ + reconnect_source_id = g_timeout_add(5000, + timer_reconnect, NULL); } void diff --git a/src/mpdclient.c b/src/mpdclient.c index 0209d64..3db559b 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -177,6 +177,7 @@ mpdclient_connect(struct mpdclient *c, if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) { mpdclient_handle_error(c); mpdclient_disconnect(c); + mpdclient_failed_callback(); return false; } @@ -184,6 +185,7 @@ mpdclient_connect(struct mpdclient *c, if (password != NULL && !mpd_run_password(c->connection, password)) { mpdclient_handle_error(c); mpdclient_disconnect(c); + mpdclient_failed_callback(); return false; } @@ -192,6 +194,8 @@ mpdclient_connect(struct mpdclient *c, ++c->connection_id; + mpdclient_connected_callback(); + return true; } -- 2.30.2