/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};
21  std::chrono::steady_clock::duration sample_kernel_queue_period;
22 };
23 
24 class TcpConnection : public std::enable_shared_from_this<TcpConnection> {
25  public:
26  using tcp = boost::asio::ip::tcp;
27 
29  TcpConnectionConfig const& config,
30  boost::asio::io_context& io,
31  tcp::socket&& socket,
32  std::unique_ptr<IFramer>&& framer,
33  std::weak_ptr<IConnectionHandler> handler,
34  TcpTelemetry& server_telemetry);
35 
37 
38  void start();
39  void sendFrame(std::shared_ptr<std::vector<uint8_t> const> frame);
40  void close();
41  void requestRead();
42  std::string localEndpoint() const;
43  std::string remoteEndpoint() const;
44  TcpConnectionId id() const { return config_.id; }
45  bool isOpen() const { return !closed_; }
46 
47  private:
48  void doRead();
49  void doWrite();
50 
51  // Sample the kernel queues periodically.
52  // Ensures telemetry is not stale on a quiescent connection.
53  void doSampleKernelQueue();
54 
55  TcpConnectionConfig config_;
56  boost::asio::io_context& io_;
57  boost::asio::strand<boost::asio::any_io_executor> strand_;
58  tcp::socket socket_;
59  std::unique_ptr<IFramer> framer_;
60  std::weak_ptr<IConnectionHandler> handler_;
61  TcpTelemetry& telemetry_;
62  boost::asio::steady_timer sample_kernel_queue_timer_;
63  bool is_reading_{false};
64  std::atomic<bool> closing_{false};
65  std::atomic<bool> closed_{false};
66 
67  // Bytes read from the socket
68  std::vector<uint8_t> read_buffer_;
69 
70  // Queued data to write to the socket
71  WriteQueue send_queue_;
72 };
73 
74 } // namespace sol3::net
Definition: tcp_connection.h:24
~TcpConnection()
Definition: tcp_connection.h:36
bool isOpen() const
Definition: tcp_connection.h:45
std::string remoteEndpoint() const
std::string localEndpoint() const
TcpConnectionId id() const
Definition: tcp_connection.h:44
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:26
Definition: tcp_telemetry.h:15
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
std::chrono::steady_clock::duration sample_kernel_queue_period
Definition: tcp_connection.h:21