acl/lib_acl_cpp/samples/master_aio_proxy/res_callback.cpp

98 lines
1.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
#include "req_callback.h"
#include "res_callback.h"
res_callback::res_callback(req_callback* req, acl::ofstream* res_fp)
: conn_(NULL)
, req_(req)
, res_fp_(res_fp)
{
}
res_callback::~res_callback()
{
acl_assert(req_ == NULL);
}
bool res_callback::read_callback(char* data, int len)
{
if (req_ == NULL)
{
logger_warn("client peer disconnected!");
return false;
}
// ȡ<>ÿͻ<C3BF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
2014-11-19 00:25:21 +08:00
acl::aio_socket_stream& peer = req_->get_conn();
peer.write(data, len);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱд<CAB1><EFBFBD><EBB1BE><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
2014-11-19 00:25:21 +08:00
if (res_fp_)
res_fp_->write(data, len);
return true;
}
void res_callback::close_callback()
{
if (req_)
{
req_callback* req = req_;
req_ = NULL;
req->disconnect();
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ˴<DAB4>ɾ<EFBFBD><C9BE><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD>ֹ<EFBFBD>ڴ<EFBFBD>й¶
2014-11-19 00:25:21 +08:00
delete this;
}
bool res_callback::open_callback()
{
// <20><><EFBFBD>ӳɹ<D3B3><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IO<49><4F>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
conn_->add_read_callback(this);
conn_->add_write_callback(this);
// <20>ȴ<EFBFBD><C8B4>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
conn_->read();
// ֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
acl_assert(req_);
req_->on_connected();
return true;
}
bool res_callback::start(acl::aio_handle& handle, const char* server_addr)
{
conn_ = acl::aio_socket_stream::open(&handle, server_addr, 0);
if (conn_ == NULL)
{
logger_error("connect server %s error %s",
server_addr, acl::last_serror());
return false;
}
conn_->add_open_callback(this);
conn_->add_timeout_callback(this);
conn_->add_close_callback(this);
return true;
}
acl::aio_socket_stream& res_callback::get_conn()
{
acl_assert(conn_);
return *conn_;
}
void res_callback::disconnect()
{
if (conn_)
{
req_ = NULL;
conn_->close();
}
else
delete this;
}