acl/lib_acl_cpp/include/acl_cpp/ipc/ipc_client.hpp

185 lines
4.0 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
#include <list>
2017-06-02 14:47:24 +08:00
#include "../stream/aio_socket_stream.hpp"
2014-11-19 00:25:21 +08:00
namespace acl {
typedef struct MSG_HDR
{
int nMsg;
int dlen;
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
__int64 magic;
#else
long long int magic;
#endif
} MSG_HDR;
typedef enum
{
IO_WAIT_HDR,
IO_WAIT_DAT
} io_status;
class aio_handle;
class ipc_adapter;
class aio_socket_stream;
class socket_stream;
/**
* IP消息类
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API ipc_client : private aio_open_callback
{
public:
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
ipc_client(__int64 magic = -1);
#else
ipc_client(long long int magic = -1);
#endif
virtual ~ipc_client();
/**
*
2014-11-19 00:25:21 +08:00
*/
virtual void destroy()
{
delete this;
}
/**
* open
2014-11-19 00:25:21 +08:00
*/
virtual void on_open() {}
/**
*
2014-11-19 00:25:21 +08:00
*/
virtual void on_close() {}
/**
*
* @param nMsg {int}
* @param data {void*}
* @param dlen {int}
2014-11-19 00:25:21 +08:00
*/
virtual void on_message(int nMsg, void* data, int dlen);
/**
*
* @param handle {aio_handle*}
* @param addr {const char*} :
* IP:PORT(_WIN32/UNIX)unix_path (UNIX)
* @param timeout {int}
2014-11-19 00:25:21 +08:00
*/
bool open(aio_handle* handle, const char* addr, int timeout);
/**
* ipc_client
* @param client {aio_socket_stream*}
2014-11-19 00:25:21 +08:00
*/
void open(aio_socket_stream* client);
/**
*
* @param addr {const char*} :
* IP:PORT(_WIN32/UNIX)unix_path (UNIX)
* @param timeout {int}
2014-11-19 00:25:21 +08:00
*/
bool open(const char* addr, int timeout);
/**
* ipc_client
* @param client {socket_stream*}
2014-11-19 00:25:21 +08:00
*/
void open(socket_stream* client);
/**
* IPC
2014-11-19 00:25:21 +08:00
*/
void wait();
/**
*
2014-11-19 00:25:21 +08:00
*/
void close();
/**
*
2014-11-19 00:25:21 +08:00
* @return {bool}
*/
bool active() const;
/**
*
* @param nMsg {int}
2014-11-19 00:25:21 +08:00
*/
void append_message(int nMsg);
/**
*
* @param nMsg {int}
2014-11-19 00:25:21 +08:00
*/
void delete_message(int nMsg);
/**
*
* @param nMsg {int}
* @param data {const void*}
* @param dlen {int}
2014-11-19 00:25:21 +08:00
*/
void send_message(int nMsg, const void* data, int dlen);
/**
*
2014-11-19 00:25:21 +08:00
* @return {aio_socket_stream*}
*/
aio_socket_stream* get_async_stream() const;
/**
*
2014-11-19 00:25:21 +08:00
*/
aio_handle& get_handle() const;
/**
*
2014-11-19 00:25:21 +08:00
* @return {socket_stream*}
*/
socket_stream* get_sync_stream() const;
protected:
/**
*
* @param nMsg {int} ID
* @param data {void*}
* @param dlen {int}
2014-11-19 00:25:21 +08:00
*/
void trigger(int nMsg, void* data, int dlen);
private:
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
__int64 magic_;
#else
long long int magic_;
#endif
char* addr_;
std::list<int> messages_;
//aio_handle* handle_;
2014-11-19 00:25:21 +08:00
aio_socket_stream* async_stream_;
socket_stream* sync_stream_;
socket_stream* sync_stream_inner_;
bool closing_;
io_status status_;
MSG_HDR hdr_;
// 基类虚函数
2014-11-19 00:25:21 +08:00
virtual bool read_callback(char* data, int len);
virtual bool write_callback();
virtual void close_callback();
virtual bool timeout_callback();
virtual bool open_callback();
};
} // namespace acl