convenience.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <felspar/io/accept.hpp>
#include <felspar/io/addrinfo.hpp>
#include <felspar/io/connect.hpp>
#include <felspar/io/pipe.hpp>
#include <felspar/io/read.hpp>
#include <felspar/io/warden.hpp>
#include <felspar/io/write.hpp>

#include <felspar/exceptions/runtime_error.hpp>
#include <felspar/exceptions/system_error.hpp>

#include <sys/types.h>
#if defined(FELSPAR_POSIX_SOCKETS)
#include <sys/socket.h>
#include <netdb.h>
#endif


felspar::io::warden::stream<felspar::io::socket_descriptor> felspar::io::accept(
        warden &ward, socket_descriptor fd, std::source_location loc) {

Because this is a coroutine it must take the source location by copy not by reference, as the referenced source location would go out of scope before it can be used in this coroutine.

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    while (true) {
        auto s = co_await ward.accept(fd, std::optional<deadline>{}, loc);
#if defined(FELSPAR_WINSOCK2)
        co_yield s;
#else
        if (s >= 0) {
            co_yield s;
        } else if (s != -EBADF) {
            throw felspar::stdexcept::system_error{
                    -s, std::system_category(), "accept", loc};
        } else {
            co_return;
        }
#endif
    }
}


felspar::coro::generator<std::pair<sockaddr *, socklen_t>> felspar::io::addrinfo(
        char const *const hostname, std::uint16_t const port) {
    struct getaddr {
        getaddr(char const *const hostname) {
            hints.ai_socktype = SOCK_STREAM;
            if (getaddrinfo(hostname, nullptr, &hints, &addresses) != 0) {

TODO Throw a proper error here

 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                throw felspar::stdexcept::runtime_error{
                        "getaddrinfo error looking up "
                        + std::string{hostname}};
            }
        }
        struct addrinfo hints = {};
        struct addrinfo *addresses = nullptr;
        ~getaddr() {
            if (addresses) { freeaddrinfo(addresses); }
        }
    };
    getaddr addrinfo{hostname};
    for (auto current = addrinfo.addresses; current != nullptr;
         current = current->ai_next) {
        felspar::posix::set_port(*current->ai_addr, port);
        co_yield std::pair{current->ai_addr, current->ai_addrlen};
    }
}


auto felspar::io::connect(
        warden &ward,
        char const *const hostname,
        std::uint16_t const port,
        std::optional<deadline> const deadline,
        std::source_location const loc) -> warden::task<posix::fd> {
    std::exception_ptr eptr;
    for (auto host : addrinfo(hostname, port)) {
        try {
            auto fd = ward.create_socket(host.first->sa_family, SOCK_STREAM, 0);
            co_await connect(ward, fd, host.first, host.second, deadline, loc);
            co_return fd;
        } catch (...) { eptr = std::current_exception(); }
    }
    if (eptr) {
        std::rethrow_exception(eptr);
    } else {
        throw felspar::stdexcept::runtime_error{
                "No host found for " + std::string{hostname}, loc};
    }
}


auto felspar::io::connect(
        warden &ward,
        char const *const hostname,
        std::uint16_t const port,
        std::chrono::nanoseconds const timeout,
        std::source_location const loc) -> warden::task<posix::fd> {
    return connect(ward, hostname, port, deadline_from(timeout), loc);
}


std::size_t felspar::io::write_some(
        socket_descriptor sock,
        void const *const data,
        std::size_t const bytes) {
#ifdef FELSPAR_WINSOCK2
    if (auto const r =
                ::send(sock, reinterpret_cast<char const *>(data), bytes, {});
        r != SOCKET_ERROR) {
#else
    if (auto const r = ::write(sock, data, bytes); r >= 0) {
#endif
        return r;
    } else {
        throw felspar::stdexcept::system_error{
                get_error(), std::system_category(),
                "Writing to socket\n" + std::to_string(bytes)};
    }
}