1 /* ncmpc (Ncurses MPD Client)
2 (c) 2004-2017 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
7 are met:
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.
27 */
29 #include "aconnect.h"
30 #include "net/async_rconnect.h"
31 #include "net/socket.h"
32 #include "Compiler.h"
34 #include <mpd/client.h>
35 #include <mpd/async.h>
37 #include <glib.h>
39 #include <assert.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <errno.h>
44 struct aconnect {
45 const struct aconnect_handler *handler;
46 void *handler_ctx;
48 struct async_rconnect *rconnect;
50 int fd;
51 guint source_id;
52 };
54 static gboolean
55 aconnect_source_callback(gcc_unused GIOChannel *source,
56 gcc_unused GIOCondition condition,
57 gpointer data)
58 {
59 struct aconnect *ac = data;
60 assert(ac->source_id != 0);
61 ac->source_id = 0;
63 char buffer[256];
64 ssize_t nbytes = recv(ac->fd, buffer, sizeof(buffer) - 1, 0);
65 if (nbytes < 0) {
66 snprintf(buffer, sizeof(buffer),
67 "Failed to receive from MPD: %s",
68 strerror(errno));
69 close_socket(ac->fd);
70 ac->handler->error(buffer, ac->handler_ctx);
71 g_free(ac);
72 return false;
73 }
75 if (nbytes == 0) {
76 close_socket(ac->fd);
77 ac->handler->error("MPD closed the connection",
78 ac->handler_ctx);
79 g_free(ac);
80 return false;
81 }
83 buffer[nbytes] = 0;
85 struct mpd_async *async = mpd_async_new(ac->fd);
86 if (async == NULL) {
87 close_socket(ac->fd);
88 ac->handler->error("Out of memory", ac->handler_ctx);
89 g_free(ac);
90 return false;
91 }
93 struct mpd_connection *c = mpd_connection_new_async(async, buffer);
94 if (c == NULL) {
95 mpd_async_free(async);
96 ac->handler->error("Out of memory", ac->handler_ctx);
97 g_free(ac);
98 return false;
99 }
101 ac->handler->success(c, ac->handler_ctx);
102 g_free(ac);
103 return false;
104 }
106 static void
107 aconnect_rconnect_success(int fd, void *ctx)
108 {
109 struct aconnect *ac = ctx;
110 ac->rconnect = NULL;
112 ac->fd = fd;
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);
118 }
120 static void
121 aconnect_rconnect_error(const char *message, void *ctx)
122 {
123 struct aconnect *ac = ctx;
124 ac->rconnect = NULL;
126 ac->handler->error(message, ac->handler_ctx);
127 g_free(ac);
128 }
130 static const struct async_rconnect_handler aconnect_rconnect_handler = {
131 .success = aconnect_rconnect_success,
132 .error = aconnect_rconnect_error,
133 };
135 void
136 aconnect_start(struct aconnect **acp,
137 const char *host, unsigned port,
138 const struct aconnect_handler *handler, void *ctx)
139 {
140 struct aconnect *ac = g_new(struct aconnect, 1);
141 ac->handler = handler;
142 ac->handler_ctx = ctx;
144 *acp = ac;
146 async_rconnect_start(&ac->rconnect, host, port,
147 &aconnect_rconnect_handler, ac);
148 }
150 void
151 aconnect_cancel(struct aconnect *ac)
152 {
153 if (ac->rconnect != NULL)
154 async_rconnect_cancel(ac->rconnect);
155 else {
156 g_source_remove(ac->source_id);
157 close_socket(ac->fd);
158 }
160 g_free(ac);
161 }