弃用mongoose,倒霉玩意
This commit is contained in:
9
.vscode/settings.json
vendored
Normal file
9
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"cmake.sourceDirectory": "/home/jianf/program/chat_rebot-connect-with-onebot-standard-/c",
|
||||||
|
"cpp-check-lint.--enable": true,
|
||||||
|
"cpp-check-lint.cppcheck.--executable": "cppcheck",
|
||||||
|
"cpp-check-lint.cppcheck.--language=": "c",
|
||||||
|
"cpp-check-lint.cppcheck.--inline-suppr": true,
|
||||||
|
"cpp-check-lint.cppcheck.--onsave": true,
|
||||||
|
"cpp-check-lint.cppcheck.--quick_fix": true
|
||||||
|
}
|
||||||
@ -11,7 +11,7 @@ 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 network/mongoose/mongoose.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(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,82 +2,18 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "network/mongoose/mongoose.h"
|
#include <sys/socket.h>
|
||||||
|
#include <bits/fcntl-linux.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "tools/log/log.h"
|
#include "tools/log/log.h"
|
||||||
#include "http_rel.h"
|
#include "http_rel.h"
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
/* 接收状态辅助结构 */
|
/* 接收状态辅助结构 */
|
||||||
struct recv_state {
|
|
||||||
char *request; // 完整请求字符串
|
|
||||||
int done; // 接收完成标志
|
|
||||||
int error; // 错误标志
|
|
||||||
};
|
|
||||||
|
|
||||||
|
const char *http_get_body(const char *buf);
|
||||||
static void http_recv_handler(struct mg_connection *c, int ev, void *ev_data)
|
char *recv_http_request(int cfd);
|
||||||
{
|
|
||||||
// 从连接对象获取用户数据
|
|
||||||
struct recv_state *state = (struct recv_state *)c->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); // 安全释放(free(NULL) 是安全的)
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 确保返回的请求不为空 */
|
|
||||||
if (!state.request) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return state.request;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* http_get_body 无需修改,保持原样 */
|
/* http_get_body 无需修改,保持原样 */
|
||||||
const char *http_get_body(const char *buf)
|
const char *http_get_body(const char *buf)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,7 @@
|
|||||||
#ifndef NETWORK
|
#ifndef NETWORK
|
||||||
#define NETWORK
|
#define NETWORK
|
||||||
|
|
||||||
#define MAX_POOL 10
|
|
||||||
#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 +22,7 @@ typedef struct net_args
|
|||||||
|
|
||||||
typedef struct network_manager//网络管理器
|
typedef struct network_manager//网络管理器
|
||||||
{
|
{
|
||||||
pth_m pool[MAX_POOL];
|
pth_m pool[NET_MAX_POOL];
|
||||||
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*);
|
||||||
@ -40,7 +39,7 @@ typedef struct network_manager//网络管理器
|
|||||||
|
|
||||||
typedef struct rebot_message
|
typedef struct rebot_message
|
||||||
{
|
{
|
||||||
char raw_message[MAX_MESSAGE_BUF];
|
char raw_message[NET_MAX_MESSAGE_BUF];
|
||||||
char nickname[64];
|
char nickname[64];
|
||||||
char gid[32];
|
char gid[32];
|
||||||
char uid[32];
|
char uid[32];
|
||||||
|
|||||||
Reference in New Issue
Block a user