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

162 lines
4.2 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "../stdlib/string.hpp"
2014-11-19 00:25:21 +08:00
#include <map>
struct ACL_VSTREAM;
namespace acl {
class stream_hook;
2014-11-19 00:25:21 +08:00
class ACL_CPP_API stream
{
public:
stream(void);
virtual ~stream(void) = 0;
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} true: <EFBFBD>رճɹ<EFBFBD>; false: <EFBFBD>ر<EFBFBD>ʧ<EFBFBD><EFBFBD>
*/
bool close(void);
/**
* <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} true: <EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>; false: <EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
bool eof(void) const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> eof_ <EFBFBD><EFBFBD>־λ<EFBFBD><EFBFBD>Ϊ false
*/
void clear_eof(void);
/**
* <EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
* @return {bool} true: <EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>; false: <EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
bool opened(void) const;
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ACL_VSTREAM <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {ACL_VSTREAM*}
*/
ACL_VSTREAM* get_vstream() const;
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> ACL_VSTREAM <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>İ󶨹<EFBFBD>ϵ<EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD> ACL_VSTREAM <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ACL_VSTREAM<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> ACL_VSTREAM <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD>ӹܸ<EFBFBD> ACL_VSTREAM <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> close/open <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>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {ACL_VSTREAM} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD> ACL_VSTREAM <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
ACL_VSTREAM* unbind();
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>İ󶨶<EFBFBD><EFBFBD><EFBFBD>
* @param ctx {void*}
* @param key {const char* } <EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD> ctx <EFBFBD>ļ<EFBFBD>
* @param replace {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> KEY <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD> replace Ϊ false <EFBFBD><EFBFBD> key <EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>򷵻<EFBFBD> false
*/
bool set_ctx(void* ctx, const char* key = NULL, bool replace = true);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󶨵Ķ<EFBFBD><EFBFBD><EFBFBD>
* @param key {const char* key} <EFBFBD>ǿ<EFBFBD>ʱʹ<EFBFBD>ø<EFBFBD> key <EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> ctx <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD>ȱʡ<EFBFBD><EFBFBD> ctx <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {void*}
*/
void* get_ctx(const char* key = NULL) const;
/**
* ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>а󶨵Ķ<EFBFBD><EFBFBD><EFBFBD>
* @param key {const char*} <EFBFBD>ǿ<EFBFBD>ʱɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> key <EFBFBD><EFBFBD> ctx <EFBFBD><EFBFBD><EFBFBD>󣬷<EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
* ȱʡ<EFBFBD><EFBFBD> ctx <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {void*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󲻴<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD>󷵻ظö<EFBFBD><EFBFBD><EFBFBD>
*/
void* del_ctx(const char* key = NULL);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD>д<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>
* @param n {int} <EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>λ: <EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ > 0 <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><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
void set_rw_timeout(int n);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD>д<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>
* @return {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD>д<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)
*/
int get_rw_timeout(void) const;
/**
* ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> hook->open <EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD>֮ǰע<EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD>
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FBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {stream_hook*}
*/
stream_hook* get_hook() const;
/**
* ɾ<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 {stream_hook*}
*/
stream_hook* remove_hook();
protected:
/**
* <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>
*/
void open_stream(bool is_file = false);
/**
* <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>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD><EFBFBD>
*/
void reopen_stream(bool is_file = false);
protected:
stream_hook* hook_;
ACL_VSTREAM *stream_;
bool eof_;
bool opened_;
void* default_ctx_;
std::map<string, void*> ctx_table_;
private:
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
static int read_hook(SOCKET fd, void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
static int send_hook(SOCKET fd, const void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
static int fread_hook(HANDLE fd, void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
static int fsend_hook(HANDLE fd, const void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
#else
static int read_hook(int fd, void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
static int send_hook(int fd, const void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
static int fread_hook(int fd, void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
static int fsend_hook(int fd, const void *buf, size_t len,
int timeout, ACL_VSTREAM* stream, void *ctx);
#endif
};
} // namespace acl