百度空间 | 百度首页 
               
 
查看文章
 
自己用C实现的comet http server,基于libevent
2008-02-21 15:02
  • 基于libevent 1.3e, 利用了libevent里面现成的http库
  • 编译方法,先安装libevent,然后
        cc -o httpd httpd.c -L/usr/local/lib/ -Wall -O2 -I/usr/local/include -ggdb -levent
  • 可以作为一个商业运行环境的基础,真正的业务系统里面,comet server也很简单,比这个程序复杂不了什么,它只处理comet,只是一个通道,复杂的东西放到其他地方。
  • 打算作为下图中的一个模块

Source code:
#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <err.h>

#include <sys/queue.h>
#include <evhttp.h>
#define MAX_CONN 10240

struct evhttp_request *requests[MAX_CONN];
struct evbuffer *bufs[MAX_CONN];
int pos[MAX_CONN], num;

void on_close(struct evhttp_request *req, void *arg) {
int* p = (int*) arg;
struct evhttp_request *p2 = requests[*p];
struct evbuffer *p3 = bufs[*p];
printf("%s closed.\n", p2->remote_host);
requests[*p] = NULL;
if (p3 != NULL) evbuffer_free(p3);
}

/* for comet recv */
void comet_handler(struct evhttp_request *req, void *arg) {
int i, j;
requests[num] = req;
struct evbuffer *buf;

req->minor = 0;
evhttp_add_header(req->output_headers, "Content-Type", "text/plain");

evhttp_send_reply_start(req, HTTP_OK, "OK");

/* Set a callback for connection close. */
evhttp_connection_set_closecb(req->evcon, on_close, &pos[num]);

buf = evbuffer_new();
bufs[num] = buf;

for (j = 0; j < 10; j++)
evbuffer_add_printf(buf, "Hi %s, this is a comet server based on libevent, visit http://hi.baidu.com/jabber for more information!\r\n", req->remote_host);
evhttp_send_reply_chunk(req, buf);

for (i = 0; i < num; i++) {
if (requests[i] == NULL) continue;
evbuffer_add_printf(bufs[i], "Oops, %s requests!\r\n", req->remote_host);
evhttp_send_reply_chunk(requests[i], bufs[i]);
// evhttp_send_reply_end(requests[i]);
}

if (num++ == MAX_CONN) err(1, "exceed max conn, TODO.");
}

/* for comet send, GET /id */
void notify_handler(struct evhttp_request *req, void *arg) {
struct evbuffer *buf;
int nudge;

buf = evbuffer_new();
if (buf == NULL) err(1, "failed to create response buffer");
const char *uri = evhttp_request_uri(req);
evbuffer_add_printf(buf, "Requested: %s, send a nudge to %s\n", uri, uri + 1);

nudge = atoi(uri + 1);
if (requests[nudge] != NULL) {
evbuffer_add_printf(bufs[nudge], "Oops, received a nudge from %s!\r\n", req->remote_host);
evhttp_send_reply_chunk(requests[nudge], bufs[nudge]);
}

evhttp_send_reply(req, HTTP_OK, "OK", buf); // TODO: free buf
}

int main(int argc, char **argv) {
struct evhttp *httpd;
int i;
for (i = 0; i < 1024; i++) {
requests[i] == NULL;
bufs[i] = NULL;
pos[i] = i;
}
num = 0;

event_init();
httpd = evhttp_start("0.0.0.0", 8080);

/* Set a comet callback for requests to "/". */
evhttp_set_cb(httpd, "/", comet_handler, NULL);

/* Set a send callback for all other requests. */
evhttp_set_gencb(httpd, notify_handler, NULL);

event_dispatch();

/* Not reached in this code as it is now. */
evhttp_free(httpd);
return 0;
}

参考:
http://blog.gslin.org/archives/2005/11/24/220/ libevent编程介绍,里面的留言较搞笑
http://unx.ca/log/tag/libevent/ 比较旧了,基于libevent 1.2 的例子
http://www.slideshare.net/simon/time-for-comet/ 什么是 comet,这里有原理说明

类别:Web Im | 添加到搜藏 | 浏览() | 评论 (7)
 
最近读者:
 
网友评论:
1
2008-04-14 16:33 | 回复
文章这张插图请问是用什么工具画的?
 
3
2008-04-15 10:37 | 回复
插图用visio
 
4
2008-04-15 19:35 | 回复
嗯,画的挺漂亮的 :)
 
5
2008-07-21 17:09 | 回复
这个c实现的server是服务端透明的么?还是需要象jetty那样有特殊的api的?有测试例子么?
 
6
2008-07-24 00:06 | 回复
这个程序从功能上来说是全的,业务上的话靠自己去扩展,没测试例子。
 
7
2008-09-10 17:31 | 回复
我修改了一下你的例子,然后通过ajax长轮训来实现来完成comet, 有个问题是,当我用两台客户端访问comet服务器后, 当服务器数据发生变化后,会返回数据给客户端,但是只有其中一个客户端会产生变化,而另外一个客户端不会获得respond,当服务器又产生变化后,没变的客户端更新了.已经变过一次的客户端这次没变,还在继续请求... 不知道libevent里面的回调函数是每个请求各自执行自己的还是,每个请求公用同一个.或者说是我的应用逻辑扩展有问题.
 
8
2008-09-11 10:50 | 回复
我发现如果有多个http请求服务器的时候,comet服务器会让每个请求排队,也就是当第一个没返回200的时候,第二个请求根本没有执行到回调函数那里. 不知道如何解决.
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu