Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
tcp_server.h
Go to the documentation of this file.
1// Copyright 2020-2022:
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_COBS_TCP_SERVER_H
25#define GOBY_MIDDLEWARE_IO_COBS_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
38#include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer
39#include "goby/middleware/io/detail/tcp_server_interface.h" // for TCPServe...
40#include "goby/middleware/protobuf/io.pb.h" // for IOData
41#include "goby/middleware/protobuf/tcp_config.pb.h" // for TCPServe...
42
43namespace goby
44{
45namespace middleware
46{
47class Group;
48}
49} // namespace goby
50
51namespace goby
52{
53namespace middleware
54{
55namespace io
56{
57template <typename TCPServerThreadType>
58class TCPSessionCOBS : public detail::TCPSession<TCPServerThreadType>
59{
60 public:
61 TCPSessionCOBS(boost::asio::ip::tcp::socket socket, TCPServerThreadType& server)
62 : detail::TCPSession<TCPServerThreadType>(std::move(socket), server)
63 {
64 }
65
66 template <class Thread>
67 friend void cobs_async_write(Thread* this_thread,
68 std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg);
69
70 template <class Thread, class ThreadBase>
71 friend void cobs_async_read(Thread* this_thread, std::shared_ptr<ThreadBase> self);
72
73 private:
74 void async_read() override
75 {
76 auto self(this->shared_from_this());
77 cobs_async_read(this, self);
78 }
79
80 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override
81 {
82 cobs_async_write(this, io_msg);
83 }
84
85 private:
86 boost::asio::streambuf buffer_;
87};
88
89template <const goby::middleware::Group& packet_in_group,
90 const goby::middleware::Group& packet_out_group,
91 // by default publish all incoming traffic to interprocess for logging
93 // but only subscribe on interthread for outgoing traffic
94 PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
96 template <class> class ThreadType = goby::middleware::SimpleThread,
97 bool use_indexed_groups = false>
99 : public detail::TCPServerThread<packet_in_group, packet_out_group, publish_layer,
100 subscribe_layer, Config, ThreadType, use_indexed_groups>
101{
102 using Base = detail::TCPServerThread<packet_in_group, packet_out_group, publish_layer,
103 subscribe_layer, Config, ThreadType, use_indexed_groups>;
104
105 public:
106 TCPServerThreadCOBS(const Config& config, int index = -1) : Base(config, index) {}
107
108 private:
109 void start_session(boost::asio::ip::tcp::socket tcp_socket)
110 {
111 std::make_shared<TCPSessionCOBS<Base>>(std::move(tcp_socket), *this)->start();
112 }
113};
114
115} // namespace io
116} // namespace middleware
117} // namespace goby
118
119#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
Implements Thread for a three layer middleware setup ([ intervehicle [ interprocess [ interthread ] ]...
Represents a thread of execution within the Goby middleware, interleaving periodic events (loop()) wi...
Definition thread.h:61
TCPServerThreadCOBS(const Config &config, int index=-1)
Definition tcp_server.h:106
TCPSessionCOBS(boost::asio::ip::tcp::socket socket, TCPServerThreadType &server)
Definition tcp_server.h:61
friend void cobs_async_write(Thread *this_thread, std::shared_ptr< const goby::middleware::protobuf::IOData > io_msg)
Definition common.h:44
friend void cobs_async_read(Thread *this_thread, std::shared_ptr< ThreadBase > self)
Definition common.h:90
TCPSession(boost::asio::ip::tcp::socket socket, TCPServerThreadType &server)
detail namespace with internal helper functions
Definition json.hpp:247
The global namespace for the Goby project.
STL namespace.