<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[转]C实现的comet http server,基于libevent ]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 06 Aug 2009 09:53:36 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	基于libevent 1.3e, 利用了libevent里面现成的http库 <br/>编译方法，先安装libevent,然后<br/>&nbsp;&nbsp;&nbsp;&nbsp;cc -o httpd httpd.c -L/usr/local/lib/ -Wall -O2 -I/usr/local/include -ggdb -levent <br/>可以作为一个商业运行环境的基础，真正的业务系统里面，comet server也很简单，比这个程序复杂不了什么，它只处理comet，只是一个通道，复杂的东西放到其他地方。 <br/>打算作为下图中的一个模块<br/><a href="http://www.jackxiang.com/attachment.php?fid=16" target="_blank"><img src="http://www.jackxiang.com/attachment.php?fid=16" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>Source code:<br/><div class="code"><br/>#include &lt;sys/types.h&gt;<br/>#include &lt;sys/time.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#include &lt;err.h&gt;<br/><br/>#include &lt;sys/queue.h&gt;<br/>#include &lt;evhttp.h&gt;<br/>#define MAX_CONN 10240<br/><br/>struct evhttp_request *requests&#91;MAX_CONN&#93;;<br/>struct evbuffer *bufs&#91;MAX_CONN&#93;;<br/>int pos&#91;MAX_CONN&#93;, num;<br/><br/>void on_close(struct evhttp_request *req, void *arg) &#123;<br/> int* p = (int*) arg;<br/> struct evhttp_request *p2 = requests&#91;*p&#93;;<br/> struct evbuffer *p3 = bufs&#91;*p&#93;;<br/> printf(&quot;%s closed.&#92;n&quot;, p2-&gt;remote_host);<br/> requests&#91;*p&#93; = NULL;<br/> if (p3 != NULL) evbuffer_free(p3);<br/>&#125;<br/><br/>/* for comet recv */<br/>void comet_handler(struct evhttp_request *req, void *arg) &#123;<br/> int i, j;<br/> requests&#91;num&#93; = req;<br/> struct evbuffer *buf; <br/>&nbsp;&nbsp;<br/> req-&gt;minor = 0; <br/> evhttp_add_header(req-&gt;output_headers, &quot;Content-Type&quot;, &quot;text/plain&quot;);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;evhttp_send_reply_start(req, HTTP_OK, &quot;OK&quot;);<br/> <br/> /* Set a callback for connection close. */<br/> evhttp_connection_set_closecb(req-&gt;evcon, on_close, &amp;pos&#91;num&#93;);<br/><br/> buf = evbuffer_new();<br/> bufs&#91;num&#93; = buf;<br/><br/> for (j = 0; j &lt; 10; j++)<br/>&nbsp;&nbsp;evbuffer_add_printf(buf, &quot;Hi %s, this is a comet server based on libevent, visit http://hi.baidu.com/jabber for more information!&#92;r&#92;n&quot;, req-&gt;remote_host);<br/>&nbsp;&nbsp;&nbsp;&nbsp;evhttp_send_reply_chunk(req, buf);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i &lt; num; i++) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (requests&#91;i&#93; == NULL) continue;<br/>&nbsp;&nbsp;&nbsp;&nbsp; evbuffer_add_printf(bufs&#91;i&#93;, &quot;Oops, %s requests!&#92;r&#92;n&quot;, req-&gt;remote_host);<br/>&nbsp;&nbsp;&nbsp;&nbsp; evhttp_send_reply_chunk(requests&#91;i&#93;, bufs&#91;i&#93;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; // evhttp_send_reply_end(requests&#91;i&#93;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (num++ == MAX_CONN) err(1, &quot;exceed max conn, TODO.&quot;);<br/>&#125;<br/><br/>/* for comet send, GET /id */<br/>void notify_handler(struct evhttp_request *req, void *arg) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;struct evbuffer *buf;<br/>&nbsp;&nbsp;&nbsp;&nbsp;int nudge;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;buf = evbuffer_new();<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (buf == NULL) err(1, &quot;failed to create response buffer&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;const char *uri = evhttp_request_uri(req);<br/>&nbsp;&nbsp;&nbsp;&nbsp;evbuffer_add_printf(buf, &quot;Requested: %s, send a nudge to %s&#92;n&quot;, uri, uri + 1);<br/><br/> nudge = atoi(uri + 1);<br/> if (requests&#91;nudge&#93; != NULL) &#123;<br/>&nbsp;&nbsp;evbuffer_add_printf(bufs&#91;nudge&#93;, &quot;Oops, received a nudge from %s!&#92;r&#92;n&quot;, req-&gt;remote_host);<br/>&nbsp;&nbsp;evhttp_send_reply_chunk(requests&#91;nudge&#93;, bufs&#91;nudge&#93;);<br/> &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;evhttp_send_reply(req, HTTP_OK, &quot;OK&quot;, buf); // TODO: free buf<br/>&#125;<br/><br/>int main(int argc, char **argv) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct evhttp *httpd;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i &lt; 1024; i++) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requests&#91;i&#93; == NULL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bufs&#91;i&#93; = NULL;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos&#91;i&#93; = i;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = 0;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event_init();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httpd = evhttp_start(&quot;0.0.0.0&quot;, 8080);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Set a comet callback for requests to &quot;/&quot;. */<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evhttp_set_cb(httpd, &quot;/&quot;, comet_handler, NULL);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Set a send callback for all other requests. */ <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evhttp_set_gencb(httpd, notify_handler, NULL);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event_dispatch();<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Not reached in this code as it is now. */<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evhttp_free(httpd);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br/>&#125;<br/><br/></div><br/>http://www.chinaunix.net/jh/27/1088810.html<br/>http://hi.baidu.com/jabber/blog/item/84aebc8f060f00eaf01f36b4.html<br/>http://hi.baidu.com/jabber/blog/item/f556c7fcbaa76286b801a042.html<br/><br/>参考：<br/>http://blog.gslin.org/archives/2005/11/24/220/ libevent编程介绍，里面的留言较搞笑<br/>http://unx.ca/log/tag/libevent/ 比较旧了，基于libevent 1.2 的例子<br/>http://www.slideshare.net/simon/time-for-comet/ 什么是 comet，这里有原理说明 <br/>类别：Web Im 查看评论 <br/><br/>最简单的WEB服务器：<br/>g++ -levent&nbsp;&nbsp;<br/><br/><div class="code"><br/>#include&lt;sys/types.h&gt;&nbsp;&nbsp; <br/>#include&lt;sys/time.h&gt;&nbsp;&nbsp; <br/>#include&lt;sys/queue.h&gt;&nbsp;&nbsp; <br/>#include&lt;stdlib.h&gt;&nbsp;&nbsp; <br/>#include&lt;err.h&gt;&nbsp;&nbsp; <br/>#include&lt;event.h&gt;&nbsp;&nbsp; <br/>#include&lt;evhttp.h&gt;&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>void generic_handler(struct evhttp_request *req, void *arg)&nbsp;&nbsp; <br/>&#123;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct evbuffer *buf;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf = evbuffer_new();&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (buf == NULL)&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err( &quot;failed to create response buffer&quot;);&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evbuffer_add_printf(buf, &quot;Fuck you!&quot;, evhttp_request_uri(req));&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evhttp_send_reply(req, HTTP_OK, &quot;OK&quot;, buf);&nbsp;&nbsp; <br/>&#125;&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>int main(int argc, char **argv)&nbsp;&nbsp; <br/>&#123;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;struct evhttp *httpd;&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;event_init();&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;httpd = evhttp_start(&quot;, );&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;/* Set a callback for requests to &quot;/specific&quot;. */&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;/* evhttp_set_cb(httpd, &quot;/specific&quot;, another_handler, NULL); */&nbsp;&nbsp;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;/* Set a callback for all other requests. */&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;evhttp_set_gencb(httpd, generic_handler, NULL);&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;event_dispatch();&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;/* Not reached in this code as it is now. */&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;evhttp_free(httpd);&nbsp;&nbsp; <br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&nbsp;&nbsp;&nbsp;<br/>&#125;&nbsp;&nbsp; </div>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [转]C实现的comet http server,基于libevent ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>