Skip to content

Commit 05f7e16

Browse files
committed
tcp_server reorganization
- handle both IPv4 as well as IPv6 in a single `tcp_server` - `client_session`s store a `weak_ptr` to the `tcp_server`, thereby not keeping it alive longer than needed
1 parent 0b35dba commit 05f7e16

File tree

5 files changed

+218
-205
lines changed

5 files changed

+218
-205
lines changed

src/stream_outlet_impl.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,26 @@ stream_outlet_impl::stream_outlet_impl(const stream_info_impl &info, int32_t chu
2929

3030
// instantiate IPv4 and/or IPv6 stacks (depending on settings)
3131
if (cfg->allow_ipv4()) try {
32-
instantiate_stack(tcp::v4(), udp::v4());
32+
instantiate_stack(udp::v4());
3333
} catch (std::exception &e) {
3434
LOG_F(WARNING, "Could not instantiate IPv4 stack: %s", e.what());
3535
}
3636
if (cfg->allow_ipv6()) try {
37-
instantiate_stack(tcp::v6(), udp::v6());
37+
instantiate_stack(udp::v6());
3838
} catch (std::exception &e) {
3939
LOG_F(WARNING, "Could not instantiate IPv6 stack: %s", e.what());
4040
}
4141

42+
// create TCP data server
43+
tcp_server_ = std::make_shared<tcp_server>(info_, io_ctx_data_, send_buffer_, sample_factory_,
44+
chunk_size_, cfg->allow_ipv4(), cfg->allow_ipv6());
45+
4246
// fail if both stacks failed to instantiate
43-
if (tcp_servers_.empty() || udp_servers_.empty())
47+
if (udp_servers_.empty())
4448
throw std::runtime_error("Neither the IPv4 nor the IPv6 stack could be instantiated.");
4549

4650
// get the async request chains set up
47-
for (auto &tcp_server : tcp_servers_) tcp_server->begin_serving();
51+
tcp_server_->begin_serving();
4852
for (auto &udp_server : udp_servers_) udp_server->begin_serving();
4953
for (auto &responder : responders_) responder->begin_serving();
5054

@@ -64,16 +68,14 @@ stream_outlet_impl::stream_outlet_impl(const stream_info_impl &info, int32_t chu
6468
}));
6569
}
6670

67-
void stream_outlet_impl::instantiate_stack(tcp tcp_protocol, udp udp_protocol) {
71+
void stream_outlet_impl::instantiate_stack(udp udp_protocol) {
6872
// get api_config
6973
const api_config *cfg = api_config::get_instance();
7074
std::string listen_address = cfg->listen_address();
7175
int multicast_ttl = cfg->multicast_ttl();
7276
uint16_t multicast_port = cfg->multicast_port();
7377
LOG_F(2, "%s: Trying to listen at address '%s'", info().name().c_str(), listen_address.c_str());
74-
// create TCP data server
75-
tcp_servers_.push_back(std::make_shared<tcp_server>(
76-
info_, io_ctx_data_, send_buffer_, sample_factory_, tcp_protocol, chunk_size_));
78+
7779
// create UDP time server
7880
udp_servers_.push_back(std::make_shared<udp_server>(info_, *io_ctx_service_, udp_protocol));
7981
// create UDP multicast responders
@@ -94,7 +96,7 @@ void stream_outlet_impl::instantiate_stack(tcp tcp_protocol, udp udp_protocol) {
9496
stream_outlet_impl::~stream_outlet_impl() {
9597
try {
9698
// cancel all request chains
97-
for (auto &tcp_server : tcp_servers_) tcp_server->end_serving();
99+
tcp_server_->end_serving();
98100
for (auto &udp_server : udp_servers_) udp_server->end_serving();
99101
for (auto &responder : responders_) responder->end_serving();
100102

src/stream_outlet_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class stream_outlet_impl {
296296

297297
private:
298298
/// Instantiate a new server stack.
299-
void instantiate_stack(tcp tcp_protocol, udp udp_protocol);
299+
void instantiate_stack(udp udp_protocol);
300300

301301
/// Allocate and enqueue a new sample into the send buffer.
302302
template <class T> void enqueue(const T *data, double timestamp, bool pushthrough);
@@ -322,8 +322,8 @@ class stream_outlet_impl {
322322
/// the IO service objects
323323
io_context_p io_ctx_data_, io_ctx_service_;
324324

325-
/// the threaded TCP data server(s); two if using both IP stacks
326-
std::vector<tcp_server_p> tcp_servers_;
325+
/// the threaded TCP data server
326+
tcp_server_p tcp_server_;
327327
/// the UDP timing & ident service(s); two if using both IP stacks
328328
std::vector<udp_server_p> udp_servers_;
329329
/// UDP multicast responders for service discovery (time features disabled);

0 commit comments

Comments
 (0)