1#ifndef MSTL_NETWORK_TCP_TCP_SERVER_HPP__
2#define MSTL_NETWORK_TCP_TCP_SERVER_HPP__
5#include "MSTL/core/container/vector.hpp"
7#include "ssl_socket.hpp"
10class MSTL_API tcp_server {
12#ifdef MSTL_SUPPORT_OPENSSL__
13 using handle_sock_t = ssl_socket;
15 using handle_sock_t = tcp_socket;
18 using client_handler_t = function<void(handle_sock_t)>;
21 tcp_socket server_socket_{};
22#ifdef MSTL_SUPPORT_OPENSSL__
23 ssl_context ssl_ctx_{};
29 vector<_MSTL thread> worker_threads_{};
31 client_handler_t client_handler_{};
34 void start_workers(
int thread_count);
38 explicit tcp_server(
uint16_t port,
int backlog = 128);
39 ~tcp_server() { stop(); }
41#ifdef MSTL_SUPPORT_OPENSSL__
42 bool load_certificate(
const string& cert_file,
const string& key_file);
45 void set_client_handler(client_handler_t handler)
noexcept {
48 MSTL_NODISCARD
const client_handler_t& client_handler() const noexcept {
49 return client_handler_;
52 MSTL_NODISCARD
uint16_t port() const noexcept {
55 MSTL_NODISCARD
bool is_running() const noexcept {
59 bool start(SOCKET_DOMAIN domain = SOCKET_DOMAIN::IPV4,
60 SOCKET_TYPE type = SOCKET_TYPE::STREAM,
61 SOCKET_PROTOCOL protocol = SOCKET_PROTOCOL::AUTO,
atomic< bool > atomic_bool
布尔原子类型
unsigned short uint16_t
16位无符号整数类型
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素