修复log记录错误(第一个节点无法落盘),更新http解析为现成库实现,优化退出逻辑。
This commit is contained in:
@ -1,10 +1,17 @@
|
|||||||
cmake_minimum_required(VERSION 3.28.3)
|
cmake_minimum_required(VERSION 3.28.3)
|
||||||
|
|
||||||
project (Onebot_back C)
|
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(Start_Onebot_back main.c tem/ctl.c)
|
||||||
add_executable(Run_pluginmanager run_pluginmanager/run_pluginmanager.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(Swmem SHARED network/swap.c)
|
||||||
add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c)
|
add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c)
|
||||||
add_library(Log SHARED tools/log/log.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);
|
on_exit(quit_all,resource);
|
||||||
//注册清理函数
|
//注册清理函数
|
||||||
teml->run(teml,fifo);
|
teml->run(teml,fifo);
|
||||||
|
|
||||||
//启动终端
|
//启动终端
|
||||||
pthread_join(network_id,NULL);
|
pthread_join(network_id,NULL);
|
||||||
//等待网络管理器进程结束
|
//等待网络管理器进程结束
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -8,6 +8,7 @@ int give_upjobs(indiector *self)
|
|||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_indector(indiector *self)
|
int init_indector(indiector *self)
|
||||||
|
|||||||
@ -1,80 +1,85 @@
|
|||||||
#include <stddef.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include "network/mongoose/mongoose.h"
|
||||||
|
|
||||||
#include "http_rel.h"
|
#include "http_rel.h"
|
||||||
|
/* 接收状态辅助结构 */
|
||||||
|
struct recv_state {
|
||||||
|
char *request; // 完整请求字符串
|
||||||
|
int done; // 接收完成标志
|
||||||
|
int error; // 错误标志
|
||||||
|
};
|
||||||
|
|
||||||
const char *http_get_body(const char *buf)
|
/* mongoose 事件处理函数 */
|
||||||
|
static void http_recv_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
|
||||||
{
|
{
|
||||||
if (!buf) return NULL;
|
struct recv_state *state = (struct recv_state *)fn_data;
|
||||||
|
|
||||||
/* 找到 header 与 body 之间的空行 "\r\n\r\n" */
|
switch (ev) {
|
||||||
const char *sep = strstr(buf, "\r\n\r\n");
|
case MG_EV_HTTP_MSG: {
|
||||||
if (!sep) return NULL; /* 格式错误 */
|
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
|
||||||
|
/* 分配内存并复制完整请求(头+体) */
|
||||||
const char *body = sep + 4; /* 跳过 "\r\n\r\n" */
|
state->request = malloc(hm->message.len + 1);
|
||||||
|
if (state->request) {
|
||||||
/* 简单判断:如果后面还有数据,就认为是 body */
|
memcpy(state->request, hm->message.buf, hm->message.len);
|
||||||
if (*body == '\0') return NULL; /* 没有 body */
|
state->request[hm->message.len] = '\0';
|
||||||
|
} else {
|
||||||
return body;
|
state->error = 1; // 内存不足
|
||||||
}
|
}
|
||||||
|
state->done = 1;
|
||||||
|
|
||||||
/* 一次性读完一个 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;
|
break;
|
||||||
}
|
}
|
||||||
++n;
|
case MG_EV_CLOSE:
|
||||||
|
case MG_EV_ERROR:
|
||||||
|
state->done = 1;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
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 *recv_http_request(int cfd)
|
||||||
char *body = NULL;
|
{
|
||||||
if (body_len > 0) {
|
struct mg_mgr mgr;
|
||||||
body = malloc(body_len + 1);
|
struct mg_connection *c;
|
||||||
if (!body) goto err;
|
struct recv_state state = {0};
|
||||||
size_t got = 0;
|
|
||||||
while (got < (size_t)body_len) {
|
/* 初始化 mongoose 管理器 */
|
||||||
ssize_t rd = read(cfd, body + got, body_len - got);
|
mg_mgr_init(&mgr);
|
||||||
if (rd <= 0) { free(body); goto err; }
|
|
||||||
got += rd;
|
/* 将已连接的 socket 包装成 mongoose 连接 */
|
||||||
}
|
c = mg_wrapfd(&mgr, cfd, http_recv_handler, &state); // ← 修复此处
|
||||||
body[got] = 0;
|
|
||||||
|
/* 设置 5 秒超时 */
|
||||||
|
int64_t end_time = mg_millis() + 5000;
|
||||||
|
while (!state.done && mg_millis() < end_time) {
|
||||||
|
mg_mgr_poll(&mgr, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 4. 拼成完整字符串 */
|
/* 超时处理 */
|
||||||
char *full = malloc(n + (body_len ? body_len : 0) + 1);
|
if (!state.done) {
|
||||||
if (!full) { free(body); goto err; }
|
state.error = 1;
|
||||||
memcpy(full, head, n);
|
}
|
||||||
if (body_len) memcpy(full + n, body, body_len);
|
|
||||||
full[n + body_len] = 0;
|
|
||||||
free(body);
|
|
||||||
return full;
|
|
||||||
|
|
||||||
err:
|
/* 清理 mongoose 资源(不会关闭原始 fd) */
|
||||||
|
mg_mgr_free(&mgr);
|
||||||
|
|
||||||
|
/* 出错时释放内存 */
|
||||||
|
if (state.error) {
|
||||||
|
free(state.request);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return state.request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* http_get_body 无需修改,保持原样 */
|
||||||
|
const char *http_get_body(const char *buf)
|
||||||
|
{
|
||||||
|
if (!buf) return NULL;
|
||||||
|
const char *sep = strstr(buf, "\r\n\r\n");
|
||||||
|
if (!sep) return NULL;
|
||||||
|
const char *body = sep + 4;
|
||||||
|
if (*body == '\0') return NULL;
|
||||||
|
return body;
|
||||||
|
}
|
||||||
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; // 成功
|
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);
|
logs *log;
|
||||||
fcntl(fd, F_GETFL);
|
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};
|
struct sockaddr_in addr = {0};
|
||||||
addr.sin_family = AF_INET; // 和 socket() 一致
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(port); // 端口号必须网络字节序
|
addr.sin_port = htons(port);
|
||||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 0.0.0.0:本机所有网卡
|
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 监听所有网卡
|
||||||
bind(fd, (struct sockaddr *)&addr, sizeof(addr));
|
|
||||||
listen(fd,10);
|
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;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,37 +175,63 @@ ssize_t read_req(int fd, void *buf)
|
|||||||
return (n > 0) ? n : -1;
|
return (n > 0) ? n : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int process_message(char *req,log_manager *logger)
|
int process_message(char *req, log_manager *logger) {
|
||||||
{
|
if(req == NULL) return 0;
|
||||||
//TODO 修改管道命令解析
|
|
||||||
if(req ==NULL )
|
|
||||||
return 0;
|
|
||||||
int fd;
|
int fd;
|
||||||
char front,rear;
|
char type[16], end[16];
|
||||||
sscanf(req,"%s/%d/%s",&rear,&fd,&rear);
|
if(sscanf(req, "%15s/%d/%15s", type, &fd, end) != 3) {
|
||||||
|
free(req);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
char *req_buf = recv_http_request(fd);
|
char *req_buf = recv_http_request(fd);
|
||||||
|
if(!req_buf) { // 检查返回值
|
||||||
|
close(fd);
|
||||||
|
free(req);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
const char *body = http_get_body(req_buf);
|
const char *body = http_get_body(req_buf);
|
||||||
free(req_buf);
|
free(req_buf);
|
||||||
|
|
||||||
rbt_msg message;
|
rbt_msg message;
|
||||||
rbt_parse_json(body,&message);
|
if(rbt_parse_json(body, &message) == 0) {
|
||||||
make_swap((void*)&message);
|
make_swap((void*)&message);
|
||||||
logs *log = malloc(sizeof log);
|
|
||||||
if(snprintf(log->log,sizeof(log->log),
|
logs *log = malloc(sizeof(logs));
|
||||||
"%s message %s processd ok\n",message.nickname,message.raw_message)<1024);
|
// 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);
|
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 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)
|
if(i<MAX_POOL)
|
||||||
i++;
|
i++;
|
||||||
else
|
else{
|
||||||
i=0;
|
i=0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//向空闲线程发送数据
|
//向空闲线程发送数据
|
||||||
write(self->pool[i].fifo_fd[0],command,strlen(command));
|
write(self->pool[i].fifo_fd[0],command,strlen(command));
|
||||||
//设置线程程为working
|
//设置线程程为working
|
||||||
@ -143,8 +245,9 @@ void *pth_module(void *args_p)
|
|||||||
pth_m *pmd = argms->pth;
|
pth_m *pmd = argms->pth;
|
||||||
log_manager *logger = argms->log;
|
log_manager *logger = argms->log;
|
||||||
//参数解析
|
//参数解析
|
||||||
|
free(args_p);
|
||||||
char name[256] = {'\0'};
|
char name[256] = {'\0'};
|
||||||
sprintf(name,"chatrebot%lu",pmd->pthread_id);
|
sprintf(name,"chatrebot%lu",pthread_self());
|
||||||
int swap = create_swap(name);
|
int swap = create_swap(name);
|
||||||
//创建共享内存
|
//创建共享内存
|
||||||
char swap_arg[64] = {'\0'};
|
char swap_arg[64] = {'\0'};
|
||||||
@ -159,16 +262,17 @@ void *pth_module(void *args_p)
|
|||||||
execv("Run_pluhginmanager",args);
|
execv("Run_pluhginmanager",args);
|
||||||
}
|
}
|
||||||
logs *pth_log = (logs*)malloc(sizeof(logs));
|
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);
|
logger->in_log(pth_log,logger);
|
||||||
//拉起python插件管理器
|
//拉起python插件管理器
|
||||||
for(;;){
|
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) {
|
if (n == EOF) {
|
||||||
@ -176,6 +280,9 @@ void *pth_module(void *args_p)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
pth_log = (logs*)malloc(sizeof(logs));
|
||||||
|
sprintf(pth_log->log,"processd message");
|
||||||
|
logger->in_log(pth_log,logger);
|
||||||
process_message(req,logger);
|
process_message(req,logger);
|
||||||
atomic_fetch_add(&pmd->status, 1);
|
atomic_fetch_add(&pmd->status, 1);
|
||||||
}
|
}
|
||||||
@ -189,11 +296,12 @@ int start_pool(netm *self)
|
|||||||
//为线程开辟管道
|
//为线程开辟管道
|
||||||
pipe(self->pool[i].fifo_fd);
|
pipe(self->pool[i].fifo_fd);
|
||||||
//启动线程
|
//启动线程
|
||||||
net_args arg;
|
net_args *arg = (net_args*)malloc(sizeof(net_args));
|
||||||
arg.pth =&self->pool[i];
|
arg->pth = &self->pool[i];
|
||||||
arg.log = self->logmanager;
|
arg->log = self->logmanager;
|
||||||
|
|
||||||
self->pool[i].status = 1;
|
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 server_run(int port,int fifo_fd,netm *self)
|
||||||
{
|
{
|
||||||
int epfd = epoll_create1(EPOLL_CLOEXEC); // 推荐
|
int epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||||
if (epfd == -1) {
|
if (epfd == -1) {
|
||||||
perror("epoll_create1");
|
perror("epoll_create1");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -222,7 +330,7 @@ int server_run(int port,int fifo_fd,netm *self)
|
|||||||
ev.data.fd = fifo_fd;
|
ev.data.fd = fifo_fd;
|
||||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
epoll_ctl(epfd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
||||||
char iss_buf[256];
|
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;
|
ev.data.fd = self->http_fd;
|
||||||
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
|
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;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
//插件更新逻辑
|
//插件更新逻辑
|
||||||
|
|
||||||
break;
|
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[0]= fifo[0];
|
||||||
self->fifo_fd[1]= fifo[1];
|
self->fifo_fd[1]= fifo[1];
|
||||||
self->last_alc = 0;
|
self->last_alc = 0;
|
||||||
|
self->port = port;
|
||||||
//初始化参数
|
//初始化参数
|
||||||
self->logmanager = logmanager;
|
self->logmanager = logmanager;
|
||||||
self->err_indictor = (indiector*)malloc(sizeof(indiector));
|
self->err_indictor = (indiector*)malloc(sizeof(indiector));
|
||||||
self->port = port;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
#define NETWORK
|
#define NETWORK
|
||||||
|
|
||||||
#define MAX_POOL 10
|
#define MAX_POOL 10
|
||||||
#define MAX_MESSAGE_BUF 10240
|
#define MAX_MESSAGE_BUF 1024
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "tools/log/log.h"
|
#include "tools/log/log.h"
|
||||||
#include "erroprocess/erroprocess.h"
|
#include "erroprocess/erroprocess.h"
|
||||||
@ -23,7 +23,7 @@ typedef struct net_args
|
|||||||
|
|
||||||
typedef struct network_manager
|
typedef struct network_manager
|
||||||
{
|
{
|
||||||
void *(*run_network)(void*);
|
void *(*run_network)(void*);//启动网络监听
|
||||||
int (*start_pool)(struct network_manager*);
|
int (*start_pool)(struct network_manager*);
|
||||||
int (*shutdown_pool)(struct network_manager*);
|
int (*shutdown_pool)(struct network_manager*);
|
||||||
int (*iss_work)(struct network_manager*,char *);
|
int (*iss_work)(struct network_manager*,char *);
|
||||||
|
|||||||
@ -211,9 +211,11 @@ int infifo(Ctl *self,const char *cmd)
|
|||||||
//存储命令历史s
|
//存储命令历史s
|
||||||
if(self->index<HISTORY_BUF){
|
if(self->index<HISTORY_BUF){
|
||||||
self->index++;
|
self->index++;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
self->index = 0;
|
self->index = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -87,19 +87,20 @@ int cleanup(log_manager *self)
|
|||||||
loc = self->log;
|
loc = self->log;
|
||||||
self->log = NULL;
|
self->log = NULL;
|
||||||
self->count = 0;//摘取log链
|
self->count = 0;//摘取log链
|
||||||
|
|
||||||
sem_post(&self->log_sem);
|
sem_post(&self->log_sem);
|
||||||
//释放信号量
|
//释放信号量
|
||||||
|
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
|
||||||
while(loc->next !=NULL)
|
while(loc->next !=NULL)
|
||||||
{
|
{
|
||||||
tobeclean = loc;
|
tobeclean = loc;
|
||||||
loc = loc->next;
|
loc = loc->next;
|
||||||
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
|
|
||||||
if(fd == -1)
|
if(fd == -1)
|
||||||
perror("file:");
|
perror("file:");
|
||||||
write(fd,loc->log,strlen(loc->log));
|
write(fd,tobeclean->log,strlen(tobeclean->log));
|
||||||
close(fd);
|
|
||||||
free(tobeclean);
|
free(tobeclean);
|
||||||
}
|
}
|
||||||
|
close(fd);
|
||||||
free(loc);
|
free(loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,10 +28,10 @@ int quit_server(netm *self)
|
|||||||
self->http_fd =-1;
|
self->http_fd =-1;
|
||||||
}
|
}
|
||||||
//关闭socket监听
|
//关闭socket监听
|
||||||
if(self->fifo_fd[0] != -1)
|
if(self->fifo_fd[1] != -1)
|
||||||
{
|
{
|
||||||
close(self->fifo_fd[0]);
|
close(self->fifo_fd[1]);
|
||||||
self->fifo_fd[0] = -1;
|
self->fifo_fd[1] = -1;
|
||||||
}
|
}
|
||||||
//关闭管道监听
|
//关闭管道监听
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user