/workspaces/astro/sol3-sdk/cpp/sol3/net/tcp_connection.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/net/tcp_connection.h Source File
Space-ng SDK
tcp_connection.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 
3 #pragma once
4 
6 #include "sol3/net/framer.h"
9 #include "sol3/net/write_queue.h"
10 
11 #include <boost/asio/any_io_executor.hpp>
12 #include <boost/asio/ip/tcp.hpp>
13 #include <boost/asio/strand.hpp>
14 
15 namespace sol3::net {
16 
20  bool automatic_read{true};
23  bool graceful_eof{false};
24  std::chrono::steady_clock::duration sample_kernel_queue_period;
25 };
26 
27 class TcpConnection : public std::enable_shared_from_this<TcpConnection> {
28  public:
29  using tcp = boost::asio::ip::tcp;
30 
32  TcpConnectionConfig const& config,
33  boost::asio::io_context& io,
34  tcp::socket&& socket,
35  std::unique_ptr<IFramer>&& framer,
36  std::weak_ptr<IConnectionHandler> handler,
37  TcpTelemetry& server_telemetry);
38 
40 
41  void start();
42  void sendFrame(std::shared_ptr<std::vector<uint8_t> const> frame);
43  void close();
44  void requestRead();
47  TcpConnectionId id() const { return config_.id; }
48  bool isOpen() const { return !closed_; }
49 
50  private:
51  void doRead();
52  void doWrite();
53 
54  // Sample the kernel queues periodically.
55  // Ensures telemetry is not stale on a quiescent connection.
56  void doSampleKernelQueue();
57 
58  TcpConnectionConfig config_;
59  boost::asio::io_context& io_;
60  boost::asio::strand<boost::asio::any_io_executor> strand_;
61  tcp::socket socket_;
62  std::unique_ptr<IFramer> framer_;
63  std::weak_ptr<IConnectionHandler> handler_;
64  TcpTelemetry& telemetry_;
65  boost::asio::steady_timer sample_kernel_queue_timer_;
66  bool is_reading_{false};
67  std::atomic<bool> closing_{false};
68  std::atomic<bool> closed_{false};
69 
70  // Bytes read from the socket
71  std::vector<uint8_t> read_buffer_;
72 
73  // Queued data to write to the socket
74  WriteQueue send_queue_;
75 };
76 
77 } // namespace sol3::net
Definition: tcp_connection.h:27
~TcpConnection()
Definition: tcp_connection.h:39
bool isOpen() const
Definition: tcp_connection.h:48
std::string remoteEndpoint() const
std::string localEndpoint() const
TcpConnectionId id() const
Definition: tcp_connection.h:47
TcpConnection(TcpConnectionConfig const &config, boost::asio::io_context &io, tcp::socket &&socket, std::unique_ptr< IFramer > &&framer, std::weak_ptr< IConnectionHandler > handler, TcpTelemetry &server_telemetry)
void sendFrame(std::shared_ptr< std::vector< uint8_t > const > frame)
boost::asio::ip::tcp tcp
Definition: tcp_connection.h:29
Definition: tcp_telemetry.h:15
enum ArtifactFormat string
Camera/host holding the original file.
auto handler(TClass *obj, TCallable &&callable)
Definition: handlers.h:161
Definition: buffer_pool.h:12
uint64_t TcpConnectionId
A unique identifier for a TCP connection.
Definition: tcp_connection_id.h:10
Definition: tcp_connection.h:17
bool automatic_read
Definition: tcp_connection.h:20
TcpConnectionId id
Definition: tcp_connection.h:18
size_t read_buffer_bytes
Definition: tcp_connection.h:19
bool graceful_eof
Definition: tcp_connection.h:23
std::chrono::steady_clock::duration sample_kernel_queue_period
Definition: tcp_connection.h:24