screen: remove redundant #ifndef
[ncmpc.git] / src / aconnect.c
blob345bc52c8f5e1dde1738c54448883d579031dffb
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.
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>
43 #include <sys/socket.h>
45 struct aconnect {
46 const struct aconnect_handler *handler;
47 void *handler_ctx;
49 struct async_rconnect *rconnect;
51 int fd;
52 guint source_id;
55 static gboolean
56 aconnect_source_callback(gcc_unused GIOChannel *source,
57 gcc_unused GIOCondition condition,
58 gpointer data)
60 struct aconnect *ac = data;
61 assert(ac->source_id != 0);
62 ac->source_id = 0;
64 char buffer[256];
65 ssize_t nbytes = recv(ac->fd, buffer, sizeof(buffer) - 1, 0);
66 if (nbytes < 0) {
67 snprintf(buffer, sizeof(buffer),
68 "Failed to receive from MPD: %s",
69 strerror(errno));
70 close_socket(ac->fd);
71 ac->handler->error(buffer, ac->handler_ctx);
72 g_free(ac);
73 return false;
76 if (nbytes == 0) {
77 close_socket(ac->fd);
78 ac->handler->error("MPD closed the connection",
79 ac->handler_ctx);
80 g_free(ac);
81 return false;
84 buffer[nbytes] = 0;
86 struct mpd_async *async = mpd_async_new(ac->fd);
87 if (async == NULL) {
88 close_socket(ac->fd);
89 ac->handler->error("Out of memory", ac->handler_ctx);
90 g_free(ac);
91 return false;
94 struct mpd_connection *c = mpd_connection_new_async(async, buffer);
95 if (c == NULL) {
96 mpd_async_free(async);
97 ac->handler->error("Out of memory", ac->handler_ctx);
98 g_free(ac);
99 return false;
102 ac->handler->success(c, ac->handler_ctx);
103 g_free(ac);
104 return false;
107 static void
108 aconnect_rconnect_success(int fd, void *ctx)
110 struct aconnect *ac = ctx;
111 assert(ac->rconnect != NULL);
112 ac->rconnect = NULL;
114 ac->fd = fd;
116 GIOChannel *channel = g_io_channel_unix_new(fd);
117 ac->source_id = g_io_add_watch(channel, G_IO_IN,
118 aconnect_source_callback, ac);
119 g_io_channel_unref(channel);
122 static void
123 aconnect_rconnect_error(const char *message, void *ctx)
125 struct aconnect *ac = ctx;
126 assert(ac->rconnect != NULL);
127 ac->rconnect = NULL;
129 ac->handler->error(message, ac->handler_ctx);
130 g_free(ac);
133 static const struct async_rconnect_handler aconnect_rconnect_handler = {
134 .success = aconnect_rconnect_success,
135 .error = aconnect_rconnect_error,
138 void
139 aconnect_start(struct aconnect **acp,
140 const char *host, unsigned port,
141 const struct aconnect_handler *handler, void *ctx)
143 struct aconnect *ac = g_new(struct aconnect, 1);
144 ac->handler = handler;
145 ac->handler_ctx = ctx;
147 async_rconnect_start(&ac->rconnect, host, port,
148 &aconnect_rconnect_handler, ac);
150 *acp = ac;
153 void
154 aconnect_cancel(struct aconnect *ac)
156 if (ac->rconnect != NULL)
157 async_rconnect_cancel(ac->rconnect);
158 else {
159 g_source_remove(ac->source_id);
160 close_socket(ac->fd);
163 g_free(ac);