|
| 1 | +// |
| 2 | +// Created by netcan on 2021/11/29. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef ASYNCIO_OPEN_CONNECTION_H |
| 6 | +#define ASYNCIO_OPEN_CONNECTION_H |
| 7 | +#include <asyncio/asyncio_ns.h> |
| 8 | +#include <asyncio/selector/event.h> |
| 9 | +#include <exception> |
| 10 | +#include <asyncio/task.h> |
| 11 | +#include <fcntl.h> |
| 12 | +#include <unistd.h> |
| 13 | +#include <sys/types.h> |
| 14 | +#include <sys/socket.h> |
| 15 | +#include <system_error> |
| 16 | +#include <netdb.h> |
| 17 | + |
| 18 | +ASYNCIO_NS_BEGIN |
| 19 | +struct Stream: NonCopyable { |
| 20 | + Stream(int fd): fd_(fd) {} |
| 21 | + Stream(Stream&& other): fd_{std::exchange(other.fd_, -1) } {} |
| 22 | + ~Stream() { if (fd_ > 0) { close(fd_); } } |
| 23 | +private: |
| 24 | + int fd_{-1}; |
| 25 | +}; |
| 26 | + |
| 27 | +namespace detail { |
| 28 | +Task<bool> connect(int fd, const sockaddr *addr, socklen_t len) noexcept { |
| 29 | + int rc = ::connect(fd, addr, len); |
| 30 | + if (rc == 0) { co_return true; } |
| 31 | + if (rc < 0 && errno != EINPROGRESS) { |
| 32 | + throw std::system_error(std::make_error_code(static_cast<std::errc>(errno))); |
| 33 | + } |
| 34 | + Event ev { .fd = fd, .events = EPOLLOUT }; |
| 35 | + auto& loop = get_event_loop(); |
| 36 | + co_await loop.wait_event(ev); |
| 37 | + |
| 38 | + int result{0}; |
| 39 | + socklen_t result_len = sizeof(result); |
| 40 | + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &result, &result_len) < 0) { |
| 41 | + // error, fail somehow, close socket |
| 42 | + co_return false; |
| 43 | + } |
| 44 | + co_return result == 0; |
| 45 | +} |
| 46 | + |
| 47 | +struct AddrInfoRAII { |
| 48 | + AddrInfoRAII(addrinfo* info): info_(info) { } |
| 49 | + ~AddrInfoRAII() { freeaddrinfo(info_); } |
| 50 | +private: |
| 51 | + addrinfo* info_{nullptr}; |
| 52 | +}; |
| 53 | +} |
| 54 | + |
| 55 | +Task<Stream> open_connection(std::string_view ip, uint16_t port) { |
| 56 | + addrinfo hints { |
| 57 | + .ai_family = AF_UNSPEC, |
| 58 | + .ai_socktype = SOCK_STREAM, |
| 59 | + }; |
| 60 | + addrinfo *server_info {nullptr}; |
| 61 | + auto service = std::to_string(port); |
| 62 | + // TODO: getaddrinfo is a blocking api |
| 63 | + if (int rv = getaddrinfo(ip.data(), service.c_str(), &hints, &server_info); |
| 64 | + rv != 0) { |
| 65 | + throw std::system_error(std::make_error_code(std::errc::address_not_available)); |
| 66 | + } |
| 67 | + detail::AddrInfoRAII _i(server_info); |
| 68 | + |
| 69 | + int sockfd = -1; |
| 70 | + for (auto p = server_info; p != nullptr; p = p->ai_next) { |
| 71 | + sockfd = -1; |
| 72 | + if ( (sockfd = socket(p->ai_family, p->ai_socktype | SOCK_NONBLOCK, p->ai_protocol)) == -1) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + if (co_await detail::connect(sockfd, p->ai_addr, p->ai_addrlen) ) { |
| 76 | + close(sockfd); |
| 77 | + continue; |
| 78 | + } |
| 79 | + } |
| 80 | + if (sockfd == -1) { |
| 81 | + throw std::system_error(std::make_error_code(std::errc::address_not_available)); |
| 82 | + } |
| 83 | + |
| 84 | + co_return Stream {sockfd}; |
| 85 | +} |
| 86 | + |
| 87 | +ASYNCIO_NS_END |
| 88 | + |
| 89 | +#endif // ASYNCIO_OPEN_CONNECTION_H |
0 commit comments