23 #ifndef CLOE_UTILITY_TCP_TRANSCEIVER_HPP_ 24 #define CLOE_UTILITY_TCP_TRANSCEIVER_HPP_ 32 #include <boost/asio.hpp> 64 TcpTransceiver(
const std::string& host, uint16_t port) { tcp_connect(host, port); }
73 tcp_stream_.connect(host, std::to_string(port));
75 throw std::ios_base::failure(tcp_stream_.error().message());
77 tcp_connected_ =
true;
94 bool tcp_is_ok()
const {
return static_cast<bool>(tcp_stream_); }
103 tcp_connected_ =
false;
106 uint16_t tcp_port()
const {
return tcp_port_; }
107 const std::string& tcp_host()
const {
return tcp_host_; }
108 std::string tcp_endpoint()
const {
return fmt::format(
"tcp://{}:{}", tcp_host_, tcp_port_); }
115 return tcp_stream_.rdbuf()->in_avail() + tcp_stream_.rdbuf()->available();
118 template <
typename M>
119 void tcp_send(M* msg,
size_t sz) {
120 tcp_stream_.write(reinterpret_cast<const char*>(msg), sz);
125 boost::asio::ip::tcp::iostream tcp_stream_;
126 bool tcp_connected_{
false};
127 std::string tcp_host_{};
128 uint16_t tcp_port_{};
150 template <
typename T>
155 config_.retry_attempts = attempts;
156 config_.retry_delay = delay;
162 int retry_attempts()
const {
return config_.retry_attempts; }
163 void set_retry_attempts(
int attempts) { config_.retry_attempts = attempts; }
165 std::chrono::duration<float> retry_delay()
const {
return config_.retry_delay; }
166 void set_retry_delay(std::chrono::duration<float> delay) { config_.retry_delay = delay; }
171 std::unique_ptr<T>
create_or_null(
const std::string& host, uint16_t port)
const {
173 return this->create_or_throw(host, port);
174 }
catch (std::ios_base::failure&) {
175 return std::unique_ptr<T>{
nullptr};
184 for (int32_t attempts = 0;
true; attempts++) {
187 factory_logger()->info(
"{} connect tcp://{}:{}", instance_name(), host, port);
189 factory_logger()->info(
"{} connect tcp://{}:{} [attempt {}/{}]", instance_name(), host,
190 port, attempts + 1, config_.retry_attempts + 1);
192 return std::make_unique<T>(host, port);
193 }
catch (std::ios_base::failure&) {
194 if (attempts == config_.retry_attempts) {
198 std::this_thread::sleep_for(config_.retry_delay);
209 for (int32_t attempts = 0;
true; attempts++) {
212 factory_logger()->info(
"{} connect tcp://{}:{}", instance_name(), host, port);
214 factory_logger()->info(
"{} connect tcp://{}:{} [attempt {}/{}]", instance_name(), host,
215 port, attempts + 1, config_.retry_attempts + 1);
217 return std::make_unique<T>(host, port);
218 }
catch (std::ios_base::failure&) {
219 if (attempts == config_.retry_attempts) {
224 std::this_thread::sleep_for(config_.retry_delay);
232 virtual Logger factory_logger()
const = 0;
233 virtual const char* instance_name()
const = 0;
244 template <
typename F>
246 -> decltype(F{c}.create_or_throw(c.host, c.port)) {
247 return F{c}.create_or_throw(c.host, c.port);
255 template <
typename F>
257 -> decltype(F{c}.create_or_throw(c.host, c.port, sig)) {
258 return F{c}.create_or_throw(c.host, c.port, sig);
266 template <
typename F>
268 -> decltype(F{c}.create_or_null(c.host, c.port)) {
269 return F{c}.create_or_null(c.host, c.port);
275 #endif // CLOE_UTILITY_TCP_TRANSCEIVER_HPP_ void abort_checkpoint(AbortFlag &sig)
Definition: abort.hpp:60
auto create_or_null_with(const TcpTransceiverFullConfiguration &c) -> decltype(F
Definition: tcp_transceiver.hpp:267
void tcp_disconnect()
Definition: tcp_transceiver.hpp:101
std::atomic_bool AbortFlag
Definition: abort.hpp:42
Definition: coordinator.hpp:36
Definition: tcp_transceiver_config.hpp:84
bool tcp_is_connected() const
Definition: tcp_transceiver.hpp:87
Definition: tcp_transceiver_config.hpp:40
auto create_or_throw_with(const TcpTransceiverFullConfiguration &c) -> decltype(F
Definition: tcp_transceiver.hpp:245
Definition: tcp_transceiver.hpp:50
std::streamsize tcp_available_data() const
Definition: tcp_transceiver.hpp:114
std::unique_ptr< T > create_or_throw(const std::string &host, uint16_t port) const
Definition: tcp_transceiver.hpp:182
Definition: tcp_transceiver.hpp:61
Definition: tcp_transceiver.hpp:151
std::unique_ptr< T > create_or_throw(const std::string &host, uint16_t port, AbortFlag &sig) const
Definition: tcp_transceiver.hpp:207
void tcp_connect(const std::string &host, uint16_t port)
Definition: tcp_transceiver.hpp:71
std::unique_ptr< T > create_or_null(const std::string &host, uint16_t port) const
Definition: tcp_transceiver.hpp:171
bool tcp_is_ok() const
Definition: tcp_transceiver.hpp:94