Goby3  3.1.4
2024.02.22
tcp_server.h
Go to the documentation of this file.
1 // Copyright 2020-2021:
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...
42 namespace goby
43 {
44 namespace middleware
45 {
46 class Group;
47 }
48 } // namespace goby
49 
50 namespace goby
51 {
52 namespace middleware
53 {
54 namespace io
55 {
56 template <typename TCPServerThreadType>
57 class TCPSessionLineBased : public detail::TCPSession<TCPServerThreadType>
58 {
59  public:
60  TCPSessionLineBased(boost::asio::ip::tcp::socket socket, TCPServerThreadType& server)
61  : detail::TCPSession<TCPServerThreadType>(std::move(socket), server),
62  eol_matcher_(this->cfg().end_of_line())
63  {
64  }
65 
66  private:
67  void async_read() override
68  {
69  auto self(this->shared_from_this());
70  boost::asio::async_read_until(
71  this->mutable_socket(), buffer_, eol_matcher_,
72  [this, self](const boost::system::error_code& ec, std::size_t bytes_transferred) {
73  if (!ec && bytes_transferred > 0)
74  {
75  auto io_msg = std::make_shared<goby::middleware::protobuf::IOData>();
76  auto& bytes = *io_msg->mutable_data();
77  bytes = std::string(bytes_transferred, 0);
78  std::istream is(&buffer_);
79  is.read(&bytes[0], bytes_transferred);
80 
81  this->handle_read_success(bytes_transferred, io_msg);
82  async_read();
83  }
84  else
85  {
86  this->handle_read_error(ec);
87  }
88  });
89  }
90 
91  private:
92  match_regex eol_matcher_;
93  boost::asio::streambuf buffer_;
94 };
95 
96 template <const goby::middleware::Group& line_in_group,
97  const goby::middleware::Group& line_out_group,
98  // by default publish all incoming traffic to interprocess for logging
99  PubSubLayer publish_layer = PubSubLayer::INTERPROCESS,
100  // but only subscribe on interthread for outgoing traffic
101  PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
103  template <class> class ThreadType = goby::middleware::SimpleThread,
104  bool use_indexed_groups = false>
106  : public detail::TCPServerThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
107  Config, ThreadType, use_indexed_groups>
108 {
109  using Base = detail::TCPServerThread<line_in_group, line_out_group, publish_layer,
110  subscribe_layer, Config, ThreadType, use_indexed_groups>;
111 
112  public:
113  TCPServerThreadLineBased(const Config& config, int index = -1) : Base(config, index) {}
114 
115  private:
116  void start_session(boost::asio::ip::tcp::socket tcp_socket)
117  {
118  std::make_shared<TCPSessionLineBased<Base>>(std::move(tcp_socket), *this)->start();
119  }
120 };
121 
122 } // namespace io
123 } // namespace middleware
124 } // namespace goby
125 
126 #endif
io.pb.h
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::io::detail::TCPSession::TCPSession
TCPSession(boost::asio::ip::tcp::socket socket, TCPServerThreadType &server)
Definition: tcp_server_interface.h:78
goby::acomms::abc::protobuf::config
extern ::google::protobuf::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::google::protobuf::internal::MessageTypeTraits< ::goby::acomms::abc::protobuf::Config >, 11, false > config
Definition: abc_driver.pb.h:203
goby::middleware::Thread< goby::middleware::protobuf::TCPServerConfig, InterVehicleForwarder< InterProcessForwarder< InterThreadTransporter > > >::index
int index() const
Definition: thread.h:145
detail
detail namespace with internal helper functions
Definition: json.hpp:246
goby::middleware::io::TCPServerThreadLineBased::TCPServerThreadLineBased
TCPServerThreadLineBased(const Config &config, int index=-1)
Definition: tcp_server.h:113
tcp_server_interface.h
goby::middleware::protobuf::TCPServerConfig
Definition: tcp_config.pb.h:80
tcp_config.pb.h
goby::middleware::io::detail::TCPSession
Definition: tcp_server_interface.h:75
goby::middleware::io::detail::TCPSession::cfg
const TCPServerThreadType::ConfigType & cfg()
Definition: tcp_server_interface.h:170
goby::middleware::SimpleThread
Implements Thread for a three layer middleware setup ([ intervehicle [ interprocess [ interthread ] ]...
Definition: simple_thread.h:43
goby::middleware::io::PubSubLayer
PubSubLayer
Definition: io_transporters.h:38
goby::middleware::io::detail::TCPSession::handle_read_error
void handle_read_error(const boost::system::error_code &ec)
Definition: tcp_server_interface.h:162
goby::middleware::io::TCPServerThreadLineBased
Definition: tcp_server.h:105
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:58
goby::middleware::io::detail::TCPSession::mutable_socket
boost::asio::ip::tcp::socket & mutable_socket()
Definition: tcp_server_interface.h:172
goby::middleware::io::TCPSessionLineBased::TCPSessionLineBased
TCPSessionLineBased(boost::asio::ip::tcp::socket socket, TCPServerThreadType &server)
Definition: tcp_server.h:60
goby::middleware::io::TCPSessionLineBased
Definition: tcp_server.h:57
goby::middleware::io::detail::TCPServerThread
Definition: tcp_server_interface.h:188
goby::middleware::io::detail::TCPSession::handle_read_success
void handle_read_success(std::size_t bytes_transferred, std::shared_ptr< goby::middleware::protobuf::IOData > io_msg)
Definition: tcp_server_interface.h:153
io_interface.h
goby::middleware::io::PubSubLayer::INTERPROCESS
@ INTERPROCESS
common.h
goby::middleware::io::PubSubLayer::INTERTHREAD
@ INTERTHREAD