OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
tcp_socket.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "tcp_socket.hpp"
7
8#include <array>
9#include <cstring>
10#include <unistd.h>
11
12namespace cbdc::network {
13 auto tcp_socket::connect(const endpoint_t& ep) -> bool {
14 return connect(ep.first, ep.second);
15 }
16
17 auto tcp_socket::connect(const ip_address& remote_address,
18 port_number_t remote_port) -> bool {
19 m_addr = remote_address;
20 m_port = remote_port;
21 auto res0 = get_addrinfo(remote_address, remote_port);
22 if(!res0) {
23 return false;
24 }
25
26 for(auto* res = res0.get(); res != nullptr; res = res->ai_next) {
27 if(!create_socket(res->ai_family,
28 res->ai_socktype,
29 res->ai_protocol)) {
30 continue;
31 }
32
33 if(::connect(m_sock_fd, res->ai_addr, res->ai_addrlen) != 0) {
34 ::close(m_sock_fd);
35 m_sock_fd = -1;
36 continue;
37 }
38
39 break;
40 }
41
42 m_connected = m_sock_fd != -1;
43
44 return m_connected;
45 }
46
48 if(m_sock_fd != -1) {
49 m_connected = false;
50 shutdown(m_sock_fd, SHUT_RDWR);
51 close(m_sock_fd);
52 m_sock_fd = -1;
53 }
54 }
55
59
60 auto tcp_socket::send(const buffer& pkt) const -> bool {
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);
69 if(n <= 0) {
70 return false;
71 }
72 total_written += static_cast<size_t>(n);
73 }
74
75 total_written = 0;
76 while(total_written
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);
81 if(n <= 0) {
82 return false;
83 }
84 total_written += static_cast<size_t>(n);
85 }
86
87 return true;
88 }
89
90 auto tcp_socket::receive(buffer& pkt) const -> bool {
91 uint64_t pkt_sz{};
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);
98 if(n <= 0) {
99 return false;
100 }
101 total_read += static_cast<uint64_t>(n);
102 }
103 std::memcpy(&pkt_sz, sz_buf.data(), sizeof(pkt_sz));
104
105 pkt.clear();
106
107 total_read = 0;
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);
112 if(n <= 0) {
113 return false;
114 }
115
116 total_read += static_cast<uint64_t>(n);
117 pkt.append(buf.data(), static_cast<size_t>(n));
118 }
119
120 return true;
121 }
122
123 auto tcp_socket::reconnect() -> bool {
124 disconnect();
125 if(!m_addr) {
126 return false;
127 }
128 return connect(*m_addr, m_port);
129 }
130
131 auto tcp_socket::connected() const -> bool {
132 return m_connected;
133 }
134}
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
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.
Definition socket.hpp:15
std::pair< ip_address, port_number_t > endpoint_t
[host name, port number].
Definition socket.hpp:19
unsigned short port_number_t
Port number.
Definition socket.hpp:17