1. 程式人生 > >基於Libevent的HTTP Server

基於Libevent的HTTP Server

簡單的Http Server

使用Libevent內建的http相關介面,可以很容易的構建一個Http Server,一個簡單的Http Server如下:

複製程式碼
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include <Winsock2.h>
#include <stdlib.h>
#include <stdio.h>

int init_win_socket()
{
    WSADATA wsaData;
    
if(WSAStartup(MAKEWORD(2,2) , &wsaData) != 0) { return -1; } return 0; } void generic_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf = evbuffer_new(); if(!buf) { puts("failed to create response buffer \n"); return; } evbuffer_add_printf(buf,
"Server Responsed. Requested: %s\n", evhttp_request_get_uri(req)); evhttp_send_reply(req, HTTP_OK, "OK", buf); evbuffer_free(buf); } int main(int argc, char* argv[]) { #ifdef WIN32 init_win_socket(); #endif short http_port = 8081; char *http_addr = "127.0.0.1";
struct event_base * base = event_base_new(); struct evhttp * http_server = evhttp_new(base); if(!http_server) { return -1; } int ret = evhttp_bind_socket(http_server,http_addr,http_port); if(ret!=0) { return -1; } evhttp_set_gencb(http_server, generic_handler, NULL); printf("http server start OK! \n"); event_base_dispatch(base); evhttp_free(http_server); WSACleanup(); return 0; }
複製程式碼

通過Libevent的介面構建一個Http Server的過程如下:

(1)初始化:在event_base上新建一個evhttp,將這個evhttp繫結到監聽的IP和埠號。

(2)設定Http回撥函式:使用evhttp_set_gencb設定Http Server的處理請求的回撥函式。

(3)啟動Http Server:等待請求進入事件迴圈。

在Http Server中使用定時器提供更新服務

複製程式碼
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include <sys/stat.h>
#include <Winsock2.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define DEFAULT_FILE "F:\\Libevent\\LibeventTest\\Debug\\sample.txt"

char *filedata;
time_t lasttime = 0;
char filename[80];
int counter = 0;

struct event *loadfile_event;
struct timeval tv;

void read_file()
{
    unsigned long size = 0;
    char *data;
    struct stat buf;

    if(stat(filename,&buf)<0)
    {
        printf("Read file error! \n");
        return;
    }

    if (buf.st_mtime > lasttime)
    {
        if (counter++)
            fprintf(stderr,"Reloading file: %s",filename);
        else
            fprintf(stderr,"Loading file: %s",filename);

        FILE *f = fopen(filename, "rb");
        if (f == NULL)
        {
            fprintf(stderr,"Couldn't open file\n");
            return;
        }

        size = buf.st_size;
        filedata = (char *)malloc(size+1);
        memset(filedata,0,size+1);
        fread(filedata, sizeof(char), size, f);
        fclose(f);

        fprintf(stderr," (%d bytes)\n",size);
        lasttime = buf.st_mtime;
    }
}

void read_file_timer_cb(evutil_socket_t listener, short event, void *arg)
{
    if (!evtimer_pending(loadfile_event, NULL)) 
    {
        event_del(loadfile_event);
        evtimer_add(loadfile_event, &tv);
    }

    read_file();
}

void load_file(struct event_base * base)
{
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    //loadfile_event = malloc(sizeof(struct event));
    loadfile_event = evtimer_new(base,read_file_timer_cb,NULL);

    //evtimer_set(loadfile_event,load_file,loadfile_event);
    evtimer_add(loadfile_event,&tv);
}

void generic_handler(struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf = evbuffer_new();
    if(!buf)
    {
        puts("failed to create response buffer \n");
        return;
    }
    evbuffer_add_printf(buf,"%s",filedata);
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}

int init_win_socket()
{
    WSADATA wsaData;
    if(WSAStartup(MAKEWORD(2,2) , &wsaData) != 0) 
    {
        return -1;
    }
    return 0;
}

