CARLA
streaming/detail/tcp/Client.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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"
14 
15 #include <boost/asio/deadline_timer.hpp>
16 #include <boost/asio/io_context.hpp>
17 #include <boost/asio/ip/tcp.hpp>
18 #include <boost/asio/strand.hpp>
19 
20 #include <atomic>
21 #include <functional>
22 #include <memory>
23 
24 namespace carla {
25 
26  class BufferPool;
27 
28 namespace streaming {
29 namespace detail {
30 namespace tcp {
31 
32  /// A client that connects to a single stream.
33  ///
34  /// @warning This client should be stopped before releasing the shared pointer
35  /// or won't be destroyed.
36  class Client
37  : public std::enable_shared_from_this<Client>,
39  private NonCopyable {
40  public:
41 
42  using endpoint = boost::asio::ip::tcp::endpoint;
43  using protocol_type = endpoint::protocol_type;
44  using callback_function_type = std::function<void (Buffer)>;
45 
46  Client(
47  boost::asio::io_context &io_context,
48  const token_type &token,
49  callback_function_type callback);
50 
51  ~Client();
52 
53  void Connect();
54 
56  return _token.get_stream_id();
57  }
58 
59  void Stop();
60 
61  private:
62 
63  void Reconnect();
64 
65  void ReadData();
66 
68 
70 
71  boost::asio::ip::tcp::socket _socket;
72 
73  boost::asio::io_context::strand _strand;
74 
75  boost::asio::deadline_timer _connection_timer;
76 
77  std::shared_ptr<BufferPool> _buffer_pool;
78 
79  std::atomic_bool _done{false};
80  };
81 
82 } // namespace tcp
83 } // namespace detail
84 } // namespace streaming
85 } // namespace carla
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
uint32_t stream_id_type
Definition: Types.h:18
A client that connects to a single stream.
Serializes a stream endpoint.
Definition: detail/Token.h:61
Client(boost::asio::io_context &io_context, const token_type &token, callback_function_type callback)
std::function< void(Buffer)> callback_function_type
const auto & get_stream_id() const
Definition: detail/Token.h:116
Inherit (privately) to suppress copy/move construction and assignment.