1 #ifndef MSP_NET_RESOLVE_H_
2 #define MSP_NET_RESOLVE_H_
6 #include <msp/core/mutex.h>
7 #include <msp/core/semaphore.h>
8 #include <msp/core/thread.h>
9 #include <msp/io/eventdispatcher.h>
10 #include <msp/io/pipe.h>
11 #include "constants.h"
18 /** Resolves host and service names into a socket address. If host is empty,
19 the loopback address will be used. If host is "*", the wildcard address will
20 be used. If service is empty, a socket address with a null service will be
21 returned. With the IP families, these are not very useful. */
22 SockAddr *resolve(const std::string &, const std::string &, Family = UNSPEC);
24 /** And overload of resolve() that takes host and service as a single string,
25 separated by a colon. If the host part contains colons, such as is the case
26 with a numeric IPv6 address, it must be enclosed in brackets. */
27 SockAddr *resolve(const std::string &, Family = UNSPEC);
31 An asynchronous name resolver. Blocking calls are performed in a thread and
32 completion is notified with one of the two signals.
42 Family family = UNSPEC;
43 SockAddr *addr = nullptr;
44 std::runtime_error *error = nullptr;
46 bool is_complete() const { return addr || error; }
49 class WorkerThread: public Thread
52 std::deque<Task> queue;
62 void add_task(const Task &);
63 Task *get_complete_task();
64 void pop_complete_task();
66 IO::Pipe &get_notify_pipe() { return notify_pipe; }
73 sigc::signal<void, unsigned, const SockAddr &> signal_address_resolved;
74 sigc::signal<void, unsigned, const std::exception &> signal_resolve_failed;
77 IO::EventDispatcher *event_disp = nullptr;
79 unsigned next_tag = 1;
84 /** Sets the event dispatcher to use. With no event dispatcher, tick() must
85 be called regularly to emit the completion signals. */
86 void use_event_dispatcher(IO::EventDispatcher *);
88 /** Resolves host and service names into a socket address. Semantics are
89 the same as the namespace-scope resolve function. */
90 unsigned resolve(const std::string &, const std::string &, Family = UNSPEC);
92 unsigned resolve(const std::string &, Family = UNSPEC);
94 /** Checks for any completed tasks and emits the appropriate singals. */