acl/lib_acl_cpp/samples/ssl/aio_client/main.cpp

473 lines
11 KiB
C++
Raw Normal View History

#include <iostream>
2015-12-06 16:56:21 +08:00
#include <assert.h>
#include "lib_acl.h"
2015-12-20 15:27:55 +08:00
#include "../../util.h"
2015-12-06 16:56:21 +08:00
#include "acl_cpp/lib_acl.hpp"
#include "acl_cpp/stream/openssl_conf.hpp"
2015-12-06 16:56:21 +08:00
typedef struct
{
acl::aio_handle* handle;
char addr[64];
int connect_timeout;
int read_timeout;
int nopen_limit;
int nopen_total;
int nwrite_limit;
int nwrite_total;
int nread_total;
int id_begin;
2015-12-20 15:27:55 +08:00
int dlen;
2015-12-06 16:56:21 +08:00
} IO_CTX;
2020-01-03 15:57:34 +08:00
static bool connect_server(acl::sslbase_conf* ssl_conf, IO_CTX* ctx, int id);
2015-12-06 16:56:21 +08:00
/**
* <EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
class client_io_callback : public acl::aio_open_callback
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param client {aio_socket_stream*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-01-03 15:57:34 +08:00
* @param ssl_conf {acl::sslbase_conf*} <EFBFBD>ǿ<EFBFBD>ʱָ<EFBFBD><EFBFBD> SSL <EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD>ʽ
2015-12-06 16:56:21 +08:00
* @param ctx {IO_CTX*}
* @param id {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
client_io_callback(acl::aio_socket_stream* client,
2020-01-03 15:57:34 +08:00
acl::sslbase_conf* ssl_conf, IO_CTX* ctx, int id)
: client_(client)
, ssl_conf_(ssl_conf)
, ctx_(ctx)
, nwrite_(0)
, nread_(0)
, id_(id)
2015-12-06 16:56:21 +08:00
{
2015-12-20 15:27:55 +08:00
dlen_ = ctx->dlen;
buff_ = (char*) malloc(dlen_);
memset(buff_, 'x', dlen_);
buff_[dlen_ - 1] = '\n';
buff_[dlen_ - 2] = '\r';
2015-12-06 16:56:21 +08:00
}
~client_io_callback()
{
std::cout << ">>>ID: " << id_
<< ", io_callback deleted now!" << std::endl;
2015-12-20 15:27:55 +08:00
free(buff_);
2015-12-06 16:56:21 +08:00
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ô˻ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param data {char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַ
* @param len {int<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
bool read_callback(char*, int len)
{
//printf(">>>>read len=%d\n", len);
2015-12-06 16:56:21 +08:00
nread_ += len;
ctx_->nread_total++;
2020-01-03 15:57:34 +08:00
if (nwrite_ < 100 || nwrite_ % 1000 == 0) {
2015-12-20 15:27:55 +08:00
char buf[256];
2015-12-06 16:56:21 +08:00
2015-12-20 15:27:55 +08:00
acl::safe_snprintf(buf, sizeof(buf),
"current len: %d, total_len: %d, nwrite: %d",
len, nread_, nwrite_);
acl::meter_time(__FILE__, __LINE__, buf);
}
write_line();
2015-12-06 16:56:21 +08:00
return true;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>ɹ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ô˻ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
bool write_callback()
{
ctx_->nwrite_total++;
nwrite_++;
return true;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ô˻ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
void close_callback()
{
2020-01-03 15:57:34 +08:00
if (!client_->is_opened()) {
2015-12-06 16:56:21 +08:00
std::cout << "Id: " << id_ << " connect "
<< ctx_->addr << " error: "
<< acl::last_serror();
// <20><><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD>˳<EFBFBD>
2020-01-03 15:57:34 +08:00
if (ctx_->nopen_total == 0) {
2015-12-06 16:56:21 +08:00
std::cout << ", first connect error, quit";
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˳<EFBFBD>״̬ */
2015-12-06 16:56:21 +08:00
client_->get_handle().stop();
}
std::cout << std::endl;
delete this;
return;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC>ص<EFBFBD><D8B5><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2015-12-06 16:56:21 +08:00
int nleft = client_->get_handle().length();
2020-01-03 15:57:34 +08:00
if (ctx_->nopen_total == ctx_->nopen_limit && nleft == 1) {
2015-12-06 16:56:21 +08:00
std::cout << "Id: " << id_ << " stop now! nstream: "
<< nleft << std::endl;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˳<EFBFBD>״̬ */
2015-12-06 16:56:21 +08:00
client_->get_handle().stop();
}
// <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>й¶
2015-12-06 16:56:21 +08:00
delete this;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
bool timeout_callback()
{
std::cout << "Connect " << ctx_->addr
<< " Timeout ..." << std::endl;
client_->close();
return false;
}
bool read_wakeup()
{
// ȡ<><C8A1>֮ǰͨ<C7B0><CDA8> setup_hook ע<><D7A2><EFBFBD><EFBFBD> SSL IO<49><4F><EFBFBD><EFBFBD>
2020-01-03 15:57:34 +08:00
acl::sslbase_io* hook = (acl::sslbase_io*) client_->get_hook();
2015-12-06 16:56:21 +08:00
2020-01-03 15:57:34 +08:00
if (hook == NULL) {
2015-12-06 16:56:21 +08:00
std::cout << "get hook error"<< std::endl;
return false;
}
printf(">>>%s: begin handshake\n", __FUNCTION__);
// <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD> SSL <20><><EFBFBD><EFBFBD>
2020-01-03 15:57:34 +08:00
if (!hook->handshake()) {
logger_error("%s: ssl handshake failed", __FUNCTION__);
2015-12-06 16:56:21 +08:00
return false;
}
printf(">>>%s: end handshake\r\n", __FUNCTION__);
// SSL <20><><EFBFBD>ֻ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ɣ<EFBFBD><C9A3>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴα<D9B4><CEB1><EFBFBD><EFBFBD><EFBFBD>
2020-01-03 15:57:34 +08:00
if (!hook->handshake_ok()) {
printf(">>>%s: not handshake ok\r\n", __FUNCTION__);
2015-12-06 16:56:21 +08:00
return true;
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
// <20><><EFBFBD><EFBFBD> SSL <20><><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
printf(">>>%s: ssl handshake ok\r\n", __FUNCTION__);
2015-12-06 16:56:21 +08:00
// <20><> reactor ģʽתΪ proactor ģʽ<C4A3><CABD><EFBFBD>Ӷ<EFBFBD>ȡ<EFBFBD><C8A1>
// read_wakeup <20>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
client_->disable_read();
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵Ķ<CBB5>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
return begin_run();
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
*/
bool open_callback()
{
// <20><><EFBFBD>ӳɹ<D3B3><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IO<49><4F>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
client_->add_read_callback(this);
client_->add_write_callback(this);
ctx_->nopen_total++;
acl::assert_(id_ > 0);
2020-01-03 15:57:34 +08:00
if (ctx_->nopen_total < ctx_->nopen_limit) {
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD>
2020-01-03 15:57:34 +08:00
if (!connect_server(ssl_conf_, ctx_, id_ + 1)) {
2015-12-06 16:56:21 +08:00
std::cout << "connect error!" << std::endl;
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
}
// <20><><EFBFBD><EFBFBD> SSL <20><>ʽ
2020-01-03 15:57:34 +08:00
if (ssl_conf_) {
2015-12-06 16:56:21 +08:00
return setup_ssl(*ssl_conf_);
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵Ķ<CBB5>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
2020-01-03 15:57:34 +08:00
else {
2015-12-06 16:56:21 +08:00
return begin_run();
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
}
private:
acl::aio_socket_stream* client_;
2020-01-03 15:57:34 +08:00
acl::sslbase_conf* ssl_conf_;
2015-12-06 16:56:21 +08:00
IO_CTX* ctx_;
int nwrite_;
int nread_;
int id_;
2015-12-20 15:27:55 +08:00
char *buff_;
int dlen_;
2015-12-06 16:56:21 +08:00
2020-01-03 15:57:34 +08:00
bool setup_ssl(acl::sslbase_conf& ssl_conf)
2015-12-06 16:56:21 +08:00
{
acl::sslbase_io* ssl = ssl_conf.create(true);
2015-12-06 16:56:21 +08:00
// <20><> SSL IO <20><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>
2020-01-03 15:57:34 +08:00
if (client_->setup_hook(ssl) == ssl) {
2015-12-06 16:56:21 +08:00
std::cout << "open ssl error!" << std::endl;
ssl->destroy();
return false;
}
// <20><>ʼ SSL <20><><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>
2020-01-03 15:57:34 +08:00
if (!ssl->handshake()) {
2015-12-06 16:56:21 +08:00
client_->remove_hook();
ssl->destroy();
return false;
}
if (ssl->handshake_ok()) {
printf("%s: ssl handshake ok\r\n", __FUNCTION__);
client_->disable_read();
return begin_run();
} else {
// <20><>ʼ<EFBFBD>첽 SSL <20><><EFBFBD>ֹ<EFBFBD><D6B9>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɶ<EFBFBD><C9B6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// read_wakeup
client_->read_wait(10);
}
2015-12-06 16:56:21 +08:00
return true;
}
bool begin_run(void)
{
2015-12-20 15:27:55 +08:00
write_line();
2015-12-06 16:56:21 +08:00
// <20><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2015-12-20 15:27:55 +08:00
//client_->read();
client_->gets(10, false);
2015-12-06 16:56:21 +08:00
return true;
}
2015-12-20 15:27:55 +08:00
void write_line(void)
{
// <20><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-20 15:27:55 +08:00
client_->write(buff_, dlen_);
2020-01-03 15:57:34 +08:00
if (nwrite_ >= ctx_->nwrite_limit) {
2015-12-20 15:27:55 +08:00
client_->close();
2020-01-03 15:57:34 +08:00
}
2015-12-20 15:27:55 +08:00
}
2015-12-06 16:56:21 +08:00
};
2020-01-03 15:57:34 +08:00
static bool connect_server(acl::sslbase_conf* ssl_conf, IO_CTX* ctx, int id)
2015-12-06 16:56:21 +08:00
{
// <20><>ʼ<EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>Զ<EFBFBD>̷<EFBFBD><CCB7><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
acl::aio_socket_stream* stream = acl::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;
2020-01-03 15:57:34 +08:00
if (id == 0) {
2015-12-06 16:56:21 +08:00
ctx->handle->stop();
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
return false;
}
acl_non_blocking(stream->sock_handle(), ACL_BLOCKING);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD><D3BA>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
client_io_callback* callback = new
client_io_callback(stream, ssl_conf, ctx, id);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳɹ<D3B3><C9B9>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
stream->add_open_callback(callback);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܺ<EFBFBD><DCBA>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
stream->add_close_callback(callback);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-12-06 16:56:21 +08:00
stream->add_timeout_callback(callback);
return true;
}
static void usage(const char* procname)
{
2015-12-20 15:27:55 +08:00
printf("usage: %s -h[help] -l server_addr \r\n"
" -d path_to_polarssl\r\n"
2015-12-20 15:27:55 +08:00
" -c nconnect\r\n"
" -n io_max\r\n"
" -k[use kernel event: epoll/kqueue/devpoll\r\n"
" -I check_fds_inter\r\n"
" -M delay_ms\r\n"
" -t connect_timeout\r\n"
" -S[use_ssl]\r\n"
" -L data_len[default: 8193]\n", procname);
2015-12-06 16:56:21 +08:00
}
int main(int argc, char* argv[])
{
2020-01-03 15:57:34 +08:00
bool use_kernel = false, use_ssl = false;
2015-12-06 16:56:21 +08:00
IO_CTX ctx;
acl::string libpath;
2020-01-03 15:57:34 +08:00
acl::sslbase_conf* ssl_conf = NULL;
2015-12-20 15:27:55 +08:00
int ch;
int check_fds_inter = 10, delay_ms = 100;
2015-12-06 16:56:21 +08:00
memset(&ctx, 0, sizeof(ctx));
ctx.connect_timeout = 5;
ctx.nopen_limit = 1;
ctx.id_begin = 1;
ctx.nwrite_limit = 1;
2015-12-20 15:27:55 +08:00
ctx.dlen = 8193;
2015-12-06 16:56:21 +08:00
acl::safe_snprintf(ctx.addr, sizeof(ctx.addr), "127.0.0.1:9800");
2022-08-22 22:03:38 +08:00
acl::log::stdout_open(true);
2020-01-03 15:57:34 +08:00
while ((ch = getopt(argc, argv, "hd:c:n:kl:t:SL:I:M:")) > 0) {
switch (ch) {
2015-12-06 16:56:21 +08:00
case 'c':
ctx.nopen_limit = atoi(optarg);
2020-01-03 15:57:34 +08:00
if (ctx.nopen_limit <= 0) {
2015-12-06 16:56:21 +08:00
ctx.nopen_limit = 10;
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
break;
case 'd':
libpath = optarg;
break;
2015-12-06 16:56:21 +08:00
case 'n':
ctx.nwrite_limit = atoi(optarg);
2020-01-03 15:57:34 +08:00
if (ctx.nwrite_limit <= 0) {
2015-12-06 16:56:21 +08:00
ctx.nwrite_limit = 10;
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
break;
case 'h':
usage(argv[0]);
return 0;
case 'k':
use_kernel = true;
break;
case 'l':
2020-01-03 15:57:34 +08:00
acl::safe_snprintf(ctx.addr, sizeof(ctx.addr), "%s", optarg);
2015-12-06 16:56:21 +08:00
break;
case 't':
ctx.connect_timeout = atoi(optarg);
break;
case 'S':
2020-01-03 15:57:34 +08:00
use_ssl = true;
2015-12-20 15:27:55 +08:00
break;
case 'L':
ctx.dlen = atoi(optarg);
break;
case 'I':
check_fds_inter = atoi(optarg);
break;
case 'M':
delay_ms = atoi(optarg);
break;
2015-12-06 16:56:21 +08:00
default:
break;
}
}
acl::meter_time(__FUNCTION__, __LINE__, "-----BEGIN-----");
acl::acl_cpp_init();
acl::log::stdout_open(true);
acl::aio_handle handle(use_kernel ? acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
2015-12-20 15:27:55 +08:00
handle.set_check_inter(check_fds_inter);
int delay_sec = delay_ms / 1000;
int delay_usec = (delay_ms - delay_sec * 1000) * 1000;
handle.set_delay_sec(delay_sec);
handle.set_delay_usec(delay_usec);
printf(">>>delay_sec: %d, delay_usec: %d, check_fds_inter: %d\r\n",
delay_sec, delay_usec, check_fds_inter);
printf("Enter any key to continue ...\r\n");
getchar();
2020-01-03 15:57:34 +08:00
if (use_ssl) {
if (libpath.find("mbedtls") != NULL) {
const std::vector<acl::string>& libs = libpath.split2(";");
if (libs.size() != 3) {
printf("invalid libpath=%s\r\n", libpath.c_str());
return 1;
}
acl::mbedtls_conf::set_libpath(libs[0], libs[1], libs[2]);
2020-01-03 15:57:34 +08:00
if (acl::mbedtls_conf::load()) {
ssl_conf = new acl::mbedtls_conf(false);
} else {
printf("load %s error\r\n", libpath.c_str());
}
} else if (libpath.find("polarssl") != NULL) {
acl::polarssl_conf::set_libpath(libpath);
if (acl::polarssl_conf::load()) {
ssl_conf = new acl::polarssl_conf;
} else {
printf("load %s error\r\n", libpath.c_str());
}
} else if (libpath.find("libssl") != NULL) {
2022-08-22 22:03:38 +08:00
const std::vector<acl::string>& libs = libpath.split2(";, \t");
if (libs.size() != 2) {
printf("invalid libpath=%s\r\n", libpath.c_str());
return 1;
}
acl::openssl_conf::set_libpath(libs[0], libs[1]);
if (acl::openssl_conf::load()) {
ssl_conf = new acl::openssl_conf(false);
} else {
printf("load %s error\r\n", libpath.c_str());
}
2020-01-03 15:57:34 +08:00
}
}
2015-12-06 16:56:21 +08:00
ctx.handle = &handle;
2020-01-03 15:57:34 +08:00
if (!connect_server(ssl_conf, &ctx, ctx.id_begin)) {
2015-12-06 16:56:21 +08:00
std::cout << "enter any key to exit." << std::endl;
getchar();
return 1;
}
std::cout << "Connect " << ctx.addr << " ..." << std::endl;
2015-12-20 15:27:55 +08:00
struct timeval begin;
gettimeofday(&begin, NULL);
2020-01-03 15:57:34 +08:00
while (true) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ټ<EFBFBD><D9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>˳<EFBFBD>
2020-01-03 15:57:34 +08:00
if (!handle.check()) {
2015-12-06 16:56:21 +08:00
break;
2020-01-03 15:57:34 +08:00
}
2015-12-06 16:56:21 +08:00
}
2015-12-20 15:27:55 +08:00
struct timeval end;
gettimeofday(&end, NULL);
2015-12-06 16:56:21 +08:00
2015-12-20 15:27:55 +08:00
double spent = util::stamp_sub(&end, &begin);
2015-12-06 16:56:21 +08:00
2015-12-20 15:27:55 +08:00
printf("total open: %d, total write: %d, total read: %d,"
" spent: %.2f ms, speed: %.2f\r\n",
ctx.nopen_total, ctx.nwrite_total, ctx.nread_total,
spent, (ctx.nread_total * 1000) / (spent > 1 ? spent : 1));
2015-12-06 16:56:21 +08:00
delete ssl_conf;
return 0;
}