CARLA
secondary.h
Go to the documentation of this file.
1 // Copyright (c) 2022 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/Buffer.h"
10 #include "carla/NonCopyable.h"
11 #include "carla/TypeTraits.h"
17 #include "carla/ThreadPool.h"
18 
19 #include <boost/asio/deadline_timer.hpp>
20 #include <boost/asio/io_context.hpp>
21 #include <boost/asio/ip/tcp.hpp>
22 #include <boost/asio/strand.hpp>
23 
24 #include <atomic>
25 #include <functional>
26 #include <memory>
27 
28 namespace carla {
29 
30  class BufferPool;
31 
32 namespace multigpu {
33 
34  class Secondary
35  : public std::enable_shared_from_this<Secondary>,
37  private NonCopyable {
38  public:
39 
40  using endpoint = boost::asio::ip::tcp::endpoint;
41  using protocol_type = endpoint::protocol_type;
42 
43  Secondary(boost::asio::ip::tcp::endpoint ep, SecondaryCommands::callback_type callback);
44  Secondary(std::string ip, uint16_t port, SecondaryCommands::callback_type callback);
45  ~Secondary();
46 
47  void Connect();
48 
49  void Stop();
50 
51  void AsyncRun(size_t worker_threads);
52 
53  void Write(std::shared_ptr<const carla::streaming::detail::tcp::Message> message);
54  void Write(Buffer buffer);
55  void Write(std::string text);
56 
58  return _commander;
59  }
60 
61  template <typename... Buffers>
62  static auto MakeMessage(Buffers... buffers) {
63  static_assert(
65  "This function only accepts arguments of type BufferView.");
66  return std::make_shared<const carla::streaming::detail::tcp::Message>(buffers...);
67  }
68 
69  private:
70 
71  void Reconnect();
72 
73  void ReadData();
74 
76  boost::asio::ip::tcp::socket _socket;
77  boost::asio::ip::tcp::endpoint _endpoint;
78  boost::asio::io_context::strand _strand;
79  boost::asio::deadline_timer _connection_timer;
80  std::shared_ptr<BufferPool> _buffer_pool;
81  std::atomic_bool _done {false};
83  };
84 
85 } // namespace multigpu
86 } // namespace carla
boost::asio::io_context::strand _strand
Definition: secondary.h:78
SecondaryCommands _commander
Definition: secondary.h:82
boost::asio::ip::tcp::endpoint endpoint
Definition: secondary.h:40
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
boost::asio::ip::tcp::endpoint _endpoint
Definition: secondary.h:77
boost::asio::deadline_timer _connection_timer
Definition: secondary.h:79
std::atomic_bool _done
Definition: secondary.h:81
std::function< void(MultiGPUCommand, carla::Buffer)> callback_type
void AsyncRun(size_t worker_threads)
Definition: secondary.cpp:130
Secondary(boost::asio::ip::tcp::endpoint ep, SecondaryCommands::callback_type callback)
Definition: secondary.cpp:27
static auto MakeMessage(Buffers... buffers)
Definition: secondary.h:62
void Write(std::shared_ptr< const carla::streaming::detail::tcp::Message > message)
Definition: secondary.cpp:134
A thread pool based on Boost.Asio&#39;s io context.
Definition: ThreadPool.h:24
std::shared_ptr< BufferPool > _buffer_pool
Definition: secondary.h:80
A piece of raw data.
Definition: carla/Buffer.h:42
boost::asio::ip::tcp::socket _socket
Definition: secondary.h:76
SecondaryCommands & GetCommander()
Definition: secondary.h:57
Inherit (privately) to suppress copy/move construction and assignment.
endpoint::protocol_type protocol_type
Definition: secondary.h:41