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 "async_rconnect.hxx"
30 #include "async_connect.hxx"
31 #include "resolver.hxx"
32 #include "../Compiler.h"
40 struct async_rconnect {
41 const struct async_rconnect_handler *handler;
45 struct resolver *resolver;
47 struct async_connect *connect = nullptr;
49 std::string last_error;
51 async_rconnect(const char *_host, struct resolver *_resolver,
52 const struct async_rconnect_handler &_handler,
54 :handler(&_handler), handler_ctx(_ctx),
55 host(_host), resolver(_resolver) {}
58 if (connect != nullptr)
59 async_connect_cancel(connect);
60 resolver_free(resolver);
65 async_rconnect_next(struct async_rconnect *rc);
68 async_rconnect_success(socket_t fd, void *ctx)
70 auto *rc = (struct async_rconnect *)ctx;
71 rc->connect = nullptr;
73 rc->handler->success(fd, rc->handler_ctx);
79 async_rconnect_error(const char *message, void *ctx)
81 auto *rc = (struct async_rconnect *)ctx;
82 rc->connect = nullptr;
84 rc->last_error = message;
86 async_rconnect_next(rc);
89 static const struct async_connect_handler async_rconnect_connect_handler = {
90 .success = async_rconnect_success,
91 .error = async_rconnect_error,
95 async_rconnect_next(struct async_rconnect *rc)
97 assert(rc->connect == nullptr);
99 const struct resolver_address *a = resolver_next(rc->resolver);
103 if (rc->last_error.empty()) {
104 snprintf(msg, sizeof(msg),
105 "Host '%s' has no address",
108 snprintf(msg, sizeof(msg),
109 "Failed to connect to host '%s': %s",
110 rc->host, rc->last_error.c_str());
113 rc->handler->error(msg, rc->handler_ctx);
118 async_connect_start(&rc->connect, a->addr, a->addrlen,
119 &async_rconnect_connect_handler, rc);
123 async_rconnect_start(struct async_rconnect **rcp,
124 const char *host, unsigned port,
125 const struct async_rconnect_handler *handler, void *ctx)
127 struct resolver *r = resolver_new(host, port);
133 snprintf(msg, sizeof(msg), "Failed to resolve host '%s'",
135 handler->error(msg, ctx);
139 auto *rc = new async_rconnect(host, r, *handler, ctx);
142 async_rconnect_next(rc);
146 async_rconnect_cancel(struct async_rconnect *rc)