Code

mpdclient: try IP connect if default local socket path fails
authorMax Kellermann <max.kellermann@gmail.com>
Sat, 25 Mar 2017 19:09:57 +0000 (20:09 +0100)
committerMax Kellermann <max.kellermann@gmail.com>
Sat, 25 Mar 2017 19:54:28 +0000 (20:54 +0100)
This is what libmpdclient does internally, and when ncmpc copied some
code from libmpdclient, it did not replicate this implementation
detail.

 https://bugs.musicpd.org/view.php?id=4672

NEWS
src/mpdclient.c
src/mpdclient.h

diff --git a/NEWS b/NEWS
index 6956a1e50443562172eabd17bdf14393924718c8..5b3a4d036e7a08809284248673c13ce5205e28ad 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,5 @@
 ncmpc 0.27 - not yet released
+* work around connect failure if /var/run/mpd/socket does not exist
 * remove the status bar clock (option "display-time")
 * fix assertion failure after connect failure
 
index 870839fd27dbb69ee8abd1d4b65e79d8c8567ee4..ffa1a5d84408e86757b82d2dbde1617dbf07c583 100644 (file)
@@ -153,6 +153,25 @@ mpdclient_handle_error(struct mpdclient *c)
        return false;
 }
 
+#ifdef ENABLE_ASYNC_CONNECT
+#ifndef WIN32
+
+static bool
+is_local_socket(const char *host)
+{
+       return *host == '/' || *host == '@';
+}
+
+static bool
+settings_is_local_socket(const struct mpd_settings *settings)
+{
+       const char *host = mpd_settings_get_host(settings);
+       return host != NULL && is_local_socket(host);
+}
+
+#endif
+#endif
+
 struct mpdclient *
 mpdclient_new(const gchar *host, unsigned port,
              unsigned timeout_ms, const gchar *password)
@@ -164,6 +183,14 @@ mpdclient_new(const gchar *host, unsigned port,
                                       NULL, NULL);
        if (c->settings == NULL)
                g_error("Out of memory");
+
+#ifndef WIN32
+       c->settings2 = host == NULL && port == 0 &&
+               settings_is_local_socket(c->settings)
+               ? mpd_settings_new(host, 6600, timeout_ms, NULL, NULL)
+               : NULL;
+#endif
+
 #else
        c->host = host;
        c->port = port;
@@ -189,6 +216,11 @@ mpdclient_free(struct mpdclient *c)
 
 #ifdef ENABLE_ASYNC_CONNECT
        mpd_settings_free(c->settings);
+
+#ifndef WIN32
+       if (c->settings2 != NULL)
+               mpd_settings_free(c->settings2);
+#endif
 #endif
 
        g_free(c);
@@ -319,6 +351,10 @@ mpdclient_connected(struct mpdclient *c,
 
 #ifdef ENABLE_ASYNC_CONNECT
 
+static void
+mpdclient_aconnect_start(struct mpdclient *c,
+                        const struct mpd_settings *settings);
+
 static void
 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
 {
@@ -336,6 +372,14 @@ mpdclient_connect_error(const char *message, void *ctx)
        assert(c->async_connect != NULL);
        c->async_connect = NULL;
 
+#ifndef WIN32
+       if (!c->connecting2 && c->settings2 != NULL) {
+               c->connecting2 = true;
+               mpdclient_aconnect_start(c, c->settings2);
+               return;
+       }
+#endif
+
        mpdclient_error_callback(message);
        mpdclient_failed_callback();
 }
@@ -364,6 +408,9 @@ mpdclient_connect(struct mpdclient *c)
        mpdclient_disconnect(c);
 
 #ifdef ENABLE_ASYNC_CONNECT
+#ifndef WIN32
+       c->connecting2 = false;
+#endif
        mpdclient_aconnect_start(c, c->settings);
 #else
        /* connect to MPD */
index d51013fc8c4176b29b4b23e1b1ccf1a74c9a3573..1d3b6e0b547d61f292480d6eff9da533230e4a03 100644 (file)
@@ -11,7 +11,21 @@ struct filelist;
 
 struct mpdclient {
 #ifdef ENABLE_ASYNC_CONNECT
+       /**
+        * These settings are used to connect to MPD asynchronously.
+        */
        struct mpd_settings *settings;
+
+#ifndef WIN32
+       /**
+        * A second set of settings, just in case #settings did not
+        * work.  This is only used if #settings refers to a local
+        * socket path, and this one is supposed to be a fallback to
+        * IP on the default port (6600).
+        */
+       struct mpd_settings *settings2;
+#endif
+
 #else
        const char *host;
        unsigned port;
@@ -58,6 +72,10 @@ struct mpdclient {
         */
        enum mpd_idle events;
 
+#if defined(ENABLE_ASYNC_CONNECT) && !defined(WIN32)
+       bool connecting2;
+#endif
+
        /**
         * This attribute is true when the connection is currently in
         * "idle" mode, and the #mpd_glib_source waits for an event.