acl/lib_acl_cpp/samples/master_aio_proxy/req_callback.cpp

104 lines
1.9 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include "stdafx.h"
#include "res_callback.h"
#include "req_callback.h"
req_callback::req_callback(acl::aio_socket_stream* conn,
acl::ofstream* req_fp, acl::ofstream* res_fp)
: conn_(conn)
, res_(NULL)
, req_fp_(req_fp)
, res_fp_(res_fp)
{
}
req_callback::~req_callback()
{
acl_assert(res_ == NULL);
}
bool req_callback::read_callback(char* data, int len)
{
if (res_ == NULL)
{
logger_warn("server peer disconnected!");
return false;
}
// ȡ<>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
acl::aio_socket_stream& peer = res_->get_conn();
peer.write(data, len);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EBB1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
if (req_fp_)
req_fp_->write(data, len);
return true;
}
void req_callback::close_callback()
{
logger("disconnect from %s, fd: %d", conn_->get_peer(),
conn_->sock_handle());
if (res_)
{
res_callback* res = res_;
res_ = NULL;
res->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>й¶
delete this;
}
void req_callback::start(const char* server_addr)
{
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ķ<EFBFBD><C4B6>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
conn_->add_read_callback(this);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
conn_->add_write_callback(this);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
conn_->add_close_callback(this);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
conn_->add_timeout_callback(this);
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Զ<EFBFBD>̷<EFBFBD><CCB7><EFBFBD><EFBFBD><EFBFBD>
acl::aio_handle& handle = conn_->get_handle();
res_ = new res_callback(this, res_fp_);
if (res_->start(handle, server_addr) == false)
{
logger_error("connect server %s error %s",
server_addr, acl::last_serror());
delete res_;
res_ = NULL;
conn_->close();
}
}
acl::aio_socket_stream& req_callback::get_conn()
{
acl_assert(conn_);
return *conn_;
}
void req_callback::on_connected()
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD>˳ɹ<CBB3><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>ӿͻ<D3BF><CDBB>˶<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
conn_->read();
}
void req_callback::disconnect()
{
if (conn_)
{
res_ = NULL;
conn_->close();
}
else
delete this;
}