acl/lib_acl_cpp/samples/http_server/http_rpc.cpp

117 lines
2.5 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
#include "rpc_stats.h"
#include "http_rpc.h"
http_rpc::http_rpc(acl::aio_socket_stream* client, unsigned buf_size)
: proc_quit_(false)
, handle_(client->get_handle())
, client_(client)
, buf_size_(buf_size)
{
res_buf_ = (char*) acl_mymalloc(buf_size + 1);
unsigned i;
for (i = 0; i < buf_size; i++)
res_buf_[i] = 'x';
res_buf_[i] = 0;
}
http_rpc::~http_rpc()
{
acl_myfree(res_buf_);
}
void http_rpc::rpc_onover()
{
// <20><><EFBFBD><EFBFBD> rpc <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
rpc_del();
if (!proc_quit_ && keep_alive_)
{
rpc_read_wait_add();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ƿ<EFBFBD><C7B7>ɶ<EFBFBD>
2014-11-19 00:25:21 +08:00
client_->read_wait(10);
}
else
// <20>ر<EFBFBD><D8B1><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client_->close();
// <20>ͻ<EFBFBD><CDBB>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (proc_quit_)
handle_.stop();
}
// <20><><EFBFBD><EFBFBD> service_.rpc_fork <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> RPC <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>е<EFBFBD><D0B5>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><E9B7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2014-11-19 00:25:21 +08:00
void http_rpc::rpc_run()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
socket_stream stream;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> get_vstream() <20><><EFBFBD>õ<EFBFBD> ACL_VSTREAM <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> stream <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA> acl_cpp <20><> acl <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD><D5B6>ǻ<EFBFBD><C7BB><EFBFBD> ACL_VSTREAM<41><4D><EFBFBD><EFBFBD> ACL_VSTREAM <20><>
// <20>ڲ<EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD>У<EFBFBD>
// <20><><EFBFBD><EFBFBD>ÿ<EFBFBD>ν<EFBFBD> ACL_VSTREAM <20><>Ϊ<EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>
2014-11-19 00:25:21 +08:00
ACL_VSTREAM* vstream = client_->get_vstream();
ACL_VSTREAM_SET_RWTIMO(vstream, 10);
(void) stream.open(vstream);
rpc_req_add();
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
handle_conn(&stream);
rpc_req_del();
// <20><> ACL_VSTREAM <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󶨣<EFBFBD><F3B6A8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܱ<EFBFBD>֤<EFBFBD><D6A4><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
// <20><><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>Ҫ<EFBFBD><D2AA>
// <20><><EFBFBD><EFBFBD><ECB2BD><EFBFBD>رշ<D8B1>ʽ<EFBFBD><CABD><EFBFBD>йر<D0B9>
2014-11-19 00:25:21 +08:00
stream.unbind();
}
void http_rpc::handle_conn(socket_stream* stream)
{
// HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
http_response res(stream);
// <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ xml <20><>ʽ
2014-11-19 00:25:21 +08:00
res.response_header().set_content_type("text/html");
// <20><> HTTP <20><><EFBFBD><EFBFBD>ͷ
2014-11-19 00:25:21 +08:00
if (res.read_header() == false)
{
keep_alive_ = false;
return;
}
string buf;
// <20><> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (res.get_body(buf) == false)
{
keep_alive_ = false;
return;
}
http_client* client = res.get_client();
// <20>жϿͻ<CFBF><CDBB><EFBFBD><EFBFBD>Ƿ<EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
keep_alive_ = client->keep_alive();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>ͻ<EFBFBD><CDBB><EFBFBD>
2014-11-19 00:25:21 +08:00
res.response_header()
.set_status(200)
.set_keep_alive(keep_alive_)
.set_content_length(buf_size_);
res.response(res_buf_, buf_size_);
// ȡ<>ÿͻ<C3BF><CDBB>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
const char* action = client->request_param("action");
if (action && (strcasecmp(action, "stop") == 0
|| strcasecmp(action, "quit") == 0))
{
proc_quit_ = true;
}
}