|
|
@ -17,117 +17,113 @@
|
|
|
|
Wu Jiaxu (wujiaxu@sogou-inc.com)
|
|
|
|
Wu Jiaxu (wujiaxu@sogou-inc.com)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _WFSERVER_H_ // 防止头文件被多次包含
|
|
|
|
#ifndef _WFSERVER_H_
|
|
|
|
#define _WFSERVER_H_
|
|
|
|
#define _WFSERVER_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h> // 引入基本数据类型定义
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h> // 引入 socket 相关的函数
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <errno.h> // 引入错误码定义
|
|
|
|
#include <errno.h>
|
|
|
|
#include <functional> // 引入函数对象
|
|
|
|
#include <functional>
|
|
|
|
#include <atomic> // 引入原子操作的支持
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex> // 引入互斥量支持
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable> // 引入条件变量支持
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <openssl/ssl.h> // 引入 OpenSSL 以支持 SSL/TLS
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include "EndpointParams.h" // 引入端点参数定义
|
|
|
|
#include "EndpointParams.h"
|
|
|
|
#include "WFTaskFactory.h" // 引入任务工厂以创建任务
|
|
|
|
#include "WFTaskFactory.h"
|
|
|
|
|
|
|
|
|
|
|
|
// 定义服务器参数结构体
|
|
|
|
|
|
|
|
struct WFServerParams
|
|
|
|
struct WFServerParams
|
|
|
|
{
|
|
|
|
{
|
|
|
|
enum TransportType transport_type; // 传输类型(TCP, UDP, SSL等)
|
|
|
|
enum TransportType transport_type;
|
|
|
|
size_t max_connections; // 最大并发连接数
|
|
|
|
size_t max_connections;
|
|
|
|
int peer_response_timeout; // 每次读取或写入操作的超时
|
|
|
|
int peer_response_timeout; /* timeout of each read or write operation */
|
|
|
|
int receive_timeout; // 接收整个消息的超时
|
|
|
|
int receive_timeout; /* timeout of receiving the whole message */
|
|
|
|
int keep_alive_timeout; // 连接保持存活的超时
|
|
|
|
int keep_alive_timeout;
|
|
|
|
size_t request_size_limit; // 请求大小限制
|
|
|
|
size_t request_size_limit;
|
|
|
|
int ssl_accept_timeout; // SSL 接受超时时间
|
|
|
|
int ssl_accept_timeout; /* if not ssl, this will be ignored */
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 设置服务器参数的默认值
|
|
|
|
|
|
|
|
static constexpr struct WFServerParams SERVER_PARAMS_DEFAULT =
|
|
|
|
static constexpr struct WFServerParams SERVER_PARAMS_DEFAULT =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.transport_type = TT_TCP, // 默认使用 TCP
|
|
|
|
.transport_type = TT_TCP,
|
|
|
|
.max_connections = 2000, // 默认最多 2000 个连接
|
|
|
|
.max_connections = 2000,
|
|
|
|
.peer_response_timeout = 10 * 1000, // 默认对端响应超时 10 秒
|
|
|
|
.peer_response_timeout = 10 * 1000,
|
|
|
|
.receive_timeout = -1, // 默认不超时
|
|
|
|
.receive_timeout = -1,
|
|
|
|
.keep_alive_timeout = 60 * 1000, // 默认连接保持存活 60 秒
|
|
|
|
.keep_alive_timeout = 60 * 1000,
|
|
|
|
.request_size_limit = (size_t)-1, // 请求大小限制为无限制
|
|
|
|
.request_size_limit = (size_t)-1,
|
|
|
|
.ssl_accept_timeout = 10 * 1000, // SSL 接受超时设置为 10 秒
|
|
|
|
.ssl_accept_timeout = 10 * 1000,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 WFServerBase 基类,用于建立服务器
|
|
|
|
|
|
|
|
class WFServerBase : protected CommService
|
|
|
|
class WFServerBase : protected CommService
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// 构造函数,接收服务器参数并初始化
|
|
|
|
|
|
|
|
WFServerBase(const struct WFServerParams *params) :
|
|
|
|
WFServerBase(const struct WFServerParams *params) :
|
|
|
|
conn_count(0) // 初始化连接计数为 0
|
|
|
|
conn_count(0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this->params = *params; // 保存服务器参数
|
|
|
|
this->params = *params;
|
|
|
|
this->unbind_finish = false; // 初始化解除绑定状态
|
|
|
|
this->unbind_finish = false;
|
|
|
|
this->listen_fd = -1; // 初始化监听文件描述符为无效值
|
|
|
|
this->listen_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// 启动一个 TCP 服务器函数
|
|
|
|
/* To start a TCP server */
|
|
|
|
|
|
|
|
|
|
|
|
/* 在指定端口上启动服务器,IPv4 */
|
|
|
|
/* Start on port with IPv4. */
|
|
|
|
int start(unsigned short port)
|
|
|
|
int start(unsigned short port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(AF_INET, NULL, port, NULL, NULL);
|
|
|
|
return start(AF_INET, NULL, port, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 根据家庭类型启动服务器,IPv4 或 IPv6 */
|
|
|
|
/* Start with family. AF_INET or AF_INET6. */
|
|
|
|
int start(int family, unsigned short port)
|
|
|
|
int start(int family, unsigned short port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(family, NULL, port, NULL, NULL);
|
|
|
|
return start(family, NULL, port, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用主机名和端口启动服务器 */
|
|
|
|
/* Start with hostname and port. */
|
|
|
|
int start(const char *host, unsigned short port)
|
|
|
|
int start(const char *host, unsigned short port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(AF_INET, host, port, NULL, NULL);
|
|
|
|
return start(AF_INET, host, port, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用家庭类型、主机名和端口启动服务器 */
|
|
|
|
/* Start with family, hostname and port. */
|
|
|
|
int start(int family, const char *host, unsigned short port)
|
|
|
|
int start(int family, const char *host, unsigned short port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(family, host, port, NULL, NULL);
|
|
|
|
return start(family, host, port, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用指定的地址绑定启动服务器 */
|
|
|
|
/* Start with binding address. */
|
|
|
|
int start(const struct sockaddr *bind_addr, socklen_t addrlen)
|
|
|
|
int start(const struct sockaddr *bind_addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(bind_addr, addrlen, NULL, NULL);
|
|
|
|
return start(bind_addr, addrlen, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 启动一个 SSL 服务器
|
|
|
|
/* To start an SSL server. */
|
|
|
|
|
|
|
|
|
|
|
|
/* 在指定端口启动 SSL 服务器 */
|
|
|
|
|
|
|
|
int start(unsigned short port, const char *cert_file, const char *key_file)
|
|
|
|
int start(unsigned short port, const char *cert_file, const char *key_file)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(AF_INET, NULL, port, cert_file, key_file);
|
|
|
|
return start(AF_INET, NULL, port, cert_file, key_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用家庭类型和端口启动 SSL 服务器 */
|
|
|
|
|
|
|
|
int start(int family, unsigned short port,
|
|
|
|
int start(int family, unsigned short port,
|
|
|
|
const char *cert_file, const char *key_file)
|
|
|
|
const char *cert_file, const char *key_file)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(family, NULL, port, cert_file, key_file);
|
|
|
|
return start(family, NULL, port, cert_file, key_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用主机名和端口启动 SSL 服务器 */
|
|
|
|
|
|
|
|
int start(const char *host, unsigned short port,
|
|
|
|
int start(const char *host, unsigned short port,
|
|
|
|
const char *cert_file, const char *key_file)
|
|
|
|
const char *cert_file, const char *key_file)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return start(AF_INET, host, port, cert_file, key_file);
|
|
|
|
return start(AF_INET, host, port, cert_file, key_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用家庭类型、主机名和端口启动 SSL 服务器 */
|
|
|
|
|
|
|
|
int start(int family, const char *host, unsigned short port,
|
|
|
|
int start(int family, const char *host, unsigned short port,
|
|
|
|
const char *cert_file, const char *key_file);
|
|
|
|
const char *cert_file, const char *key_file);
|
|
|
|
|
|
|
|
|
|
|
|
/* 使用指定文件描述符启动服务器,用于优雅重启或 SCTP 服务器 */
|
|
|
|
/* This is the only necessary start function. */
|
|
|
|
|
|
|
|
int start(const struct sockaddr *bind_addr, socklen_t addrlen,
|
|
|
|
|
|
|
|
const char *cert_file, const char *key_file);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* To start with a specified fd. For graceful restart or SCTP server. */
|
|
|
|
int serve(int listen_fd)
|
|
|
|
int serve(int listen_fd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return serve(listen_fd, NULL, NULL);
|
|
|
|
return serve(listen_fd, NULL, NULL);
|
|
|
@ -135,119 +131,114 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
int serve(int listen_fd, const char *cert_file, const char *key_file);
|
|
|
|
int serve(int listen_fd, const char *cert_file, const char *key_file);
|
|
|
|
|
|
|
|
|
|
|
|
/* 停止服务器是一个阻塞操作 */
|
|
|
|
/* stop() is a blocking operation. */
|
|
|
|
void stop()
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this->shutdown(); // 关闭服务器
|
|
|
|
this->shutdown();
|
|
|
|
this->wait_finish(); // 等待完成
|
|
|
|
this->wait_finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 非阻塞地终止服务器。用于停止多个服务器。
|
|
|
|
/* Nonblocking terminating the server. For stopping multiple servers.
|
|
|
|
* 通常,调用 shutdown() 然后 wait_finish()。
|
|
|
|
* Typically, call shutdown() and then wait_finish().
|
|
|
|
* 但实际上,可以在其他线程中在 shutdown() 之前甚至在 start() 之前调用 wait_finish()。 */
|
|
|
|
* But indeed wait_finish() can be called before shutdown(), even before
|
|
|
|
void shutdown(); // 停止服务器
|
|
|
|
* start() in another thread. */
|
|
|
|
void wait_finish(); // 等待所有连接和任务完成
|
|
|
|
void shutdown();
|
|
|
|
|
|
|
|
void wait_finish();
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// 获取当前连接数
|
|
|
|
|
|
|
|
size_t get_conn_count() const { return this->conn_count; }
|
|
|
|
size_t get_conn_count() const { return this->conn_count; }
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取监听地址。这通常在服务器在随机端口上启动后使用(start() 端口为 0)。 */
|
|
|
|
/* Get the listening address. This is often used after starting
|
|
|
|
|
|
|
|
* server on a random port (start() with port == 0). */
|
|
|
|
int get_listen_addr(struct sockaddr *addr, socklen_t *addrlen) const
|
|
|
|
int get_listen_addr(struct sockaddr *addr, socklen_t *addrlen) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (this->listen_fd >= 0)
|
|
|
|
if (this->listen_fd >= 0)
|
|
|
|
return getsockname(this->listen_fd, addr, addrlen); // 获取当前的 socket 地址
|
|
|
|
return getsockname(this->listen_fd, addr, addrlen);
|
|
|
|
|
|
|
|
|
|
|
|
errno = ENOTCONN; // 如果没有连接,设置错误号为没有连接
|
|
|
|
errno = ENOTCONN;
|
|
|
|
return -1; // 返回错误
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取服务器参数
|
|
|
|
|
|
|
|
const struct WFServerParams *get_params() const { return &this->params; }
|
|
|
|
const struct WFServerParams *get_params() const { return &this->params; }
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
/* 重写此函数以创建服务器的初始 SSL CTX */
|
|
|
|
/* Override this function to create the initial SSL CTX of the server */
|
|
|
|
virtual SSL_CTX *new_ssl_ctx(const char *cert_file, const char *key_file);
|
|
|
|
virtual SSL_CTX *new_ssl_ctx(const char *cert_file, const char *key_file);
|
|
|
|
|
|
|
|
|
|
|
|
/* 重写此函数以实现支持 TLS SNI 的服务器。
|
|
|
|
/* Override this function to implement server that supports TLS SNI.
|
|
|
|
* "servername" 如果客户端未设置主机名将为 NULL。
|
|
|
|
* "servername" will be NULL if client does not set a host name.
|
|
|
|
* 返回 NULL 表示服务器名称不受支持。 */
|
|
|
|
* Returning NULL to indicate that servername is not supported. */
|
|
|
|
virtual SSL_CTX *get_server_ssl_ctx(const char *servername)
|
|
|
|
virtual SSL_CTX *get_server_ssl_ctx(const char *servername)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return this->get_ssl_ctx(); // 返回当前的 SSL 上下文
|
|
|
|
return this->get_ssl_ctx();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 这个函数可以在 'new_ssl_ctx' 的实现中使用。 */
|
|
|
|
/* This can be used by the implementation of 'new_ssl_ctx'. */
|
|
|
|
static int ssl_ctx_callback(SSL *ssl, int *al, void *arg);
|
|
|
|
static int ssl_ctx_callback(SSL *ssl, int *al, void *arg);
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
WFServerParams params; // 服务器参数结构体
|
|
|
|
WFServerParams params;
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
// 创建新连接的方法
|
|
|
|
|
|
|
|
virtual int create_listen_fd();
|
|
|
|
virtual int create_listen_fd();
|
|
|
|
virtual WFConnection *new_connection(int accept_fd); // 创建新连接
|
|
|
|
virtual WFConnection *new_connection(int accept_fd);
|
|
|
|
void delete_connection(WFConnection *conn); // 删除连接
|
|
|
|
void delete_connection(WFConnection *conn);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
// 初始化服务器的方法
|
|
|
|
|
|
|
|
int init(const struct sockaddr *bind_addr, socklen_t addrlen,
|
|
|
|
int init(const struct sockaddr *bind_addr, socklen_t addrlen,
|
|
|
|
const char *cert_file, const char *key_file);
|
|
|
|
const char *cert_file, const char *key_file);
|
|
|
|
virtual void handle_unbound(); // 处理解除绑定
|
|
|
|
virtual void handle_unbound();
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
std::atomic<size_t> conn_count; // 当前连接计数
|
|
|
|
std::atomic<size_t> conn_count;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
int listen_fd; // 监听文件描述符
|
|
|
|
int listen_fd;
|
|
|
|
bool unbind_finish; // 标记解除绑定是否完成
|
|
|
|
bool unbind_finish;
|
|
|
|
|
|
|
|
|
|
|
|
std::mutex mutex; // 互斥锁,用于线程安全
|
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable cond; // 条件变量,用于线程间通知
|
|
|
|
std::condition_variable cond;
|
|
|
|
|
|
|
|
|
|
|
|
class CommScheduler *scheduler; // 调度器
|
|
|
|
class CommScheduler *scheduler;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 模板类,用于定义特定协议的服务器
|
|
|
|
|
|
|
|
template<class REQ, class RESP>
|
|
|
|
template<class REQ, class RESP>
|
|
|
|
class WFServer : public WFServerBase
|
|
|
|
class WFServer : public WFServerBase
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// 构造函数,接收参数和处理函数
|
|
|
|
|
|
|
|
WFServer(const struct WFServerParams *params,
|
|
|
|
WFServer(const struct WFServerParams *params,
|
|
|
|
std::function<void (WFNetworkTask<REQ, RESP> *)> proc) :
|
|
|
|
std::function<void (WFNetworkTask<REQ, RESP> *)> proc) :
|
|
|
|
WFServerBase(params), // 调用基类构造函数
|
|
|
|
WFServerBase(params),
|
|
|
|
process(std::move(proc)) // 移动处理函数,避免不必要的拷贝
|
|
|
|
process(std::move(proc))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 构造函数,使用默认参数
|
|
|
|
|
|
|
|
WFServer(std::function<void (WFNetworkTask<REQ, RESP> *)> proc) :
|
|
|
|
WFServer(std::function<void (WFNetworkTask<REQ, RESP> *)> proc) :
|
|
|
|
WFServerBase(&SERVER_PARAMS_DEFAULT), // 调用基类构造函数,传入默认参数
|
|
|
|
WFServerBase(&SERVER_PARAMS_DEFAULT),
|
|
|
|
process(std::move(proc)) // 移动处理函数
|
|
|
|
process(std::move(proc))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
virtual CommSession *new_session(long long seq, CommConnection *conn); // 创建新会话
|
|
|
|
virtual CommSession *new_session(long long seq, CommConnection *conn);
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
std::function<void (WFNetworkTask<REQ, RESP> *)> process; // 处理函数
|
|
|
|
std::function<void (WFNetworkTask<REQ, RESP> *)> process;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新会话的方法,实现具体的会话处理
|
|
|
|
|
|
|
|
template<class REQ, class RESP>
|
|
|
|
template<class REQ, class RESP>
|
|
|
|
CommSession *WFServer<REQ, RESP>::new_session(long long seq, CommConnection *conn)
|
|
|
|
CommSession *WFServer<REQ, RESP>::new_session(long long seq, CommConnection *conn)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
using factory = WFNetworkTaskFactory<REQ, RESP>; // 使用工厂来创建任务
|
|
|
|
using factory = WFNetworkTaskFactory<REQ, RESP>;
|
|
|
|
WFNetworkTask<REQ, RESP> *task; // 声明任务指针
|
|
|
|
WFNetworkTask<REQ, RESP> *task;
|
|
|
|
|
|
|
|
|
|
|
|
task = factory::create_server_task(this, this->process); // 使用工厂创建任务
|
|
|
|
task = factory::create_server_task(this, this->process);
|
|
|
|
task->set_keep_alive(this->params.keep_alive_timeout); // 设置保持连接的超时
|
|
|
|
task->set_keep_alive(this->params.keep_alive_timeout);
|
|
|
|
task->set_receive_timeout(this->params.receive_timeout); // 设置接收超时
|
|
|
|
task->set_receive_timeout(this->params.receive_timeout);
|
|
|
|
task->get_req()->set_size_limit(this->params.request_size_limit); // 设置请求大小限制
|
|
|
|
task->get_req()->set_size_limit(this->params.request_size_limit);
|
|
|
|
|
|
|
|
|
|
|
|
return task; // 返回创建的会话任务
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _WFSERVER_H_
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|