14 return connect(ep.first, ep.second);
19 m_addr = remote_address;
21 auto res0 = get_addrinfo(remote_address, remote_port);
26 for(
auto* res = res0.get(); res !=
nullptr; res = res->ai_next) {
27 if(!create_socket(res->ai_family,
33 if(::connect(m_sock_fd, res->ai_addr, res->ai_addrlen) != 0) {
42 m_connected = m_sock_fd != -1;
50 shutdown(m_sock_fd, SHUT_RDWR);
61 const auto sz_val =
static_cast<uint64_t
>(pkt.size());
62 std::array<std::byte,
sizeof(sz_val)> sz_arr{};
63 std::memcpy(sz_arr.data(), &sz_val,
sizeof(sz_val));
64 size_t total_written = 0;
65 while(total_written !=
sizeof(sz_val)) {
66 auto n = write(m_sock_fd,
67 &sz_arr.at(total_written),
68 sizeof(sz_val) - total_written);
72 total_written +=
static_cast<size_t>(n);
77 !=
static_cast<decltype(total_written)
>(pkt.size())) {
78 auto n = write(m_sock_fd,
79 pkt.data_at(total_written),
80 pkt.size() - total_written);
84 total_written +=
static_cast<size_t>(n);
92 std::array<std::byte,
sizeof(pkt_sz)> sz_buf{};
93 uint64_t total_read{0};
94 while(total_read != sz_buf.size()) {
95 auto n = read(m_sock_fd,
96 &sz_buf.at(total_read),
97 sizeof(pkt_sz) - total_read);
101 total_read +=
static_cast<uint64_t
>(n);
103 std::memcpy(&pkt_sz, sz_buf.data(),
sizeof(pkt_sz));
108 auto buf = std::vector<std::byte>(pkt_sz);
109 while(total_read < pkt_sz) {
110 const auto buf_sz = pkt_sz - total_read;
111 auto n = read(m_sock_fd, buf.data(), buf_sz);
116 total_read +=
static_cast<uint64_t
>(n);
117 pkt.append(buf.data(),
static_cast<size_t>(n));
128 return connect(*m_addr, m_port);
Buffer to store and retrieve byte data.
auto reconnect() -> bool
Reconnects to the previously connected endpoint.
auto connected() const -> bool
Returns whether the socket successfully connected to an endpoint.
auto send(const buffer &pkt) const -> bool
Sends the given packet to the remote host.
void disconnect()
Closes the connection with the remote host and unblocks any blocking calls to this socket.
auto receive(buffer &pkt) const -> bool
Attempts to receive a packet from the remote host this function will block until there is data ready ...
auto connect(const endpoint_t &ep) -> bool
Attempts to connect to the given endpoint.
std::string ip_address
An IP addresses.
std::pair< ip_address, port_number_t > endpoint_t
[host name, port number].
unsigned short port_number_t
Port number.