Goby3  3.1.5a
2024.05.23
tcp_client.h
Go to the documentation of this file.
1 // Copyright 2019-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_CLIENT_H
25 #define GOBY_MIDDLEWARE_IO_LINE_BASED_TCP_CLIENT_H
26 
27 #include <istream> // for istream
28 #include <memory> // for make_shared
29 #include <string> // for basic_st...
30 
31 #include <boost/asio/read_until.hpp> // for async_re...
32 #include <boost/asio/streambuf.hpp> // for streambuf
33 #include <boost/system/error_code.hpp> // for error_code
34 
35 #include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer
36 #include "goby/middleware/io/detail/tcp_client_interface.h" // for TCPClien...
37 #include "goby/middleware/io/line_based/common.h" // for match_regex
38 #include "goby/middleware/protobuf/io.pb.h" // for IOData
39 
40 namespace goby
41 {
42 namespace middleware
43 {
44 class Group;
45 }
46 } // namespace goby
47 namespace goby
48 {
49 namespace middleware
50 {
51 namespace protobuf
52 {
53 class TCPClientConfig;
54 }
55 } // namespace middleware
56 } // namespace goby
57 
58 namespace goby
59 {
60 namespace middleware
61 {
62 namespace io
63 {
67 template <const goby::middleware::Group& line_in_group,
68  const goby::middleware::Group& line_out_group,
69  PubSubLayer publish_layer = PubSubLayer::INTERPROCESS,
70  PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
72  template <class> class ThreadType = goby::middleware::SimpleThread,
73  bool use_indexed_groups = false>
75  : public detail::TCPClientThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
76  Config, ThreadType, use_indexed_groups>
77 {
78  using Base = detail::TCPClientThread<line_in_group, line_out_group, publish_layer,
79  subscribe_layer, Config, ThreadType, use_indexed_groups>;
80 
81  public:
86  int index = -1)
87  : Base(config, index), eol_matcher_(this->cfg().end_of_line())
88  {
89  }
90 
92 
93  private:
95  void async_read() override;
96 
97  private:
98  match_regex eol_matcher_;
99  boost::asio::streambuf buffer_;
100 };
101 } // namespace io
102 } // namespace middleware
103 } // namespace goby
104 
105 template <const goby::middleware::Group& line_in_group,
106  const goby::middleware::Group& line_out_group,
107  goby::middleware::io::PubSubLayer publish_layer,
108  goby::middleware::io::PubSubLayer subscribe_layer, typename Config,
109  template <class> class ThreadType, bool use_indexed_groups>
110 void goby::middleware::io::TCPClientThreadLineBased<line_in_group, line_out_group, publish_layer,
111  subscribe_layer, Config, ThreadType,
112  use_indexed_groups>::async_read()
113 {
114  boost::asio::async_read_until(
115  this->mutable_socket(), buffer_, eol_matcher_,
116  [this](const boost::system::error_code& ec, std::size_t bytes_transferred) {
117  if (!ec && bytes_transferred > 0)
118  {
119  auto io_msg = std::make_shared<goby::middleware::protobuf::IOData>();
120  auto& bytes = *io_msg->mutable_data();
121  bytes = std::string(bytes_transferred, 0);
122  std::istream is(&buffer_);
123  is.read(&bytes[0], bytes_transferred);
124  this->insert_endpoints(io_msg);
125  this->handle_read_success(bytes_transferred, io_msg);
126  this->async_read();
127  }
128  else
129  {
130  this->handle_read_error(ec);
131  }
132  });
133 }
134 
135 #endif
io.pb.h
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::Thread< goby::middleware::protobuf::TCPClientConfig, InterVehicleForwarder< InterProcessForwarder< InterThreadTransporter > > >::cfg
const goby::middleware::protobuf::TCPClientConfig & cfg() const
Definition: thread.h:201
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::protobuf::TCPClientConfig
Definition: tcp_config.pb.h:235
goby::middleware::Thread< goby::middleware::protobuf::TCPClientConfig, InterVehicleForwarder< InterProcessForwarder< InterThreadTransporter > > >::index
int index() const
Definition: thread.h:145
goby::middleware::io::TCPClientThreadLineBased::~TCPClientThreadLineBased
~TCPClientThreadLineBased()
Definition: tcp_client.h:91
tcp_client_interface.h
goby::middleware::io::detail::TCPClientThread
Definition: tcp_client_interface.h:55
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::TCPClientThreadLineBased
Reads/Writes strings from/to a TCP connection using a line-based (typically ASCII) protocol with a de...
Definition: tcp_client.h:74
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
io_interface.h
goby::middleware::io::PubSubLayer::INTERPROCESS
@ INTERPROCESS
common.h
goby::middleware::io::TCPClientThreadLineBased::TCPClientThreadLineBased
TCPClientThreadLineBased(const goby::middleware::protobuf::TCPClientConfig &config, int index=-1)
Constructs the thread.
Definition: tcp_client.h:85
goby::middleware::io::match_regex
Provides a matching function object for the boost::asio::async_read_until based on a std::regex.
Definition: common.h:52
goby::middleware::io::PubSubLayer::INTERTHREAD
@ INTERTHREAD