2019-07-28 10:31:56 +08:00
|
|
|
|
#include "StdAfx.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "lib_acl.h"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include "acl_cpp/stream/aio_handle.hpp"
|
|
|
|
|
#include "AioClient.h"
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
# ifndef snprintf
|
|
|
|
|
# define snprintf _snprintf
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
using namespace acl;
|
|
|
|
|
|
|
|
|
|
CConnectClientCallback::CConnectClientCallback(IO_CTX* ctx,
|
|
|
|
|
aio_socket_stream* client, int id)
|
|
|
|
|
: client_(client)
|
|
|
|
|
, ctx_(ctx)
|
|
|
|
|
, nwrite_(0)
|
|
|
|
|
, id_(id)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CConnectClientCallback::~CConnectClientCallback()
|
|
|
|
|
{
|
|
|
|
|
std::cout << ">>>ID: " << id_ << ", CConnectClientCallback deleted now!"
|
|
|
|
|
<< std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConnectClientCallback::read_callback(char* data, int len)
|
|
|
|
|
{
|
|
|
|
|
(void) data;
|
|
|
|
|
(void) len;
|
|
|
|
|
|
|
|
|
|
ctx_->nread_total++;
|
|
|
|
|
|
|
|
|
|
if (ctx_->debug)
|
|
|
|
|
{
|
|
|
|
|
if (nwrite_ < 10)
|
|
|
|
|
std::cout << "gets(" << nwrite_ << "): " << data;
|
|
|
|
|
else if (nwrite_ % 2000 == 0)
|
|
|
|
|
std::cout << ">>ID: " << id_ << ", I: "
|
|
|
|
|
<< nwrite_ << "; "<< data;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ҲӦ<D2B2>˳<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (strncasecmp(data, "quit", 4) == 0)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->format("Bye!\r\n");
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>ر<EFBFBD><D8B1>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->close();
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nwrite_ >= ctx_->nwrite_limit)
|
|
|
|
|
{
|
|
|
|
|
if (ctx_->debug)
|
|
|
|
|
std::cout << "ID: " << id_
|
|
|
|
|
<< ", nwrite: " << nwrite_
|
|
|
|
|
<< ", nwrite_limit: " << ctx_->nwrite_limit
|
|
|
|
|
<< ", quiting ..." << std::endl;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>Ϣ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->format("quit\r\n");
|
|
|
|
|
client_->close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
snprintf(buf, sizeof(buf), "hello world: %d\n", nwrite_);
|
|
|
|
|
client_->write(buf, (int) strlen(buf));
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
//client_->format("hello world: %d\n", nwrite_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConnectClientCallback::write_callback()
|
|
|
|
|
{
|
|
|
|
|
ctx_->nwrite_total++;
|
|
|
|
|
nwrite_++;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->gets(ctx_->read_timeout, false);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CConnectClientCallback::close_callback()
|
|
|
|
|
{
|
|
|
|
|
if (client_->is_opened() == false)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Id: " << id_ << " connect "
|
|
|
|
|
<< ctx_->addr << " error: "
|
|
|
|
|
<< acl_last_serror();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD>˳<EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (ctx_->nopen_total == 0)
|
|
|
|
|
{
|
|
|
|
|
std::cout << ", first connect error, quit";
|
2019-07-28 10:31:56 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˳<EFBFBD>״̬ */
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->get_handle().stop();
|
|
|
|
|
}
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
delete this;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <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 CConnectClientCallback::timeout_callback()
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Connect " << ctx_->addr << " Timeout ..." << std::endl;
|
|
|
|
|
client_->close();
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConnectClientCallback::open_callback()
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <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
|
|
|
|
client_->add_read_callback(this);
|
|
|
|
|
client_->add_write_callback(this);
|
|
|
|
|
ctx_->nopen_total++;
|
|
|
|
|
|
|
|
|
|
acl_assert(id_ > 0);
|
|
|
|
|
if (ctx_->nopen_total < ctx_->nopen_limit)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (connect_server(ctx_, id_ + 1) == false)
|
|
|
|
|
std::cout << "connect error!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
//client_->format("hello world: %d\n", nwrite_);
|
|
|
|
|
char buf[256];
|
|
|
|
|
snprintf(buf, sizeof(buf), "hello world: %d\n", nwrite_);
|
|
|
|
|
client_->write(buf, (int) strlen(buf));
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->gets(ctx_->read_timeout, false);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CConnectClientCallback::connect_server(IO_CTX* ctx, int id)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>Զ<EFBFBD>̷<EFBFBD><CCB7><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
// const char* addr = "221.194.139.155:18887";
|
|
|
|
|
// ctx->connect_timeout = 1;
|
|
|
|
|
aio_socket_stream* stream = aio_socket_stream::open(ctx->handle,
|
|
|
|
|
ctx->addr, ctx->connect_timeout);
|
|
|
|
|
if (stream == NULL)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "connect " << ctx->addr << " error!" << std::endl;
|
|
|
|
|
std::cout << "stoping ..." << std::endl;
|
|
|
|
|
if (id == 0)
|
|
|
|
|
ctx->handle->stop();
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD><D3BA>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
CConnectClientCallback* callback = new CConnectClientCallback(ctx, stream, id);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳɹ<D3B3><C9B9>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
stream->add_open_callback(callback);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܺ<EFBFBD><DCBA>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
stream->add_close_callback(callback);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
stream->add_timeout_callback(callback);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|