在C++中没有接口的概念,使用纯虚函数来实现接口的定义。
下面分别是基础接口,网络接口,连接回调接口,接收回调接口的定义。
IUnknownEx.h
#ifndef IUNKNOWN_EX_HEAD_FILE
#define IUNKNOWN_EX_HEAD_FILE
#include "../GlobalDef.h"
//
#define VER_IUnknownEx 0x0101
static const string IID_IUnknownEx="0x92d87e91,0x5a5b,0x4a45,0x8c,0xfd,0x38,0x2f,0xe6,0xb3,0x2b,0xb";
//基础接口
class IUnknownEx
{
public:
//是否有效
virtual bool IsValid()=NULL;
//释放对象
virtual bool Release()=NULL;
//接口查询
virtual void * QueryInterface(const string Guid, DWORD dwQueryVer)=NULL;
};
#endif
#ifndef ICLIENT_SOCKET_HEAD_FILE
#define ICLIENT_SOCKET_HEAD_FILE
#include "IUnknownEx.h"
//
//公共宏定义
//代理类型
#define PROXY_NONE 0 //没有代理
#define PROXY_SOCKS4 1 //SOCKS4 代理
#define PROXY_SOCKS5 2 //SOCKS5 代理
#define PROXY_HTTP 3 //HTTP 代理
//
//枚举定义
//连接状态定义
enum enSocketState
{
SocketState_NoConnect, //没有连接
SocketState_Connecting, //正在连接
SocketState_Connected, //成功连接
};
//
//结构定义
//代理信息结构
struct tagProxyInfo
{
BYTE cbProxyType; //代理类型
WORD wProxyPort; //代理端口
string strProxyName; //代理用户名
string strProxyPass; //代理密码
string strProxyServer; //代理地址
string strHttpDomain; //代理域名
};
//
class ClientSocketDelegate;
class ClientSocketRecvDelegate;
#define VER_IClientSocket 0x0101
static const string IID_IClientSocket="0x8b0e3918,0x5139,0x48da,0xbf,0x40,0xba,0x95,0xad,0xce,0x59,0x64";
class IClientSocket : public IUnknownEx
{
public:
//取消代理
virtual void CancelProxyServer()=0;
//代理服务器
virtual void SetProxyServer(tagProxyInfo & ProxyInfo)=0;
//设置接口
virtual bool SetSocketDelegate(ClientSocketDelegate * pIUnknownEx)=0;
//获取接口
virtual void * GetSocketDelegate(const string Guid, DWORD dwQueryVer)=0;
//添加接口
virtual bool AddSocketRecvDelegate(ClientSocketRecvDelegate * pIUnknownEx)=0;
//添加接口
virtual bool RemoveSocketRecvDelegate(ClientSocketRecvDelegate * pIUnknownEx)=0;
//获取发送间隔
virtual DWORD GetLastSendTick()=0;
//获取接收间隔
virtual DWORD GetLastRecvTick()=0;
//获取发送数目
virtual DWORD GetSendPacketCount()=0;
//获取接收数目
virtual DWORD GetRecvPacketCount()=0;
//获取状态
virtual enSocketState GetConnectState()=0;
//连接服务器
virtual bool Connect(DWORD dwServerIP, WORD wPort)=0;
//连接服务器
virtual bool Connect(const char* szServerIP, WORD wPort)=0;
//发送函数
virtual bool SendData(WORD wMainCmdID, WORD wSubCmdID)=0;
//发送函数
virtual bool SendData(WORD wMainCmdID, WORD wSubCmdID, void * pData, WORD wDataSize)=0;
//关闭连接
virtual bool CloseSocket(bool bNotify)=0;
};
//
#define VER_ClientSocketProtocol 0x0101
static const string IID_ClientSocketProtocol="0x55fcf8aa,0x526a,0x44c2,0x9a,0xd1,0x4,0xe,0x7,0xd6,0x8a,0x2a";
//网络钩子接口
class ClientSocketDelegate
{
public:
//网络连接消息
virtual bool OnSocketConnect(int iErrorCode, string pszErrorDesc, IClientSocket * pIClientSocket)=0;
//网络读取消息
virtual bool OnSocketRead(CMD_Command Command, void * pBuffer, WORD wDataSize, IClientSocket * pIClientSocket)=0;
//网络关闭消息
virtual bool OnSocketClose(IClientSocket * pIClientSocke, bool bCloseByServer)=0;
};
//
//网络钩子接口
class ClientSocketRecvDelegate
{
public:
//网络读取消息
virtual bool OnSocketRead(CMD_Command Command, void * pBuffer, WORD wDataSize, IClientSocket * pIClientSocket)=0;
};
#endif
cocos2d-x是从object-c版本衍生过来的,一些概念比如delegate,protocol,对象的内存计数,自动释放池等。所以在类的命名上修改为cocos2d类似的命名:ClientSocketDelegate,ClientSocketRecvDelegate
QQ群:239759131 cocos 技术交流 欢迎您