From: Max Kellermann Date: Sun, 19 Mar 2017 12:57:46 +0000 (+0100) Subject: mpdclient: move code to mpdclient_connected() X-Git-Tag: v0.26~18 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d007e2dcb6061fc516802175fe98df951253a097;p=ncmpc.git mpdclient: move code to mpdclient_connected() --- diff --git a/src/mpdclient.c b/src/mpdclient.c index 73bb85f..a7d1ef8 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -219,18 +219,13 @@ mpdclient_disconnect(struct mpdclient *c) c->events |= MPD_IDLE_ALL; } -bool -mpdclient_connect(struct mpdclient *c) +static bool +mpdclient_connected(struct mpdclient *c, + struct mpd_connection *connection) { - /* close any open connection */ - mpdclient_disconnect(c); - - /* connect to MPD */ - c->connection = mpd_connection_new(c->host, c->port, c->timeout_ms); - if (c->connection == NULL) - g_error("Out of memory"); + c->connection = connection; - if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) { + if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) { mpdclient_invoke_error_callback1(c); mpdclient_disconnect(c); mpdclient_failed_callback(); @@ -239,24 +234,38 @@ mpdclient_connect(struct mpdclient *c) /* send password */ if (c->password != NULL && - !mpd_run_password(c->connection, c->password)) { + !mpd_run_password(connection, c->password)) { mpdclient_invoke_error_callback1(c); mpdclient_disconnect(c); mpdclient_failed_callback(); return false; } - c->source = mpd_glib_new(c->connection, + c->source = mpd_glib_new(connection, mpdclient_gidle_callback, c); mpdclient_schedule_enter_idle(c); ++c->connection_id; mpdclient_connected_callback(); - return true; } +bool +mpdclient_connect(struct mpdclient *c) +{ + /* close any open connection */ + mpdclient_disconnect(c); + + /* connect to MPD */ + struct mpd_connection *connection = + mpd_connection_new(c->host, c->port, c->timeout_ms); + if (connection == NULL) + g_error("Out of memory"); + + return mpdclient_connected(c, connection); +} + bool mpdclient_update(struct mpdclient *c) {