unilink  0.4.3
A simple C++ library for unified async communication
tcp_server_session.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <array>
20 #include <atomic>
21 #include <boost/asio.hpp>
22 #include <cstdint>
23 #include <deque>
24 #include <functional>
25 #include <memory>
26 #include <optional>
27 #include <variant>
28 #include <vector>
29 
39 
40 namespace unilink {
41 namespace transport {
42 
43 namespace net = boost::asio;
44 
45 using base::LinkState;
46 using interface::TcpSocketInterface;
47 using tcp = net::ip::tcp;
48 
49 class UNILINK_API TcpServerSession : public std::enable_shared_from_this<TcpServerSession> {
50  public:
53  using OnClose = std::function<void()>;
54  using BufferVariant =
55  std::variant<memory::PooledBuffer, std::vector<uint8_t>, std::shared_ptr<const std::vector<uint8_t>>>;
56 
57  TcpServerSession(net::io_context& ioc, tcp::socket sock,
58  size_t backpressure_threshold = common::constants::DEFAULT_BACKPRESSURE_THRESHOLD,
59  int idle_timeout_ms = 0);
60  // Constructor for testing with dependency injection
61  TcpServerSession(net::io_context& ioc, std::unique_ptr<interface::TcpSocketInterface> socket,
62  size_t backpressure_threshold = common::constants::DEFAULT_BACKPRESSURE_THRESHOLD,
63  int idle_timeout_ms = 0);
64 
65  void start();
66  void async_write_copy(memory::ConstByteSpan data);
67  void async_write_move(std::vector<uint8_t>&& data);
68  void async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data);
69  void on_bytes(OnBytes cb);
70  void on_backpressure(OnBackpressure cb);
71  void on_close(OnClose cb);
72  bool alive() const;
73  void stop();
74  void cancel();
75 
76  private:
77  void start_read();
78  void do_write();
79  void do_close();
80  void report_backpressure(size_t queued_bytes);
81  void reset_idle_timer();
82 
83  private:
84  net::io_context& ioc_;
85  net::strand<net::io_context::executor_type> strand_;
86  net::steady_timer idle_timer_;
87  std::unique_ptr<interface::TcpSocketInterface> socket_;
88  std::array<uint8_t, common::constants::DEFAULT_READ_BUFFER_SIZE> rx_{};
89  std::deque<BufferVariant> tx_;
90  std::optional<BufferVariant> current_write_buffer_;
91  bool writing_ = false;
92  size_t queue_bytes_ = 0;
93  size_t bp_high_; // Configurable backpressure threshold
94  size_t bp_limit_; // Hard cap for queued bytes
95  size_t bp_low_; // Backpressure relief threshold
96  bool backpressure_active_ = false;
97  int idle_timeout_ms_ = 0;
98 
99  OnBytes on_bytes_;
100  OnBackpressure on_bp_;
101  OnClose on_close_;
102  std::atomic<bool> alive_{false};
103  std::atomic<bool> closing_{false};
104  std::atomic<bool> cleanup_done_{false};
105 };
106 } // namespace transport
107 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37