acl/lib_acl_cpp/samples/winaio/AioServer.cpp

205 lines
4.1 KiB
C++
Raw Normal View History

#include "StdAfx.h"
2014-11-19 00:25:21 +08:00
#include <iostream>
#include "lib_acl.h"
#include "acl_cpp/stream/aio_handle.hpp"
#include "acl_cpp/stream/aio_listen_stream.hpp"
#include "acl_cpp/stream/aio_socket_stream.hpp"
#include "AioServer.h"
using namespace acl;
/**
* <EFBFBD>ӳٶ<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
class timer_reader: public aio_timer_reader
{
public:
timer_reader()
{
std::cout << "timer_reader init now" << std::endl;
}
~timer_reader()
{
}
// aio_timer_reader <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
void destroy()
{
std::cout << "timer_reader delete now" << std::endl;
delete this;
}
// <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void timer_callback(unsigned int id)
{
std::cout << "timer_reader: timer_callback now" << std::endl;
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
aio_timer_reader::timer_callback(id);
}
};
/**
* <EFBFBD>ӳ<EFBFBD>д<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
class timer_writer: public aio_timer_writer
{
public:
timer_writer()
{
std::cout << "timer_writer init now" << std::endl;
}
~timer_writer()
{
}
// aio_timer_reader <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
void destroy()
{
std::cout << "timer_writer delete now" << std::endl;
delete this;
}
// <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void timer_callback(unsigned int id)
{
std::cout << "timer_writer: timer_callback now" << std::endl;
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
aio_timer_writer::timer_callback(0);
}
};
CAcceptedClientCallback::CAcceptedClientCallback(aio_socket_stream* client)
: client_(client)
, i_(0)
{
}
CAcceptedClientCallback::~CAcceptedClientCallback()
{
std::cout << "delete io_callback now ..." << std::endl;
}
bool CAcceptedClientCallback::read_callback(char* data, int len)
{
i_++;
if (i_ < 10)
std::cout << ">>gets(i:" << i_ << "): " << data;
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̿ͻ<CCBF><CDBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>֮
2014-11-19 00:25:21 +08:00
if (strncasecmp(data, "quit", 4) == 0)
{
client_->format("Bye!\r\n");
client_->close();
return (true);
}
// <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>
2014-11-19 00:25:21 +08:00
else if (strncasecmp(data, "stop", 4) == 0)
{
client_->format("Stop now!\r\n");
client_->close(); // <20>ر<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><ECB2BD>
2014-11-19 00:25:21 +08:00
// ֪ͨ<CDA8><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client_->get_handle().stop();
return (true);
}
int delay = 0;
if (strncasecmp(data, "write_delay", strlen("write_delay")) == 0)
{
// <20>ӳ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
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();
client_->write(data, len, delay, timer);
client_->gets(10, false);
return (true);
}
}
else if (strncasecmp(data, "read_delay", strlen("read_delay")) == 0)
{
// <20>ӳٶ<D3B3><D9B6><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
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();
client_->gets(10, false, delay, timer);
return (true);
}
}
// <20><>Զ<EFBFBD>̿ͻ<CCBF><CDBB>˻<EFBFBD>д<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client_->write(data, len);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client_->gets(10, false);
return (true);
}
bool CAcceptedClientCallback::write_callback()
{
return (true);
}
void CAcceptedClientCallback::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>й¶
2014-11-19 00:25:21 +08:00
delete this;
}
bool CAcceptedClientCallback::timeout_callback()
{
std::cout << "Timeout ..." << std::endl;
return (true);
}
///////////////////////////////////////////////////////////////////////////////
CServerCallback::CServerCallback()
{
}
CServerCallback::~CServerCallback()
{
}
bool CServerCallback::accept_callback(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>
2014-11-19 00:25:21 +08:00
CAcceptedClientCallback* callback = new CAcceptedClientCallback(client);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ķ<EFBFBD><C4B6>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_read_callback(callback);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_write_callback(callback);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_close_callback(callback);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_timeout_callback(callback);
// <20><><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->gets(10, false);
return (true);
}