2019-07-28 10:31:56 +08:00
|
|
|
|
#include <iostream>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
#include <assert.h>
|
2019-07-30 09:44:46 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
#include <io.h>
|
|
|
|
|
#endif
|
2014-11-30 21:15:35 +08:00
|
|
|
|
#include "lib_acl.h"
|
|
|
|
|
#include "acl_cpp/acl_cpp_init.hpp"
|
2015-11-27 23:07:43 +08:00
|
|
|
|
#include "acl_cpp/stdlib/log.hpp"
|
2014-11-30 21:15:35 +08:00
|
|
|
|
#include "acl_cpp/stdlib/string.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/util.hpp"
|
|
|
|
|
#include "acl_cpp/stream/polarssl_conf.hpp"
|
|
|
|
|
#include "acl_cpp/stream/polarssl_io.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_handle.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_socket_stream.hpp"
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
# ifndef snprintf
|
|
|
|
|
# define snprintf _snprintf
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static acl::polarssl_conf* __ssl_conf;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
acl::aio_handle* handle;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
char addr[64];
|
2014-11-30 21:15:35 +08:00
|
|
|
|
int connect_timeout;
|
|
|
|
|
int read_timeout;
|
|
|
|
|
int nopen_limit;
|
|
|
|
|
int nopen_total;
|
|
|
|
|
int nwrite_limit;
|
|
|
|
|
int nwrite_total;
|
|
|
|
|
int nread_total;
|
|
|
|
|
int id_begin;
|
|
|
|
|
bool debug;
|
|
|
|
|
} IO_CTX;
|
|
|
|
|
|
|
|
|
|
static bool connect_server(IO_CTX* ctx, int id);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
|
|
|
|
class client_io_callback : public acl::aio_open_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
* @param ctx {IO_CTX*}
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* @param client {aio_socket_stream*} <EFBFBD>첽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param id {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
|
|
|
|
client_io_callback(IO_CTX* ctx, acl::aio_socket_stream* client, int id)
|
2019-06-16 20:10:40 +08:00
|
|
|
|
: client_(client)
|
|
|
|
|
, ctx_(ctx)
|
|
|
|
|
, nwrite_(0)
|
|
|
|
|
, id_(id)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
~client_io_callback(void)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << ">>>ID: " << id_ << ", io_callback deleted now!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
|
|
|
|
bool read_callback(char* data, int len)
|
|
|
|
|
{
|
|
|
|
|
(void) len;
|
|
|
|
|
|
|
|
|
|
ctx_->nread_total++;
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (ctx_->debug) {
|
|
|
|
|
if (nwrite_ < 10) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "gets(" << nwrite_ << "): " << data;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
} else if (nwrite_ % 2000 == 0) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << ">>ID: " << id_ << ", I: "
|
|
|
|
|
<< nwrite_ << "; "<< data;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
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>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (acl::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-30 21:15:35 +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-30 21:15:35 +08:00
|
|
|
|
client_->close();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (nwrite_ >= ctx_->nwrite_limit) {
|
|
|
|
|
if (ctx_->debug) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "ID: " << id_
|
|
|
|
|
<< ", nwrite: " << nwrite_
|
|
|
|
|
<< ", nwrite_limit: " << ctx_->nwrite_limit
|
|
|
|
|
<< ", quiting ..." << std::endl;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-30 21:15:35 +08:00
|
|
|
|
|
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-30 21:15:35 +08:00
|
|
|
|
client_->format("quit\r\n");
|
|
|
|
|
client_->close();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
} else {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
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-30 21:15:35 +08:00
|
|
|
|
//client_->format("hello world: %d\n", nwrite_);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool write_callback(void)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
{
|
|
|
|
|
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-30 21:15:35 +08:00
|
|
|
|
client_->gets(ctx_->read_timeout, false);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +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>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
void close_callback(void)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
{
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!client_->is_opened()) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
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>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (ctx_->nopen_total == 0) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
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-30 21:15:35 +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>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC>ص<EFBFBD><D8B5>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
2014-11-30 21:15:35 +08:00
|
|
|
|
int nleft = client_->get_handle().length();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (ctx_->nopen_total == ctx_->nopen_limit && nleft == 1) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "Id: " << id_ << " stop now! nstream: "
|
|
|
|
|
<< nleft << std::endl;
|
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-30 21:15:35 +08:00
|
|
|
|
client_->get_handle().stop();
|
|
|
|
|
}
|
|
|
|
|
|
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-30 21:15:35 +08:00
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool timeout_callback(void)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "Connect " << ctx_->addr << " Timeout ..." << std::endl;
|
|
|
|
|
client_->close();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return false;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool read_wakeup(void)
|
2015-11-27 23:07:43 +08:00
|
|
|
|
{
|
|
|
|
|
acl::polarssl_io* hook = (acl::polarssl_io*) client_->get_hook();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (hook == NULL) {
|
2015-11-27 23:07:43 +08:00
|
|
|
|
std::cout << "get hook error"<< std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD> SSL <20><><EFBFBD><EFBFBD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!hook->handshake()) {
|
2015-11-27 23:07:43 +08:00
|
|
|
|
logger_error("ssl handshake failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> SSL <20><><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (hook->handshake_ok()) {
|
2015-11-27 23:07:43 +08:00
|
|
|
|
printf("ssl handshake ok\r\n");
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><> reactor ģʽתΪ proactor ģʽ<C4A3><CABD><EFBFBD>Ӷ<EFBFBD>ȡ<EFBFBD><C8A1>
|
|
|
|
|
// read_wakeup <20>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2015-11-27 23:07:43 +08:00
|
|
|
|
client_->disable_read();
|
|
|
|
|
|
|
|
|
|
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>
|
2015-11-27 23:07:43 +08:00
|
|
|
|
client_->gets(ctx_->read_timeout, false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// SSL <20><><EFBFBD>ֻ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ɣ<EFBFBD><C9A3>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴα<D9B4><CEB1><EFBFBD><EFBFBD><EFBFBD>
|
2015-11-27 23:07:43 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-30 21:15:35 +08:00
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool open_callback(void)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
{
|
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-30 21:15:35 +08:00
|
|
|
|
client_->add_read_callback(this);
|
|
|
|
|
client_->add_write_callback(this);
|
|
|
|
|
ctx_->nopen_total++;
|
|
|
|
|
|
|
|
|
|
acl::assert_(id_ > 0);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
|
|
|
|
|
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>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (connect_server(ctx_, id_ + 1) == false) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "connect error!" << std::endl;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> SSL <20><>ʽ
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (__ssl_conf) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
acl::polarssl_io* ssl =
|
|
|
|
|
new acl::polarssl_io(*__ssl_conf, false, true);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (client_->setup_hook(ssl) == ssl) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "open ssl error!" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!ssl->handshake()) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
client_->remove_hook();
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-27 23:07:43 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> SSL <20><><EFBFBD>ֽ<D6BD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
client_->read_wait(ctx_->read_timeout);
|
2015-11-27 23:07:43 +08:00
|
|
|
|
return true;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
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-30 21:15:35 +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-30 21:15:35 +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>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::aio_socket_stream* client_;
|
|
|
|
|
IO_CTX* ctx_;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
int nwrite_;
|
|
|
|
|
int id_;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool 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>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_socket_stream* stream = acl::aio_socket_stream::open
|
|
|
|
|
(ctx->handle, ctx->addr, ctx->connect_timeout);
|
|
|
|
|
if (stream == NULL) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "connect " << ctx->addr << " error!" << std::endl;
|
|
|
|
|
std::cout << "stoping ..." << std::endl;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (id == 0) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
ctx->handle->stop();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
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-30 21:15:35 +08:00
|
|
|
|
client_io_callback* callback = new client_io_callback(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-30 21:15:35 +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-30 21:15:35 +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-30 21:15:35 +08:00
|
|
|
|
stream->add_timeout_callback(callback);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
2019-06-16 20:10:40 +08:00
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
|
|
|
|
" -l server_addr\r\n"
|
|
|
|
|
" -c nconnect\r\n"
|
|
|
|
|
" -n io_max\r\n"
|
|
|
|
|
" -k[use kernel event: epoll/kqueue/devpoll\r\n"
|
|
|
|
|
" -t connect_timeout\r\n"
|
|
|
|
|
" -d[debug]\r\n"
|
|
|
|
|
" -S libpolarssl.so [if set the ssl will be used]\n",
|
|
|
|
|
procname);
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
bool use_kernel = false;
|
|
|
|
|
int ch;
|
|
|
|
|
IO_CTX ctx;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::string libssl_path;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
|
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
|
ctx.connect_timeout = 5;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
ctx.nopen_limit = 10;
|
|
|
|
|
ctx.id_begin = 1;
|
|
|
|
|
ctx.nwrite_limit = 10;
|
|
|
|
|
ctx.debug = false;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
snprintf(ctx.addr, sizeof(ctx.addr), "127.0.0.1:9001");
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "hc:n:kl:dt:S:")) > 0) {
|
|
|
|
|
switch (ch) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
case 'c':
|
|
|
|
|
ctx.nopen_limit = atoi(optarg);
|
|
|
|
|
if (ctx.nopen_limit <= 0)
|
|
|
|
|
ctx.nopen_limit = 10;
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
ctx.nwrite_limit = atoi(optarg);
|
|
|
|
|
if (ctx.nwrite_limit <= 0)
|
|
|
|
|
ctx.nwrite_limit = 10;
|
|
|
|
|
break;
|
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return (0);
|
|
|
|
|
case 'k':
|
|
|
|
|
use_kernel = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
snprintf(ctx.addr, sizeof(ctx.addr), "%s", optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
|
|
|
|
ctx.debug = true;
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
ctx.connect_timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'S':
|
2019-06-16 20:10:40 +08:00
|
|
|
|
libssl_path = optarg;
|
|
|
|
|
break;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::meter_time(__FUNCTION__, __LINE__, "-----BEGIN-----");
|
|
|
|
|
acl::acl_cpp_init();
|
2015-11-27 23:07:43 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
2014-11-30 21:15:35 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
if (!libssl_path.empty()
|
2019-07-30 09:44:46 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
&& !_access(libssl_path.c_str(), 00)) {
|
|
|
|
|
#else
|
2019-07-28 10:31:56 +08:00
|
|
|
|
&& !access(libssl_path.c_str(), R_OK)) {
|
2019-07-30 09:44:46 +08:00
|
|
|
|
#endif
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> libpolarssl.so <20><>ȫ·<C8AB><C2B7>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::polarssl_conf::set_libpath(libssl_path);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> libpolarssl.so <20><>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::polarssl_conf::load();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB> SSL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
__ssl_conf = new acl::polarssl_conf();
|
|
|
|
|
} else {
|
|
|
|
|
__ssl_conf = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-30 21:15:35 +08:00
|
|
|
|
acl::aio_handle handle(use_kernel ? acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
|
|
|
|
|
ctx.handle = &handle;
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!connect_server(&ctx, ctx.id_begin)) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
std::cout << "enter any key to exit." << std::endl;
|
|
|
|
|
getchar();
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << "Connect " << ctx.addr << " ..." << std::endl;
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while (true) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ټ<EFBFBD><D9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>˳<EFBFBD>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (handle.check() == false) {
|
2014-11-30 21:15:35 +08:00
|
|
|
|
break;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-30 21:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::string buf;
|
|
|
|
|
|
|
|
|
|
buf << "total open: " << ctx.nopen_total
|
|
|
|
|
<< ", total write: " << ctx.nwrite_total
|
|
|
|
|
<< ", total read: " << ctx.nread_total;
|
|
|
|
|
|
|
|
|
|
acl::meter_time(__FUNCTION__, __LINE__, buf.c_str());
|
|
|
|
|
|
|
|
|
|
delete __ssl_conf;
|
|
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
|