- 新增 log.c/log.h 分级日志系统(error/warn/info/debug) - 新增连接空闲超时管理:使用 coco_timer + shutdown + coco_cancel 自动清理长时间无数据的僵尸连接,默认 30 秒 - 新增最大并发连接数限制(-m <num>),超限返回 503 - 命令行新增选项:-m 最大连接数, -o 超时毫秒, -l 日志级别 - 替换所有 printf 为 log_info/log_error/log_debug 等分级输出 - 编译通过,单线程/多线程模式均正常
73 lines
1.9 KiB
C
73 lines
1.9 KiB
C
/**
|
||
* cocoon.h - Cocoon 公共头文件
|
||
*
|
||
* 定义错误码、配置结构体、函数声明。
|
||
*
|
||
* @author xfy
|
||
*/
|
||
|
||
#ifndef COCOON_H
|
||
#define COCOON_H
|
||
|
||
#include <stdint.h>
|
||
#include <stdbool.h>
|
||
#include <stddef.h>
|
||
|
||
#include "log.h"
|
||
|
||
#define COCOON_OK 0 /**< 成功 */
|
||
#define COCOON_ERROR -1 /**< 通用错误 */
|
||
#define COCOON_NOMEM -2 /**< 内存不足 */
|
||
#define COCOON_NOTFOUND -3 /**< 文件未找到 */
|
||
#define COCOONForbidden -4 /**< 禁止访问 */
|
||
#define COCOON_BADREQUEST -5 /**< 请求格式错误 */
|
||
|
||
/* === 服务器配置 === */
|
||
/**
|
||
* cocoon_config - 服务器配置结构体
|
||
*
|
||
* 命令行参数解析后的配置。
|
||
*/
|
||
typedef struct cocoon_config {
|
||
const char *root_dir; /**< 静态资源根目录 */
|
||
uint16_t port; /**< 监听端口 */
|
||
bool threaded; /**< 是否启用多线程调度 */
|
||
uint32_t num_workers; /**< 工作线程数(0 = 自动检测) */
|
||
uint32_t max_connections; /**< 最大并发连接数(0 = 无限制) */
|
||
uint32_t timeout_ms; /**< 连接空闲超时毫秒(0 = 默认30000) */
|
||
log_level_t log_level; /**< 日志级别 */
|
||
} cocoon_config_t;
|
||
|
||
/* === 服务器生命周期 API === */
|
||
/**
|
||
* cocoon_server_create - 创建服务器实例
|
||
*
|
||
* @param config 配置指针
|
||
* @return 服务器句柄,失败返回 NULL
|
||
*/
|
||
void *cocoon_server_create(const cocoon_config_t *config);
|
||
|
||
/**
|
||
* cocoon_server_run - 启动服务器(阻塞)
|
||
*
|
||
* @param server 服务器句柄
|
||
* @return COCOON_OK 成功,负值错误码
|
||
*/
|
||
int cocoon_server_run(void *server);
|
||
|
||
/**
|
||
* cocoon_server_stop - 优雅关闭服务器
|
||
*
|
||
* @param server 服务器句柄
|
||
*/
|
||
void cocoon_server_stop(void *server);
|
||
|
||
/**
|
||
* cocoon_server_destroy - 销毁服务器实例
|
||
*
|
||
* @param server 服务器句柄
|
||
*/
|
||
void cocoon_server_destroy(void *server);
|
||
|
||
#endif /* COCOON_H */
|