Goby3 3.4.0
2026.04.13
Loading...
Searching...
No Matches
tcp_server.h
Go to the documentation of this file.
1// Copyright 2020-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_IO_LINE_BASED_TCP_SERVER_H
25#define GOBY_MIDDLEWARE_IO_LINE_BASED_TCP_SERVER_H
26
27#include <istream> // for istream
28#include <memory> // for make_shared
29#include <string> // for basic_st...
30#include <utility> // for move
31
32#include <boost/asio/ip/tcp.hpp> // for tcp, tcp...
33#include <boost/asio/read_until.hpp> // for async_re...
34#include <boost/asio/streambuf.hpp> // for streambuf
35#include <boost/system/error_code.hpp> // for error_code
36
37#include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer
38#include "goby/middleware/io/detail/tcp_server_interface.h" // for TCPServe...
39#include "goby/middleware/io/line_based/common.h" // for match_regex
40#include "goby/middleware/protobuf/io.pb.h" // for IOData
41#include "goby/middleware/protobuf/tcp_config.pb.h" // for TCPServe...
43
44namespace goby
45{
46namespace middleware
47{
48class Group;
49}
50} // namespace goby
51
52namespace goby
53{
54namespace middleware
55{
56namespace io
57{
58template <typename TCPServerThreadType>
59class TCPSessionLineBased : public detail::TCPSession<TCPServerThreadType>
60{
61 public:
62 TCPSessionLineBased(boost::asio::ip::tcp::socket socket, TCPServerThreadType& server)
63 : detail::TCPSession<TCPServerThreadType>(std::move(socket), server),
64 eol_matcher_(this->cfg().end_of_line())
65 {
66 }
67
68 private:
69 void async_read() override
70 {
71 auto self(this->shared_from_this());
72 boost::asio::async_read_until(
73 this->mutable_socket(), buffer_, eol_matcher_,
74 [this, self](const boost::system::error_code& ec, std::size_t bytes_transferred)
75 {
76 if (!ec && bytes_transferred > 0)
77 {
78 auto io_msg = std::make_shared<goby::middleware::protobuf::IOData>();
79 auto& bytes = *io_msg->mutable_data();
80 bytes = std::string(bytes_transferred, 0);
81 std::istream is(&buffer_);
82 is.read(&bytes[0], bytes_transferred);
83
84 this->handle_read_success(bytes_transferred, io_msg);
85 async_read();
86 }
87 else
88 {
89 this->handle_read_error(ec);
90 }
91 });
92 }
93
94 private:
95 match_regex eol_matcher_;
96 boost::asio::streambuf buffer_;
97};
98
99template <
100 const goby::middleware::Group& line_in_group, const goby::middleware::Group& line_out_group,
101 // by default publish all incoming traffic to interprocess for logging
103 // but only subscribe on interthread for outgoing traffic
104 PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
106 template <class> class ThreadType = goby::zeromq::SimpleThread, bool use_indexed_groups = false>
108 : public detail::TCPServerThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
109 Config, ThreadType, use_indexed_groups>
110{
111 using Base = detail::TCPServerThread<line_in_group, line_out_group, publish_layer,
112 subscribe_layer, Config, ThreadType, use_indexed_groups>;
113
114 public:
115 TCPServerThreadLineBased(const Config& config, int index = -1) : Base(config, index) {}
116
117 private:
118 void start_session(boost::asio::ip::tcp::socket tcp_socket)
119 {
120 std::make_shared<TCPSessionLineBased<Base>>(std::move(tcp_socket), *this)->start();
121 }
122};
123
124} // namespace io
125} // namespace middleware
126} // namespace goby
127
128#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
TCPServerThreadLineBased(const Config &config, int index=-1)
Definition tcp_server.h:115
TCPSessionLineBased(boost::asio::ip::tcp::socket socket, TCPServerThreadType &server)
Definition tcp_server.h:62
const TCPServerThreadType::ConfigType & cfg()
TCPSession(boost::asio::ip::tcp::socket socket, TCPServerThreadType &server)
void handle_read_error(const boost::system::error_code &ec)
boost::asio::ip::tcp::socket & mutable_socket()
void handle_read_success(std::size_t bytes_transferred, std::shared_ptr< goby::middleware::protobuf::IOData > io_msg)
detail namespace with internal helper functions
Definition json.hpp:247
middleware::SimpleThread< Config, detail::InterProcessTag > SimpleThread
Zeromq-backed SimpleThread. Derives from middleware::SimpleThread using InterProcessTag.
The global namespace for the Goby project.
STL namespace.