1 /* ncmpc (Ncurses MPD Client)
2 (c) 2004-2018 The Music Player Daemon Project
3 Project homepage: http://musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "aconnect.hxx"
30 #include "net/async_rconnect.hxx"
31 #include "net/socket.hxx"
34 #include <mpd/client.h>
35 #include <mpd/async.h>
44 struct AsyncMpdConnect {
45 const AsyncMpdConnectHandler *handler;
48 struct async_rconnect *rconnect;
55 aconnect_source_callback(gcc_unused GIOChannel *source,
56 gcc_unused GIOCondition condition,
59 auto *ac = (AsyncMpdConnect *)data;
60 assert(ac->source_id != 0);
64 ssize_t nbytes = recv(ac->fd, buffer, sizeof(buffer) - 1, 0);
66 snprintf(buffer, sizeof(buffer),
67 "Failed to receive from MPD: %s",
70 ac->handler->error(buffer, ac->handler_ctx);
77 ac->handler->error("MPD closed the connection",
85 struct mpd_async *async = mpd_async_new(ac->fd);
86 if (async == nullptr) {
88 ac->handler->error("Out of memory", ac->handler_ctx);
93 struct mpd_connection *c = mpd_connection_new_async(async, buffer);
95 mpd_async_free(async);
96 ac->handler->error("Out of memory", ac->handler_ctx);
101 ac->handler->success(c, ac->handler_ctx);
107 aconnect_rconnect_success(int fd, void *ctx)
109 auto *ac = (AsyncMpdConnect *)ctx;
110 ac->rconnect = nullptr;
114 GIOChannel *channel = g_io_channel_unix_new(fd);
115 ac->source_id = g_io_add_watch(channel, G_IO_IN,
116 aconnect_source_callback, ac);
117 g_io_channel_unref(channel);
121 aconnect_rconnect_error(const char *message, void *ctx)
123 auto *ac = (AsyncMpdConnect *)ctx;
124 ac->rconnect = nullptr;
126 ac->handler->error(message, ac->handler_ctx);
130 static const struct async_rconnect_handler aconnect_rconnect_handler = {
131 .success = aconnect_rconnect_success,
132 .error = aconnect_rconnect_error,
136 aconnect_start(AsyncMpdConnect **acp,
137 const char *host, unsigned port,
138 const AsyncMpdConnectHandler &handler, void *ctx)
140 auto *ac = new AsyncMpdConnect();
141 ac->handler = &handler;
142 ac->handler_ctx = ctx;
146 async_rconnect_start(&ac->rconnect, host, port,
147 &aconnect_rconnect_handler, ac);
151 aconnect_cancel(AsyncMpdConnect *ac)
153 if (ac->rconnect != nullptr)
154 async_rconnect_cancel(ac->rconnect);
156 g_source_remove(ac->source_id);
157 close_socket(ac->fd);