warden.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
#include <felspar/exceptions.hpp>
#include <felspar/io/warden.hpp>

#ifdef FELSPAR_POSIX_SOCKETS
#include <fcntl.h>
#endif


felspar::posix::fd felspar::io::warden::create_socket(
        int domain,
        int type,
        int protocol,
        felspar::source_location const &loc) {
    posix::fd s{::socket(domain, type, protocol)};
    if (not s) {
        throw felspar::stdexcept::system_error{
                get_error(), std::system_category(), "Error creating socket",
                loc};
    } else {
        do_prepare_socket(s.native_handle(), loc);
        return s;
    }
}


auto felspar::io::warden::create_pipe(felspar::source_location const &loc)
        -> pipe {
#ifdef FELSPAR_WINSOCK2
    posix::fd server{::socket(AF_INET, SOCK_STREAM, 0)};
    if (not server) {
        throw felspar::stdexcept::system_error{
                get_error(), std::system_category(),
                "Error creating pipe accept socket", loc};
    }
    posix::bind(server, INADDR_LOOPBACK, 0);
    sockaddr name;
    int namelen = sizeof(sockaddr);
    if (getsockname(server.native_handle(), &name, &namelen) != 0) {
        throw felspar::stdexcept::system_error{
                get_error(), std::system_category(),
                "getsockname to determine port", loc};
    }
    posix::listen(server, 1, loc);

    auto write = create_socket(AF_INET, SOCK_STREAM, 0, loc);
    if (::connect(write.native_handle(), &name, namelen) != 0) {
        auto const error = io::get_error();
        if (error != WSAEWOULDBLOCK) {
            throw felspar::stdexcept::system_error{
                    error, std::system_category(),
                    "connecting write end part 1", loc};
        }
    } else {
        throw felspar::stdexcept::runtime_error{
                "Expected WSAEWOULDBLOCK when connecting write end", loc};
    }

    auto read = posix::fd{::accept(server.native_handle(), &name, &namelen)};
    if (not read) {
        throw felspar::stdexcept::system_error{
                get_error(), std::system_category(),
                "Didn't get a valid read socket from accept for a pipe", loc};
    } else {
        posix::set_non_blocking(read);
    }

    return {std::move(read), std::move(write)};
#else
    int fds[2] = {};
    if (::pipe2(fds, O_NONBLOCK) == 0) {
        return {posix::fd{fds[0]}, posix::fd{fds[1]}};
    } else {
        throw felspar::stdexcept::system_error{
                io::get_error(), std::system_category(), "Creating pipe", loc};
    }
#endif
}