Compare commits
15 Commits
55f3e1411b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 02a9d73817 | |||
| d81a3c8042 | |||
| b618cc359a | |||
| b23828cd5c | |||
| bdf8ef62f7 | |||
| a8c5f0dabf | |||
| 19e0392db6 | |||
| d01219da30 | |||
| 4445567169 | |||
| 674f611d07 | |||
| 3673b10942 | |||
| ee596a654d | |||
| 65115e1a74 | |||
| 73a94417b0 | |||
| a73b317547 |
2
.gitignore
vendored
Normal file → Executable file
2
.gitignore
vendored
Normal file → Executable file
@ -6,7 +6,7 @@ __pycache__/
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
.vscode/
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
|
||||
2
README.md
Normal file → Executable file
2
README.md
Normal file → Executable file
@ -1,6 +1,6 @@
|
||||
# OneBot Chatbot Framework
|
||||
|
||||
该项目是一个基于OneBot标准的聊天机器人后端框架,采用高度可扩展的插件架构设计,支持消息的模块化处理和插件热加载。
|
||||
该项目是一个基于OneBot标准的聊天机器人后端框架,采用高度可扩展的插件架构设计,支持消息的模块化处理和插件热加载。如果你有任何意见或建议,可以通过 jianfeee@outlook.com 联系我。
|
||||
|
||||
## 项目特点
|
||||
|
||||
|
||||
3
c/CMakeLists.txt
Normal file → Executable file
3
c/CMakeLists.txt
Normal file → Executable file
@ -17,7 +17,8 @@ add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstal
|
||||
add_library(Log SHARED tools/log/log.c)
|
||||
add_library(Toml SHARED tools/toml/toml.c)
|
||||
add_library(Quit SHARED tools/quit/quit.c)
|
||||
add_library(Memctl SHARED memctl/memctl.c)
|
||||
|
||||
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit)
|
||||
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit Memctl)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR})
|
||||
26
c/config.h
Normal file → Executable file
26
c/config.h
Normal file → Executable file
@ -1,15 +1,37 @@
|
||||
#ifndef SEVERCONFG
|
||||
#define SEVERCONFG
|
||||
|
||||
#define MAX_LOG 256
|
||||
/*------日志管理---------*/
|
||||
#define MAX_LOG 50
|
||||
#define MAX_LOG_LENGTH 4080
|
||||
#define INFO_LENGTH 8
|
||||
#define LOG_SLEEP_LENGTH 1
|
||||
/*------日志管理---------*/
|
||||
|
||||
/*-------终端管理-------*/
|
||||
#define TEM_MAX_BUF 256
|
||||
#define TEM_HISTORY_BUF 210
|
||||
#define TEM_PROMPT "chatbot$$ "
|
||||
/*----终端管理--------*/
|
||||
|
||||
#define NET_MAX_POOL 10
|
||||
/*-------网路池管理-----*/
|
||||
#define NET_MAX_POOL 1
|
||||
#define NET_MAX_MESSAGE_BUF 1024
|
||||
#define HTTP_BLOCK_SIZE 512
|
||||
#define MAX_HTTP_LENGTH 20
|
||||
/*-------网路池管理-----*/
|
||||
|
||||
/*------解释器管理-------*/
|
||||
#define INTER_MAX_BUF 256
|
||||
/*------解释器管理-------*/
|
||||
|
||||
/*------内存池管理------*/
|
||||
#define MAX_MEM_SIZE 512
|
||||
#define COMINE_MEM_SIZE 448
|
||||
#define MEM_BLOCK_SIZE 4096
|
||||
#define POOL_EXPEND_ID 3
|
||||
#define POOL_EXPEND_SIZE 4
|
||||
#define CYCLE_NUM 7
|
||||
/*------内存池管理------*/
|
||||
|
||||
#endif
|
||||
34
c/interpreter/interpreter.c
Normal file → Executable file
34
c/interpreter/interpreter.c
Normal file → Executable file
@ -9,24 +9,22 @@
|
||||
#include "interpreter.h"
|
||||
#include "tools/pkgmanager/pkginstall.h"
|
||||
|
||||
int inter_in_log(const char *log,log_manager *manager)
|
||||
int inter_in_log(const char *log,const char *info,log_manager *manager)
|
||||
{
|
||||
if(strlen(log)>1024)
|
||||
return -1;
|
||||
logs* logss = (logs*)malloc(sizeof(logs));
|
||||
memcpy(logss->log,log,strlen(log));
|
||||
manager->in_log(logss,manager);
|
||||
manager->in_log(manager,log,info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager)
|
||||
{
|
||||
printf("SYS:prepare env\n");
|
||||
inter_in_log("SYS:prepare env\n",log_manager);
|
||||
inter_in_log("prepare env\n","SYS",log_manager);
|
||||
printf("SYS:env ready\n");
|
||||
inter_in_log("SYS:env ready\n",log_manager);
|
||||
inter_in_log("env ready\n","SYS",log_manager);
|
||||
printf("SYS:loading cmd_dic\n");
|
||||
inter_in_log("SYS:loading cmd_dic\n",log_manager);
|
||||
inter_in_log("loading cmd_dic\n","SYS",log_manager);
|
||||
sprintf(cmd_dic[0].name, "pkginstall");
|
||||
cmd_dic[0].cmd = INSTALL;
|
||||
|
||||
@ -37,7 +35,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager
|
||||
cmd_dic[2].cmd = QUIT;
|
||||
|
||||
printf("SYS:cmd_dir load complite\n");
|
||||
inter_in_log("SYS:cmd_dir load complite\n",log_manager);
|
||||
inter_in_log("cmd_dir load complite\n","SYS",log_manager);
|
||||
|
||||
for(int i =0;i<10;i++)
|
||||
{
|
||||
@ -45,7 +43,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager
|
||||
}
|
||||
self->arg = NULL;
|
||||
printf("SYS:Creating ctl fifo\n");
|
||||
inter_in_log("SYS:Creating ctl fifo\n",log_manager);
|
||||
inter_in_log("Creating ctl fifo\n","SYS",log_manager);
|
||||
memcpy(self->fifofd,fifo,2*sizeof(int));
|
||||
self->log_manager = log_manager;
|
||||
}
|
||||
@ -71,7 +69,7 @@ int get_args(ctx *self)
|
||||
arg->next = (args*)malloc(sizeof(args));
|
||||
if(arg->next == NULL){
|
||||
perror("ERROR:fail to get mem");
|
||||
inter_in_log("ERROR:fail to get mem\n",self->log_manager);
|
||||
inter_in_log("fail to get mem\n","ERROR",self->log_manager);
|
||||
return -1;
|
||||
}
|
||||
arg = arg->next;
|
||||
@ -139,38 +137,38 @@ int match_cmd(const Cmd* cmd_dic,char *cmd_buf)
|
||||
}
|
||||
|
||||
|
||||
int exce(const int command,ctx *all_ctx)
|
||||
int exec(const int command,ctx *all_ctx)
|
||||
{
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case BAD_INPUT:
|
||||
printf("SYS:bad input,try again\n");
|
||||
inter_in_log("SYS:bad input,try again\n",all_ctx->log_manager);
|
||||
inter_in_log("bad input,try again\n","SYS",all_ctx->log_manager);
|
||||
return BAD_INPUT;
|
||||
|
||||
case INSTALL:
|
||||
if(all_ctx->arg == NULL){
|
||||
printf("SYS:Missing args\n");
|
||||
inter_in_log("SYS:Missng args\n",all_ctx->log_manager);
|
||||
inter_in_log("Missng args\n","SYS",all_ctx->log_manager);
|
||||
return 1;
|
||||
}
|
||||
printf("SYS:init pkgmanager\n");
|
||||
inter_in_log("SYS:init pkgmanager\n",all_ctx->log_manager);
|
||||
inter_in_log("init pkgmanager\n","SYS",all_ctx->log_manager);
|
||||
pkger *manager = init_pkginstaller();
|
||||
printf("SYS:installing\n");
|
||||
inter_in_log("SYS:installing\n",all_ctx->log_manager);
|
||||
inter_in_log("installing\n","SYS",all_ctx->log_manager);
|
||||
manager->packup(manager);
|
||||
return 1;
|
||||
|
||||
case RUN:
|
||||
printf("SYS:runing\n");
|
||||
inter_in_log("SYS:running\n",all_ctx->log_manager);
|
||||
inter_in_log("running\n","SYS",all_ctx->log_manager);
|
||||
return 1;
|
||||
|
||||
case QUIT:
|
||||
printf("SYS:shuting down\n");
|
||||
inter_in_log("SYS:shuting down\n",all_ctx->log_manager);
|
||||
inter_in_log("shuting down\n","SYS",all_ctx->log_manager);
|
||||
all_ctx->statue = -1;
|
||||
write(all_ctx->fifofd[1],"q",1);
|
||||
return 1;
|
||||
@ -202,7 +200,7 @@ int interpret(int mod, ctx *all_ctx,Cmd *cmd_dic)
|
||||
if(cmd_buf[len-1] == '\n')
|
||||
cmd_buf[len-1] = '\0';
|
||||
//执行命令
|
||||
exce(match_cmd(cmd_dic, cmd_buf),all_ctx);
|
||||
exec(match_cmd(cmd_dic, cmd_buf),all_ctx);
|
||||
|
||||
//释放所有堆内存
|
||||
free(cmd_buf);
|
||||
|
||||
0
c/interpreter/interpreter.h
Normal file → Executable file
0
c/interpreter/interpreter.h
Normal file → Executable file
22
c/main.c
Normal file → Executable file
22
c/main.c
Normal file → Executable file
@ -3,6 +3,8 @@
|
||||
#include "network/network.h"
|
||||
#include "tools/toml/toml.h"
|
||||
#include "tools/quit/quit.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -33,9 +35,12 @@ int main()
|
||||
perror("load config error");
|
||||
int port = (int)toml_int_in(server,"list_port").u.i;
|
||||
//加载配置文件,读取端口
|
||||
mem_ctl *mem_ctler = (mem_ctl*)malloc(sizeof(mem_ctl));
|
||||
init_memctl(mem_ctler);
|
||||
log_manager *logsmanager=(log_manager*)malloc(sizeof(log_manager));
|
||||
init_loger(logsmanager);
|
||||
|
||||
//创建日志管理器与定时清理线层
|
||||
init_loger(logsmanager,mem_ctler);
|
||||
pthread_create(&logsmanager->pid,NULL,logsmanager->clear_log,logsmanager);
|
||||
Ctl *teml = init_tem(logsmanager);
|
||||
teml->config = server;
|
||||
//初始化终端对象
|
||||
@ -45,20 +50,27 @@ int main()
|
||||
netm *networkmanager = (netm*)malloc(sizeof(netm));
|
||||
init_networkmanager(networkmanager,fifo,logsmanager,port);
|
||||
//初始化网络管理器对象
|
||||
pthread_t network_id;
|
||||
pthread_create(&network_id,NULL,networkmanager->run_network,(void*)networkmanager);
|
||||
|
||||
pthread_create(&networkmanager->pid,NULL,networkmanager->run_network,(void*)networkmanager);
|
||||
//启动网络监听与线程池,并加载插件
|
||||
alres *resource = (alres*)malloc(sizeof(alres));
|
||||
resource->loger = logsmanager;
|
||||
resource->network = networkmanager;
|
||||
resource->tem = teml;
|
||||
resource->memctler = mem_ctler;
|
||||
on_exit(quit_all,resource);
|
||||
//注册清理函数
|
||||
teml->run(teml,fifo);
|
||||
|
||||
//启动终端
|
||||
pthread_join(network_id,NULL);
|
||||
|
||||
//等待网络管理器进程结束
|
||||
pthread_join(networkmanager->pid,NULL);
|
||||
networkmanager->pid = -1;
|
||||
close(fifo[1]);
|
||||
log_manager_stop(logsmanager);
|
||||
pthread_join(logsmanager->pid,NULL);
|
||||
logsmanager->pid = -1;
|
||||
return 1;
|
||||
|
||||
}
|
||||
159
c/memctl/memctl.c
Normal file
159
c/memctl/memctl.c
Normal file
@ -0,0 +1,159 @@
|
||||
#include "memctl.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
((type *)((char *)(ptr) - offsetof(type, member)))
|
||||
|
||||
int extend_pool(mem_ctl* self,int size)
|
||||
{
|
||||
if(self == NULL)//入参检查
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int poolsize = atomic_load(&self->poolsize);
|
||||
int target = poolsize +size;//计算目标大小
|
||||
if(target >MAX_MEM_SIZE)
|
||||
target = MAX_MEM_SIZE;//计算限制
|
||||
|
||||
for(int i = poolsize;i<target;i++)//分配内存
|
||||
{
|
||||
if(self->blocks[i].location !=NULL)//若存在未释放内存
|
||||
free(self->blocks[i].location);
|
||||
self->blocks[i].location = malloc(MEM_BLOCK_SIZE);
|
||||
if(self->blocks[i].location == NULL)
|
||||
return target-i;//堆内存不足
|
||||
atomic_store(&self->blocks[i].condition,MEM_FREE);
|
||||
}
|
||||
atomic_fetch_add(&self->poolsize,size);
|
||||
int log = atomic_load(&self->logbuf)+size/2;
|
||||
atomic_store(&self->logbuf,log);//重新划定日志区
|
||||
return 0;
|
||||
}
|
||||
|
||||
void **GetBlock(mem_ctl* self,int type)
|
||||
{
|
||||
int status = 0;
|
||||
int sp=0,start = 0;
|
||||
int id;
|
||||
//模式判断
|
||||
if(type == LOGMOD)
|
||||
{
|
||||
id = atomic_load(&self->Loglast_loc);
|
||||
sp = atomic_load(&self->poolsize);
|
||||
start = atomic_load(&self->logbuf);
|
||||
if(id<start)
|
||||
id = start;
|
||||
}
|
||||
else
|
||||
{
|
||||
id = atomic_load(&self->Commenlast_loc);
|
||||
sp = atomic_load(&self->logbuf)+1;
|
||||
if(id>sp)
|
||||
id = start;
|
||||
}
|
||||
int i = id;
|
||||
for(status;status<CYCLE_NUM;status++)//最多扫描轮次
|
||||
{
|
||||
//获取空闲块
|
||||
for(i;i<sp;i++)
|
||||
{
|
||||
if(atomic_fetch_sub(&self->blocks[i].condition,1) == MEM_FREE)
|
||||
{
|
||||
atomic_fetch_sub(&self->blocks[i].condition,1);
|
||||
if(i>self->logbuf)
|
||||
atomic_store(&self->Loglast_loc,i);
|
||||
else
|
||||
atomic_store(&self->Commenlast_loc,i);
|
||||
return &self->blocks[i].location;
|
||||
}
|
||||
else{
|
||||
atomic_fetch_add(&self->blocks[i].condition,1);//回滚路径
|
||||
}
|
||||
}
|
||||
i = start;
|
||||
atomic_fetch_add(&self->mem_e_indicator,1);
|
||||
//检查是否需要扩池
|
||||
if(atomic_fetch_sub(&self->mem_e_indicator,POOL_EXPEND_ID)>POOL_EXPEND_ID)
|
||||
{
|
||||
extend_pool(self,POOL_EXPEND_SIZE);
|
||||
}
|
||||
else{
|
||||
atomic_fetch_add(&self->mem_e_indicator,POOL_EXPEND_ID);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int FreeBlock(mem_ctl* self,void** block_p)
|
||||
{
|
||||
mem_block *block;
|
||||
block = container_of(block_p,mem_block,location);
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
if(block == NULL)
|
||||
return -1;
|
||||
if(block < &self->blocks[COMINE_MEM_SIZE])//标准池部分归还
|
||||
{
|
||||
atomic_fetch_add(&block->condition,2);
|
||||
block = NULL;
|
||||
return 0;
|
||||
}
|
||||
else//扩容池尝试回收
|
||||
{
|
||||
atomic_fetch_add(&block->condition,1);
|
||||
free(block->location);
|
||||
block->location = NULL;
|
||||
int poolsize = atomic_load(&self->poolsize);
|
||||
if(&self->blocks[poolsize-1] == block)
|
||||
{
|
||||
for(int i = poolsize -1;i>COMINE_MEM_SIZE;i--)
|
||||
{
|
||||
if(atomic_load(&self->blocks[i].condition) == PROCESSING)
|
||||
{
|
||||
poolsize--;
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
if(poolsize-COMINE_MEM_SIZE%2 == 0)
|
||||
{
|
||||
atomic_fetch_sub(&self->logbuf,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
atomic_store(&self->poolsize,poolsize);
|
||||
}
|
||||
}
|
||||
|
||||
int init_memctl(mem_ctl *self)
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
self->poolsize = 0;
|
||||
for(int i =0;i<MAX_MEM_SIZE;i++)
|
||||
{
|
||||
self->blocks[i].location = NULL;
|
||||
atomic_init(&self->blocks[i].condition,PROCESSING);//初始化池
|
||||
}
|
||||
extend_pool(self,COMINE_MEM_SIZE);//预分配内存
|
||||
self->Commenlast_loc = 0;
|
||||
self->logbuf = COMINE_MEM_SIZE/2;
|
||||
self->mem_e_indicator = 0;
|
||||
self->FreeBlock = FreeBlock;
|
||||
self->GetBlock = GetBlock;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int free_memctl(mem_ctl *self)//清除内存池
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
for(int i = 0;i<MAX_MEM_SIZE;i++)
|
||||
{
|
||||
if(self->blocks[i].location!=NULL)
|
||||
free(self->blocks[i].location);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
36
c/memctl/memctl.h
Normal file
36
c/memctl/memctl.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef MEMCTL
|
||||
#define MEMCTL
|
||||
#include "config.h"
|
||||
#include <stdatomic.h>
|
||||
|
||||
#define MEM_FREE 1
|
||||
#define INUSE -1
|
||||
#define PROCESSING 0
|
||||
|
||||
#define LOGMOD 0
|
||||
#define COMMENMOD 1
|
||||
|
||||
typedef struct mem_block
|
||||
{
|
||||
void *location;//块地址
|
||||
atomic_int condition;//块状态
|
||||
}mem_block;
|
||||
|
||||
typedef struct mem_ctl
|
||||
{
|
||||
mem_block blocks[MAX_MEM_SIZE];
|
||||
atomic_int logbuf;
|
||||
atomic_int poolsize;
|
||||
atomic_int Commenlast_loc;//分配起始
|
||||
atomic_int Loglast_loc;
|
||||
atomic_int mem_e_indicator;//内存不足指示器
|
||||
//获取一个内存块
|
||||
void** (*GetBlock)(struct mem_ctl*,int);
|
||||
//释放一个内存块
|
||||
int (*FreeBlock)(struct mem_ctl*,void**);
|
||||
}mem_ctl;
|
||||
|
||||
int init_memctl(mem_ctl *self);
|
||||
int free_memctl(mem_ctl *self);
|
||||
|
||||
#endif
|
||||
18
c/network/cJSON.c
Normal file → Executable file
18
c/network/cJSON.c
Normal file → Executable file
@ -160,25 +160,11 @@ typedef struct internal_hooks
|
||||
void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
|
||||
} internal_hooks;
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
/* work around MSVC error C2322: '...' address of dllimport '...' is not static */
|
||||
static void * CJSON_CDECL internal_malloc(size_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
static void CJSON_CDECL internal_free(void *pointer)
|
||||
{
|
||||
free(pointer);
|
||||
}
|
||||
static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
|
||||
{
|
||||
return realloc(pointer, size);
|
||||
}
|
||||
#else
|
||||
|
||||
#define internal_malloc malloc
|
||||
#define internal_free free
|
||||
#define internal_realloc realloc
|
||||
#endif
|
||||
|
||||
|
||||
/* strlen of character literals resolved at compile time */
|
||||
#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
|
||||
|
||||
0
c/network/cJSON.h
Normal file → Executable file
0
c/network/cJSON.h
Normal file → Executable file
0
c/network/erroprocess/erroprocess.c
Normal file → Executable file
0
c/network/erroprocess/erroprocess.c
Normal file → Executable file
0
c/network/erroprocess/erroprocess.h
Normal file → Executable file
0
c/network/erroprocess/erroprocess.h
Normal file → Executable file
86
c/network/http/http_rel.c
Normal file → Executable file
86
c/network/http/http_rel.c
Normal file → Executable file
@ -3,28 +3,22 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bits/fcntl-linux.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "tools/log/log.h"
|
||||
#include "http_rel.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
/* 接收状态辅助结构 */
|
||||
#include "errno.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "http_rel.h"
|
||||
|
||||
const char *http_get_body(const char *buf);
|
||||
char *recv_http_request(int cfd);
|
||||
|
||||
/* 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;
|
||||
int write_in_bk(char input,httpbuf* blk){
|
||||
|
||||
}
|
||||
char *recv_http_request(int cfd){
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 初始化HTTP监听socket,所有错误通过logmanager记录
|
||||
@ -34,49 +28,48 @@ const char *http_get_body(const char *buf)
|
||||
*/
|
||||
int init_http_network(int port, log_manager *logger)
|
||||
{
|
||||
logs *log;
|
||||
int fd;
|
||||
|
||||
int fd;
|
||||
char log[MAX_LOG_LENGTH];
|
||||
/* 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);
|
||||
snprintf(log, sizeof(log),
|
||||
"socket() failed: %s", strerror(errno));
|
||||
logger->in_log(logger, log,"FATAL");
|
||||
|
||||
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",
|
||||
snprintf(log, sizeof(log),
|
||||
"setsockopt(SO_REUSEADDR) on fd=%d failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
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);
|
||||
|
||||
snprintf(log, sizeof(log),
|
||||
"fcntl(F_GETFL) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
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);
|
||||
snprintf(log, sizeof(log),
|
||||
"fcntl(O_NONBLOCK) on fd=%d failed: %s", fd, strerror(errno));
|
||||
logger->in_log(logger,log,"ERROR:");
|
||||
close(fd);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -87,30 +80,29 @@ int init_http_network(int port, log_manager *logger)
|
||||
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)",
|
||||
snprintf(log, sizeof(log),
|
||||
"bind(port %d) failed: %s (fd=%d)",
|
||||
port, strerror(errno), fd);
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"FATAL:");
|
||||
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",
|
||||
snprintf(log, sizeof(log),
|
||||
"listen(fd=%d, backlog=10) failed: %s",
|
||||
fd, strerror(errno));
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"FATAL:");
|
||||
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);
|
||||
snprintf(log, sizeof(log),
|
||||
"Successfully listening on port %d (fd=%d)", port, fd);
|
||||
logger->in_log(logger, log,"HTTP:");
|
||||
return fd;
|
||||
}
|
||||
|
||||
9
c/network/http/http_rel.h
Normal file → Executable file
9
c/network/http/http_rel.h
Normal file → Executable file
@ -1,7 +1,14 @@
|
||||
#ifndef HTTP_REL
|
||||
#define HTTP_REL
|
||||
|
||||
const char *http_get_body(const char *buf);
|
||||
#include "config.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
typedef struct httpbuf{
|
||||
int size;
|
||||
char buf[MEM_BLOCK_SIZE-5];
|
||||
}httpbuf;//http分块结构体
|
||||
|
||||
char *recv_http_request(int cfd);
|
||||
int init_http_network(int port, log_manager *logger);
|
||||
|
||||
|
||||
85
c/network/network.c
Normal file → Executable file
85
c/network/network.c
Normal file → Executable file
@ -33,6 +33,8 @@ static void safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
/* 主解析 */
|
||||
int rbt_parse_json(const char *json_text, rbt_msg *out)
|
||||
{
|
||||
if(json_text == NULL)
|
||||
return -1;
|
||||
memset(out, 0, sizeof(*out)); // 统一清 0,gid 天然 '\0'
|
||||
|
||||
cJSON *root = cJSON_Parse(json_text);
|
||||
@ -74,7 +76,6 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
|
||||
out->message_type = 'p';
|
||||
/* else 保持 0 */
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
return 0; // 成功
|
||||
}
|
||||
@ -99,25 +100,17 @@ int process_message(char *req, log_manager *logger,rbt_msg *swap) {
|
||||
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);
|
||||
const char *body = recv_http_request(fd);
|
||||
|
||||
if(rbt_parse_json(body,swap) == 0) {
|
||||
logs *log = malloc(sizeof(logs));
|
||||
char log[MAX_LOG_LENGTH];
|
||||
// cppcheck-suppress uninitdata
|
||||
snprintf(log->log, sizeof(log->log), "%s message %s processed ok\n",
|
||||
snprintf(log, sizeof(log), "%s message %s processed ok\n",
|
||||
swap->nickname,swap->raw_message);
|
||||
make_swap(swap);
|
||||
logger->in_log(log, logger);
|
||||
logger->in_log(logger,log,"PROCESSER:");
|
||||
}
|
||||
|
||||
//通知前端已收到消息
|
||||
const char *response =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
@ -172,11 +165,11 @@ void *pth_module(void *args_p)
|
||||
NULL};
|
||||
execv("Run_pluhginmanager",args);
|
||||
}
|
||||
logs *pth_log = (logs*)malloc(sizeof(logs));
|
||||
char pth_log[40];
|
||||
// cppcheck-suppress uninitdata
|
||||
sprintf(pth_log->log,"PID:%lu launched python plugines\n",pthread_self());
|
||||
sprintf(pth_log,"launched python plugines,pid:%ld\n",pthread_self());
|
||||
|
||||
logger->in_log(pth_log,logger);
|
||||
logger->in_log(logger,pth_log,"PROCESSER:");
|
||||
rbt_msg *swap = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED,swapfd, 0);
|
||||
//拉起python插件管理器
|
||||
for(;;){
|
||||
@ -192,9 +185,6 @@ 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,swap);
|
||||
atomic_fetch_add(&pmd->status, 1);
|
||||
}
|
||||
@ -226,6 +216,7 @@ int shutdown_pool(netm *self)
|
||||
self->pool[i].status = -1;
|
||||
close(self->pool[i].fifo_fd[1]);
|
||||
}
|
||||
self->statue = ALL_STOP;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -248,11 +239,11 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
|
||||
struct epoll_event events[10];
|
||||
self->epoll_fd = epfd;
|
||||
self->statue = SERVER_ON;
|
||||
for(;;)
|
||||
{
|
||||
/*工作循环-----------------------------*/
|
||||
int nf = epoll_wait(epfd,events,10,-1);
|
||||
printf("%d\n",nf);
|
||||
if (nf == -1) {
|
||||
perror("epoll_wait");
|
||||
break;
|
||||
@ -268,21 +259,43 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
sprintf(iss_buf,"s/%d/e",nt_fd);
|
||||
self->iss_work(self,iss_buf);
|
||||
}
|
||||
if(events[i].data.fd == fifo_fd)
|
||||
{
|
||||
char command;
|
||||
while(read(fifo_fd,&command,1)==1)
|
||||
{
|
||||
switch(command){
|
||||
case 'q':
|
||||
//退出逻辑
|
||||
quit_server(self);
|
||||
return 1;
|
||||
break;
|
||||
case 'u':
|
||||
//插件更新逻辑
|
||||
break;
|
||||
if(events[i].data.fd == fifo_fd) {
|
||||
char buffer[256];
|
||||
ssize_t bytes_read;
|
||||
|
||||
// 一次性读取所有可用数据
|
||||
bytes_read = read(fifo_fd, buffer, sizeof(buffer));
|
||||
|
||||
if (bytes_read > 0) {
|
||||
printf("DEBUG: Read %zd bytes from pipe: ", bytes_read);
|
||||
for (int j = 0; j < bytes_read; j++) {
|
||||
printf("%c ", buffer[j]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// 处理每个命令(按接收顺序)
|
||||
for (int j = 0; j < bytes_read; j++) {
|
||||
printf("Processing command[%d]: %c\n", j, buffer[j]);
|
||||
|
||||
switch(buffer[j]) {
|
||||
case 'q':
|
||||
printf("Quit command found at position %d\n", j);
|
||||
quit_server(self);
|
||||
return 1; // 立即退出,不处理后续命令
|
||||
case 'u':
|
||||
printf("Update command\n");
|
||||
// 更新逻辑
|
||||
break;
|
||||
default:
|
||||
printf("Unknown command: %c (ASCII: %d)\n",
|
||||
buffer[j], buffer[j]);
|
||||
}
|
||||
}
|
||||
} else if (bytes_read == 0) {
|
||||
printf("Pipe closed by writer\n");
|
||||
close(fifo_fd);
|
||||
} else if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||
perror("Error reading from pipe");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -294,6 +307,7 @@ void *run_network(void *self_d)
|
||||
{
|
||||
netm *self = (netm*)self_d;
|
||||
self->start_pool(self);
|
||||
self->statue = POOL_ON;
|
||||
server_run(self->port,self->fifo_fd[0],self);
|
||||
self->shutdown_pool(self);
|
||||
}
|
||||
@ -312,5 +326,6 @@ int init_networkmanager(netm *self,int *fifo,log_manager *logmanager,int port)
|
||||
//初始化参数
|
||||
self->logmanager = logmanager;
|
||||
self->err_indictor = (indiector*)malloc(sizeof(indiector));
|
||||
self->statue = ALL_STOP;
|
||||
return 0;
|
||||
}
|
||||
5
c/network/network.h
Normal file → Executable file
5
c/network/network.h
Normal file → Executable file
@ -1,6 +1,9 @@
|
||||
#ifndef NETWORK
|
||||
#define NETWORK
|
||||
|
||||
#define POOL_ON 1
|
||||
#define SERVER_ON 2
|
||||
#define ALL_STOP 0
|
||||
|
||||
#include <pthread.h>
|
||||
#include "tools/log/log.h"
|
||||
@ -29,12 +32,14 @@ typedef struct network_manager//网络管理器
|
||||
int (*iss_work)(struct network_manager*,char *);
|
||||
|
||||
int fifo_fd[2];
|
||||
pthread_t pid;
|
||||
log_manager *logmanager;
|
||||
indiector *err_indictor;
|
||||
int last_alc;
|
||||
int port;
|
||||
int epoll_fd;
|
||||
int http_fd;
|
||||
int statue;
|
||||
}netm;
|
||||
|
||||
typedef struct rebot_message
|
||||
|
||||
17
c/network/protocal.h
Normal file
17
c/network/protocal.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef PROTOCAL
|
||||
#define PROTOCAL
|
||||
|
||||
typedef struct network_pakage
|
||||
{
|
||||
char *data;
|
||||
int buf_block;
|
||||
}network_package;
|
||||
|
||||
typedef struct net_protocal
|
||||
{
|
||||
void *protocal;
|
||||
int (*init)(void *self);
|
||||
int (*rev_message)(void *self,network_package *data);
|
||||
}net_protocal;
|
||||
|
||||
#endif
|
||||
6
c/network/swap.c
Normal file → Executable file
6
c/network/swap.c
Normal file → Executable file
@ -33,13 +33,13 @@ int create_swap(const char *name)
|
||||
fcntl(fd, F_SETFD, flags);
|
||||
//调整大小
|
||||
rbt_msg *init_msg = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
char buf[MAX_MESSAGE_BUF] = {'\0'};
|
||||
char buf[NET_MAX_MESSAGE_BUF] = {'\0'};
|
||||
//初始化
|
||||
memcpy(init_msg->raw_message,buf,MAX_MESSAGE_BUF);
|
||||
memcpy(init_msg->raw_message,buf,NET_MAX_MESSAGE_BUF);
|
||||
memcpy(init_msg->nickname,buf,64);
|
||||
sem_init(&init_msg->status,1,1);
|
||||
init_msg->raw_message[0] = '\0';
|
||||
init_msg->state = FREE;
|
||||
init_msg->state = MEM_FREE;
|
||||
init_msg->uid[0] = '\0';
|
||||
munmap((void*)init_msg,sizeof(rbt_msg));
|
||||
return fd;
|
||||
|
||||
2
c/network/swap.h
Normal file → Executable file
2
c/network/swap.h
Normal file → Executable file
@ -4,7 +4,7 @@
|
||||
#include "config.h"
|
||||
#define QUITPLG 0
|
||||
#define NEWMSG 1
|
||||
#define FREE 2
|
||||
#define SW_FREE 2
|
||||
|
||||
int make_swap(rbt_msg *swap);
|
||||
int create_swap(const char *name);
|
||||
|
||||
0
c/run_pluginmanager/run_pluginmanager.c
Normal file → Executable file
0
c/run_pluginmanager/run_pluginmanager.c
Normal file → Executable file
0
c/run_pluginmanager/run_pluginmanager.h
Normal file → Executable file
0
c/run_pluginmanager/run_pluginmanager.h
Normal file → Executable file
15
c/tem/ctl.c
Normal file → Executable file
15
c/tem/ctl.c
Normal file → Executable file
@ -241,9 +241,7 @@ int teml(Ctl *self,int fifo[2])
|
||||
char input[TEM_MAX_BUF] = {'\0'};
|
||||
ctx *command = (ctx*)malloc(sizeof(ctx));
|
||||
Cmd cmd_dir[10];
|
||||
init_interpreter(cmd_dir,command,fifo,self->logmanager);//初始化解释器
|
||||
//创建线程用于定期清理日志
|
||||
pthread_create(&self->logwathcher,NULL,self->logmanager->clear_log,self->logmanager);
|
||||
init_interpreter(cmd_dir,command,fifo,self->logmanager);
|
||||
command->statue = 0;
|
||||
self->command = command;
|
||||
do
|
||||
@ -254,20 +252,13 @@ int teml(Ctl *self,int fifo[2])
|
||||
perror("sys error");
|
||||
//将用户输入入队
|
||||
infifo(self,input);
|
||||
logs *log = (logs*)malloc(sizeof(logs));
|
||||
memcpy(log->log,input,sizeof(input));
|
||||
self->logmanager->in_log(log,self->logmanager);
|
||||
|
||||
self->logmanager->in_log(self->logmanager,input,"USER:");
|
||||
memcpy(command->command,input,sizeof(input));
|
||||
interpret(SIG_MOD,command,cmd_dir);
|
||||
const char fexp[256] = {'\0'};
|
||||
memcpy(&input,&fexp,TEM_MAX_BUF);
|
||||
}while(command->statue == 0);
|
||||
log_manager_stop(self->logmanager);
|
||||
pthread_join(self->logwathcher,NULL);
|
||||
//关闭log定期清理程序
|
||||
close(fifo[0]);
|
||||
|
||||
close(fifo[1]);
|
||||
free_history(self);
|
||||
self->command = NULL;
|
||||
free(command);
|
||||
|
||||
1
c/tem/ctl.h
Normal file → Executable file
1
c/tem/ctl.h
Normal file → Executable file
@ -15,7 +15,6 @@ typedef struct Ctl
|
||||
int (*infifo)(struct Ctl*,const char*);
|
||||
int index;
|
||||
char *history[TEM_HISTORY_BUF];
|
||||
pthread_t logwathcher;
|
||||
log_manager *logmanager;
|
||||
ctx *command;//解释器上下文
|
||||
toml_table_t *config;
|
||||
|
||||
186
c/tools/log/log.c
Normal file → Executable file
186
c/tools/log/log.c
Normal file → Executable file
@ -9,42 +9,77 @@
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
int in_log(logs *log,log_manager *self)
|
||||
|
||||
logs* getbody(void **log_p)
|
||||
{
|
||||
return (logs*)*log_p;
|
||||
}
|
||||
int write_into_block(char *writein,char *org,size_t* length,size_t maxlength,char *logname)
|
||||
{
|
||||
if(writein == NULL||org == NULL||length == NULL||logname == NULL)
|
||||
return -1;
|
||||
if(*length+strlen(org)<maxlength-1)
|
||||
{
|
||||
strcpy(&writein[*length],org);
|
||||
*length +=strlen(org);//栈内存充足
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int n = *length + strlen(org) - maxlength+1;
|
||||
strncpy(&writein[*length],org,strlen(org)-n);//栈内存不足
|
||||
writein[maxlength-1] = '\0';
|
||||
int fd = open(logname,O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd != -1)
|
||||
{
|
||||
int eno = write(fd,writein,strlen(writein));
|
||||
if(eno <strlen(writein))
|
||||
perror("log");
|
||||
close(fd);
|
||||
}
|
||||
else if(fd == -1)
|
||||
perror("log:");//仅警告
|
||||
strncpy(writein,&org[0],n);//剩余部分拷贝
|
||||
*length = n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int in_log(log_manager *self,const char *logbody,const char *info)
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
void **log_p = self->mempool->GetBlock(self->mempool,LOGMOD);
|
||||
if(log_p == NULL)
|
||||
{
|
||||
perror("Mem_runout");
|
||||
return -1;
|
||||
}
|
||||
logs *log = getbody(log_p);
|
||||
snprintf(log->info,INFO_LENGTH,"%s",info);
|
||||
snprintf(log->log,MAX_LOG_LENGTH,"%s",logbody);
|
||||
log->log[MAX_LOG_LENGTH-1] = '\0';
|
||||
log->next = NULL;
|
||||
sem_wait(&self->log_sem);//加锁
|
||||
logs *buf = self->rear;
|
||||
if(self->log == NULL){
|
||||
self->log = log;
|
||||
self->rear = log;
|
||||
self->count++;
|
||||
self->log = log_p;
|
||||
self->rear = getbody(log_p);
|
||||
atomic_fetch_add(&self->count,1);
|
||||
sem_post(&self->log_sem);
|
||||
return 0;
|
||||
return self->count;
|
||||
}
|
||||
if(self->count == 1){
|
||||
logs *p = getbody(self->log);
|
||||
p->next = log_p;
|
||||
}
|
||||
self->count++;
|
||||
buf->next = log;
|
||||
log->next = NULL;
|
||||
self->rear->next = log_p;
|
||||
self->rear = log;
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
|
||||
logs *out_log(log_manager *self)
|
||||
{
|
||||
sem_wait(&self->log_sem);
|
||||
logs *buf = self->log;
|
||||
if(self->log==NULL)
|
||||
{
|
||||
sem_post(&self->log_sem);
|
||||
return NULL;
|
||||
}
|
||||
if(self->log->next ==NULL)
|
||||
self->log = self->rear = NULL;
|
||||
self->count--;
|
||||
sem_post(&self->log_sem);
|
||||
buf->next =NULL;
|
||||
return buf;
|
||||
}
|
||||
|
||||
int sleep_with_signal(log_manager *self)
|
||||
{
|
||||
struct timespec ts;
|
||||
@ -54,7 +89,7 @@ int sleep_with_signal(log_manager *self)
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
||||
return -1; /* 罕见失败 */
|
||||
|
||||
ts.tv_sec += 1000;
|
||||
ts.tv_sec += LOG_SLEEP_LENGTH;
|
||||
/* 纳秒部分无需处理,1000 s 整不会溢出 */
|
||||
|
||||
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
|
||||
@ -83,35 +118,74 @@ int cleanup(log_manager *self)
|
||||
if(self->log ==NULL)
|
||||
return 1;
|
||||
logs *tobeclean,*loc;
|
||||
void **tobeclean_p;
|
||||
sem_wait(&self->log_sem);//获取信号量
|
||||
loc = self->log;
|
||||
self->log = NULL;
|
||||
self->count = 0;//摘取log链
|
||||
void **loc_p = self->log;
|
||||
|
||||
self->log = NULL;
|
||||
atomic_store(&self->count,0);//摘取log链
|
||||
sem_post(&self->log_sem);
|
||||
//释放信号量
|
||||
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
|
||||
loc = getbody(loc_p);
|
||||
|
||||
char logbuf[MAX_LOG_LENGTH];
|
||||
|
||||
size_t buf_length = 0;
|
||||
int fd;
|
||||
while(loc->next !=NULL)
|
||||
{
|
||||
tobeclean = loc;
|
||||
loc = loc->next;
|
||||
if(fd == -1)
|
||||
perror("file:");
|
||||
write(fd,tobeclean->log,strlen(tobeclean->log));
|
||||
free(tobeclean);
|
||||
tobeclean_p = loc_p;
|
||||
tobeclean = getbody(tobeclean_p);
|
||||
loc_p = loc->next;
|
||||
loc = getbody(loc_p);
|
||||
|
||||
int eno = write_into_block(logbuf,tobeclean->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
if(eno == -1)
|
||||
perror("log");
|
||||
eno = write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
eno = write_into_block(logbuf,tobeclean->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
if(eno == -1)
|
||||
perror("log");//非业务逻辑只警告
|
||||
|
||||
self->mempool->FreeBlock(self->mempool,tobeclean_p);
|
||||
}
|
||||
write_into_block(logbuf,loc->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
write_into_block(logbuf,loc->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
|
||||
self->mempool->FreeBlock(self,loc_p);
|
||||
|
||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd == -1){
|
||||
perror("log:");
|
||||
}
|
||||
int error_buf = write(fd,logbuf,strlen(logbuf));
|
||||
if(error_buf==-1){
|
||||
return -1;
|
||||
}
|
||||
else if(error_buf<strlen(logbuf)){
|
||||
perror("file");
|
||||
write(fd,"unknown error case log write cut down\n",38);
|
||||
}
|
||||
close(fd);
|
||||
free(loc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void log_manager_stop(log_manager *self)
|
||||
{
|
||||
pthread_mutex_lock(&self->mtx);
|
||||
self->stop = 1; /* 置退出标志 */
|
||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||
if(self->stop == 1){
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
return ;
|
||||
}
|
||||
self->stop = 1;
|
||||
/* 置退出标志 */
|
||||
printf("SYS:stopping loger\n");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
self->in_log(self,"stopping loger\n","SYS:");
|
||||
printf("SYS:done\n");
|
||||
self->in_log(self,"done","SYS:");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||
}
|
||||
|
||||
//定期清理函数
|
||||
@ -122,7 +196,7 @@ void *clear_log(void *self_p)
|
||||
{
|
||||
sleep_with_signal(self);
|
||||
sem_wait(&self->log_sem);
|
||||
if((self->count<256||self->log==NULL)&&self->stop !=1){
|
||||
if((self->count<MAX_LOG||self->log==NULL)&&self->stop !=1){
|
||||
sem_post(&self->log_sem);
|
||||
continue;
|
||||
}
|
||||
@ -134,21 +208,41 @@ void *clear_log(void *self_p)
|
||||
}
|
||||
}
|
||||
|
||||
int init_loger(log_manager *self)
|
||||
int init_loger(log_manager *self,mem_ctl *mempool)
|
||||
{
|
||||
if(self == NULL)
|
||||
{
|
||||
perror("NULL\n");
|
||||
return -1;
|
||||
}
|
||||
sem_init(&self->log_sem, 0,1);
|
||||
pthread_mutex_init(&self->mtx,NULL);
|
||||
pthread_cond_init(&self->cond,NULL);
|
||||
if(sem_init(&self->log_sem, 0,1)==-1)
|
||||
return -1;
|
||||
if(pthread_mutex_init(&self->mtx,NULL)==-1)
|
||||
{
|
||||
if(sem_destroy(&self->log_sem)==-1)
|
||||
{
|
||||
perror("log:");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if(pthread_cond_init(&self->cond,NULL)==-1)
|
||||
{
|
||||
if(sem_destroy(&self->log_sem)==-1)
|
||||
{
|
||||
perror("log:");
|
||||
}
|
||||
if(pthread_mutex_destroy(&self->mtx)==-1)
|
||||
{
|
||||
perror("log:");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
self->in_log = in_log;
|
||||
self->out_log = out_log;
|
||||
self->clear_log = clear_log;
|
||||
self->log = NULL;
|
||||
self->stop = 0;
|
||||
self->cleanup = cleanup;
|
||||
self->count = 0;
|
||||
atomic_init(&self->count,0);
|
||||
self->mempool = mempool;
|
||||
return 0;
|
||||
}
|
||||
17
c/tools/log/log.h
Normal file → Executable file
17
c/tools/log/log.h
Normal file → Executable file
@ -2,32 +2,35 @@
|
||||
#define LOG
|
||||
|
||||
#include "config.h"
|
||||
#include "memctl/memctl.h"
|
||||
#include <semaphore.h>
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
typedef struct logs
|
||||
{
|
||||
char log[1024];
|
||||
struct logs *next;
|
||||
char log[MAX_LOG_LENGTH];
|
||||
void **next;
|
||||
char info[INFO_LENGTH];
|
||||
}logs;
|
||||
|
||||
typedef struct log_manager
|
||||
{
|
||||
int (*in_log)(logs *,struct log_manager*);
|
||||
logs* (*out_log)(struct log_manager*);
|
||||
pthread_t pid;
|
||||
mem_ctl *mempool;
|
||||
int (*in_log)(struct log_manager*,const char *,const char *);
|
||||
void *(*clear_log)(void*);
|
||||
int (*cleanup)(struct log_manager*);
|
||||
sem_t log_sem;
|
||||
logs *log;
|
||||
void **log;
|
||||
logs *rear;
|
||||
int count;
|
||||
atomic_int count;
|
||||
pthread_mutex_t mtx;
|
||||
pthread_cond_t cond;
|
||||
int stop;
|
||||
}log_manager;
|
||||
|
||||
void log_manager_stop(log_manager *self);
|
||||
int init_loger(log_manager *self);
|
||||
int init_loger(log_manager *self,mem_ctl *mempool);
|
||||
|
||||
#endif
|
||||
0
c/tools/pkgmanager/pkginstall.c
Normal file → Executable file
0
c/tools/pkgmanager/pkginstall.c
Normal file → Executable file
0
c/tools/pkgmanager/pkginstall.h
Normal file → Executable file
0
c/tools/pkgmanager/pkginstall.h
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.c
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.c
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.h
Normal file → Executable file
0
c/tools/pkgmanager/update_pkg.h
Normal file → Executable file
43
c/tools/quit/quit.c
Normal file → Executable file
43
c/tools/quit/quit.c
Normal file → Executable file
@ -11,46 +11,62 @@
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/toml/toml.h"
|
||||
|
||||
|
||||
int quit_server(netm *self)
|
||||
{
|
||||
if(self ==NULL)
|
||||
return -1;
|
||||
|
||||
//关闭epoll监听
|
||||
if(self->epoll_fd != -1)
|
||||
{
|
||||
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->http_fd,NULL);
|
||||
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->fifo_fd[0],NULL);
|
||||
self->epoll_fd = -1;
|
||||
}
|
||||
//关闭epoll监听
|
||||
//关闭socket监听
|
||||
if(self->http_fd != -1)
|
||||
{
|
||||
shutdown(self->http_fd, SHUT_RDWR);
|
||||
if(close(self->http_fd)==-1)
|
||||
return -1;
|
||||
perror("http");
|
||||
self->http_fd =-1;
|
||||
}
|
||||
//关闭socket监听
|
||||
//关闭管道监听
|
||||
if(self->fifo_fd[1] != -1)
|
||||
{
|
||||
if(close(self->fifo_fd[1])==-1)
|
||||
return -1;
|
||||
self->fifo_fd[1] = -1;
|
||||
}
|
||||
//关闭管道监听
|
||||
|
||||
free(self->err_indictor);
|
||||
self->statue = POOL_ON;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int quit_mempool(mem_ctl *mem_ctler)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void quit_all(int status,void *self_p)
|
||||
{
|
||||
alres *resouce =(alres*)self_p;
|
||||
//转换参数
|
||||
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
logs *netlog = (logs*)malloc(sizeof(logs));
|
||||
netlog->next = NULL;
|
||||
memcpy(netlog->log,"shuting down networkserver",27);
|
||||
quit_server(resouce->network);
|
||||
resouce->loger->in_log(netlog,resouce->loger);
|
||||
|
||||
|
||||
if(resouce->network->statue == SERVER_ON)
|
||||
{
|
||||
quit_server(resouce->network);
|
||||
}
|
||||
if(resouce->network->statue == POOL_ON)
|
||||
{
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
}
|
||||
resouce->loger->in_log(resouce->loger,"shutting down network pool","SYS:");
|
||||
free(resouce->network);
|
||||
//释放网络资源
|
||||
if(resouce->tem->command !=NULL){
|
||||
@ -72,11 +88,18 @@ void quit_all(int status,void *self_p)
|
||||
free(resouce->tem->command);
|
||||
}
|
||||
//释放终端资源
|
||||
//释放日志管理器
|
||||
if(resouce->loger->pid != -1){
|
||||
log_manager_stop(resouce->loger);
|
||||
pthread_join(resouce->loger->pid,NULL);
|
||||
}
|
||||
pthread_mutex_destroy(&resouce->loger->mtx);
|
||||
resouce->loger->cleanup(resouce->loger);
|
||||
pthread_cond_destroy(&resouce->loger->cond);
|
||||
log_manager_stop(resouce->loger);
|
||||
sem_destroy(&resouce->loger->log_sem);
|
||||
//销毁信号量
|
||||
|
||||
free(resouce->loger);
|
||||
//清理日志
|
||||
free(resouce);
|
||||
}
|
||||
|
||||
3
c/tools/quit/quit.h
Normal file → Executable file
3
c/tools/quit/quit.h
Normal file → Executable file
@ -4,11 +4,14 @@
|
||||
#include "network/network.h"
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
typedef struct all_resources
|
||||
{
|
||||
Ctl *tem;
|
||||
netm *network;
|
||||
log_manager *loger;
|
||||
mem_ctl *memctler;
|
||||
|
||||
}alres;
|
||||
|
||||
|
||||
0
c/tools/toml/toml.c
Normal file → Executable file
0
c/tools/toml/toml.c
Normal file → Executable file
0
c/tools/toml/toml.h
Normal file → Executable file
0
c/tools/toml/toml.h
Normal file → Executable file
0
config/config.toml
Normal file → Executable file
0
config/config.toml
Normal file → Executable file
0
src/file_store_api.py
Normal file → Executable file
0
src/file_store_api.py
Normal file → Executable file
0
src/modules/__init__.py
Normal file → Executable file
0
src/modules/__init__.py
Normal file → Executable file
0
src/modules/plugin_modules.py
Normal file → Executable file
0
src/modules/plugin_modules.py
Normal file → Executable file
0
src/modules/user_modules.py
Normal file → Executable file
0
src/modules/user_modules.py
Normal file → Executable file
0
src/plugin_manager.py
Normal file → Executable file
0
src/plugin_manager.py
Normal file → Executable file
Reference in New Issue
Block a user