使用libevent实现一个简单的http服务器,源码如下:
//简单http服务
#include <event.h>
#include <evhttp.h>
#include <event2/thread.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <assert.h>
#include <string>
#include <unistd.h>
#include <functional>
#include <memory>
///
//监听类
class simple_listener
{
public:
simple_listener(const std::string &addr, int port)
: m_address(addr)
, m_port(port)
, m_socket(-1)
{}
~simple_listener(void) { clear(); }
bool start(void)
{
assert(m_socket == -1);
do {
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
fprintf(stderr, "ERROR failed to set sigpipe signal\n");
break;
}
if ((m_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "ERROR failed to create socket\n");
break;
}
int one = 1;
if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(int)) < 0) {
fprintf(stderr, "ERROR failed to set reuseaddr\n");
break;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(m_address.c_str());
addr.sin_port = htons(m_port);
if (bind(m_socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
fprintf(stderr, "ERORR failed to bind %s:%d\n", m_address.c_str(), m_port);
break;
}
if (listen(m_socket, 512) < 0) {
fprintf(stderr, "ERROR failed to listen\n");
break;
}
int flags;
if ((flags = fcntl(m_socket, F_GETFL, 0)) < 0 || fcntl(m_socket, F_SETFL, flags | O_NONBLOCK) < 0) {
fprintf(stderr, "ERROR failed to set nonblock\n");
break;
}
return true;
} while (false);
clear();
return false;
}
void stop(void) { clear(); }
int get_socket(void) const { return m_socket; }
simple_listener(const simple_listener &) = delete;
simple_listener(simple_listener &&) = delete;
simple_listener &operator=(const simple_listener &) = delete;
simple_listener &operator=(simple_listener &&) = delete;
protected:
void clear(void)
{
if (m_socket != -1) {
close(m_socket);
m_socket = -1;
}
}
const std::string m_address; //监听地址
const int m_port; //监听端口
int m_socket; //socket 句柄
};
///
//处理类
class simple_processor
{
public:
simple_processor(int identify) : m_identify(identify) {}
void process(struct evhttp_request *ctx)
{
fprintf(stdout, "INFO process %d recv request!\n", m_identify);
evhttp_add_header(evhttp_request_get_output_headers(ctx), "Content-Type", "text/html");
evbuffer *buf = evbuffer_new();
std::string str = "Hello World !";
evbuffer_add(buf, str.c_str(), str.size());
evhttp_send_reply(ctx, 200, "OK", buf);
}
simple_processor(const simple_processor &) = delete;
simple_processor(simple_processor &&) = delete;
simple_processor operator=(const simple_processor &) = delete;
simple_processor operator=(simple_processor &&) = delete;
private:
const int m_identify; //唯一标识
};
///
//http服务类
//L 监听类,必须支持int get_socket(void)
//P 处理类,必须支持void process(struct evhttp_request *)
template <typename L, typename P>
class simple_httpserver
{
public:
static void http_callback(evhttp_request *ctx, void *args)
{
assert(args != nullptr);
auto pthis = (simple_httpserver *)args;
pthis->process(ctx);
}
public:
simple_httpserver(std::shared_ptr<L> &listener, std::shared_ptr<P> &processer)
: m_listener_ptr(listener)
, m_proccessor_ptr(processer)
, m_event_base_ptr(nullptr)
, m_http_ptr(nullptr)
{}
~simple_httpserver(void) { clear();}
bool start(void)
{
assert(m_event_base_ptr == nullptr && m_http_ptr == nullptr);
do {
m_event_base_ptr = event_base_new();
if (m_event_base_ptr == nullptr) {
fprintf(stderr, "ERROR failed to new event base \n");
break;
}
m_http_ptr = evhttp_new(m_event_base_ptr);
if (m_http_ptr == nullptr) {
fprintf(stderr, "ERROR failed to new event http\n");
break;
}
if (evhttp_accept_socket(m_http_ptr, m_listener_ptr->get_socket()) < 0) {
fprintf(stderr, "ERROR failed to accept socket");
break;
}
evhttp_set_gencb(m_http_ptr, &simple_httpserver::http_callback, this);
return true;
} while (false);
clear();
return false;
}
bool run(void)
{
assert(m_event_base_ptr != nullptr);
int ret_code = event_base_dispatch(m_event_base_ptr);
printf("INFO event dispatch exit , ret code : %d\n", ret_code);
return ret_code == 0;
}
bool stop(void)
{
if (m_event_base_ptr != nullptr)
event_base_loopbreak(m_event_base_ptr);
return true;
}
simple_httpserver(const simple_httpserver &) = delete;
simple_httpserver(simple_httpserver &&) = delete;
simple_httpserver &operator=(const simple_httpserver &) = delete;
simple_httpserver &operator=(simple_httpserver &&) = delete;
protected:
void clear(void)
{
if (m_http_ptr != nullptr) {
evhttp_free(m_http_ptr);
m_http_ptr = nullptr;
}
if (m_event_base_ptr != nullptr) {
event_base_free(m_event_base_ptr);
m_event_base_ptr = nullptr;
}
}
void process(evhttp_request *ctx)
{
m_proccessor_ptr->process(ctx);
}
std::shared_ptr<L> m_listener_ptr; //socket 句柄
std::shared_ptr<P> m_proccessor_ptr; //实例标识
event_base *m_event_base_ptr; //event_base指针
evhttp *m_http_ptr; //http指针
};
std::shared_ptr<simple_listener> g_listener_ptr; //监听对象
std::shared_ptr<simple_processor> g_processor_ptr; //请求处理对象
std::shared_ptr<simple_httpserver<simple_listener, simple_processor>> g_httpserver_ptr; //http服务对象
void sigterm(int sig)
{
fprintf(stdout, "INFO recv term signal !\n");
if (g_httpserver_ptr.get() != nullptr) {
fprintf(stdout, "INFO httpserver before stop !\n");
g_httpserver_ptr->stop();
fprintf(stdout, "INFO httpserver after stop !\n");
}
}
int main(int argc, char **argv)
{
signal(SIGTERM, sigterm);
g_listener_ptr = std::make_shared<simple_listener>("0.0.0.0", 8080);
if (!g_listener_ptr->start()) {
fprintf(stderr, "ERROR failed to open listener\n");
return -1;
}
g_processor_ptr = std::make_shared<simple_processor>(0);
g_httpserver_ptr = std::make_shared<simple_httpserver<simple_listener, simple_processor>>(g_listener_ptr, g_processor_ptr);
if (!g_httpserver_ptr->start()) {
fprintf(stderr, "ERROR failed to start http server\n");
return -1;
}
g_httpserver_ptr->run();
return 0;
}