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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* File: net/modem.hpp
* Purpose: Modem concept in modern C++
* Author: Amlal El Mahrouss (amlal@nekernel.org)
* Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License.
*/
#ifndef _OCL_NET_NETWORK_HPP
#define _OCL_NET_NETWORK_HPP
#include <lib/tests/hpptest.hpp>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string>
#include <cstddef>
#define OCL_MODEM_INTERFACE : public ocl::net::modem
namespace ocl::net
{
class modem;
using socket_type = int64_t;
/// =============================================================================
/// @brief Modem container concept, a container to read and write on a network stream.
/// =============================================================================
class modem final
{
private:
socket_type fd_{};
bool server_fd_{false};
bool bad_{true};
public:
const bool& bad{bad_};
explicit modem() = default;
~modem()
{
this->destroy();
}
modem& operator=(const modem&) = delete;
modem(const modem&) = delete;
static constexpr auto local_address_ip4 = "127.0.0.1";
static constexpr auto local_address_ip6 = "::1";
static constexpr const auto backlog_count = 5U;
/// =============================================================================
/// @brief Check if the modem is valid.
/// @return true if valid, false otherwise.
/// =============================================================================
bool is_valid() const noexcept
{
return this->fd_ != -1 && !this->bad_;
}
template <typename ptr_type>
bool receive(ptr_type& out, std::size_t len) noexcept
{
static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!");
if (!out)
return false;
if (!len)
return false;
socket_type cl_{fd_};
if (this->server_fd_)
cl_ = ::accept(fd_, nullptr, nullptr);
auto ret = ::recv(cl_, out, len, 0);
return ret > 0L;
}
template <typename ptr_type>
bool transmit(ptr_type& out, std::size_t len) noexcept
{
static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!");
if (!out)
return false;
if (!len)
return false;
auto ret = ::send(fd_, out, len, 0);
bad_ = !(ret >= 0L);
return ret >= 0L;
}
template <typename char_type>
bool transmit(const std::basic_string<char_type>& out) noexcept
{
if (out.empty())
return false;
auto ret = ::send(fd_, out.data(), out.size(), 0);
bad_ = !(ret >= 0L);
return ret >= 0L;
}
template <int32_t af, int32_t kind, int32_t port>
bool construct(const char* addr = modem::local_address_ip4, const bool& is_server = false) noexcept
{
static_assert(af != 0, "Address family is zero");
static_assert(kind != 0, "Kind is zero");
must_pass<af == AF_INET || af == AF_INET6>();
must_pass<kind == SOCK_STREAM || kind == SOCK_DGRAM>();
must_pass<(port > 0) && (port < 65536)>();
fd_ = ::socket(af, kind, 0);
server_fd_ = is_server;
if (fd_ == -1)
return false;
struct sockaddr_in addr_;
std::memset(&addr_, 0, sizeof(struct sockaddr_in));
addr_.sin_addr.s_addr = ::inet_addr(addr);
addr_.sin_port = htons(port);
addr_.sin_family = af;
if (!is_server)
{
const auto ret = ::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));
return ret == 0L;
}
int ret = ::bind(fd_, (struct sockaddr*)&addr_, sizeof(addr_));
bad_ = ret == -1;
::listen(fd_, modem::backlog_count);
return bad_ == false;
}
bool destroy() noexcept
{
if (!fd_)
return false;
::shutdown(fd_, SHUT_RDWR);
::close(fd_);
fd_ = 0L;
return true;
}
};
} // namespace ocl::net
#endif // ifndef _OCL_NET_NETWORK_HPP
|