2014-11-19 00:25:21 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include "lib_acl.h"
|
2019-06-14 18:32:12 +08:00
|
|
|
|
#include "acl_cpp/lib_acl.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/stream/aio_istream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_listen_stream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_socket_stream.hpp"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>첽<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2019-06-14 18:32:12 +08:00
|
|
|
|
class io_callback : public acl::aio_callback
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-14 18:32:12 +08:00
|
|
|
|
io_callback(acl::aio_socket_stream* client)
|
|
|
|
|
: client_(client)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~io_callback()
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
// <20><>Զ<EFBFBD>̿ͻ<CCBF><CDBB>˻<EFBFBD>д<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-14 18:32:12 +08:00
|
|
|
|
client_->write(data, len);
|
2014-11-19 00:25: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>ص<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()
|
|
|
|
|
{
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
// <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()
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Timeout ..." << std::endl;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2019-06-14 18:32:12 +08:00
|
|
|
|
acl::aio_socket_stream* client_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>첽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2019-06-14 18:32:12 +08:00
|
|
|
|
class io_accept_callback : public acl::aio_accept_callback
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
io_accept_callback() {}
|
|
|
|
|
~io_accept_callback()
|
|
|
|
|
{
|
|
|
|
|
printf(">>io_accept_callback over!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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>
|
|
|
|
|
*/
|
2019-06-14 18:32:12 +08:00
|
|
|
|
bool accept_callback(acl::aio_socket_stream* client)
|
2014-11-19 00:25:21 +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>
|
|
|
|
|
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><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-14 18:32:12 +08:00
|
|
|
|
int count = 0, timeout = 10;
|
|
|
|
|
client->read(count, timeout);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
2019-06-14 18:32:12 +08:00
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
|
|
|
|
" -s listen_addr\r\n"
|
|
|
|
|
" -k[use kernel event: epoll/iocp/kqueue/devpool]\n"
|
|
|
|
|
, procname);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
2019-06-14 18:32:12 +08:00
|
|
|
|
bool use_kernel = false;
|
|
|
|
|
acl::string addr("127.0.0.1:9001");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
int ch;
|
|
|
|
|
|
2019-06-14 18:32:12 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "hks:")) > 0) {
|
|
|
|
|
switch (ch) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return 0;
|
2019-06-14 18:32:12 +08:00
|
|
|
|
case 's':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'k':
|
|
|
|
|
use_kernel = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 18:32:12 +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>)
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-06-14 18:32:12 +08:00
|
|
|
|
acl::aio_handle handle(use_kernel ? acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD>
|
2019-06-14 18:32:12 +08:00
|
|
|
|
acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ĵ<EFBFBD>ַ
|
2019-06-14 18:32:12 +08:00
|
|
|
|
if (!sstream->open(addr)) {
|
|
|
|
|
std::cout << "open " << addr.c_str() << " error!" << std::endl;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
sstream->close();
|
|
|
|
|
// XXX: Ϊ<>˱<EFBFBD>֤<EFBFBD>ܹرռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ڴ˴<DAB4><CBB4><EFBFBD> check һ<><D2BB>
|
|
|
|
|
handle.check();
|
|
|
|
|
|
|
|
|
|
getchar();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <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;
|
|
|
|
|
sstream->add_accept_callback(&callback);
|
2019-06-14 18:32:12 +08:00
|
|
|
|
std::cout << "Listen: " << addr.c_str() << " ok!" << std::endl;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-14 18:32:12 +08:00
|
|
|
|
while (true) {
|
2014-11-19 00:25:21 +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-14 18:32:12 +08:00
|
|
|
|
if (handle.check() == false) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|