int main(int argc, char* argv[])
{
#ifdef WIN32
    init_win_socket();
#endif

    short          http_port = 8081;
    char          *http_addr = "127.0.0.1";

    if (argc > 1)
    {
        strcpy(filename,argv[1]);
        printf("Using %s\n",filename);
    }
    else
    {
        strcpy(filename,DEFAULT_FILE);
    }

    struct event_base * base = event_base_new();

    struct evhttp * http_server = evhttp_new(base);
    if(!http_server)
    {
        return -1;
    }

    int ret = evhttp_bind_socket(http_server,http_addr,http_port);
    if(ret!=0)
    {
        return -1;
    }

    evhttp_set_gencb(http_server, generic_handler, NULL);

    read_file();

    load_file(base);

    printf("http server start OK! \n");

    event_base_dispatch(base);

    evhttp_free(http_server);

    WSACleanup();
    return 0;
}
複製程式碼

在這個Http Server中提供了一個每5秒觸發一次的定時器,讀取一個檔案,如果這個檔案被更新過,則讀取更新後的內容。

當訪問這個Http Server時,提供這個檔案中最新的內容。

多執行緒的Http Server

在上面的Http Server中,處理Http請求的回撥函式generic_handler和定時器讀取檔案的回撥函式read_file_timer_cb都在同一個event_base的dispatch中,並且都在同一個程序中,使用多執行緒可以改善程式的效能,下面是一個來自網路的多執行緒Http Server:

複製程式碼
#include <event.h>
#include <evhttp.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
 
int httpserver_bindsocket(int port, int backlog);
int httpserver_start(int port, int nthreads, int backlog);
void* httpserver_Dispatch(void *arg);
void httpserver_GenericHandler(struct evhttp_request *req, void *arg);
void httpserver_ProcessRequest(struct evhttp_request *req);
 
int httpserver_bindsocket(int port, int backlog) {
  int r;
  int nfd;
  nfd = socket(AF_INET, SOCK_STREAM, 0);
  if (nfd < 0) return -1;
 
  int one = 1;
  r = setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(int));
 
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(port);
 
  r = bind(nfd, (struct sockaddr*)&addr, sizeof(addr));
  if (r < 0) return -1;
  r = listen(nfd, backlog);
  if (r < 0) return -1;
 
  int flags;
  if ((flags = fcntl(nfd, F_GETFL, 0)) < 0
      || fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
    return -1;
 
  return nfd;
}
 
int httpserver_start(int port, int nthreads, int backlog) {
  int r, i;
  int nfd = httpserver_bindsocket(port, backlog);
  if (nfd < 0) return -1;
  pthread_t ths[nthreads];
  for (i = 0; i < nthreads; i++) {
    struct event_base *base = event_init();
    if (base == NULL) return -1;
    struct evhttp *httpd = evhttp_new(base);
    if (httpd == NULL) return -1;
    r = evhttp_accept_socket(httpd, nfd);
    if (r != 0) return -1;
    evhttp_set_gencb(httpd, httpserver_GenericHandler, NULL);
    r = pthread_create(&ths[i], NULL, httpserver_Dispatch, base);
    if (r != 0) return -1;
  }
  for (i = 0; i < nthreads; i++) {
    pthread_join(ths[i], NULL);
  }
}
 
void* httpserver_Dispatch(void *arg) {
  event_base_dispatch((struct event_base*)arg);
  return NULL;
}
 
void httpserver_GenericHandler(struct evhttp_request *req, void *arg) {
      httpserver_ProcessRequest(req);
}
 
void httpserver_ProcessRequest(struct evhttp_request *req) {
    struct evbuffer *buf = evbuffer_new();
    if (buf == NULL) return;
    
    //here comes the magic
}
 
int main(void) {
    httpserver_start(80, 10, 10240);
}
複製程式碼

上面的程式碼基於Libevent 1.X版本的,不過很容易很看懂:在一個監聽socket上建立了多個event_base例項和evhttp例項,在不同的執行緒中排程不同的event_base,繼而可以在不同的執行緒中處理http請求。

這裡還有一個基於Libevent的多執行緒Http Server:https://sourceforge.net/projects/libevent-thread/,看原始碼處理的過程和上面類似,只是每次在監聽的socket上accept一個連線請求時,將對應的處理放到一個工作佇列裡,在佇列裡由多執行緒處理相應的回撥函式。