Created
October 26, 2015 15:47
-
-
Save macmata/9ac4530ab273ef537cb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "server.h" | |
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { | |
switch (ev) { | |
case MG_EV_RECV: | |
// do something | |
mg_printf(nc, "alsothis_1"); | |
break; | |
case MG_EV_HTTP_REQUEST: | |
mg_printf(nc, "alsothis_2"); | |
mg_printf(nc,"this_also_2"); | |
nc->flags|= MG_F_SEND_AND_CLOSE; | |
break; | |
default: | |
//nc->flags |= MG_F_CLOSE_IMMEDIATELY; this cause problem | |
break; | |
} | |
} | |
void * server::run(void *mgMgr){ | |
std::cout << "server starting" << std::endl; | |
while(true/** TODO while no ctr c**/){ | |
mg_mgr_poll((mg_mgr*)mgMgr, 1000); | |
} | |
} | |
void server::start() { | |
for (int i = 0 ; i < 4 ; i++) | |
{ | |
struct mg_mgr *mgMgr = new mg_mgr; | |
vectorMgr.push_back(mgMgr); | |
mg_mgr_init(mgMgr, this); | |
struct mg_connection *nc = mg_bind(mgMgr, s_http_port, ev_handler); | |
assert(nc!= nullptr); | |
} | |
for(auto server: vectorMgr){ | |
mg_start_thread(run, server); | |
} | |
struct mg_mgr *mgMgr = new mg_mgr; | |
vectorMgr.push_back(mgMgr); | |
mg_mgr_init(mgMgr, this); | |
struct mg_connection *nc = mg_bind(mgMgr, s_http_port, ev_handler); | |
assert(nc!= nullptr); | |
run(mgMgr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment