acl/lib_acl_cpp/include/acl_cpp/stream/aio_istream.hpp

223 lines
7.6 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "aio_handle.hpp"
#include "aio_timer_callback.hpp"
#include "aio_stream.hpp"
2014-11-19 00:25:21 +08:00
namespace acl
{
class aio_istream;
/**
* aio_timer_callback (see aio_handle.hpp)
* (aio_istream)
* ( aio_handle )
* ( timer_callback
* )( destroy )
* aio_timer_reader
* destroy方法 destroy
* destroy
* aio_timer_reader destroy-- delete this
* )
2014-11-19 00:25:21 +08:00
*
*/
class ACL_CPP_API aio_timer_reader : public aio_timer_callback
{
public:
2019-07-07 11:00:12 +08:00
aio_timer_reader(void) {}
2014-11-19 00:25:21 +08:00
/**
* aio_istream
2014-11-19 00:25:21 +08:00
*/
2019-07-07 11:00:12 +08:00
virtual void destroy(void)
2014-11-19 00:25:21 +08:00
{
delete this;
}
protected:
2019-07-07 11:00:12 +08:00
virtual ~aio_timer_reader(void) {}
2014-11-19 00:25:21 +08:00
/**
* aio_timer_callback
2014-11-19 00:25:21 +08:00
*/
virtual void timer_callback(unsigned int id);
private:
// 允许 aio_istream 可以直接修改本类的私有成员变量
2014-11-19 00:25:21 +08:00
friend class aio_istream;
aio_istream* in_;
//int read_delayed_;
2014-11-19 00:25:21 +08:00
bool delay_gets_;
int delay_timeout_;
bool delay_nonl_;
int delay_count_;
};
/**
* close
*
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API aio_istream : virtual public aio_stream
{
public:
/**
*
* @param handle {aio_handle*}
2014-11-19 00:25:21 +08:00
*/
aio_istream(aio_handle* handle);
/**
* hook /
* @param handle {aio_handle*}
* @param fd {int}
*/
#if defined(_WIN32) || defined(_WIN64)
aio_istream(aio_handle* handle, SOCKET fd);
#else
aio_istream(aio_handle* handle, int fd);
#endif
2014-11-19 00:25:21 +08:00
/**
*
* 使
* @param callback {aio_callback*} aio_callback
* read_callback
2014-11-19 00:25:21 +08:00
*/
void add_read_callback(aio_callback* callback);
/**
*
* @param callback {aio_read_callback*}
*
* @return {int}
2014-11-19 00:25:21 +08:00
*/
/**
*
* @param callback {aio_callback*} aio_callback
*
* @return {int}
2014-11-19 00:25:21 +08:00
*/
int del_read_callback(aio_callback* callback = NULL);
/**
*
*
* @param callback {aio_callback*} aio_callback
*
* @return {int}
2014-11-19 00:25:21 +08:00
*/
int disable_read_callback(aio_callback* callback = NULL);
/**
*
* @param callback {aio_callback*} aio_callback
*
* @return {int}
2014-11-19 00:25:21 +08:00
*/
int enable_read_callback(aio_callback* callback = NULL);
/**
*
*
* @param timeout {int} () 0
*
* @param nonl {bool}
* @param delay {int64}
* 0
* ()
* @param callback {aio_timer_reader*}
* delay > 0
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
void gets(int timeout = 0, bool nonl = true,
__int64 delay = 0, aio_timer_reader* callback = NULL);
#else
void gets(int timeout = 0, bool nonl = true,
long long int delay = 0, aio_timer_reader* callback = NULL);
#endif
/**
*
*
* @param count {int} 0
*
* @param timeout {int} () 0
*
* @param delay {int64}
* 0
* ()
* @param callback {aio_timer_reader*}
*
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
void read(int count = 0, int timeout = 0,
__int64 delay = 0, aio_timer_reader* callback = NULL);
#else
void read(int count = 0, int timeout = 0,
long long int delay = 0, aio_timer_reader* callback = NULL);
#endif
/**
*
*
* @param timeout {int} () 0
2014-11-19 00:25:21 +08:00
*/
void read_wait(int timeout = 0);
/**
*
* (
* )
2014-11-19 00:25:21 +08:00
*/
2019-07-07 11:00:12 +08:00
void disable_read(void);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
* @param onoff {bool}
*/
void keep_read(bool onoff);
/**
*
2014-11-19 00:25:21 +08:00
* @return {bool}
*/
2019-07-07 11:00:12 +08:00
bool keep_read(void) const;
2014-11-19 00:25:21 +08:00
/**
* 0
2014-11-19 00:25:21 +08:00
* @param int {max}
* @return {aio_istream&}
*/
aio_istream& set_buf_max(int max);
/**
*
* @return {int} <= 0
2014-11-19 00:25:21 +08:00
*/
int get_buf_max(void) const;
protected:
2019-07-07 11:00:12 +08:00
virtual ~aio_istream(void);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
*/
2019-07-07 11:00:12 +08:00
virtual void destroy(void);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
*/
2019-07-07 11:00:12 +08:00
void hook_read(void);
2014-11-19 00:25:21 +08:00
private:
friend class aio_timer_reader;
aio_timer_reader* timer_reader_;
std::list<AIO_CALLBACK*> read_callbacks_;
bool read_hooked_;
static int read_callback(ACL_ASTREAM*, void*, char*, int);
static int read_wakeup(ACL_ASTREAM* stream, void* ctx);
};
} // namespace acl