OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
tcp_socket.hpp
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#ifndef OPENCBDC_TX_SRC_NETWORK_TCP_SOCKET_H_
7#define OPENCBDC_TX_SRC_NETWORK_TCP_SOCKET_H_
8
9#include "socket.hpp"
13
14#include <atomic>
15
16namespace cbdc::network {
24 class tcp_socket : public socket {
25 public:
27 tcp_socket() = default;
28 ~tcp_socket() override;
29
30 tcp_socket(const tcp_socket&) = delete;
31 auto operator=(const tcp_socket&) -> tcp_socket& = delete;
32
34 auto operator=(tcp_socket&&) -> tcp_socket& = delete;
35
39 auto connect(const endpoint_t& ep) -> bool;
40
46 auto connect(const ip_address& remote_address,
47 port_number_t remote_port) -> bool;
48
52 [[nodiscard]] auto send(const buffer& pkt) const -> bool;
53
57 template<typename T>
58 [[nodiscard]] auto send(const T& data) const -> bool {
59 auto pkt = make_buffer(data);
60 return send(pkt);
61 }
62
68 [[nodiscard]] auto receive(buffer& pkt) const -> bool;
69
72 void disconnect();
73
78 auto reconnect() -> bool;
79
85 [[nodiscard]] auto connected() const -> bool;
86
87 private:
88 std::optional<ip_address> m_addr{};
89 port_number_t m_port{};
90 std::atomic_bool m_connected{false};
91 };
92}
93
94#endif
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
Generic superclass for network sockets.
Definition socket.hpp:30
Wrapper for a TCP socket.
auto send(const T &data) const -> bool
Serialize the data and transmit it in a packet to the remote host.
auto reconnect() -> bool
Reconnects to the previously connected endpoint.
tcp_socket(tcp_socket &&)=delete
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.
auto operator=(const tcp_socket &) -> tcp_socket &=delete
tcp_socket(const tcp_socket &)=delete
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.
tcp_socket()=default
Constructs an empty, unconnected TCP socket.
auto operator=(tcp_socket &&) -> tcp_socket &=delete
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
auto make_buffer(const T &obj) -> std::enable_if_t< std::is_same_v< B, nuraft::ptr< nuraft::buffer > >, nuraft::ptr< nuraft::buffer > >
Serialize object into nuraft::buffer using a cbdc::nuraft_serializer.