基于Libevent的HTTP Server_第1页
基于Libevent的HTTP Server_第2页
基于Libevent的HTTP Server_第3页
基于Libevent的HTTP Server_第4页
基于Libevent的HTTP Server_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、基于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 ws

2、aData; 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. Requ

3、ested: %sn", 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 = "" struct event_base * base = event_base_new(); str

4、uct 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

5、_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 &

6、lt;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:LibeventLibeventTestDebugsample.txt"char *filedata;time_t lasttime = 0;char filename80;int co

7、unter = 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",

8、filename); else fprintf(stderr,"Loading file: %s",filename); FILE *f = fopen(filename, "rb"); if (f = NULL) fprintf(stderr,"Couldn't open filen"); return; size = buf.st_size; filedata = (char *)malloc(size+1); memset(filedata,0,size+1); fread(filedata, sizeof(char),

9、 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_fi

10、le(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

11、 *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(WSAStar

12、tup(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 = "" if (argc > 1) strcpy(filename,argv1); printf("Using %sn",filename); else strcpy(filename,DEFAUL

13、T_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); p

14、rintf("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_

15、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 <net

16、inet/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 httpserv

17、er_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

18、.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) retu

19、rn -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 thsnthreads; 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(&t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论