2019-07-28 10:31:56 +08:00
|
|
|
|
#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 "acl_cpp/stdlib/util.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/snprintf.hpp"
|
2015-12-06 16:56:21 +08:00
|
|
|
|
#include "acl_cpp/acl_cpp_init.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/log.hpp"
|
|
|
|
|
#include "acl_cpp/stream/polarssl_conf.hpp"
|
|
|
|
|
#include "acl_cpp/stream/polarssl_io.hpp"
|
2020-01-03 15:57:34 +08:00
|
|
|
|
#include "acl_cpp/stream/mbedtls_conf.hpp"
|
|
|
|
|
#include "acl_cpp/stream/mbedtls_io.hpp"
|
2015-12-06 16:56:21 +08:00
|
|
|
|
#include "acl_cpp/stream/aio_handle.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_istream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_listen_stream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_socket_stream.hpp"
|
|
|
|
|
|
|
|
|
|
static int __max = 0;
|
|
|
|
|
static int __timeout = 0;
|
|
|
|
|
static int __max_used = 0;
|
|
|
|
|
static int __cur_used = 0;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// SSL ģʽ<C4A3>µ<EFBFBD> SSL <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
static acl::sslbase_conf* __ssl_conf;
|
2015-12-06 16:56:21 +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>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
*/
|
|
|
|
|
class io_callback : public acl::aio_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
io_callback(acl::aio_socket_stream* client)
|
2020-01-03 15:57:34 +08:00
|
|
|
|
: client_(client)
|
|
|
|
|
, nread_cnt_(0)
|
|
|
|
|
, nread_(0)
|
2015-12-06 16:56:21 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~io_callback()
|
|
|
|
|
{
|
|
|
|
|
printf("delete io_callback now ...\r\n");
|
|
|
|
|
__cur_used++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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> aio_istream ʵ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>
|
|
|
|
|
* gets/read <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>ݶ<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>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
* @param len {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>(> 0)
|
|
|
|
|
* @return {bool} <EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false ֪ͨ<EFBFBD>첽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رո<EFBFBD><EFBFBD>첽<EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
*/
|
|
|
|
|
bool read_wakeup()
|
|
|
|
|
{
|
2020-01-03 15:57:34 +08:00
|
|
|
|
acl::sslbase_io* hook = (acl::sslbase_io*) client_->get_hook();
|
|
|
|
|
if (hook == NULL) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><> SSL ģʽ<C4A3><CABD><EFBFBD>첽<EFBFBD><ECB2BD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
2015-12-20 15:27:55 +08:00
|
|
|
|
//client_->read(__timeout);
|
|
|
|
|
client_->gets(__timeout, false);
|
2015-12-06 16:56:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD> SSL <20><><EFBFBD><EFBFBD>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (!hook->handshake()) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
printf("ssl handshake failed\r\n");
|
|
|
|
|
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>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (hook->handshake_ok()) {
|
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-12-06 16:56:21 +08:00
|
|
|
|
client_->disable_read();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>첽<EFBFBD><ECB2BD>ȡ<EFBFBD><C8A1><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD> read_callback
|
2015-12-20 15:27:55 +08:00
|
|
|
|
//client_->read(__timeout);
|
|
|
|
|
client_->gets(__timeout, false);
|
2015-12-06 16:56:21 +08:00
|
|
|
|
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-12-06 16:56:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
|
* @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>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <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* data, int len)
|
|
|
|
|
{
|
2015-12-20 15:27:55 +08:00
|
|
|
|
nread_cnt_++;
|
2015-12-06 16:56:21 +08:00
|
|
|
|
nread_ += len;
|
|
|
|
|
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (nread_cnt_ <= 100 || nread_cnt_ % 1000 == 0) {
|
2015-12-20 15:27:55 +08:00
|
|
|
|
char buf[256];
|
|
|
|
|
acl::safe_snprintf(buf, sizeof(buf),
|
|
|
|
|
"read len: %d, total read: %d, nread_cnt: %d",
|
|
|
|
|
len, nread_, nread_cnt_);
|
|
|
|
|
acl::meter_time(__FILE__, __LINE__, buf);
|
|
|
|
|
}
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̿ͻ<CCBF><CDBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>֮
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (strncasecmp(data, "quit", 4) == 0) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client_->format("Bye!\r\n");
|
|
|
|
|
client_->close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̿ͻ<CCBF><CDBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>رգ<D8B1><D5A3><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>첽<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (strncasecmp(data, "stop", 4) == 0) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client_->format("Stop now!\r\n");
|
2019-07-28 10:31:56 +08:00
|
|
|
|
client_->close(); // <20>ر<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>첽<EFBFBD><ECB2BD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ֪ͨ<CDA8>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client_->get_handle().stop();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>Զ<EFBFBD>̿ͻ<CCBF><CDBB>˻<EFBFBD>д<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
|
|
|
|
client_->write(data, len);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <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()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
*/
|
|
|
|
|
void close_callback()
|
|
|
|
|
{
|
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>й¶
|
2015-12-06 16:56:21 +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>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <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 << "Timeout, delete it ..." << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::aio_socket_stream* client_;
|
2015-12-20 15:27:55 +08:00
|
|
|
|
int nread_cnt_;
|
2015-12-06 16:56:21 +08:00
|
|
|
|
int nread_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
*/
|
|
|
|
|
class io_accept_callback : public acl::aio_accept_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
io_accept_callback() {}
|
|
|
|
|
~io_accept_callback()
|
|
|
|
|
{
|
|
|
|
|
printf(">>io_accept_callback over!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
|
* @param client {aio_socket_stream*} <EFBFBD>첽<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <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 accept_callback(acl::aio_socket_stream* client)
|
|
|
|
|
{
|
|
|
|
|
//acl_non_blocking(client->sock_handle(), ACL_BLOCKING);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>а<EFBFBD><D0B0><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
io_callback* callback = new io_callback(client);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD>Ķ<EFBFBD><C4B6>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client->add_read_callback(callback);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client->add_write_callback(callback);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client->add_close_callback(callback);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client->add_timeout_callback(callback);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><DEB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
2015-12-06 16:56:21 +08:00
|
|
|
|
if (__max > 0)
|
|
|
|
|
client->set_buf_max(__max);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// SSL ģʽ<C4A3>£<EFBFBD><C2A3>ȴ<EFBFBD><C8B4>ͻ<EFBFBD><CDBB>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (__ssl_conf != NULL) {
|
|
|
|
|
acl::sslbase_io* ssl = __ssl_conf->open(true, true);
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
2020-01-03 15:57:34 +08:00
|
|
|
|
// ע<><D7A2> SSL IO <20><><EFBFBD>̵Ĺ<CCB5><C4B9><EFBFBD>
|
|
|
|
|
if (client->setup_hook(ssl) == ssl) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
std::cout << "setup_hook error" << std::endl;
|
|
|
|
|
ssl->destroy();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>Դ<EFBFBD><D4B4><EFBFBD> read_wakeup <20>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>̣<EFBFBD>
|
|
|
|
|
// SSL <20><><EFBFBD>ֹ<EFBFBD><D6B9>̽<EFBFBD><CCBD><EFBFBD> read_wakeup <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
client->read_wait(__timeout);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><> SSL ģʽ<C4A3>£<EFBFBD><C2A3><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
else {
|
2015-12-20 15:27:55 +08:00
|
|
|
|
//client->read(__timeout);
|
|
|
|
|
client->gets(__timeout, false);
|
|
|
|
|
}
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
2020-01-03 15:57:34 +08:00
|
|
|
|
" -d path_to_polarssl\r\n"
|
|
|
|
|
" -l server_addr[ip:port, default: 127.0.0.1:9800]\r\n"
|
|
|
|
|
" -L line_max_length\r\n"
|
|
|
|
|
" -t timeout\r\n"
|
|
|
|
|
" -n conn_used_limit\r\n"
|
|
|
|
|
" -k[use kernel event: epoll/iocp/kqueue/devpool]\r\n"
|
|
|
|
|
" -M delay_ms\r\n"
|
|
|
|
|
" -I check_fds_inter\r\n"
|
|
|
|
|
" -K ssl_key_file -C ssl_cert_file [in SSL mode]\r\n",
|
2015-12-06 16:56:21 +08:00
|
|
|
|
procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA>еĸ<D0B5>Чģʽ
|
2015-12-06 16:56:21 +08:00
|
|
|
|
bool use_kernel = false;
|
2017-08-04 22:37:00 +08:00
|
|
|
|
acl::string key_file, cert_file, libpath("../libpolarssl.so");
|
2015-12-06 16:56:21 +08:00
|
|
|
|
acl::string addr("127.0.0.1:9800");
|
2015-12-20 15:27:55 +08:00
|
|
|
|
int ch, delay_ms = 100, check_fds_inter = 10;
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
2020-01-03 15:57:34 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "ld::hkL:t:K:C:n:M:I:")) > 0) {
|
|
|
|
|
switch (ch) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
2017-08-04 22:37:00 +08:00
|
|
|
|
case 'd':
|
|
|
|
|
libpath = optarg;
|
|
|
|
|
break;
|
2015-12-06 16:56:21 +08:00
|
|
|
|
case 'l':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'k':
|
|
|
|
|
use_kernel = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'L':
|
|
|
|
|
__max = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
__timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'K':
|
|
|
|
|
key_file = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'C':
|
|
|
|
|
cert_file = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
__max_used = atoi(optarg);
|
|
|
|
|
break;
|
2015-12-20 15:27:55 +08:00
|
|
|
|
case 'M':
|
|
|
|
|
delay_ms = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'I':
|
|
|
|
|
check_fds_inter = atoi(optarg);
|
|
|
|
|
break;
|
2015-12-06 16:56:21 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (libpath.find("mbedtls") != NULL) {
|
|
|
|
|
acl::mbedtls_conf::set_libpath(libpath);
|
|
|
|
|
|
|
|
|
|
if (acl::mbedtls_conf::load()) {
|
|
|
|
|
__ssl_conf = new acl::mbedtls_conf(true);
|
|
|
|
|
printf("load %s ok\r\n", libpath.c_str());
|
|
|
|
|
} else {
|
|
|
|
|
key_file.clear();
|
|
|
|
|
cert_file.clear();
|
|
|
|
|
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();
|
|
|
|
|
printf("load %s ok\r\n", libpath.c_str());
|
|
|
|
|
} else {
|
|
|
|
|
key_file.clear();
|
|
|
|
|
cert_file.clear();
|
|
|
|
|
printf("load %s error\r\n", libpath.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>˽Կ<CBBD><D4BF>֤<EFBFBD>鶼<EFBFBD><E9B6BC><EFBFBD><EFBFBD>ʱ<EFBFBD>Ų<EFBFBD><C5B2><EFBFBD> SSL ͨ<>ŷ<EFBFBD>ʽ
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (!key_file.empty() && !cert_file.empty() && __ssl_conf) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> SSL <20>Ự<EFBFBD><E1BBB0><EFBFBD>湦<EFBFBD><E6B9A6>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
__ssl_conf->enable_cache(true);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (!__ssl_conf->add_cert(cert_file.c_str())) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
delete __ssl_conf;
|
|
|
|
|
__ssl_conf = NULL;
|
|
|
|
|
std::cout << "add_cert error: " << cert_file.c_str()
|
|
|
|
|
<< std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ӱ<EFBFBD><D3B1>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>Կ
|
2020-01-03 15:57:34 +08:00
|
|
|
|
else if (!__ssl_conf->set_key(key_file.c_str())) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
delete __ssl_conf;
|
|
|
|
|
__ssl_conf = NULL;
|
|
|
|
|
std::cout << "set_key error: " << key_file.c_str()
|
|
|
|
|
<< std::endl;
|
2020-01-03 15:57:34 +08:00
|
|
|
|
} else {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
std::cout << "Load cert&key OK!" << std::endl;
|
2020-01-03 15:57:34 +08:00
|
|
|
|
}
|
2015-12-06 16:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (__ssl_conf) {
|
2017-08-04 22:37:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
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);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>ACL<43><4C>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>WIN32<33><32>һ<EFBFBD><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UNIXƽ̨<C6BD>¿ɲ<C2BF><C9B2><EFBFBD><EFBFBD><EFBFBD>)
|
2015-12-06 16:56:21 +08:00
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ĵ<EFBFBD>ַ
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (!sstream->open(addr.c_str())) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
std::cout << "open " << addr.c_str() << " error!" << std::endl;
|
|
|
|
|
sstream->close();
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// XXX: Ϊ<>˱<EFBFBD>֤<EFBFBD>ܹرռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ڴ˴<DAB4><CBB4><EFBFBD> check һ<><D2BB>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
handle.check();
|
|
|
|
|
|
|
|
|
|
getchar();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
io_accept_callback callback;
|
|
|
|
|
sstream->add_accept_callback(&callback);
|
|
|
|
|
std::cout << "Listen: " << addr.c_str() << " ok!" << std::endl;
|
|
|
|
|
|
2020-01-03 15:57:34 +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>
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (!handle.check()) {
|
2015-12-06 16:56:21 +08:00
|
|
|
|
std::cout << "aio_server stop now ..." << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 15:57:34 +08:00
|
|
|
|
if (__max_used > 0 && __cur_used >= __max_used) {
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
sstream->close();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// XXX: Ϊ<>˱<EFBFBD>֤<EFBFBD>ܹرռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ڴ˴<DAB4><CBB4><EFBFBD> check һ<><D2BB>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
handle.check();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ɾ<><C9BE> acl::polarssl_conf <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>
|
2015-12-06 16:56:21 +08:00
|
|
|
|
delete __ssl_conf;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|