Code

callbacks: add "connected" and "failed" callback
authorMax Kellermann <max.kellermann@gmail.com>
Fri, 17 Mar 2017 22:33:49 +0000 (23:33 +0100)
committerMax Kellermann <max.kellermann@gmail.com>
Fri, 17 Mar 2017 22:33:49 +0000 (23:33 +0100)
More preparations for asynchronous connect.

src/callbacks.h
src/main.c
src/mpdclient.c

index c4f1cf929f4c47f2ba86e148f24b1b59bd573981..6c315e45649396063003924ab5c48a13766623b0 100644 (file)
 
 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.
index ec051f4a8c83de04a54b1e9983910a3847614662..12bee3d733a2e9733ab27f8a8feb37c4079500e5 100644 (file)
@@ -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
index 0209d6464bf8287ab4294ded44d35995ab05043b..3db559b3640c4872fe592159ea430b2667c707f3 100644 (file)
@@ -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;
 }