修复log记录错误(第一个节点无法落盘),更新http解析为现成库实现,优化退出逻辑。
This commit is contained in:
@ -1,10 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.28.3)
|
||||
|
||||
project (Onebot_back C)
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
|
||||
add_compile_options(-O0 -g -fno-omit-frame-pointer)
|
||||
add_link_options(-O0)
|
||||
endif()
|
||||
if(MSVC)
|
||||
add_compile_options(/Od /Zi)
|
||||
endif()
|
||||
|
||||
add_executable(Start_Onebot_back main.c tem/ctl.c)
|
||||
add_executable(Run_pluginmanager run_pluginmanager/run_pluginmanager.c)
|
||||
add_library(Network SHARED network/network.c network/swap.c network/cJSON.c network/http/http_rel.c network/erroprocess/erroprocess.c)
|
||||
add_library(Network SHARED network/network.c network/swap.c network/cJSON.c network/http/http_rel.c network/erroprocess/erroprocess.c network/mongoose/mongoose.c)
|
||||
add_library(Swmem SHARED network/swap.c)
|
||||
add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c)
|
||||
add_library(Log SHARED tools/log/log.c)
|
||||
|
||||
2
c/main.c
2
c/main.c
@ -55,8 +55,10 @@ int main()
|
||||
on_exit(quit_all,resource);
|
||||
//注册清理函数
|
||||
teml->run(teml,fifo);
|
||||
|
||||
//启动终端
|
||||
pthread_join(network_id,NULL);
|
||||
//等待网络管理器进程结束
|
||||
return 1;
|
||||
|
||||
}
|
||||
@ -8,6 +8,7 @@ int give_upjobs(indiector *self)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int init_indector(indiector *self)
|
||||
|
||||
@ -1,80 +1,85 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include "network/mongoose/mongoose.h"
|
||||
|
||||
#include "http_rel.h"
|
||||
/* 接收状态辅助结构 */
|
||||
struct recv_state {
|
||||
char *request; // 完整请求字符串
|
||||
int done; // 接收完成标志
|
||||
int error; // 错误标志
|
||||
};
|
||||
|
||||
/* mongoose 事件处理函数 */
|
||||
static void http_recv_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
|
||||
{
|
||||
struct recv_state *state = (struct recv_state *)fn_data;
|
||||
|
||||
switch (ev) {
|
||||
case MG_EV_HTTP_MSG: {
|
||||
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
|
||||
/* 分配内存并复制完整请求(头+体) */
|
||||
state->request = malloc(hm->message.len + 1);
|
||||
if (state->request) {
|
||||
memcpy(state->request, hm->message.buf, hm->message.len);
|
||||
state->request[hm->message.len] = '\0';
|
||||
} else {
|
||||
state->error = 1; // 内存不足
|
||||
}
|
||||
state->done = 1;
|
||||
break;
|
||||
}
|
||||
case MG_EV_CLOSE:
|
||||
case MG_EV_ERROR:
|
||||
state->done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char *recv_http_request(int cfd)
|
||||
{
|
||||
struct mg_mgr mgr;
|
||||
struct mg_connection *c;
|
||||
struct recv_state state = {0};
|
||||
|
||||
/* 初始化 mongoose 管理器 */
|
||||
mg_mgr_init(&mgr);
|
||||
|
||||
/* 将已连接的 socket 包装成 mongoose 连接 */
|
||||
c = mg_wrapfd(&mgr, cfd, http_recv_handler, &state); // ← 修复此处
|
||||
|
||||
/* 设置 5 秒超时 */
|
||||
int64_t end_time = mg_millis() + 5000;
|
||||
while (!state.done && mg_millis() < end_time) {
|
||||
mg_mgr_poll(&mgr, 100);
|
||||
}
|
||||
|
||||
/* 超时处理 */
|
||||
if (!state.done) {
|
||||
state.error = 1;
|
||||
}
|
||||
|
||||
/* 清理 mongoose 资源(不会关闭原始 fd) */
|
||||
mg_mgr_free(&mgr);
|
||||
|
||||
/* 出错时释放内存 */
|
||||
if (state.error) {
|
||||
free(state.request);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return state.request;
|
||||
}
|
||||
|
||||
/* http_get_body 无需修改,保持原样 */
|
||||
const char *http_get_body(const char *buf)
|
||||
{
|
||||
if (!buf) return NULL;
|
||||
|
||||
/* 找到 header 与 body 之间的空行 "\r\n\r\n" */
|
||||
const char *sep = strstr(buf, "\r\n\r\n");
|
||||
if (!sep) return NULL; /* 格式错误 */
|
||||
|
||||
const char *body = sep + 4; /* 跳过 "\r\n\r\n" */
|
||||
|
||||
/* 简单判断:如果后面还有数据,就认为是 body */
|
||||
if (*body == '\0') return NULL; /* 没有 body */
|
||||
|
||||
if (!sep) return NULL;
|
||||
const char *body = sep + 4;
|
||||
if (*body == '\0') return NULL;
|
||||
return body;
|
||||
}
|
||||
|
||||
|
||||
/* 一次性读完一个 HTTP 请求,返回 malloc 得到的完整字符串(含头+体),调用者 free。
|
||||
返回 NULL 表示读取出错或内存不足。
|
||||
*/
|
||||
char *recv_http_request(int cfd)
|
||||
{
|
||||
char head[8192];
|
||||
int body_len = 0;
|
||||
|
||||
/* 1. 读头部到 \r\n\r\n */
|
||||
size_t n = 0;
|
||||
while (n < sizeof(head)) {
|
||||
if (read(cfd, head + n, 1) != 1) goto err;
|
||||
if (n >= 3 && memcmp(head + n - 3, "\r\n\r", 3) == 0) {
|
||||
if (read(cfd, head + n, 1) != 1) goto err; /* 再吃一个 \n */
|
||||
++n;
|
||||
break;
|
||||
}
|
||||
++n;
|
||||
}
|
||||
head[n] = 0;
|
||||
|
||||
/* 2. 解析 Content-Length */
|
||||
const char *p = head;
|
||||
while ((p = strchr(p, '\n')) != NULL) {
|
||||
if (sscanf(p, "\nContent-Length: %d", &body_len) == 1) break;
|
||||
++p;
|
||||
}
|
||||
|
||||
/* 3. 读 body */
|
||||
char *body = NULL;
|
||||
if (body_len > 0) {
|
||||
body = malloc(body_len + 1);
|
||||
if (!body) goto err;
|
||||
size_t got = 0;
|
||||
while (got < (size_t)body_len) {
|
||||
ssize_t rd = read(cfd, body + got, body_len - got);
|
||||
if (rd <= 0) { free(body); goto err; }
|
||||
got += rd;
|
||||
}
|
||||
body[got] = 0;
|
||||
}
|
||||
|
||||
/* 4. 拼成完整字符串 */
|
||||
char *full = malloc(n + (body_len ? body_len : 0) + 1);
|
||||
if (!full) { free(body); goto err; }
|
||||
memcpy(full, head, n);
|
||||
if (body_len) memcpy(full + n, body, body_len);
|
||||
full[n + body_len] = 0;
|
||||
free(body);
|
||||
return full;
|
||||
|
||||
err:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
26413
c/network/mongoose/mongoose.c
Normal file
26413
c/network/mongoose/mongoose.c
Normal file
File diff suppressed because it is too large
Load Diff
3925
c/network/mongoose/mongoose.h
Normal file
3925
c/network/mongoose/mongoose.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -77,16 +77,92 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
|
||||
return 0; // 成功
|
||||
}
|
||||
|
||||
int init_http_network(int port)
|
||||
/**
|
||||
* @brief 初始化HTTP监听socket,所有错误通过logmanager记录
|
||||
* @param port 监听端口
|
||||
* @param logger 日志管理器实例指针
|
||||
* @return 成功返回监听fd,失败返回-1并记录日志
|
||||
*/
|
||||
int init_http_network(int port, log_manager *logger)
|
||||
{
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
fcntl(fd, F_GETFL);
|
||||
logs *log;
|
||||
int fd;
|
||||
|
||||
/* 1. 创建socket */
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] socket() failed: %s", strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 2. 设置SO_REUSEADDR,避免TIME_WAIT状态导致bind失败 */
|
||||
int opt = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 3. 设置为非阻塞模式(配合epoll使用) */
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] fcntl(F_GETFL) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[ERROR] fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 4. 绑定到指定端口 */
|
||||
struct sockaddr_in addr = {0};
|
||||
addr.sin_family = AF_INET; // 和 socket() 一致
|
||||
addr.sin_port = htons(port); // 端口号必须网络字节序
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 0.0.0.0:本机所有网卡
|
||||
bind(fd, (struct sockaddr *)&addr, sizeof(addr));
|
||||
listen(fd,10);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 监听所有网卡
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] bind(port %d) failed: %s (fd=%d)",
|
||||
port, strerror(errno), fd);
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 5. 开始监听 */
|
||||
if (listen(fd, 10) == -1) {
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[FATAL] listen(fd=%d, backlog=10) failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 6. 成功日志 */
|
||||
log = malloc(sizeof(logs));
|
||||
snprintf(log->log, sizeof(log->log),
|
||||
"[HTTP] Successfully listening on port %d (fd=%d)", port, fd);
|
||||
logger->in_log(log, logger);
|
||||
return fd;
|
||||
}
|
||||
|
||||
@ -99,37 +175,63 @@ ssize_t read_req(int fd, void *buf)
|
||||
return (n > 0) ? n : -1;
|
||||
}
|
||||
|
||||
int process_message(char *req,log_manager *logger)
|
||||
{
|
||||
//TODO 修改管道命令解析
|
||||
if(req ==NULL )
|
||||
return 0;
|
||||
int process_message(char *req, log_manager *logger) {
|
||||
if(req == NULL) return 0;
|
||||
|
||||
int fd;
|
||||
char front,rear;
|
||||
sscanf(req,"%s/%d/%s",&rear,&fd,&rear);
|
||||
char type[16], end[16];
|
||||
if(sscanf(req, "%15s/%d/%15s", type, &fd, end) != 3) {
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *req_buf = recv_http_request(fd);
|
||||
if(!req_buf) { // 检查返回值
|
||||
close(fd);
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *body = http_get_body(req_buf);
|
||||
free(req_buf);
|
||||
|
||||
rbt_msg message;
|
||||
rbt_parse_json(body,&message);
|
||||
if(rbt_parse_json(body, &message) == 0) {
|
||||
make_swap((void*)&message);
|
||||
logs *log = malloc(sizeof log);
|
||||
if(snprintf(log->log,sizeof(log->log),
|
||||
"%s message %s processd ok\n",message.nickname,message.raw_message)<1024);
|
||||
logger->in_log(log,logger);
|
||||
|
||||
logs *log = malloc(sizeof(logs));
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log->log, sizeof(log->log), "%s message %s processed ok\n",
|
||||
message.nickname, message.raw_message);
|
||||
logger->in_log(log, logger);
|
||||
}
|
||||
|
||||
// ****** 修复2:发送HTTP响应 ******
|
||||
const char *response =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Content-Length: 2\r\n"
|
||||
"\r\n"
|
||||
"OK";
|
||||
write(fd, response, strlen(response));
|
||||
|
||||
close(fd);
|
||||
free(req);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iss_work(netm *self,char *command)
|
||||
{
|
||||
int i = self->last_alc+1;
|
||||
int i = self->last_alc;
|
||||
//查询空闲线程
|
||||
while(self->pool[i].fifo_fd ==0)
|
||||
while(atomic_load(&(self->pool[i].status)) ==0)
|
||||
{
|
||||
if(i<MAX_POOL)
|
||||
i++;
|
||||
else
|
||||
else{
|
||||
i=0;
|
||||
}
|
||||
}
|
||||
//向空闲线程发送数据
|
||||
write(self->pool[i].fifo_fd[0],command,strlen(command));
|
||||
//设置线程程为working
|
||||
@ -143,8 +245,9 @@ void *pth_module(void *args_p)
|
||||
pth_m *pmd = argms->pth;
|
||||
log_manager *logger = argms->log;
|
||||
//参数解析
|
||||
free(args_p);
|
||||
char name[256] = {'\0'};
|
||||
sprintf(name,"chatrebot%lu",pmd->pthread_id);
|
||||
sprintf(name,"chatrebot%lu",pthread_self());
|
||||
int swap = create_swap(name);
|
||||
//创建共享内存
|
||||
char swap_arg[64] = {'\0'};
|
||||
@ -159,16 +262,17 @@ void *pth_module(void *args_p)
|
||||
execv("Run_pluhginmanager",args);
|
||||
}
|
||||
logs *pth_log = (logs*)malloc(sizeof(logs));
|
||||
sprintf(pth_log->log,"PID:%lu launched python plugines\n",pmd->pthread_id);
|
||||
// cppcheck-suppress uninitdata
|
||||
sprintf(pth_log->log,"PID:%lu launched python plugines\n",pthread_self());
|
||||
|
||||
logger->in_log(pth_log,logger);
|
||||
//拉起python插件管理器
|
||||
for(;;){
|
||||
//线程池中,单个线程模型
|
||||
|
||||
char req[64];
|
||||
char *req = (char*)malloc(MAX_MESSAGE_BUF);
|
||||
//从管道中读取请求,并解析,无内容时休眠
|
||||
int n = read_req(pmd->fifo_fd[0],req);
|
||||
int n = read_req(pmd->fifo_fd[0],(void*)req);
|
||||
//管道关闭时退出;
|
||||
|
||||
if (n == EOF) {
|
||||
@ -176,6 +280,9 @@ void *pth_module(void *args_p)
|
||||
break;
|
||||
}
|
||||
else{
|
||||
pth_log = (logs*)malloc(sizeof(logs));
|
||||
sprintf(pth_log->log,"processd message");
|
||||
logger->in_log(pth_log,logger);
|
||||
process_message(req,logger);
|
||||
atomic_fetch_add(&pmd->status, 1);
|
||||
}
|
||||
@ -189,11 +296,12 @@ int start_pool(netm *self)
|
||||
//为线程开辟管道
|
||||
pipe(self->pool[i].fifo_fd);
|
||||
//启动线程
|
||||
net_args arg;
|
||||
arg.pth =&self->pool[i];
|
||||
arg.log = self->logmanager;
|
||||
net_args *arg = (net_args*)malloc(sizeof(net_args));
|
||||
arg->pth = &self->pool[i];
|
||||
arg->log = self->logmanager;
|
||||
|
||||
self->pool[i].status = 1;
|
||||
pthread_create(&self->pool[i].pthread_id,NULL,pth_module,(void*)&arg);
|
||||
pthread_create(&self->pool[i].pthread_id,NULL,pth_module,(void*)arg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +319,7 @@ int shutdown_pool(netm *self)
|
||||
|
||||
int server_run(int port,int fifo_fd,netm *self)
|
||||
{
|
||||
int epfd = epoll_create1(EPOLL_CLOEXEC); // 推荐
|
||||
int epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
if (epfd == -1) {
|
||||
perror("epoll_create1");
|
||||
exit(EXIT_FAILURE);
|
||||
@ -222,7 +330,7 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
ev.data.fd = fifo_fd;
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
||||
char iss_buf[256];
|
||||
self->http_fd = init_http_network(port);
|
||||
self->http_fd = init_http_network(port,self->logmanager);
|
||||
|
||||
ev.data.fd = self->http_fd;
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
|
||||
@ -261,7 +369,6 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
break;
|
||||
case 'u':
|
||||
//插件更新逻辑
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -289,8 +396,9 @@ int init_networkmanager(netm *self,int *fifo,log_manager *logmanager,int port)
|
||||
self->fifo_fd[0]= fifo[0];
|
||||
self->fifo_fd[1]= fifo[1];
|
||||
self->last_alc = 0;
|
||||
self->port = port;
|
||||
//初始化参数
|
||||
self->logmanager = logmanager;
|
||||
self->err_indictor = (indiector*)malloc(sizeof(indiector));
|
||||
self->port = port;
|
||||
return 0;
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
#define NETWORK
|
||||
|
||||
#define MAX_POOL 10
|
||||
#define MAX_MESSAGE_BUF 10240
|
||||
#define MAX_MESSAGE_BUF 1024
|
||||
#include <pthread.h>
|
||||
#include "tools/log/log.h"
|
||||
#include "erroprocess/erroprocess.h"
|
||||
@ -23,7 +23,7 @@ typedef struct net_args
|
||||
|
||||
typedef struct network_manager
|
||||
{
|
||||
void *(*run_network)(void*);
|
||||
void *(*run_network)(void*);//启动网络监听
|
||||
int (*start_pool)(struct network_manager*);
|
||||
int (*shutdown_pool)(struct network_manager*);
|
||||
int (*iss_work)(struct network_manager*,char *);
|
||||
|
||||
@ -211,9 +211,11 @@ int infifo(Ctl *self,const char *cmd)
|
||||
//存储命令历史s
|
||||
if(self->index<HISTORY_BUF){
|
||||
self->index++;
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
self->index = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -87,19 +87,20 @@ int cleanup(log_manager *self)
|
||||
loc = self->log;
|
||||
self->log = NULL;
|
||||
self->count = 0;//摘取log链
|
||||
|
||||
sem_post(&self->log_sem);
|
||||
//释放信号量
|
||||
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
|
||||
while(loc->next !=NULL)
|
||||
{
|
||||
tobeclean = loc;
|
||||
loc = loc->next;
|
||||
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
|
||||
if(fd == -1)
|
||||
perror("file:");
|
||||
write(fd,loc->log,strlen(loc->log));
|
||||
close(fd);
|
||||
write(fd,tobeclean->log,strlen(tobeclean->log));
|
||||
free(tobeclean);
|
||||
}
|
||||
close(fd);
|
||||
free(loc);
|
||||
}
|
||||
|
||||
|
||||
@ -28,10 +28,10 @@ int quit_server(netm *self)
|
||||
self->http_fd =-1;
|
||||
}
|
||||
//关闭socket监听
|
||||
if(self->fifo_fd[0] != -1)
|
||||
if(self->fifo_fd[1] != -1)
|
||||
{
|
||||
close(self->fifo_fd[0]);
|
||||
self->fifo_fd[0] = -1;
|
||||
close(self->fifo_fd[1]);
|
||||
self->fifo_fd[1] = -1;
|
||||
}
|
||||
//关闭管道监听
|
||||
|
||||
|
||||
Reference in New Issue
Block a user