#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mongoose.h"
#define MAX_THREADS 2
static struct mg_server *server[MAX_THREADS];
static bool stop_webserver = false;
static const char* port = "8080";
static const char* local_fullpath = "/Users/alex/";
static int request_handler(struct mg_connection *conn)
{
if(strcmp(conn->uri, "/post") == 0)
{
#define BUF_LEN (8 * 1024 * 1024)
/*unsigned char *data = new unsigned char[BUF_LEN];
memset(data, 0, BUF_LEN);
mg_get_var(conn, get_post_adv_image_key().c_str(), (char*)data, BUF_LEN);
std::string filename;
memory_buffer_ptr bp_image;
parse_adv_image_post_data(std::string((char*)data), filename, bp_image);
std::string cache_path = parameters::get_instance()->default_cache_path();
write_binary_file(cache_path + filename, bp_image);
mg_send_data(conn, post_adv_image_ok, (int)strlen(post_adv_image_ok));
delete[] data;*/
sleep(10 );
return MG_REQUEST_NOT_PROCESSED;
}
else
{
return MG_REQUEST_NOT_PROCESSED;
}
}
void* process_proc(void* p_server)
{
while(!stop_webserver)
{
mg_poll_server((struct mg_server*)p_server, 500);
}
return NULL;
}
bool start_simple_webserver(void) {
stop_webserver = false;
for(int i = 0; i < MAX_THREADS; i++)
{
server[i] = mg_create_server(NULL);
if(i == 0)
{
mg_set_option(server[i], "listening_port", port);
}
else
{
mg_set_listening_socket(server[i], mg_get_listening_socket(server[0]));
}
mg_set_option(server[i], "document_root", local_fullpath);
mg_set_request_handler(server[i], request_handler);
mg_start_thread(process_proc, server[i]);
}
return true;
}
void stop_simple_webserver()
{
stop_webserver = true;
for(int i = 0; i < MAX_THREADS; i++)
{
mg_destroy_server(&server[i]);
}
}
5.3版本mongoose的多线程实例源代码
最新推荐文章于 2023-08-14 17:35:48 发布