Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1552426
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: LINUX

2013-10-09 11:10:45

 是一个高性能事件驱动网络库,是  的底层实现。经过我(Liigo)在实际项目中的深度应用,发现 libuv 在代码质量、运行效率、网络吞吐量、稳定性、跨平台等多方面都相当优秀,是一款不可多得的开源产品,可以说从质量到名气都不差。libuv 的缺点是易用性太差,文档严重不足,入手门槛较高。在这些方面它跟陈硕的 库差距很大,muduo的易用性太棒了,还有一本作者的专注《》 质量很高。muduo的性能应该很好,可惜仅它支持Linux系统,跨平台特性为零,而且C++编译出来的可执行文件尺寸较大,部署到嵌入式平台时有很大 的局限性。关于libuv和muduo的选择,我(Liigo)的观点是:如果条件允许使用muduo,我建议您优先考虑muduo;否则,libuv也 是很好的方案。

  好了,言归正传。因为项目需要在软件中内嵌一个极简单的Web Server,于是我用libuv写了这个tinyweb,以下是全部源代码:

  1. #include "tinyweb.h"
  2. #include <uv.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <memory.h>

  8. uv_tcp_t _server;
  9. uv_tcp_t _client;
  10. uv_loop_t* _loop;

  11. static void tinyweb_on_connection(uv_stream_t* server, int status);

  12. void tinyweb_start(uv_loop_t* loop, const char* ip, int port) {
  13.     _loop = loop;
  14.     uv_tcp_init(_loop, &_server);
  15.     uv_tcp_bind(&_server, uv_ip4_addr(ip&&ip[0]?ip:"0.0.0.0", port));
  16.     uv_listen((uv_stream_t*)&_server, 8, tinyweb_on_connection);
  17. }

  18. static void after_uv_close(uv_handle_t* handle) {
  19.     free(handle); //uv_tcp_t* client
  20. }

  21. static void after_uv_write(uv_write_t* w, int status) {
  22.     if(w->data)
  23.         free(w->data);
  24.     uv_close((uv_handle_t*)w->handle, after_uv_close); //close client
  25.     free(w);
  26. }

  27. static void write_uv_data(uv_stream_t* stream, const void* data, unsigned int len, int need_copy_data) {
  28.     if(data == NULL || len == 0) return;
  29.     if(len ==(unsigned int)-1)
  30.         len = strlen(data);

  31.     void* newdata = (void*)data;
  32.     if(need_copy_data) {
  33.         newdata = malloc(len);
  34.         memcpy(newdata, data, len);
  35.     }

  36.     uv_buf_t buf = uv_buf_init(newdata, len);
  37.     uv_write_t* w = (uv_write_t*)malloc(sizeof(uv_write_t));
  38.     w->data = need_copy_data ? newdata : NULL;
  39.     //free w and w->data in after_uv_write()
  40.     uv_write(w, stream, &buf, 1, after_uv_write);
  41. }

  42. static const char* http_respone = "HTTP/1.1 200 OK\r\n"
  43.     "Content-Type:text/html;charset=utf-8\r\n"
  44.     "Content-Length:11\r\n"
  45.     "\r\n"
  46.     "Hello world";

  47. static void tinyweb_on_connection(uv_stream_t* server, int status) {
  48.     assert(server == (uv_stream_t*)&_server);
  49.     if(status == 0) {
  50.         uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
  51.         uv_tcp_init(_loop, client);
  52.         uv_accept((uv_stream_t*)&_server, (uv_stream_t*)client);
  53.         write_uv_data((uv_stream_t*)client, http_respone, -1, 0);
  54.         //close client after uv_write, and free it in after_uv_close()
  55.     }
  56. }
启动代码:

  1. #include "tinyweb.h"
  2. int main() {
  3.     tinyweb_start(uv_default_loop(), "127.0.0.1", 8080);
  4.     uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  5. }

转载自:http://blog.csdn.net/liigo/article/details/9149415

阅读(3171) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~