使用协程方式编写高并发的 WEB 服务
2016-07-06 23:01
1006 查看
[p] 在《[url=https://blog.csdn.net/iteye_18264/article/details/https:https:/blog.csdn.net/https:/blog.csdn.net/blog.csdn.nethttps:/blog.csdn.net/bloghttps:/blog.csdn.net/2309654]使用 acl 协程编写高并发网络服务[https://blog.csdn.net/url]》中介绍了一个使用 acl 协程库编写高并发网络服务的应用示例,本节将展示一个稍微复杂些且更具实际意义的例子:基于协程的 WEB 服务器程序。下面首先展示这个 WEB 服务器程序:[https://blog.csdn.net/p]
[code]#include "lib_acl.h" https://blog.csdn.net/https://blog.csdn.net/ acl 基础库头文件
#include "fiberhttps://blog.csdn.net/lib_fiber.h" https://blog.csdn.net/https://blog.csdn.net/ acl 协程库头文件
#include "acl_cpphttps://blog.csdn.net/lib_acl.hpp https://blog.csdn.net/https://blog.csdn.net/ acl C++ 封装库头文件
class http_servlet : public acl::HttpServlet
{
public:
http_servlet(acl::socket_stream* stream, acl::session* session)
: HttpServlet(stream, session)
{
}
~http_servlet(void)
{
}
https://blog.csdn.net/https://blog.csdn.net/ override
bool doGet(acl::HttpServletRequest& req, acl::HttpServletResponse& res)
{
return doPost(req, res);
}
https://blog.csdn.net/https://blog.csdn.net/ override
bool doPost(acl::HttpServletRequest&, acl::HttpServletResponse& res)
{
const char* buf = "hello world!";
size_t len = strlen(buf);
res.setContentLength(len);
res.setKeepAlive(true);
https://blog.csdn.net/https://blog.csdn.net/ 发送 http 响应体
return res.write(buf, len) && res.write(NULL, 0);
}
};
#define STACK_SIZE 32000 https://blog.csdn.net/https://blog.csdn.net/ 指定协程堆栈大小(字节)
static int __rw_timeout = 0; https://blog.csdn.net/https://blog.csdn.net/ 网络 IO 超时时间(秒)
static void http_server(ACL_FIBER *, void *ctx)
{
acl::socket_stream *conn = (acl::socket_stream *) ctx;
printf("start one http_server\r\n");
acl::memcache_session session("127.0.0.1:11211");
https://blog.csdn.net/https://blog.csdn.net/ 基于 ACL HTTP 模块的 Http 服务类
http_servlet servlet(conn, &session);
servlet.setLocalCharset("gb2312");
https://blog.csdn.net/https://blog.csdn.net/ 循环处理客户端的 HTTP 请求
while (true)
{
https://blog.csdn.net/https://blog.csdn.net/ 调用 acl::HttpServlet 类中的方法,从而触发子类重载的 doPosthttps://blog.csdn.net/doGet 方法
if (servlet.doRun() == false)
break;
}
printf("close one connection: %d, %s\r\n", conn->sock_handle(), acl::last_serror());
https://blog.csdn.net/https://blog.csdn.net/ 销毁客户端连接对象
delete conn;
}
static void fiber_accept(ACL_FIBER *, void *ctx)
{
const char* addr = (const char* ) ctx;
acl::server_socket server;
https://blog.csdn.net/https://blog.csdn.net/ 监听本机服务端口
if (server.open(addr) == false)
{
printf("open %s error\r\n", addr);
exit (1);
}
else
printf("open %s ok\r\n", addr);
while (true)
{
https://blog.csdn.net/https://blog.csdn.net/ 等待接收外来 HTTP 客户端连接
acl::socket_stream* client = server.accept();
if (client == NULL)
{
printf("accept failed: %s\r\n", acl::last_serror());
break;
}
client->set_rw_timeout(__rw_timeout);
printf("accept one: %d\r\n", client->sock_handle());
https://blog.csdn.net/https://blog.csdn.net/ 创建协程处理 HTTP 客户端连接请求
acl_fiber_create(http_server, client, STACK_SIZE);
}
exit (0);
}
static void usage(const char* procname)
{
printf("usage: %s -h [help] -s listen_addr -r rw_timeout\r\n", procname);
}
int main(int argc, char *argv[])
{
acl::string addr("127.0.0.1:9001");
int ch;
while ((ch = getopt(argc, argv, "hs:r:")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'r':
__rw_timeout = atoi(optarg);
break;
default:
break;
}
}
acl::acl_cpp_init();
acl::log::stdout_open(true);
https://blog.csdn.net/https://blog.csdn.net/ 创建服务监听协程
acl_fiber_create(fiber_accept, addr.c_str(), STACK_SIZE);
https://blog.csdn.net/https://blog.csdn.net/ 启动协程调度过程
acl_fiber_schedule();
return 0;
}
[https://blog.csdn.net/code]
[p] 此例子的流程为:创建服务监听协程 ---> 启动协程调度过程 ---> 监听协程收到 HTTP 客户端连接后,创建 HTTP 协程处理该连接 ---> HTTP 协程与 HTTP 客户端进行交互。[https://blog.csdn.net/p]
[p] 因为协程相对于线程来说是非常轻量级的,所以虽然针对每一个 HTTP 客户端连接都会创建一个新的协程,但这并不会费太多系统资源,因而可以支持非常高的并发连接。[https://blog.csdn.net/p]
[p] [https://blog.csdn.net/p]
[p] 参考协程编程文章:[url=https://blog.csdn.net/iteye_18264/article/details/https:https:/blog.csdn.net/https:/blog.csdn.net/blog.csdn.nethttps:/blog.csdn.net/bloghttps:/blog.csdn.net/2312043]网络协程编程[https://blog.csdn.net/url][https://blog.csdn.net/p]
[p] 个人微博:[url=https://blog.csdn.net/iteye_18264/article/details/http:https:/blog.csdn.net/https:/blog.csdn.net/weibo.comhttps:/blog.csdn.net/zsxxsz]https://blog.csdn.net/iteye_18264/article/details/http:https:/blog.csdn.net/https:/blog.csdn.net/weibo.comhttps:/blog.csdn.net/zsxxsz[https://blog.csdn.net/url][https://blog.csdn.net/p]
[p] acl 下载:[url=https://blog.csdn.net/iteye_18264/article/details/https:https:/blog.csdn.net/https:/blog.csdn.net/sourceforge.nethttps:/blog.csdn.net/phttps:/blog.csdn.net/aclhttps:/blog.csdn.net/]https://blog.csdn.net/iteye_18264/article/details/https:https:/blog.csdn.net/https:/blog.csdn.net/sourceforge.nethttps:/blog.csdn.net/phttps:/blog.csdn.net/aclhttps:/blog.csdn.net/[https://blog.csdn.net/url][https://blog.csdn.net/p]
[p] github 地址:[url=https://blog.csdn.net/iteye_18264/article/details/https:https:/blog.csdn.net/https:/blog.csdn.net/github.comhttps:/blog.csdn.net/acl-devhttps:/blog.csdn.net/acl]https://blog.csdn.net/iteye_18264/article/details/https:https:/blog.csdn.net/https:/blog.csdn.net/github.comhttps:/blog.csdn.net/acl-devhttps:/blog.csdn.net/acl[https://blog.csdn.net/url][https://blog.csdn.net/p]
[p] 国内镜像:[url=https://blog.csdn.net/iteye_18264/article/details/http:https:/blog.csdn.net/https:/blog.csdn.net/git.oschina.nethttps:/blog.csdn.net/acl-devhttps:/blog.csdn.net/acl]https://blog.csdn.net/iteye_18264/article/details/http:https:/blog.csdn.net/https:/blog.csdn.net/git.oschina.nethttps:/blog.csdn.net/acl-devhttps:/blog.csdn.net/acl[https://blog.csdn.net/url][https://blog.csdn.net/p]
[p] [url=https://blog.csdn.net/]更多文章[https://blog.csdn.net/url][https://blog.csdn.net/p]
[p] QQ 群:242722074[https://blog.csdn.net/p]
[p] [https://blog.csdn.net/p]
相关文章推荐
- 使用Python WSGI编写Web服务器前端,并利用Boost.Python在同一进程内连接到C++服务后端实现
- 使用HTML5 Web存储的localStorage方式进行编写一个Web页面。
- 在 IBM Lotus Domino 7 中使用 Web 服务,第 3 部分: 编写复杂的 Web 服务
- golang中使用原生的http包编写一个web服务
- 尝试向 URI“ ”发出请求时出错。这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP 服务...。出现此错误也可能是由于使用的是 Web 服务代理中的内部类型而没有使用 InternalsVisibleToAttribute 属性。
- webservice开发---------如何使用cxf发布soap方式的web服务
- AJAX及使用E4X编写Web服务脚本
- Maven搭建webService (二) 创建服务端---使用web方式发布服务
- 使用Python 2.7中pycurl模块编写探测多节点Web服务质量脚本
- AJAX及使用E4X编写Web服务脚本
- 使用Python编写免安装运行时、以Windows后台服务形式运行的WEB服务器
- 通过WCF 服务 使用 EntityFramework5.0 CodeFirst方式 通过 DTO Automapper 更新实体的并发控制
- Web服务协议以及使用方式
- 编写一个使用 Node.js/MongoDB Web 服务的 iOS 应用
- 使用Axis的wsdd描述方式发布Web服务
- 不使用Web服务器编写SOAP服务程序相关联接
- AJAX及使用E4X编写Web服务脚本
- 使用Frigga实现WEB方式对服务的监控
- 使用Axis部署Web服务时的常见问题及其解决方法(转)
- 从 PHP 脚本中访问企业应用程序 使用 PHP 5 SOAP 扩展访问 WebSphere Web 服务