gpt4 book ai didi

c++ - C++中的 Mongoose 服务器

转载 作者:太空宇宙 更新时间:2023-11-04 04:30:11 27 4
gpt4 key购买 nike

我正在使用 mongoose 库在 C++ OOP 中开发一个 API 服务器,我不断收到错误,有人可以看看我的代码并给我一些指导。embServer.h

   class embServer
{

private:
constexpr static const char *s_http_port = "8000";
constexpr static struct mg_serve_http_opts s_http_server_opts = { .document_root = "web_root" }; // C99;
static int s_sig_num;
constexpr static const struct mg_str s_get_method = MG_MK_STR("GET");
constexpr static const struct mg_str s_put_method = MG_MK_STR("PUT");
constexpr static const struct mg_str s_delele_method = MG_MK_STR("DELETE");


public:
embServer();
~embServer();

Database dbConn;
//int embServer::s_sig_num = 0;


static void signal_handler(int sig_num)
{
signal(sig_num, signal_handler);
s_sig_num = sig_num;
}

static int has_prefix(const struct mg_str *uri, const struct mg_str *prefix)
{
return uri->len > prefix->len && memcmp(uri->p, prefix->p, prefix->len) == 0;
}

static int is_equal(const struct mg_str *s1, const struct mg_str *s2)
{
return s1->len == s2->len && memcmp(s1->p, s2->p, s2->len) == 0;
}

static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
{
static const struct mg_str api_prefix = MG_MK_STR("/api/v1");
struct http_message *hm = (struct http_message *) ev_data;
struct mg_str key;

switch (ev)
{
case MG_EV_HTTP_REQUEST:

if (has_prefix(&hm->uri, &api_prefix))
{
key.p = hm->uri.p + api_prefix.len;

key.len = hm->uri.len - api_prefix.len;

if (embServer::is_equal(&hm->method, &s_get_method))
{
//dbConn.db_op(nc, hm, &key, 1);
}
else if (embServer::is_equal(&hm->method, &s_put_method))
{
//dbConn.db_op(nc, hm, &key, 2);
}
else if (embServer::is_equal(&hm->method, &s_delele_method))
{
//dbConn.db_op(nc, hm, &key, 3);
}
else
{
mg_printf(nc, "%s","HTTP/1.0 501 Not Implemented\r\n" "Content-Length: 0\r\n\r\n");
}
}
else
{
mg_serve_http(nc, hm, &s_http_server_opts); /* Serve static content */
}
break;
default:
break;
}
}

void Run()
{

struct mg_mgr mgr;
struct mg_connection *nc;
//int i;

/* Open listening socket */
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, embServer::ev_handler);
mg_set_protocol_http_websocket(nc);


/* For each new connection, execute ev_handler in a separate thread */
mg_enable_multithreading(nc);



/**
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-D") == 0)
{
mgr.hexdump_file = argv[++i];
}
else if (strcmp(argv[i], "-f") == 0)
{
s_db_path = argv[++i];
}
else if (strcmp(argv[i], "-r") == 0)
{
s_http_server_opts.document_root = argv[++i];
}
}
Parse command line arguments */

signal(SIGINT, &embServer::signal_handler);
signal(SIGTERM, &embServer::signal_handler);

/* Run event loop until signal is received */
printf("Starting RESTful server on port %s\n", s_http_port);
while (s_sig_num == 0)
{
mg_mgr_poll(&mgr, 1000);
}

/* Cleanup */
mg_mgr_free(&mgr);
printf("Exiting on signal %d\n", s_sig_num);


}
};
int embServer::s_sig_num = 0;

显示错误

    `F:\PROJECTS\CP\apiCrud\embServer.h|48|warning: variable 'key' set but not used [-Wunused-but-set-variable]|
obj\Debug\main.o||In function `ZN9embServer10ev_handlerEP13mg_connectioniPv':|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
obj\Debug\main.o:F:\PROJECTS\CP\apiCrud\embServer.h|79|more undefined references to `embServer::s_http_server_opts' follow|
obj\Debug\main.o||In function `main':|
F:\PROJECTS\CP\apiCrud\main.cpp|15|undefined reference to `embServer::embServer()'|
F:\PROJECTS\CP\apiCrud\main.cpp|15|undefined reference to `embServer::~embServer()'|
F:\PROJECTS\CP\apiCrud\main.cpp|15|undefined reference to `embServer::~embServer()'|
||error: ld returned 1 exit status|
||=== Build failed: 10 error(s),(0 minute(s), 22 second(s)) ===|`

int main(int argc, char *argv[])
{
embServer emb;
emb.Run();

return 0;
}

最佳答案

编译器直接告诉您需要进行这些更改:

  1. constexpr 添加到 static struct mg_serve_http_opts s_http_server_opts
  2. mg_serve_http(nc, hm, &s_http_server_opts) 中删除 &

关于c++ - C++中的 Mongoose 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com