acl/lib_acl_cpp/samples/aio/aio_echo/main.cpp

185 lines
4.4 KiB
C++
Raw Normal View History

#include <iostream>
2014-11-19 00:25:21 +08:00
#include <assert.h>
#include "lib_acl.h"
#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"
/**
*
2014-11-19 00:25:21 +08:00
*/
class io_callback : public acl::aio_callback
2014-11-19 00:25:21 +08:00
{
public:
io_callback(acl::aio_socket_stream* client)
: client_(client)
2014-11-19 00:25:21 +08:00
{
}
2019-06-16 20:10:40 +08:00
private:
~io_callback(void)
2014-11-19 00:25:21 +08:00
{
std::cout << "delete io_callback now ..." << std::endl;
}
2019-06-16 20:10:40 +08:00
public:
2014-11-19 00:25:21 +08:00
/**
*
* @param data {char*}
* @param len {int}
* @return {bool} true
2014-11-19 00:25:21 +08:00
*/
bool read_callback(char* data, int len)
{
// 向远程客户端回写收到的数据
client_->write(data, len);
2014-11-19 00:25:21 +08:00
return true;
}
/**
*
* @return {bool} true
2014-11-19 00:25:21 +08:00
*/
2019-06-16 20:10:40 +08:00
bool write_callback(void)
2014-11-19 00:25:21 +08:00
{
return true;
}
/**
*
2014-11-19 00:25:21 +08:00
*/
2019-06-16 20:10:40 +08:00
void close_callback(void)
2014-11-19 00:25:21 +08:00
{
// 必须在此处删除该动态分配的回调类对象以防止内存泄露
2014-11-19 00:25:21 +08:00
delete this;
}
/**
*
* @return {bool} true
2014-11-19 00:25:21 +08:00
*/
2019-06-16 20:10:40 +08:00
bool timeout_callback(void)
2014-11-19 00:25:21 +08:00
{
std::cout << "Timeout ..." << std::endl;
return true;
}
private:
acl::aio_socket_stream* client_;
2014-11-19 00:25:21 +08:00
};
/**
*
2014-11-19 00:25:21 +08:00
*/
class io_accept_callback : public acl::aio_accept_callback
2014-11-19 00:25:21 +08:00
{
public:
2019-06-16 20:10:40 +08:00
io_accept_callback(void) {}
~io_accept_callback(void)
2014-11-19 00:25:21 +08:00
{
printf(">>io_accept_callback over!\n");
}
/**
*
* @param client {aio_socket_stream*}
* @return {bool} true
2014-11-19 00:25:21 +08:00
*/
bool accept_callback(acl::aio_socket_stream* client)
2014-11-19 00:25:21 +08:00
{
// 创建异步客户端流的回调对象并与该异步流进行绑定
2014-11-19 00:25:21 +08:00
io_callback* callback = new io_callback(client);
// 注册异步流的读回调过程
2014-11-19 00:25:21 +08:00
client->add_read_callback(callback);
// 注册异步流的写回调过程
2014-11-19 00:25:21 +08:00
client->add_write_callback(callback);
// 注册异步流的关闭回调过程
2014-11-19 00:25:21 +08:00
client->add_close_callback(callback);
// 注册异步流的超时回调过程
2014-11-19 00:25:21 +08:00
client->add_timeout_callback(callback);
// 从异步流读数据
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)
{
printf("usage: %s -h[help]\r\n"
" -s listen_addr\r\n"
2019-06-16 20:10:40 +08:00
" -k[use kernel event: epoll/iocp/kqueue/devpool]\n",
procname);
2014-11-19 00:25:21 +08:00
}
int main(int argc, char* argv[])
{
bool use_kernel = false;
acl::string addr("127.0.0.1:9001");
2014-11-19 00:25:21 +08:00
int ch;
while ((ch = getopt(argc, argv, "hks:")) > 0) {
switch (ch) {
2014-11-19 00:25:21 +08:00
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
2014-11-19 00:25:21 +08:00
case 'k':
use_kernel = true;
break;
default:
break;
}
}
// 初始化ACL库(尤其是在WIN32下一定要调用此函数在UNIX平台下可不调用)
acl::acl_cpp_init();
// 构建异步引擎类对象
acl::aio_handle handle(use_kernel ? acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
2014-11-19 00:25:21 +08:00
// 创建监听异步流
acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle);
2014-11-19 00:25:21 +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: 为了保证能关闭监听流,应在此处再 check 一下
2014-11-19 00:25:21 +08:00
handle.check();
getchar();
return 1;
}
// 创建回调类对象,当有新连接到达时自动调用此类对象的回调过程
2014-11-19 00:25:21 +08:00
io_accept_callback callback;
sstream->add_accept_callback(&callback);
std::cout << "Listen: " << addr.c_str() << " ok!" << std::endl;
2014-11-19 00:25:21 +08:00
while (true) {
// 如果返回 false 则表示不再继续,需要退出
2019-06-16 20:10:40 +08:00
if (!handle.check()) {
2014-11-19 00:25:21 +08:00
std::cout << "aio_server stop now ..." << std::endl;
break;
}
}
// 关闭监听流并释放流对象
2014-11-19 00:25:21 +08:00
sstream->close();
// XXX: 为了保证能关闭监听流,应在此处再 check 一下
2014-11-19 00:25:21 +08:00
handle.check();
return 0;
}