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 <functional>
20 #include <future>
21 #include <memory>
22 #include <string>
23 #include <string_view>
24 #include <vector>
25 
28 
29 namespace boost {
30 namespace asio {
31 class io_context;
32 }
33 } // namespace boost
34 
35 namespace unilink {
36 
37 namespace interface {
38 class Channel;
39 }
40 
41 namespace wrapper {
42 
47  public:
48  explicit TcpServer(uint16_t port);
49  TcpServer(uint16_t port, std::shared_ptr<boost::asio::io_context> external_ioc);
50  explicit TcpServer(std::shared_ptr<interface::Channel> channel);
51  ~TcpServer() override;
52 
53  // Move semantics
54  TcpServer(TcpServer&&) noexcept;
55  TcpServer& operator=(TcpServer&&) noexcept;
56 
57  // Disable copy
58  TcpServer(const TcpServer&) = delete;
59  TcpServer& operator=(const TcpServer&) = delete;
60 
61  // ServerInterface implementation
62  std::future<bool> start() override;
63  void stop() override;
64  bool is_listening() const override;
65 
66  // Transmission
67  bool broadcast(std::string_view data) override;
68  bool send_to(size_t client_id, std::string_view data) override;
69 
70  // Event handlers
71  ServerInterface& on_client_connect(ConnectionHandler handler) override;
72  ServerInterface& on_client_disconnect(ConnectionHandler handler) override;
73  ServerInterface& on_data(MessageHandler handler) override;
74  ServerInterface& on_error(ErrorHandler handler) override;
75 
76  // Client count and management
77  size_t get_client_count() const override;
78  std::vector<size_t> get_connected_clients() const override;
79 
80  // Configuration (Fluent API)
81  TcpServer& auto_manage(bool manage = true);
82  TcpServer& enable_port_retry(bool enable = true, int max_retries = 3, int retry_interval_ms = 1000);
83  TcpServer& idle_timeout(int timeout_ms);
84  TcpServer& set_client_limit(size_t max_clients);
85  TcpServer& set_unlimited_clients();
86  TcpServer& notify_send_failure(bool enable = true);
87  TcpServer& set_manage_external_context(bool manage);
88 
89  private:
90  struct Impl;
91  const Impl* get_impl() const { return impl_.get(); }
92  Impl* get_impl() { return impl_.get(); }
93  std::unique_ptr<Impl> impl_;
94 };
95 
96 } // namespace wrapper
97 } // namespace unilink
Definition: serial.hpp:27
#define UNILINK_API
Definition: visibility.hpp:37