acl/lib_fiber/samples/httpd/main.cpp

175 lines
3.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
2018-11-30 14:38:22 +08:00
#include <stdlib.h>
#include <stdio.h>
#include "http_servlet.h"
static int STACK_SIZE = 128000;
2018-11-30 14:38:22 +08:00
static int __rw_timeout = 0;
static int __schedule_event = FIBER_EVENT_KERNEL;
static void http_server(ACL_FIBER *, void *ctx)
{
acl::socket_stream *conn = (acl::socket_stream *) ctx;
2022-03-26 14:18:31 +08:00
acl::memcache_session *session = new acl::memcache_session("127.0.0.1:11211");
http_servlet *servlet = new http_servlet(conn, session);
servlet->setLocalCharset("gb2312");
2018-11-30 14:38:22 +08:00
2022-03-26 14:18:31 +08:00
//printf("start one http_server\r\n");
2018-11-30 14:38:22 +08:00
2022-03-26 14:18:31 +08:00
while (true) {
if (servlet->doRun() == false) {
2018-11-30 14:38:22 +08:00
break;
2022-03-26 14:18:31 +08:00
}
2018-11-30 14:38:22 +08:00
}
printf("close one connection: %d, %s\r\n", conn->sock_handle(),
acl::last_serror());
2022-03-26 14:18:31 +08:00
delete session;
delete servlet;
2018-11-30 14:38:22 +08:00
delete conn;
}
static void fiber_accept(ACL_FIBER *, void *ctx)
{
acl::server_socket* server = (acl::server_socket *) ctx;
2022-03-26 14:18:31 +08:00
while (true) {
2018-11-30 14:38:22 +08:00
acl::socket_stream* client = server->accept();
2022-03-26 14:18:31 +08:00
if (client == NULL) {
2018-11-30 14:38:22 +08:00
printf("accept failed: %s\r\n", acl::last_serror());
break;
}
client->set_rw_timeout(__rw_timeout);
printf("accept one: %d\r\n", client->sock_handle());
acl_fiber_create(http_server, client, STACK_SIZE);
}
exit (1);
}
class thread_server : public acl::thread
{
public:
thread_server(acl::server_socket& server)
: server_inner_(NULL)
, server_(&server)
{
}
thread_server(const char* addr)
{
2022-04-04 12:24:16 +08:00
server_inner_ = new acl::server_socket(acl::OPEN_FLAG_REUSEPORT, 128);
2022-03-26 14:18:31 +08:00
if (server_inner_->open(addr) == false) {
2022-04-04 12:24:16 +08:00
printf("%s(%d): open %s error %s\r\n", __FUNCTION__,
__LINE__, addr, acl::last_serror());
2018-11-30 14:38:22 +08:00
exit (1);
}
server_ = server_inner_;
}
~thread_server(void) { delete server_inner_; }
private:
acl::server_socket* server_inner_;
acl::server_socket* server_;
void* run(void)
{
acl_fiber_create(fiber_accept, server_, STACK_SIZE);
acl_fiber_schedule_with(__schedule_event);
return NULL;
}
};
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -s listen_addr\r\n"
" -e event\r\n"
" -R reuse_port\r\n"
" -t threads\r\n"
" -z stack_size[default: 128000]\r\n"
2018-11-30 14:38:22 +08:00
" -r rw_timeout\r\n", procname);
}
int main(int argc, char *argv[])
{
acl::string addr("127.0.0.1:9001");
int ch, nthreads = 2;
bool reuse_port = false;
while ((ch = getopt(argc, argv, "hs:r:t:Re:z:")) > 0) {
2022-03-26 14:18:31 +08:00
switch (ch) {
2018-11-30 14:38:22 +08:00
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'r':
__rw_timeout = atoi(optarg);
break;
case 't':
nthreads = atoi(optarg);
break;
case 'R':
reuse_port = true;
break;
case 'e':
2022-03-26 14:18:31 +08:00
if (strcasecmp(optarg, "kernel") == 0) {
2018-11-30 14:38:22 +08:00
__schedule_event = FIBER_EVENT_KERNEL;
2022-03-26 14:18:31 +08:00
} else if (strcasecmp(optarg, "poll") == 0) {
2018-11-30 14:38:22 +08:00
__schedule_event = FIBER_EVENT_POLL;
2022-03-26 14:18:31 +08:00
} else if (strcasecmp(optarg, "select") == 0) {
__schedule_event = FIBER_EVENT_SELECT;
}
2022-03-31 21:44:43 +08:00
break;
case 'z':
STACK_SIZE = atoi(optarg);
break;
2018-11-30 14:38:22 +08:00
default:
break;
}
}
acl::acl_cpp_init();
acl::log::stdout_open(true);
acl::server_socket server;
2022-03-26 14:18:31 +08:00
if (!reuse_port) {
if (server.open(addr) == false) {
2022-04-04 12:24:16 +08:00
printf("%s(%d): open %s error %s\r\n", __FUNCTION__,
__LINE__, addr.c_str(), acl::last_serror());
2018-11-30 14:38:22 +08:00
exit (1);
2022-03-26 14:18:31 +08:00
} else {
2018-11-30 14:38:22 +08:00
printf("open %s ok\r\n", addr.c_str());
2022-03-26 14:18:31 +08:00
}
2018-11-30 14:38:22 +08:00
}
std::vector<acl::thread*> threads;
2022-03-26 14:18:31 +08:00
for (int i = 0; i < nthreads; i++) {
2018-11-30 14:38:22 +08:00
acl::thread* thr;
2022-03-26 14:18:31 +08:00
if (reuse_port) {
2018-11-30 14:38:22 +08:00
thr = new thread_server(addr);
2022-03-26 14:18:31 +08:00
} else {
2018-11-30 14:38:22 +08:00
thr = new thread_server(server);
2022-03-26 14:18:31 +08:00
}
2018-11-30 14:38:22 +08:00
threads.push_back(thr);
thr->set_detachable(false);
thr->start();
}
for (std::vector<acl::thread*>::iterator it = threads.begin();
2022-03-26 14:18:31 +08:00
it != threads.end(); ++it) {
2018-11-30 14:38:22 +08:00
(*it)->wait();
delete *it;
}
return 0;
}