{"rsdb":{"rid":"2621","subhead":"","postdate":"0","aid":"1091","fid":"56","uid":"1","topic":"1","content":"

\u4eca\u5929\u5728\u7814\u7a76memcached\u8f6f\u4ef6\u65f6\uff0c\u63a5\u89e6\u5230\u4e86libevent \uff0d \u4e8b\u4ef6\u901a\u77e5\u63a5\u53e3\u51fd\u6570\u5e93\uff0c\u53d1\u73b0\u4e86\u5b83\u5bf9\u9ad8\u6027\u80fd\uff0c\u9ad8\u5e76\u53d1\uff0c\u53ef\u79fb\u690d\u7b49\u8bf8\u591a\u65b9\u9762\u63d0\u4f9b\u4e86\u76f8\u5f53\u591a\u7684\u4fbf\u5229\u3002\u5fc3\u91cc\u751a\u662f\u6b22\u559c\uff0c\u56e0\u6b64\uff0c\u8fd9\u91cc\u5c31\u60f3\u628a\u8fd9\u4e2a\u597d\u5bb6\u4f19\u4ecb\u7ecd\u7ed9\u5927\u5bb6\uff0c\u5171\u540c\u5206\u4eab\u5206\u4eab\u3002<\/p>

    \u8bf4\u5728\u524d\u8fb9\uff0c\u6055\u5c0f\u5f1f\u65e0\u77e5\uff0c\u5176\u5b9elibevent\u8f6f\u4ef6\u5305\u65e9\u57282000\u5e7411\u670814\u53f7\u5c31\u53d1\u5e03\u4e860.1\u7248\u672c\uff0c\u5df2\u7ecf\u53d1\u5c55\u4e8611\u4e2a\u5e74\u5934\u4e86\u3002\u5982\u4eca\u624d\u88ab\u7a9d\u53d1\u73b0\uff0c\u771f\u662f\u7f9e\u6127\u96be\u5f53\u554a\uff1a-< \u4e0d\u8fc7\u8fd8\u597d\uff0c\u53d1\u73b0\u4e86\u603b\u5f52\u662f\u597d\u4e8b\u3002\u90a3\u63a5\u4e0b\u6765\uff0c\u5c31\u8be5\u6293\u7d27\u65f6\u95f4\u8865\u4e60\u529f\u8bfe\u4e86\u3002<\/p>

    1. \u4ecb\u7ecd<\/p>

    libevent\u662f\u4e00\u4e2a\u7528\u6765\u5f00\u53d1\u53ef\u6269\u5c55\u7684\u7f51\u7edc\u670d\u52a1\u5668<\/a>\u7684\u4e8b\u4ef6\u901a\u77e5\u51fd\u6570\u5e93\u3002\u5f53\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u6216\u662f\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u5230\u8fbe\u540e\uff0clibevent API\u63d0\u4f9b\u4e00\u79cd\u6267\u884c\u56de\u8c03\u51fd\u6570\u7684\u673a\u5236\u3002\u800c\u4e14\uff0clibevent\u8fd8\u652f\u6301\u57fa\u4e8e\u4fe1\u53f7\u6216\u5b9a\u671f\u8d85\u65f6\u7684\u56de\u8c03\u529f\u80fd\u3002<\/p>

    libevent\u65e8\u5728\u66ff\u6362\u5728\u539f\u6709\u4e8b\u4ef6\u9a71\u52a8\u7f51\u7edc\u670d\u52a1\u5668<\/a>\u4e8b\u4ef6\u5faa\u73af\u800c\u8bbe\u8ba1\u7684\u3002\u5e94\u7528\u7a0b\u5e8f\u4ec5\u4ec5\u9700\u8981\u8c03\u7528event_dispatch\uff08\uff09\uff0c\u7136\u540e\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u662f\u79fb\u9664\u4e8b\u4ef6\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e0d\u9700\u8981\u6539\u53d8\u539f\u6709\u7684\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>

    \u76ee\u524d\uff0clibevent\u652f\u6301\/dev\/poll\uff0ckqueue\uff082\uff09\uff0cselect\uff082\uff09\uff0cpoll\uff082\uff09\u548cepoll\uff084\uff09\u7b49\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b<\/a>(www.cppentry.com)\u6a21\u578b\u3002\u800c\u5b83\u5bf9\u5b9e\u65f6\u4fe1\u53f7\u7684\u652f\u6301\u6b63\u5904\u4e8e\u5b9e\u9a8c\u6027\u9636\u6bb5\u3002\u5185\u90e8\u7684\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u662f\u5b8c\u5168\u72ec\u7acb\u4e8e\u66b4\u9732\u51fa\u6765\u7684API\u7684\uff0c\u5e76\u4e14\u65b0\u529f\u80fd\u7684\u52a0\u5165\u5e76\u4e0d\u9700\u8981\u91cd\u65b0\u8bbe\u8ba1\u5e94\u7528\u7a0b\u5e8f\uff0c\u800c\u662f\u4ec5\u4ec5\u9700\u8981\u505a\u4e00\u4e2a\u7b80\u5355\u7684libevent\u66f4\u65b0\u5373\u53ef\u3002\u56e0\u6b64\uff0clievent\u5141\u8bb8\u53ef\u79fb\u690d\u6027\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\uff0c\u5e76\u4e14\u80fd\u591f\u63d0\u4f9b\u9002\u5408\u7279\u5b9a\u64cd\u4f5c\u7cfb\u7edf\u7684\u6700\u5177\u53ef\u6269\u5c55\u6027\u7684\u4e8b\u4ef6\u901a\u77e5\u673a\u5236\u3002libevent\u540c\u65f6\u4e5f\u53ef\u7528\u4e8e\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/a>(www.cppentry.com)\u73af\u5883\uff0c\u66f4\u591a\u8bf4\u660e\u8bf7\u770bSteven Grimm\u7684\u8bf4\u660e\u3002libevent\u53ef\u4ee5\u5728Linux\uff0c\uff0aBSD\uff0cMac OS X\uff0cSolaris\u548cWindows<\/a>\u7cfb\u7edf\u4e0a\u7f16\u8bd1\u3002<\/p>

    2. \u6807\u51c6\u7528\u6cd5<\/p>

    \u6bcf\u4e00\u4e2a\u4f7f\u7528libevent\u7684\u7a0b\u5e8f\uff0c\u90fd\u9700\u8981\u5305\u542b<event.h>\u5934\u6587\u4ef6\uff0c\u5e76\u4e14\u9700\u8981\u4f20\u9012-levent\u6807\u5fd7\u7ed9\u8fde\u63a5\u5668linker\u3002\u5728\u4f7f\u7528\u4efb\u4f55\u5e93\u51fd\u6570\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8c03\u7528event_init\uff08\uff09\u6216\u8005event_base_new\uff08\uff09\u51fd\u6570\u5236\u6267\u884c\u4e00\u6b21libevent\u5e93\u7684\u521d\u59cb\u5316\u3002<\/p>

    3. \u4e8b\u4ef6\u901a\u77e5<\/p>

    \u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u4f60\u60f3\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4f60\u5fc5\u987b\u58f0\u660e\u4e00\u4e2a\u4e8b\u4ef6\u7ed3\u6784\u5e76\u4e14\u8c03\u7528event_set\uff08\uff09\u53bb\u521d\u59cb\u5316\u7ed3\u6784\u4e2d\u7684\u6210\u5458\u3002\u4e3a\u4e86\u6fc0\u6d3b\u901a\u77e5\uff0c\u4f60\u9700\u8981\u901a\u8fc7\u8c03\u7528event_add\uff08\uff09\u5c06\u8be5\u7ed3\u6784\u6dfb\u52a0\u5230\u76d1\u89c6\u4e8b\u4ef6\u5217\u8868\u3002\u53ea\u8981\u662f\u8be5\u4e8b\u4ef6\u5b58\u6d3b\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4fdd\u6301\u8be5\u5df2allocated\u7684\u4e8b\u4ef6\u7ed3\u6784\uff0c\u56e0\u6b64\u8be5\u4e8b\u4ef6\u7ed3\u6784\u9700\u8981\u5728\u5806\uff08heap\uff09\u4e0a\u7533\u8bf7\u3002\u6700\u540e\uff0c\u9700\u8981\u8c03\u7528event_dispatch\uff08\uff09\u51fd\u6570\u5faa\u73af\u548c\u8c03\u5ea6\u4e8b\u4ef6\u3002<\/p>

    4. I\/O\u7f13\u51b2\u533a<\/p>

    libevent\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b9a\u671f\u56de\u8c03\u4e8b\u4ef6\u9876\u5c42\u7684\u62bd\u8c61\u3002\u8be5\u62bd\u8c61\u88ab\u79f0\u4e3a\u7f13\u51b2\u4e8b\u4ef6\uff08buffered event\uff09\u3002\u7f13\u51b2\u4e8b\u4ef6\u63d0\u4f9b\u81ea\u52a8\u5730\u586b\u5145\u548c\u6d41\u6389\uff08drained\uff09\u7684\u8f93\u5165\u548c\u8f93\u51fa\u7f13\u51b2\u533a\u3002\u7f13\u51b2\u65f6\u95f4\u7684\u7528\u6237\u4e0d\u518d\u9700\u8981\u76f4\u63a5\u64cd\u4f5cI\/O\uff0c\u53d6\u800c\u5f85\u4e4b\u7684\u662f\u4ec5\u4ec5\u4ece\u8f93\u5165\u7f13\u51b2\u533a\u8bfb\uff0c\u5411\u8f93\u51fa\u7f13\u51b2\u533a\u5199\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>

    \u4e00\u65e6\u901a\u8fc7bufferevent_new\uff08\uff09\u8fdb\u884c\u4e86\u521d\u59cb\u5316\uff0cbufferevent\u7ed3\u6784\u5c31\u53ef\u4ee5\u901a\u8fc7bufferevent_enable\uff08\uff09\u548cbufferevent_disable\uff08\uff09\u91cd\u590d\u5730\u4f7f\u7528\u4e86\u3002\u4f5c\u4e3a\u66ff\u4ee3\uff0c\u5bf9\u4e00\u4e2a\u5957\u63a5\u53e3\u7684\u8bfb\u5199\u9700\u8981\u901a\u8fc7\u8c03\u7528bufferevent_read\uff08\uff09\u548cbufferevent_write\uff08\uff09\u51fd\u6570\u6765\u5b8c\u6210\u3002<\/p>

    \u5f53\u7531\u4e8e\u8bfb\u4e8b\u4ef6\u800c\u6fc0\u6d3bbufferevent\u65f6\uff0c\u90a3\u4e48\u540e\u7eed\u5c06\u4f1a\u81ea\u52a8\u56de\u8c03\u8bfb\u51fd\u6570\u4ece\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u53d6\u6570\u636e\u3002\u5199\u51fd\u6570\u5c06\u4f1a\u88ab\u56de\u8c03\uff0c\u65e0\u8bba\u4f55\u65f6\u8fd9\u4e2a\u8f93\u51fa\u7f13\u51b2\u533a\u7a7a\u95f4\u88ab\u8017\u5c3d\u5230\u4f4e\u4e8e\u5199\u7684\u4e0b\u6c34\u4f4d\uff08low watemark\uff09\uff0c\u901a\u5e38\u8be5\u503c\u9ed8\u8ba4\u4e3a0\u3002<\/p>

    5. \u5b9a\u65f6\u5668<\/p>

    libevent\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\u6765\u53c2\u4e0e\u5230\u4e00\u4e2a\u7ecf\u8fc7\u4e00\u5b9a\u8d85\u65f6\u65f6\u95f4\u540e\u7684\u56de\u8c03\u4e8b\u4ef6\u4e2d\u3002evtimer_set\uff08\uff09\u51fd\u6570\u5c06\u51c6\u5907\uff08\u5206\u914d\uff09\u4e00\u4e2a\u4e8b\u4ef6\u7ed3\u6784\u88ab\u7528\u4e8e\u4f5c\u4e3a\u4e00\u4e2a\u5b9a\u65f6\u5668\u3002\u4e3a\u4e86\u6fc0\u6d3b\u5b9a\u65f6\u5668\uff0c\u9700\u8981\u8c03\u7528evtimer_add\uff08\uff09\u51fd\u6570\u3002\u76f8\u53cd\uff0c\u9700\u8981\u8c03\u7528evtimer_del\uff08\uff09\u51fd\u6570\u3002<\/p>

    6. \u8d85\u65f6<\/p>

    \u9664\u4e86\u7b80\u5355\u7684\u5b9a\u65f6\u5668\uff0clibevent\u53ef\u4ee5\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26\u6307\u5b9a\u4e00\u4e2a\u8d85\u65f6\u4e8b\u4ef6\uff0c\u7528\u4e8e\u89e6\u53d1\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u540e\u800c\u6ca1\u6709\u88ab\u6fc0\u6d3b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002timeout_set\uff08\uff09\u51fd\u6570\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u521d\u59cb\u5316\u4e00\u4e2a\u4e8b\u4ef6\u7ed3\u6784\u3002\u4e00\u65e6\u88ab\u521d\u59cb\u5316\u6210\u529f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u4e8b\u4ef6\u5fc5\u987b\u901a\u8fc7timeout_add\uff08\uff09\u51fd\u6570\u6fc0\u6d3b\u3002\u4e3a\u4e86\u53d6\u6d88\u4e00\u4e2a\u8d85\u65f6\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u8c03\u7528timeout_del\uff08\uff09\u51fd\u6570\u3002<\/p>

    7. \u5f02\u6b65DNS\u89e3\u6790<\/p>

    libevent\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f02\u6b65DNS\u89e3\u6790\u5668\uff0c\u53ef\u7528\u4e8e\u4ee3\u66ff\u6807\u51c6\u7684DNS\u89e3\u6790\u5668\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5728\u7a0b\u5e8f\u4e2d\u5305\u542b<evdns.h>\u5934\u6587\u4ef6\u800c\u5c06\u5176\u5bfc\u5165\u3002\u5728\u4f7f\u7528\u4efb\u4f55\u89e3\u6790\u5668\u51fd\u6570\u4e4b\u524d\uff0c\u4f60\u5fc5\u987b\u8c03\u7528evdns_init\uff08\uff09\u51fd\u6570\u521d\u59cb\u5316\u51fd\u6570\u5e93\u3002\u4e3a\u8f6c\u5316\u4e00\u4e2a\u57df\u540d\u5230IP\u5730\u5740\uff0c\u53ef\u4ee5\u8c03\u7528evdns_resolve_ipv4\uff08\uff09\u51fd\u6570\u3002\u4e3a\u4e86\u6267\u884c\u4e00\u4e2a\u53cd\u54cd\u67e5\u8be2\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528evdns_resolve_reverse\uff08\uff09\u51fd\u6570\u3002\u6240\u6709\u7684\u8fd9\u4e9b\u51fd\u6570\uff0c\u5728\u67e5\u627e\u65f6\u90fd\u4f1a\u4f7f\u7528\u56de\u8c03\u7684\u65b9\u5f0f\u800c\u907f\u514d\u963b\u585e\u7684\u53d1\u751f\u3002<\/p>

    8. \u4e8b\u4ef6\u9a71\u52a8\u7684HTTP\u670d\u52a1\u5668<\/p>

    libevent\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u53ef\u4ee5\u5d4c\u5165\u5230\u4f60\u7684\u7a0b\u5e8f\u4e2d\u7684\u5e76\u80fd\u5904\u7406HTTP\u8bf7\u6c42\u7684\u4e8b\u4ef6\u9a71\u52a8HTTP\u670d\u52a1\u5668\u3002<\/p>

    \u4e3a\u4e86\u4f7f\u7528\u8fd9\u79cd\u80fd\u529b\uff0c\u4f60\u5e94\u8be5\u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5305\u542b<evhttp.h>\u5934\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528evhttp_new\uff08\uff09\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u5668\u3002\u901a\u8fc7evhttp_bind_socket\uff08\uff09\u51fd\u6570\u6dfb\u52a0\u7528\u4e8e\u76d1\u542c\u7684\u5730\u5740\u548c\u7aef\u53e3\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u6ce8\u518c\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u5230\u6765\u8bf7\u6c42\u7684\u5904\u7406\u53e5\u67c4\u3002\u5bf9\u4e8e\u6bcf\u4e00\u4e2aURI\u53ef\u4ee5\u901a\u8fc7evhttp_set_cb\uff08\uff09\u51fd\u6570\u6307\u5b9a\u4e00\u4e2a\u56de\u8c03\u3002\u901a\u5e38\uff0c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7evhttp_set_gencb\uff08\uff09\u51fd\u6570\u5b8c\u6210\u6ce8\u518c\uff1b\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7684\u56de\u8c03\u5df2\u7ecf\u88ab\u6ce8\u518c\u5f97\u5230\u8be5URI\uff0c\u90a3\u4e48\u8fd9\u4e2a\u56de\u8c03\u5c06\u4f1a\u4e0e\u5176\u5173\u8054\u3002<\/p>

    9. RPC\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u6846\u67b6<\/p>

    libevent\u63d0\u4f9b\u4e86\u4e00\u4e2a\u521b\u5efaRPC\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u7f16\u7a0b(www.cppentry.com)\u6846\u67b6\u3002\u5b83\u5c06\u6258\u7ba1\u6240\u6709\u7684\u7f16\u7ec4\u548c\u89e3\u7ec4\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p><\/span>","orderid":"0","title":"libevent C \u4e8b\u4ef6\u901a\u77e5\u63a5\u53e3\u51fd\u6570\u5e93","smalltitle":"","mid":"0","fname":"\u7f51\u7edc\u7f16\u7a0b","special_id":"0","bak_id":"0","info":"0","hits":"2484","pages":"1","comments":"0","posttime":"2011-12-15 09:30:41","list":"1323912641","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"libevent<\/A> \u4e8b\u4ef6<\/A> \u901a\u77e5<\/A> \u63a5\u53e3<\/A> \u51fd\u6570<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"121.14.96.125","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u4eca\u5929\u5728\u7814\u7a76memcached\u8f6f\u4ef6\u65f6\uff0c\u63a5\u89e6\u5230\u4e86libevent \uff0d \u4e8b\u4ef6\u901a\u77e5\u63a5\u53e3\u51fd\u6570\u5e93\uff0c\u53d1\u73b0\u4e86\u5b83\u5bf9\u9ad8\u6027\u80fd\uff0c\u9ad8\u5e76\u53d1\uff0c\u53ef\u79fb\u690d\u7b49\u8bf8\u591a\u65b9\u9762\u63d0\u4f9b\u4e86\u76f8\u5f53\u591a\u7684\u4fbf\u5229\u3002\u5fc3\u91cc\u751a\u662f\u6b22\u559c\uff0c\u56e0\u6b64\uff0c\u8fd9\u91cc\u5c31\u60f3\u628a\u8fd9\u4e2a\u597d\u5bb6\u4f19\u4ecb\u7ecd\u7ed9\u5927\u5bb6\uff0c\u5171\u540c\u5206\u4eab\u5206\u4eab\u3002 \u8bf4\u5728\u524d\u8fb9\uff0c\u6055\u5c0f\u5f1f\u65e0\u77e5\uff0c\u5176\u5b9elibevent\u8f6f\u4ef6\u5305\u65e9\u57282000\u5e741..","lastview":"1493263012","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"|2~~\u6b20\u6241|1","htmlname":"","city_id":"0"},"page":"1"}