2020-09-30 22:29:40 +08:00
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "lib_acl.h" // just for getopt on Windows
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "acl_cpp/stdlib/util.hpp"
|
|
|
|
|
#include "acl_cpp/acl_cpp_init.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/log.hpp"
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>ӳٶ<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
class timer_reader: public acl::aio_timer_reader
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_reader(int delay)
|
|
|
|
|
{
|
|
|
|
|
delay_ = delay;
|
|
|
|
|
std::cout << "timer_reader init, delay: " << delay << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
~timer_reader(void) {}
|
|
|
|
|
|
|
|
|
|
// aio_timer_reader <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy <20><><EFBFBD><EFBFBD>
|
|
|
|
|
// @override
|
|
|
|
|
void destroy(void)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_reader delete, delay: " << delay_ << std::endl;
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// @override
|
|
|
|
|
void timer_callback(unsigned int id)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_reader(" << id
|
|
|
|
|
<< "): timer_callback, delay: " << delay_ << std::endl;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
aio_timer_reader::timer_callback(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int delay_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>ӳ<EFBFBD>д<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
class timer_writer: public acl::aio_timer_writer
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_writer(int delay)
|
|
|
|
|
{
|
|
|
|
|
delay_ = delay;
|
|
|
|
|
std::cout << "timer_writer init, delay: " << delay << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
~timer_writer(void) {}
|
|
|
|
|
|
|
|
|
|
// aio_timer_reader <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy <20><><EFBFBD><EFBFBD>
|
|
|
|
|
// @override
|
|
|
|
|
void destroy(void)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_writer delete, delay: " << delay_ << std::endl;
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// @override
|
|
|
|
|
void timer_callback(unsigned int id)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_writer(" << id << "): timer_callback, delay: "
|
|
|
|
|
<< delay_ << std::endl;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
acl::aio_timer_writer::timer_callback(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int delay_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>첽<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
class io_callback : public acl::aio_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
io_callback(acl::aio_socket_stream* client)
|
|
|
|
|
: client_(client)
|
|
|
|
|
, i_(0) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
~io_callback(void)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "delete io_callback now ..." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ʵ<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>
|
|
|
|
|
*/
|
|
|
|
|
bool read_callback(char* data, int len)
|
|
|
|
|
{
|
|
|
|
|
i_++;
|
|
|
|
|
if (i_ < 5) {
|
|
|
|
|
std::cout << ">>gets(i:" << i_ << "): "
|
|
|
|
|
<< data << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̿ͻ<CCBF><CDBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>֮
|
|
|
|
|
if (strncasecmp(data, "quit", 4) == 0) {
|
|
|
|
|
client_->format("Bye!\r\n");
|
|
|
|
|
client_->close();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <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>
|
|
|
|
|
else if (strncasecmp(data, "stop", 4) == 0) {
|
|
|
|
|
client_->format("Stop now!\r\n");
|
|
|
|
|
client_->close(); // <20>ر<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>첽<EFBFBD><ECB2BD>
|
|
|
|
|
|
|
|
|
|
// ֪ͨ<CDA8>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
client_->get_handle().stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>Զ<EFBFBD>̿ͻ<CCBF><CDBB>˻<EFBFBD>д<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
int delay = 0;
|
|
|
|
|
|
|
|
|
|
if (!strncasecmp(data, "write_delay", strlen("write_delay"))) {
|
|
|
|
|
// <20>ӳ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
const char* ptr = data + strlen("write_delay");
|
|
|
|
|
delay = atoi(ptr);
|
|
|
|
|
if (delay > 0) {
|
|
|
|
|
std::cout << ">> write delay " << delay
|
|
|
|
|
<< " second ..." << std::endl;
|
|
|
|
|
timer_writer* timer = new timer_writer(delay);
|
|
|
|
|
client_->write(data, len, delay * 1000000, timer);
|
|
|
|
|
client_->gets(10, false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else if (!strncasecmp(data, "read_delay", strlen("read_delay"))) {
|
|
|
|
|
// <20>ӳٶ<D3B3><D9B6><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
const char* ptr = data + strlen("read_delay");
|
|
|
|
|
delay = atoi(ptr);
|
|
|
|
|
if (delay > 0) {
|
|
|
|
|
client_->write(data, len);
|
|
|
|
|
std::cout << ">> read delay " << delay
|
|
|
|
|
<< " second ..." << std::endl;
|
|
|
|
|
timer_reader* timer = new timer_reader(delay);
|
|
|
|
|
client_->gets(10, false, delay * 1000000, timer);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client_->write(data, len);
|
|
|
|
|
//client_->gets(10, false);
|
|
|
|
|
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><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>
|
|
|
|
|
*/
|
|
|
|
|
bool write_callback(void)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ʵ<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>
|
|
|
|
|
*/
|
|
|
|
|
void close_callback(void)
|
|
|
|
|
{
|
|
|
|
|
// <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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ʵ<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>
|
|
|
|
|
*/
|
|
|
|
|
bool timeout_callback(void)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Timeout, delete it ..." << std::endl;
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::aio_socket_stream* client_;
|
|
|
|
|
int i_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>첽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
class io_accept_callback : public acl::aio_accept_callback
|
|
|
|
|
, public acl::aio_listen_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
io_accept_callback(void) {}
|
|
|
|
|
~io_accept_callback(void)
|
|
|
|
|
{
|
|
|
|
|
printf(">>io_accept_callback over!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> aio_accept_callback <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>
|
|
|
|
|
*/
|
|
|
|
|
bool accept_callback(acl::aio_socket_stream* client)
|
|
|
|
|
{
|
|
|
|
|
printf("proactor accept one\r\n");
|
|
|
|
|
return handle_client(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> aio_listen_callback <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 server {acl::aio_listen_stream&} <EFBFBD>첽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool listen_callback(acl::aio_listen_stream& server)
|
|
|
|
|
{
|
|
|
|
|
// reactor ģʽ<C4A3><CABD><EFBFBD><EFBFBD>Ҫ<EFBFBD>û<EFBFBD><C3BB>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD> accept <20><><EFBFBD><EFBFBD>
|
|
|
|
|
acl::aio_socket_stream* client = server.accept();
|
|
|
|
|
if (client == NULL) {
|
|
|
|
|
printf("accept error %s\r\n", acl::last_serror());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("reactor accept one\r\n");
|
|
|
|
|
return handle_client(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool handle_client(acl::aio_socket_stream* client)
|
|
|
|
|
{
|
|
|
|
|
// <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>
|
|
|
|
|
io_callback* callback = new io_callback(client);
|
|
|
|
|
|
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD>Ķ<EFBFBD><C4B6>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
client->add_read_callback(callback);
|
|
|
|
|
|
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
client->add_write_callback(callback);
|
|
|
|
|
|
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
client->add_close_callback(callback);
|
|
|
|
|
|
|
|
|
|
// ע<><D7A2><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
client->add_timeout_callback(callback);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><DEB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|
|
|
|
if (__max > 0) {
|
|
|
|
|
client->set_buf_max(__max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
client->gets(__timeout, false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class mytimer : public acl::aio_timer_callback {
|
|
|
|
|
public:
|
|
|
|
|
mytimer(long long delay) : id_(0), last_(time(NULL)), delay_(delay) {}
|
|
|
|
|
~mytimer(void) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void destroy(void) {
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @override
|
|
|
|
|
void timer_callback(unsigned int id) {
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
delay_ += 1000000;
|
|
|
|
|
this->set_task(id_, delay_);
|
|
|
|
|
|
|
|
|
|
printf("timer id=%u, delay=%ld, next delay=%lld\r\n",
|
|
|
|
|
id, (long) (now - last_), delay_);
|
|
|
|
|
|
|
|
|
|
last_ = now;
|
|
|
|
|
id_ = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
unsigned int id_;
|
|
|
|
|
time_t last_;
|
|
|
|
|
long long delay_;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-16 16:22:38 +08:00
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
|
2020-10-08 22:09:34 +08:00
|
|
|
|
static void aio_run(bool use_reactor, acl::aio_handle& handle,
|
2020-09-30 22:29:40 +08:00
|
|
|
|
acl::aio_listen_stream* sstream)
|
|
|
|
|
{
|
|
|
|
|
// <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>
|
|
|
|
|
io_accept_callback callback;
|
|
|
|
|
|
2023-01-16 16:22:38 +08:00
|
|
|
|
#if 0
|
|
|
|
|
struct epoll_event event;
|
|
|
|
|
int pfd = epoll_create(100);
|
|
|
|
|
int epfd = fcntl(pfd, F_DUPFD);
|
|
|
|
|
printf(">>>epfd=%d, pfd=%d\n", epfd, pfd);
|
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
|
event.data.ptr = sstream;
|
|
|
|
|
int ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sstream->sock_handle(), &event);
|
|
|
|
|
printf(">>>epoll add ret=%d\n", ret);
|
|
|
|
|
ret = epoll_ctl(epfd, EPOLL_CTL_DEL, sstream->sock_handle(), &event);
|
|
|
|
|
printf(">>>epoll del ret=%d\n", ret);
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-09-30 22:29:40 +08:00
|
|
|
|
if (use_reactor) {
|
|
|
|
|
sstream->add_listen_callback(&callback);
|
|
|
|
|
} else {
|
|
|
|
|
sstream->add_accept_callback(&callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("aio begin running ...\r\n");
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
if (!handle.check()) {
|
|
|
|
|
std::cout << "aio_server stop now ..." << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
sstream->close();
|
|
|
|
|
|
|
|
|
|
// XXX: Ϊ<>˱<EFBFBD>֤<EFBFBD>ܹرռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ڴ˴<DAB4><CBB4><EFBFBD> check һ<><D2BB>
|
|
|
|
|
handle.check();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 22:09:34 +08:00
|
|
|
|
static acl::aio_listen_stream* bind_addr(acl::aio_handle& handle,
|
|
|
|
|
const acl::string& addr)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD>
|
|
|
|
|
acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ĵ<EFBFBD>ַ
|
|
|
|
|
if (!sstream->open(addr.c_str())) {
|
|
|
|
|
std::cout << "open " << addr.c_str() << " error!" << std::endl;
|
|
|
|
|
sstream->close();
|
|
|
|
|
// XXX: Ϊ<>˱<EFBFBD>֤<EFBFBD>ܹرռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ڴ˴<DAB4><CBB4><EFBFBD> check һ<><D2BB>
|
|
|
|
|
handle.check();
|
|
|
|
|
|
|
|
|
|
getchar();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << "Listen: " << addr.c_str() << " ok!" << std::endl;
|
|
|
|
|
return sstream;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 22:29:40 +08:00
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
|
|
|
|
" -l ip:port\r\n"
|
|
|
|
|
" -L line_max_length\r\n"
|
|
|
|
|
" -t timeout\r\n"
|
|
|
|
|
" -r [use reactor mode other proactor mode, default: proactor mode]\r\n"
|
|
|
|
|
" -f [if use fiber mode]\r\n"
|
|
|
|
|
" -k[use kernel event: epoll/iocp/kqueue/devpool]\r\n",
|
|
|
|
|
procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
bool use_kernel = false, use_reactor = false, use_fiber = false;
|
|
|
|
|
int ch;
|
|
|
|
|
acl::string addr(":9001");
|
|
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "l:hkL:t:rf")) > 0) {
|
|
|
|
|
switch (ch) {
|
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
case 'l':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'k':
|
|
|
|
|
use_kernel = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'L':
|
|
|
|
|
__max = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
__timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
use_reactor = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'f':
|
|
|
|
|
use_fiber = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <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>)
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
|
2023-01-16 16:22:38 +08:00
|
|
|
|
acl::fiber::stdout_open(true);
|
2020-09-30 22:29:40 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (use_fiber) {
|
|
|
|
|
go[&] {
|
2020-10-08 22:09:34 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
acl::aio_handle handle(use_kernel ?
|
|
|
|
|
acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
|
|
|
|
|
|
|
|
|
|
long long delay = 1000000;
|
|
|
|
|
mytimer* timer = new mytimer(delay);
|
|
|
|
|
timer->keep_timer(true);
|
|
|
|
|
handle.set_timer(timer, delay);
|
|
|
|
|
|
|
|
|
|
acl::aio_listen_stream* sstream = bind_addr(handle, addr);
|
|
|
|
|
if (sstream) {
|
|
|
|
|
printf(">>>begin run use fiber mode<<<\r\n");
|
|
|
|
|
aio_run(use_reactor, std::ref(handle), sstream);
|
|
|
|
|
}
|
2020-09-30 22:29:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-08 22:09:34 +08:00
|
|
|
|
go[=] {
|
|
|
|
|
while (true) {
|
|
|
|
|
sleep(2);
|
|
|
|
|
printf("---wakeup---\r\n");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
acl::fiber::schedule();
|
|
|
|
|
} else {
|
|
|
|
|
acl::aio_handle handle(use_kernel ?
|
|
|
|
|
acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
|
|
|
|
|
|
|
|
|
|
long long delay = 1000000;
|
|
|
|
|
mytimer* timer = new mytimer(delay);
|
|
|
|
|
timer->keep_timer(true);
|
|
|
|
|
handle.set_timer(timer, delay);
|
|
|
|
|
|
|
|
|
|
acl::aio_listen_stream* sstream = bind_addr(handle, addr);
|
|
|
|
|
if (sstream) {
|
|
|
|
|
printf(">>>begin run not use fiber mode\r\n");
|
|
|
|
|
aio_run(use_reactor, std::ref(handle), sstream);
|
2020-09-30 22:29:40 +08:00
|
|
|
|
}
|
2020-10-08 22:09:34 +08:00
|
|
|
|
}
|
2020-09-30 22:29:40 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|