• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jbaldwin / libcoro / 589b3b5782f41a4fd3268d9c21dff5e855f02bca

19 Nov 2023 08:25PM UTC coverage: 83.333% (-0.3%) from 83.584%
589b3b5782f41a4fd3268d9c21dff5e855f02bca

push

github

web-flow
Fix the coverage status badge in the readme (#214)

It was incorrectly pointing to the original issue-1/ci branch.. oops.

Closes #213

555 of 666 relevant lines covered (83.33%)

9669336.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.56
/src/net/socket.cpp
1
#include "coro/net/socket.hpp"
2

3
namespace coro::net
4
{
5
auto socket::type_to_os(type_t type) -> int
653✔
6
{
7
    switch (type)
653✔
8
    {
9
        case type_t::udp:
12✔
10
            return SOCK_DGRAM;
12✔
11
        case type_t::tcp:
641✔
12
            return SOCK_STREAM;
641✔
13
        default:
×
14
            throw std::runtime_error{"Unknown socket::type_t."};
×
15
    }
16
}
17

18
auto socket::operator=(socket&& other) noexcept -> socket&
×
19
{
20
    if (std::addressof(other) != this)
×
21
    {
22
        m_fd = std::exchange(other.m_fd, -1);
×
23
    }
24

25
    return *this;
×
26
}
27

28
auto socket::blocking(blocking_t block) -> bool
1,227✔
29
{
30
    if (m_fd < 0)
1,227✔
31
    {
32
        return false;
×
33
    }
34

35
    int flags = fcntl(m_fd, F_GETFL, 0);
1,227✔
36
    if (flags == -1)
1,255✔
37
    {
38
        return false;
×
39
    }
40

41
    // Add or subtract non-blocking flag.
42
    flags = (block == blocking_t::yes) ? flags & ~O_NONBLOCK : (flags | O_NONBLOCK);
1,255✔
43

44
    return (fcntl(m_fd, F_SETFL, flags) == 0);
1,255✔
45
}
46

47
auto socket::shutdown(poll_op how) -> bool
×
48
{
49
    if (m_fd != -1)
×
50
    {
51
        int h{0};
×
52
        switch (how)
×
53
        {
54
            case poll_op::read:
×
55
                h = SHUT_RD;
×
56
                break;
×
57
            case poll_op::write:
×
58
                h = SHUT_WR;
×
59
                break;
×
60
            case poll_op::read_write:
×
61
                h = SHUT_RDWR;
×
62
                break;
×
63
        }
64

65
        return (::shutdown(m_fd, h) == 0);
×
66
    }
67
    return false;
×
68
}
69

70
auto socket::close() -> void
3,079✔
71
{
72
    if (m_fd != -1)
3,079✔
73
    {
74
        ::close(m_fd);
660✔
75
        m_fd = -1;
660✔
76
    }
77
}
3,079✔
78

79
auto make_socket(const socket::options& opts) -> socket
652✔
80
{
81
    socket s{::socket(static_cast<int>(opts.domain), socket::type_to_os(opts.type), 0)};
652✔
82
    if (s.native_handle() < 0)
629✔
83
    {
84
        throw std::runtime_error{"Failed to create socket."};
×
85
    }
86

87
    if (opts.blocking == socket::blocking_t::no)
631✔
88
    {
89
        if (s.blocking(socket::blocking_t::no) == false)
626✔
90
        {
91
            throw std::runtime_error{"Failed to set socket to non-blocking mode."};
×
92
        }
93
    }
94

95
    return s;
646✔
96
}
97

98
auto make_accept_socket(const socket::options& opts, const net::ip_address& address, uint16_t port, int32_t backlog)
48✔
99
    -> socket
100
{
101
    socket s = make_socket(opts);
48✔
102

103
    int sock_opt{1};
48✔
104
    if (setsockopt(s.native_handle(), SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &sock_opt, sizeof(sock_opt)) < 0)
48✔
105
    {
106
        throw std::runtime_error{"Failed to setsockopt(SO_REUSEADDR | SO_REUSEPORT)"};
×
107
    }
108

109
    sockaddr_in server{};
48✔
110
    server.sin_family = static_cast<int>(opts.domain);
48✔
111
    server.sin_port   = htons(port);
48✔
112
    server.sin_addr   = *reinterpret_cast<const in_addr*>(address.data().data());
48✔
113

114
    if (bind(s.native_handle(), (struct sockaddr*)&server, sizeof(server)) < 0)
48✔
115
    {
116
        throw std::runtime_error{"Failed to bind."};
×
117
    }
118

119
    if (opts.type == socket::type_t::tcp)
48✔
120
    {
121
        if (listen(s.native_handle(), backlog) < 0)
39✔
122
        {
123
            throw std::runtime_error{"Failed to listen."};
×
124
        }
125
    }
126

127
    return s;
96✔
128
}
129

130
} // namespace coro::net
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc