unilink  0.4.3
A simple C++ library for unified async communication
tcp_server.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 <memory>
20 #include <string>
21 #include <vector>
22 
26 
27 namespace boost {
28 namespace asio {
29 class io_context;
30 }
31 } // namespace boost
32 
33 namespace unilink {
34 
35 namespace interface {
36 class TcpAcceptorInterface;
37 }
38 
39 namespace transport {
40 
44 class UNILINK_API TcpServer : public interface::Channel, public std::enable_shared_from_this<TcpServer> {
45  public:
46  static std::shared_ptr<TcpServer> create(const config::TcpServerConfig& cfg);
47  static std::shared_ptr<TcpServer> create(const config::TcpServerConfig& cfg,
48  std::unique_ptr<interface::TcpAcceptorInterface> acceptor,
49  boost::asio::io_context& ioc);
50  ~TcpServer() override;
51 
52  // Move semantics
53  TcpServer(TcpServer&&) noexcept;
54  TcpServer& operator=(TcpServer&&) noexcept;
55 
56  // Non-copyable
57  TcpServer(const TcpServer&) = delete;
58  TcpServer& operator=(const TcpServer&) = delete;
59 
60  // Channel implementation
61  void start() override;
62  void stop() override;
63  bool is_connected() const override;
64  void async_write_copy(memory::ConstByteSpan data) override;
65  void async_write_move(std::vector<uint8_t>&& data) override;
66  void async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
67  void on_bytes(OnBytes cb) override;
68  void on_state(OnState cb) override;
69  void on_backpressure(OnBackpressure cb) override;
70 
71  // Multi-client support
72  bool broadcast(const std::string& message);
73  bool send_to_client(size_t client_id, const std::string& message);
74  size_t get_client_count() const;
75  std::vector<size_t> get_connected_clients() const;
76 
77  void request_stop();
78 
79  using MultiClientConnectHandler = std::function<void(size_t client_id, const std::string& client_info)>;
80  using MultiClientDataHandler = std::function<void(size_t client_id, const std::string& data)>;
81  using MultiClientDisconnectHandler = std::function<void(size_t client_id)>;
82 
83  void on_multi_connect(MultiClientConnectHandler handler);
84  void on_multi_data(MultiClientDataHandler handler);
85  void on_multi_disconnect(MultiClientDisconnectHandler handler);
86 
87  void set_client_limit(size_t max_clients);
88  void set_unlimited_clients();
89 
90  base::LinkState get_state() const;
91 
92  private:
93  explicit TcpServer(const config::TcpServerConfig& cfg);
94  TcpServer(const config::TcpServerConfig& cfg, std::unique_ptr<interface::TcpAcceptorInterface> acceptor,
95  boost::asio::io_context& ioc);
96 
97  struct Impl;
98  const Impl* get_impl() const { return impl_.get(); }
99  Impl* get_impl() { return impl_.get(); }
100  std::unique_ptr<Impl> impl_;
101 };
102 } // namespace transport
103 } // namespace unilink
Definition: serial.hpp:27
#define UNILINK_API
Definition: visibility.hpp:37