远程调用内核接口的封装类(RCKObjs)

Posted mqxnongmin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了远程调用内核接口的封装类(RCKObjs)相关的知识,希望对你有一定的参考价值。

RCK 包括 Application, Function, Connection, Command, Response 和 Fields 六
大类, 其主要功能例如以下:
??? a. Application 类主要负责 Function 的组织和 Response 的分发运行;
??? b. Function??? 类主要负责 Function 的定义及按各模式调用;
??? c. Connection? 类主要负责登录对端应用, Command 请求运行, Response 应答管理,
?????? 以及发送和接收数据等等;
??? d. Command???? 类主要负责函数參数传入, 以及返回值和返回字段值读取;
??? e. Response??? 类主要负责对端指定 Function 请求的运行处理, 如參数读取、返回
?????? 值编辑及提交;
??? f. Fields????? 类主要负责数据库字段值及定义集的组包和拆包。

?

封装类的头文件(RCKObjs.h)

?

// =======================================
// Unit   : RCKernel objects
// Version: 5.0.0.0 (build 2014.07.07)
// Author : Kyee Ye
// Email  : kyee_ye(at)126.com
// Copyright (C) Kyee workroom
// =======================================

#ifndef _RCKObjs_H_
#define _RCKObjs_H_

#include "KYLib.h"
#include "RCKernel.h"

// 使用 RCKernel 的命名空间
namespace RCKernel
{
   namespace __object__
   {

/* begin namespace */

class TRCAppObj;
class TRCFuncObj;
class TRCConnObj;
class TRCCmdObj;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 初始化/释放单元 */

// 初始化/释放 RCKObjs 单元(注: 内部有引用计数)
void        InitRCKObjs();
void        FreeRCKObjs(bool AForced = false);

// ID -> 对象
TRCCmdObj*  CmdID2Object(void* ACmdID);   // 命令ID -> 对象
TRCFuncObj* FuncID2Object(void* AFuncID); // 函数ID -> 对象

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 应用对象(TRCAppObj)的相关函数 */

// 创建/释放应用对象
TRCAppObj*  CreateAppObj(const char* AName, const char* APassword, long* ARetCode = NULL);
void        FreeAppObj(TRCAppObj* AppObj);

// 应用对象引用计数增 1
TRCAppObj*  IncRefAppObj(void* AppID);
bool        IncRefAppObj_(TRCAppObj* AppObj);

// 应用对象引用计数增/减 1(注: AppObj 对象必须存在)
void        IncRefAppObj(TRCAppObj* AppObj);
void        DecRefAppObj(TRCAppObj* AppObj);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 连接对象(TRCConnObj)的相关函数 */

// 创建/释放连接对象(注: AConnID 必须存在)
TRCConnObj* CreateConnObj(void* AConnID, bool ANeedFree, long* ARetCode = NULL);
void        FreeConnObj(TRCConnObj* AConnObj);

// 连接对象引用计数增 1
TRCConnObj* IncRefConnObj(void* AConnID);
bool        IncRefConnObj_(TRCConnObj* AConnObj);

// 连接对象引用计数增/减 1(注: AConnObj 对象必须存在)
void        IncRefConnObj(TRCConnObj* AConnObj);
void        DecRefConnObj(TRCConnObj* AConnObj);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 命令对象(TRCCmdObj)的相关函数 */

// 创建/释放命令对象(注: AConnObj 必须存在)
TRCCmdObj*  CreateCmdObj(void* AConnID = NULL);
TRCCmdObj*  CreateCmdObj(TRCConnObj* AConnObj);
void        FreeCmdObj(TRCCmdObj* ACmdObj);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCAppObj - RCK 应用类 */

class TRCAppObj
{
private:
   // 映射列表类型
   typedef TKYMapObjKey<void>    TObjList;

public:
   // 连接登录事件
   typedef void (TObject::*TDoConnLogin)(TRCAppObj* AppObj, void* AConnID, bool& AIsRefused);
   typedef struct
   {
      TDoConnLogin   Method;
      void*          Object;
   } TOnConnLogin;

   // 连接登出事件
   typedef void (TObject::*TDoConnLogout)(TRCAppObj* AppObj, void* AConnID);
   typedef struct
   {
      TDoConnLogout  Method;
      void*          Object;
   } TOnConnLogout;

   // 应答事件
   typedef void (TObject::*TDoRespEvent)(TRCAppObj* AppObj, void* AFuncID, void* AConnID);
   typedef struct
   {
      TDoRespEvent   Method;
      void*          Object;
   } TOnRespEvent;

   // 函数释放事件
   typedef void (TObject::*TDoFuncFree)(TRCAppObj* AppObj, TRCFuncObj* AFuncObj);
   typedef struct
   {
      TDoFuncFree    Method;
      void*          Object;
   } TOnFuncFree;

public:
   TRCAppObj();
   virtual ~TRCAppObj();

   // 属性
   void*          Data() const         { return FData; }
   KYString       Name() const         { return FName; }
   void*          AppID() const        { return FAppID; }
   long           State() const        { return RCKAppGetInt(FAppID, kaaState, NULL); }
   long           FuncCount() const    { return RCKAppGetInt(FAppID, kaaFuncCount, NULL); }
   long           ConnCount() const    { return RCKAppGetInt(FAppID, kaaConnCount, NULL); }
   long           MaxThreads() const   { return RCKAppGetInt(FAppID, kaaMaxThreads, NULL); }
   long           MaxConnCount() const { return RCKAppGetInt(FAppID, kaaMaxConnCount, NULL); }

   // 设置属性
   void           SetData(void* AData) { FData = AData; }
   long           SetName(const KYString& AName);
   long           SetPassword(const char* APassword, long ALength)
                  { return RCKAppSetStr(FAppID, kaaPassword, APassword, ALength); }
   long           SetPassword(const KYString& APassword)
                  { return SetPassword((char*)APassword, APassword.Length()); }
   long           SetMaxThreads(long ACount)
                  { return RCKAppSetInt(FAppID, kaaMaxThreads, ACount); }
   long           SetMaxConnCount(long ACount)
                  { return RCKAppSetInt(FAppID, kaaMaxConnCount, ACount); }

   // 读取应用的连接/函数
   void*          ConnID(long AIndex) const
                  { return RCKAppGetObj(FAppID, kaaBaseConnObj + AIndex, NULL); }
   void*          FuncID(long AIndex) const
                  { return RCKAppGetObj(FAppID, kaaBaseFuncObj + AIndex, NULL); }
   void*          FuncID(const char* AName) const
                  { return RCKAppFuncObj(FAppID, AName); }
   char*          FuncName(long AIndex) const
                  { return RCKAppGetStr(FAppID, kaaBaseFuncName + AIndex, NULL); }

   // 開始/结束函数定义
   long           BeginDefs()          { return RCKAppFuncBegin(FAppID); }
   long           EndDefs()            { return RCKAppFuncEnd(FAppID); }

   // 新建/删除/清除函数定义
   TRCFuncObj*    NewFuncObj(const char* AName, long ARetType,  long  ARetSize,
                                                long ACallMode, void* ACallback);
   bool           DeleteFuncObj(TRCFuncObj* AFuncObj);
   void           ClearFuncObjs();

   // 打开/关闭应用/强制注销应用的连接
   long           Open()               { return RCKAppOpen(FAppID); }
   long           Close()              { return RCKAppClose(FAppID); }
   long           Logout(void* AConnID){ return RCKAppLogout(FAppID, AConnID); }

   // 设置事件
   long           SetOnConnLogin(TDoConnLogin AMethod, void* AObject);
   long           SetOnConnLogout(TDoConnLogout AMethod, void* AObject);
   long           SetOnExecResp(TDoRespEvent AMethod, void* AObject);
   long           SetOnRespCancel(TDoRespEvent AMethod, void* AObject);
   long           SetOnRespConfirm(TDoRespEvent AMethod, void* AObject);

   // 事件
   TOnFuncFree    OnFuncFree;

protected:
   // 当前锁
   void           Lock() const         { FLock->Enter(); }
   void           Unlock() const       { FLock->Leave(); }

   // 对象次数增/减 1
   long           IncObjTimes()        { return InterlockedIncrement(&FObjTimes); }
   long           DecObjTimes()        { return InterlockedDecrement(&FObjTimes); }

private:
   // 运行初始化事件/清除函数列表
   void           DoInitEvent();
   void           DoClearFuncObjs();

   // 运行事件方法
   void           DoConnLogin(void* AConnID, bool& AIsRefused);
   void           DoConnLogout(void* AConnID);
   void           DoExecResp(void* AFuncID, void* AConnID);
   void           DoRespCancel(void* AFuncID, void* AConnID);
   void           DoRespConfirm(void* AFuncID, void* AConnID);

   // 事件方法
   void           DoIDDeletion(void* AFuncID);
   void           DoObjDeletion(TRCFuncObj* AFuncObj);

private:
   void*          FData;               // 自己定义数据
   TKYCritSect*   FLock;               // 当前锁
   KYString       FName;               // 应用名
   void*          FAppID;              // 应用 ID
   long           FObjTimes;           // 对象引用次数
   TObjList*      FFuncList;           // 函数列表

   // 事件
   TOnConnLogin   FOnConnLogin;        // 连接登录
   TOnConnLogout  FOnConnLogout;       // 连接登出
   TOnRespEvent   FOnExecResp;         // 运行应答
   TOnRespEvent   FOnRespCancel;       // 应答已取消
   TOnRespEvent   FOnRespConfirm;      // 应答已确认

private:
   // 应用事件的回调函数
   static void __stdcall _RCKOnConnLogin(void* AppID, void* AConnID, bool& AIsRefused);
   static void __stdcall _RCKOnConnLogout(void* AppID, void* AConnID);
   static void __stdcall _RCKOnExecResp(void* AppID, void* AFuncID, void* AConnID);
   static void __stdcall _RCKOnRespCancel(void* AppID, void* AFuncID, void* AConnID);
   static void __stdcall _RCKOnRespConfirm(void* AppID, void* AFuncID, void* AConnID);

private:
   friend class TRCKObjs;
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCFuncObj - RCK 函数类 */

class TRCFuncObj
{
public:
   TRCFuncObj();
   virtual ~TRCFuncObj();

   // 属性
   void*          Data() const         { return FData; }
   KYString       Name() const         { return FName; }
   void*          FuncID() const       { return FFuncID; }
   TRCAppObj*     AppObj() const       { return FAppObj; }
   bool           Active() const       { return RCKFuncGetInt(FFuncID, kfaActive, NULL) == 1; }

   long           RetType() const      { return RCKFuncGetInt(FFuncID, kfaRetType, NULL); }
   long           RetSize() const      { return RCKFuncGetInt(FFuncID, kfaRetSize, NULL); }
   long           CallMode() const     { return RCKFuncGetInt(FFuncID, kfaCallMode, NULL); }
   void*          Callback() const     { return RCKFuncGetObj(FFuncID, kfaCallback, NULL); }
   bool           NeedConfirm() const  { return RCKFuncGetInt(FFuncID, kfaNeedConfirm, NULL) == 1; }
   bool           NeedEncrypt() const  { return RCKFuncGetInt(FFuncID, kfaNeedEncrypt, NULL) == 1; }

   // 设置属性
   void           SetData(void* AData) { FData = AData; }
   long           SetNeedConfirm(bool ANeed)
                  { return RCKFuncSetInt(FFuncID, kfaNeedConfirm, ANeed); }
   long           SetNeedEncrypt(bool ANeed)
                  { return RCKFuncSetInt(FFuncID, kfaNeedEncrypt, ANeed); }

   // 读取參数定义
   long           ParamCount() const   { return RCKFuncGetInt(FFuncID, kfaParamCount, NULL); }
   long           ParamIndex(const char* AName) const
                  { return RCKFuncParamIndex(FFuncID, AName); }
   long           ParamIndex(const KYString& AName) const
                  { return RCKFuncParamIndex(FFuncID, (char*)AName); }
   KYString       ParamName(long AIndex) const
                  { return RCKFuncGetStr(FFuncID, kfaBaseParamName + AIndex, NULL); }
   long           ParamType(long AIndex) const
                  { return RCKFuncGetInt(FFuncID, kfaBaseParamType + AIndex, NULL); }
   long           ParamSize(long AIndex) const
                  { return RCKFuncGetInt(FFuncID, kfaBaseParamSize + AIndex, NULL); }
   bool           ParamIsRef(long AIndex) const
                  { return RCKFuncGetInt(FFuncID, kfaBaseParamIsRef + AIndex, NULL) == 1; }

   // 读取返回字段定义
   long           FieldCount() const   { return RCKFuncGetInt(FFuncID, kfaFieldCount, NULL); }
   long           FieldIndex(const char* AName) const
                  { return RCKFuncFieldIndex(FFuncID, AName); }
   long           FieldIndex(const KYString& AName) const
                  { return RCKFuncFieldIndex(FFuncID, (char*)AName); }
   KYString       FieldName(long AIndex) const
                  { return RCKFuncGetStr(FFuncID, kfaBaseFieldName + AIndex, NULL); }
   long           FieldType(long AIndex) const
                  { return RCKFuncGetInt(FFuncID, kfaBaseFieldType + AIndex, NULL); }
   long           FieldSize(long AIndex) const
                  { return RCKFuncGetInt(FFuncID, kfaBaseFieldSize + AIndex, NULL); }

   // 加入參数
   long           AddParam(const char* AName, long AType, long ASize = 0, bool AIsRef = false)
                  { return RCKFuncAddParam(FFuncID, AName, AType, ASize, AIsRef); }
   long           AddParam(const KYString& AName, long AType, long ASize = 0, bool AIsRef = false)
                  { return RCKFuncAddParam(FFuncID, (char*)AName, AType, ASize, AIsRef); }

   // 加入返回字段
   long           AddField(const char* AName, long AType, long ASize = 0)
                  { return RCKFuncAddField(FFuncID, AName, AType, ASize); }
   long           AddField(const KYString& AName, long AType, long ASize = 0)
                  { return RCKFuncAddField(FFuncID, (char*)AName, AType, ASize); }

private:
   void*          FData;               // 自己定义数据
   KYString       FName;               // 函数名
   void*          FFuncID;             // 函数 ID
   TRCAppObj*     FAppObj;             // 应用对象

private:
   friend class TRCKObjs;
   friend class TRCAppObj;
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCConnObj - RCK 连接类 */

class TRCConnObj
{
public:
   // 连接通知事件
   typedef void (TObject::*TDoNotify)(TRCConnObj* AConnObj);
   typedef struct
   {
      TDoNotify      Method;
      void*          Object;
   } TOnNotify;

   // 连接命令返回事件
   typedef void (TObject::*TDoCmdReturn)(TRCConnObj* AConnObj, void* ACmdID, long AResult);
   typedef struct
   {
      TDoCmdReturn   Method;
      void*          Object;
   } TOnCmdReturn;

   // 接收自己定义数据事件
   typedef void (TObject::*TDoRecvData)(TRCConnObj* AConnObj, const void* AData, long ASize);
   typedef struct
   {
      TDoRecvData    Method;
      void*          Object;
   } TOnRecvData;

public:
   TRCConnObj(void* AConnID = NULL, bool ANeedFree = false);
   virtual ~TRCConnObj();

   // 属性
   void*          Data() const         { return FData; }
   void*          ConnID() const       { return FConnID; }
   bool           CanEvent() const     { return FCanEvent; }
   bool           NeedFree() const     { return FNeedFree; }

   KYString       Error() const        { return RCKConnGetStr(FConnID, kcaError, NULL); }
   long           Type() const         { return RCKConnGetInt(FConnID, kcaType, NULL); }
   long           State() const        { return RCKConnGetInt(FConnID, kcaState, NULL); }
   long           Timeout() const      { return RCKConnGetInt(FConnID, kcaTimeout, NULL); }
   void*          SlotObj() const      { return RCKConnGetObj(FConnID, kcaSlotObj, NULL); }
   bool           CanLogin() const     { return RCKConnGetInt(FConnID, kcaCanLogin, NULL) == 1; }
   bool           CanBindApp() const   { return RCKConnGetInt(FConnID, kcaCanBindApp, NULL) == 1; }
   bool           IsSyncLink() const   { return RCKConnGetInt(FConnID, kcaIsSyncLink, NULL) == 1; }
   void*          BindAppObj() const   { return RCKConnGetObj(FConnID, kcaBindAppObj, NULL); }
   long           BindTimeout() const  { return RCKConnGetInt(FConnID, kcaBindTimeout, NULL); }
   long           SlotTimeout() const  { return RCKConnGetInt(FConnID, kcaSlotTimeout, NULL); }
   long           SlotMaxSize() const  { return RCKConnGetInt(FConnID, kcaSlotMaxSize, NULL); }
   long           SendQueueSize() const{ return RCKConnGetInt(FConnID, kcaSendQueueSize, NULL); }

   KYString       AppName() const      { return RCKConnGetStr(FConnID, kcaAppName, NULL); }
   KYString       CurrAddr() const     { return RCKConnGetStr(FConnID, kcaCurrAddr, NULL); }
   long           CurrPort() const     { return RCKConnGetInt(FConnID, kcaCurrPort, NULL); }
   KYString       PeerAddr() const     { return RCKConnGetStr(FConnID, kcaPeerAddr, NULL); }
   long           PeerPort() const     { return RCKConnGetInt(FConnID, kcaPeerPort, NULL); }
   long           LastTick() const     { return RCKConnGetInt(FConnID, kcaLastTick, NULL); }

   long           KeepTimeout() const  { return RCKConnGetInt(FConnID, kcaKeepTimeout, NULL); }
   long           KeepInterval() const { return RCKConnGetInt(FConnID, kcaKeepInterval, NULL); }
   long           KeepRetryTimes()const{ return RCKConnGetInt(FConnID, kcaKeepRetryTimes, NULL); }

   long           FuncCount() const    { return RCKConnGetInt(FConnID, kcaFuncCount, NULL); }
   KYString       FuncName(long AIndex) const
                  { return RCKConnGetStr(FConnID, kcaBaseFuncName + AIndex, NULL); }

   // 设置属性
   void           SetData(void* AData) { FData = AData; }
   long           SetTimeout(long ATimeout)
                  { return RCKConnSetInt(FConnID, kcaTimeout, ATimeout); }
   long           SetCanLogin(bool ACanLogin)
                  { return RCKConnSetInt(FConnID, kcaCanLogin, ACanLogin); }
   long           SetCanBindApp(bool ACanBindApp)
                  { return RCKConnSetInt(FConnID, kcaCanBindApp, ACanBindApp); }
   long           SetBindTimeout(long ATimeout)
                  { return RCKConnSetInt(FConnID, kcaBindTimeout, ATimeout); }
   long           SetIsSyncLink(bool AIsSyncLink)
                  { return RCKConnSetInt(FConnID, kcaIsSyncLink, AIsSyncLink); }
   long           SetSlotTimeout(long ATimeout)
                  { return RCKConnSetInt(FConnID, kcaSlotTimeout, ATimeout); }
   long           SetSlotMaxSize(long AMaxSize)
                  { return RCKConnSetInt(FConnID, kcaSlotMaxSize, AMaxSize); }
   long           SetSendQueueSize(long AQueueSize)
                  { return RCKConnSetInt(FConnID, kcaSendQueueSize, AQueueSize); }

   long           SetAppName(const char* AName, long ALength)
                  { return RCKConnSetStr(FConnID, kcaAppName, AName, ALength); }
   long           SetAppName(const KYString& AName)
                  { return SetAppName((char*)AName, AName.Length()); }

   long           SetAppPass(const char* APassword, long ALength)
                  { return RCKConnSetStr(FConnID, kcaAppPass, APassword, ALength); }
   long           SetAppPass(const KYString& APassword)
                  { return SetAppPass((char*)APassword, APassword.Length()); }

   long           SetCurrAddr(const char* Addr, long ALength)
                  { return RCKConnSetStr(FConnID, kcaCurrAddr, Addr, ALength); }
   long           SetCurrAddr(const KYString& Addr)
                  { return SetCurrAddr((char*)Addr, Addr.Length()); }
   long           SetCurrPort(long APort)
                  { return RCKConnSetInt(FConnID, kcaCurrPort, APort); }

   long           SetPeerAddr(const char* Addr, long ALength)
                  { return RCKConnSetStr(FConnID, kcaPeerAddr, Addr, ALength); }
   long           SetPeerAddr(const KYString& Addr)
                  { return SetPeerAddr((char*)Addr, Addr.Length()); }
   long           SetPeerPort(long APort)
                  { return RCKConnSetInt(FConnID, kcaPeerPort, APort); }

   long           SetKeepTimeout(long ATimeout)
                  { return RCKConnSetInt(FConnID, kcaKeepTimeout, ATimeout); }
   long           SetKeepInterval(long AInterval)
                  { return RCKConnSetInt(FConnID, kcaKeepInterval, AInterval); }
   long           SetKeepRetryTimes(long ARetryTimes)
                  { return RCKConnSetInt(FConnID, kcaKeepRetryTimes, ARetryTimes); }

   // slot 扩展属性
   bool           SlotExtBool(long AExt) const
                  { return RCKConnGetInt(FConnID, kcaBaseSlotExt + AExt, NULL) == 1; }
   long           SlotExtInt(long AExt) const
                  { return RCKConnGetInt(FConnID, kcaBaseSlotExt + AExt, NULL); }
   void*          SlotExtObj(long AExt) const
                  { return RCKConnGetObj(FConnID, kcaBaseSlotExt + AExt, NULL); }
   char*          SlotExtStr(long AExt, long* ARetSize = NULL) const
                  { return RCKConnGetStr(FConnID, kcaBaseSlotExt + AExt, ARetSize); }

   // 设置 slot 扩展属性
   long           SetSlotExtInt(long AExt, long AValue)
                  { return RCKConnSetInt(FConnID, kcaBaseSlotExt + AExt, AValue); }
   long           SetSlotExtObj(long AExt, void* AValue)
                  { return RCKConnSetObj(FConnID, kcaBaseSlotExt + AExt, AValue); }
   long           SetSlotExtStr(long AExt, const char* AValue, long ASize)
                  { return RCKConnSetStr(FConnID, kcaBaseSlotExt + AExt, AValue, ASize); }

   // 打开/关闭连接
   long           Open()               { return RCKConnOpen(FConnID); }
   long           Close()              { return RCKConnClose(FConnID); }

   // 登录/登出对端应用
   long           Login()              { return RCKConnLogin(FConnID); }
   long           Logout()             { return RCKConnLogout(FConnID); }

   // 载入对端应用的函数列表
   long           LoadDefs()           { return RCKConnLoad(FConnID); }

   // 发送数据(參数 ANeedPack 表示数据是否须要压缩发送)
   long           SendData(const void* AData, long ASize, bool ANeedPack)
                  { return RCKConnSendData(FConnID, AData, ASize, ANeedPack); }

   // 设置事件
   long           SetOnConnect(TDoNotify AMethod, void* AObject);
   long           SetOnDisconnect(TDoNotify AMethod, void* AObject);
   long           SetOnLogin(TDoNotify AMethod, void* AObject);
   long           SetOnLogout(TDoNotify AMethod, void* AObject);
   long           SetOnCmdReturn(TDoCmdReturn AMethod, void* AObject);
   long           SetOnRecvData(TDoRecvData AMethod, void* AObject);

protected:
   // 对象次数增/减 1
   long           IncObjTimes()        { return InterlockedIncrement(&FObjTimes); }
   long           DecObjTimes()        { return InterlockedDecrement(&FObjTimes); }

private:
   // 运行初始化事件
   void           DoInitEvent();

   // 运行事件方法
   void           DoConnect();
   void           DoDisconnect();
   void           DoLogin();
   void           DoLogout();
   void           DoCmdReturn(void* ACmdID, long AResult);
   void           DoRecvData(const void* AData, long ASize);

private:
   void*          FData;               // 自己定义数据
   void*          FConnID;             // 连接 ID
   long           FObjTimes;           // 对象引用次数
   bool           FCanEvent;           // 是否可以事件(注: CreateConnObj 创建才可以)
   bool           FNeedFree;           // 是否须要释放

   // 事件
   TOnNotify      FOnConnect;          // 连接成功
   TOnNotify      FOnDisconnect;       // 断开连接
   TOnNotify      FOnLogin;            // 登录对端应用
   TOnNotify      FOnLogout;           // 登出对端应用
   TOnCmdReturn   FOnCmdReturn;        // 命令返回
   TOnRecvData    FOnRecvData;         // 接收数据

private:
   // 连接事件的回调函数
   static void __stdcall _RCKOnConnect(void* AConnID);
   static void __stdcall _RCKOnDisconnect(void* AConnID);
   static void __stdcall _RCKOnLogin(void* AConnID);
   static void __stdcall _RCKOnLogout(void* AConnID);
   static void __stdcall _RCKOnCmdReturn(void* AConnID, void* ACmdID, long AResult);
   static void __stdcall _RCKOnRecvData(void* AConnID, const void* AData, long ASize);

private:
   friend class TRCKObjs;
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCCmdObj - RCK 命令类 */

class TRCCmdObj
{
public:
   TRCCmdObj();
   virtual ~TRCCmdObj();

   // 属性
   void*          Data() const         { return FData; }
   KYString       Name() const         { return RCKCmdGetStr(FCmdID, kmaName, NULL); }
   long           State() const        { return RCKCmdGetInt(FCmdID, kmaState, NULL); }
   void*          CmdID() const        { return FCmdID; }
   void*          ConnID() const       { return RCKCmdGetObj(FCmdID, kmaConnObj, NULL); }
   TRCConnObj*    ConnObj() const;
   long           RetType() const      { return RCKCmdGetInt(FCmdID, kmaRetType, NULL); }
   long           RetSize() const      { return RCKCmdGetInt(FCmdID, kmaRetSize, NULL); }
   bool           Encrypted() const    { return RCKCmdGetInt(FCmdID, kmaEncrypted, NULL) == 1; }
   bool           AppIsValid() const   { return RCKCmdGetInt(FCmdID, kmaAppIsValid, NULL) == 1; }
   long           ExecResult() const   { return RCKCmdGetInt(FCmdID, kmaExecResult, NULL); }

   // 读取參数定义
   long           ParamCount() const   { return RCKCmdGetInt(FCmdID, kmaParamCount, NULL); }
   long           ParamIndex(const char* AName) const
                  { return RCKCmdParamIndex(FCmdID, AName); }
   long           ParamIndex(const KYString& AName) const
                  { return RCKCmdParamIndex(FCmdID, (char*)AName); }
   KYString       ParamName(long AIndex) const
                  { return RCKCmdGetStr(FCmdID, kmaBaseParamName + AIndex, NULL); }
   long           ParamType(long AIndex) const
                  { return RCKCmdGetInt(FCmdID, kmaBaseParamType + AIndex, NULL); }
   long           ParamSize(long AIndex) const
                  { return RCKCmdGetInt(FCmdID, kmaBaseParamSize + AIndex, NULL); }

   // 读取返回字段定义
   long           FieldCount() const   { return RCKCmdGetInt(FCmdID, kmaFieldCount, NULL); }
   long           FieldIndex(const char* AName) const
                  { return RCKCmdFieldIndex(FCmdID, AName); }
   long           FieldIndex(const KYString& AName) const
                  { return RCKCmdFieldIndex(FCmdID, (char*)AName); }
   KYString       FieldName(long AIndex) const
                  { return RCKCmdGetStr(FCmdID, kmaBaseFieldName + AIndex, NULL); }
   long           FieldType(long AIndex) const
                  { return RCKCmdGetInt(FCmdID, kmaBaseFieldType + AIndex, NULL); }
   long           FieldSize(long AIndex) const
                  { return RCKCmdGetInt(FCmdID, kmaBaseFieldSize + AIndex, NULL); }
   bool           FieldIsNull(long AIndex) const
                  { return RCKCmdFieldIsNull(FCmdID, AIndex, NULL); }
   bool           FieldIsNull(const char* AName) const
                  { return RCKCmdFieldIsNull_(FCmdID, AName, NULL); }
   bool           FieldIsNull(const KYString& AName) const
                  { return RCKCmdFieldIsNull_(FCmdID, (char*)AName, NULL); }

   // 设置属性
   void           SetData(void* AData) { FData = AData; }
   long           SetConnID(void* AConnID) const
                  { return RCKCmdSetObj(FCmdID, kmaConnObj, AConnID); }
   long           SetConnObj(TRCConnObj* AConnObj) const
                  { return RCKCmdSetObj(FCmdID, kmaConnObj, (AConnObj != NULL)
                                                   ? AConnObj->ConnID() : NULL); }

   // 開始命令
   long           Begin(const char* AName) const
                  { return RCKCmdBegin(FCmdID, AName); }
   long           Begin(const KYString& AName) const
                  { return RCKCmdBegin(FCmdID, (char*)AName); }

   // 结束命令/又一次開始命令
   long           End() const          { return RCKCmdEnd(FCmdID); }
   long           Restart() const      { return RCKCmdRestart(FCmdID); }

   // 运行命令
   long           Execute(Longword ATimeout) const
                  { return RCKCmdExecute(FCmdID, ATimeout); }
   long           ExecByParams(Longword ATimeout, ...) const;

   // 清除当前命令參数值
   long           ParamClear() const   { return RCKCmdParamClear(FCmdID); }

   // 设置各类型參数值的相关方法
   long           ParamByStr(long AIndex, const char* AValue, long ASize = -1) const
                  { return RCKCmdParamByStr(FCmdID, AIndex, AValue, ASize); }
   long           ParamByStr(long AIndex, const KYString& AValue) const
                  { return RCKCmdParamByStr(FCmdID, AIndex, (char*)AValue, AValue.Length()); }
   long           ParamByStr(const char* AName, const KYString& AValue) const
                  { return RCKCmdParamByStr_(FCmdID, AName, (char*)AValue, AValue.Length()); }
   long           ParamByStr(const KYString& AName, const KYString& AValue) const
                  { return RCKCmdParamByStr_(FCmdID, (char*)AName,
                                             (char*)AValue, AValue.Length()); }

   long           ParamByInt(long AIndex, long AValue) const
                  { return RCKCmdParamByInt(FCmdID, AIndex, AValue); }
   long           ParamByInt(const char* AName, long AValue) const
                  { return RCKCmdParamByInt_(FCmdID, AName, AValue); }
   long           ParamByInt(const KYString& AName, long AValue) const
                  { return RCKCmdParamByInt_(FCmdID, (char*)AName, AValue); }

   long           ParamByBool(long AIndex, bool AValue) const
                  { return RCKCmdParamByBool(FCmdID, AIndex, AValue); }
   long           ParamByBool(const char* AName, bool AValue) const
                  { return RCKCmdParamByBool_(FCmdID, AName, AValue); }
   long           ParamByBool(const KYString& AName, bool AValue) const
                  { return RCKCmdParamByBool_(FCmdID, (char*)AName, AValue); }

   long           ParamByFloat(long AIndex, double AValue) const
                  { return RCKCmdParamByFloat(FCmdID, AIndex, AValue); }
   long           ParamByFloat(const char* AName, double AValue) const
                  { return RCKCmdParamByFloat_(FCmdID, AName, AValue); }
   long           ParamByFloat(const KYString& AName, double AValue) const
                  { return RCKCmdParamByFloat_(FCmdID, (char*)AName, AValue); }

   long           ParamByStruct(long AIndex, void* AValue) const
                  { return RCKCmdParamByStruct(FCmdID, AIndex, AValue); }
   long           ParamByStruct(const char* AName, void* AValue) const
                  { return RCKCmdParamByStruct_(FCmdID, AName, AValue); }
   long           ParamByStruct(const KYString& AName, void* AValue) const
                  { return RCKCmdParamByStruct_(FCmdID, (char*)AName, AValue); }

   long           ParamByInt64(long AIndex, __int64 AValue) const
                  { return RCKCmdParamByInt64(FCmdID, AIndex, AValue); }
   long           ParamByInt64(const char* AName, __int64 AValue) const
                  { return RCKCmdParamByInt64_(FCmdID, AName, AValue); }
   long           ParamByInt64(const KYString& AName, __int64 AValue) const
                  { return RCKCmdParamByInt64_(FCmdID, (char*)AName, AValue); }

   long           ParamByDate(long AIndex, TDateTime AValue) const
                  { return RCKCmdParamByDate(FCmdID, AIndex, AValue); }
   long           ParamByDate(const char* AName, TDateTime AValue) const
                  { return RCKCmdParamByDate_(FCmdID, AName, AValue); }
   long           ParamByDate(const KYString& AName, TDateTime AValue) const
                  { return RCKCmdParamByDate_(FCmdID, (char*)AName, AValue); }

   long           ParamByCustom(long AIndex, const void* AValue, long ASize) const
                  { return RCKCmdParamByCustom(FCmdID, AIndex, AValue, ASize); }
   long           ParamByCustom(const char* AName, const void* AValue, long ASize) const
                  { return RCKCmdParamByCustom_(FCmdID, AName, AValue, ASize); }
   long           ParamByCustom(const KYString& AName, const void* AValue, long ASize) const
                  { return RCKCmdParamByCustom_(FCmdID, (char*)AName, AValue, ASize); }

   // 取各类型返回值的相关方法
   KYString       RetAsStr() const;
   char*          RetAsStr(long& ASize) const
                  { return RCKCmdRetStr(FCmdID, &ASize); }
   long           RetAsInt() const     { return RCKCmdRetInt(FCmdID, NULL); }
   bool           RetAsBool() const    { return RCKCmdRetBool(FCmdID, NULL); }
   double         RetAsFloat() const   { return RCKCmdRetFloat(FCmdID, NULL); }
   void*          RetAsStruct() const  { return RCKCmdRetStruct(FCmdID, NULL); }
   __int64        RetAsInt64() const   { return RCKCmdRetInt64(FCmdID, NULL); }
   TDateTime      RetAsDate() const    { return RCKCmdRetDate(FCmdID, NULL); }
   long           RetAsCustom(void* AValue, long ASize) const
                  { return RCKCmdRetCustom(FCmdID, AValue, ASize); }

   // 取各类型返回字段值的相关方法
   KYString       FieldAsStr(long AIndex) const;
   KYString       FieldAsStr(const char* AName) const;
   KYString       FieldAsStr(const KYString& AName) const
                  { return FieldAsStr((char*)AName); }
   char*          FieldAsStr(long AIndex, long& ASize) const
                  { return RCKCmdFieldStr(FCmdID, AIndex, &ASize); }
   char*          FieldAsStr(const char* AName, long& ASize) const
                  { return RCKCmdFieldStr_(FCmdID, AName, &ASize); }

   long           FieldAsInt(long AIndex) const
                  { return RCKCmdFieldInt(FCmdID, AIndex, NULL); }
   long           FieldAsInt(const char* AName) const
                  { return RCKCmdFieldInt_(FCmdID, AName, NULL); }
   long           FieldAsInt(const KYString& AName) const
                  { return RCKCmdFieldInt_(FCmdID, (char*)AName, NULL); }

   bool           FieldAsBool(long AIndex) const
                  { return RCKCmdFieldBool(FCmdID, AIndex, NULL); }
   bool           FieldAsBool(const char* AName) const
                  { return RCKCmdFieldBool_(FCmdID, AName, NULL); }
   bool           FieldAsBool(const KYString& AName) const
                  { return RCKCmdFieldBool_(FCmdID, (char*)AName, NULL); }

   double         FieldAsFloat(long AIndex) const
                  { return RCKCmdFieldFloat(FCmdID, AIndex, NULL); }
   double         FieldAsFloat(const char* AName) const
                  { return RCKCmdFieldFloat_(FCmdID, AName, NULL); }
   double         FieldAsFloat(const KYString& AName) const
                  { return RCKCmdFieldFloat_(FCmdID, (char*)AName, NULL); }

   void*          FieldAsStruct(long AIndex) const
                  { return RCKCmdFieldStruct(FCmdID, AIndex, NULL); }
   void*          FieldAsStruct(const char* AName) const
                  { return RCKCmdFieldStruct_(FCmdID, AName, NULL); }
   void*          FieldAsStruct(const KYString& AName) const
                  { return RCKCmdFieldStruct_(FCmdID, (char*)AName, NULL); }

   __int64        FieldAsInt64(long AIndex) const
                  { return RCKCmdFieldInt64(FCmdID, AIndex, NULL); }
   __int64        FieldAsInt64(const char* AName) const
                  { return RCKCmdFieldInt64_(FCmdID, AName, NULL); }
   __int64        FieldAsInt64(const KYString& AName) const
                  { return RCKCmdFieldInt64_(FCmdID, (char*)AName, NULL); }

   TDateTime      FieldAsDate(long AIndex) const
                  { return RCKCmdFieldDate(FCmdID, AIndex, NULL); }
   TDateTime      FieldAsDate(const char* AName) const
                  { return RCKCmdFieldDate_(FCmdID, AName, NULL); }
   TDateTime      FieldAsDate(const KYString& AName) const
                  { return RCKCmdFieldDate_(FCmdID, (char*)AName, NULL); }

   long           FieldAsCustom(long AIndex, void* AValue, long ASize) const
                  { return RCKCmdFieldCustom(FCmdID, AIndex, AValue, ASize); }
   long           FieldAsCustom(const char* AName, void* AValue, long ASize) const
                  { return RCKCmdFieldCustom_(FCmdID, AName, AValue, ASize); }
   long           FieldAsCustom(const KYString& AName, void* AValue, long ASize) const
                  { return RCKCmdFieldCustom_(FCmdID, (char*)AName, AValue, ASize); }

private:
   void*          FData;               // 自己定义数据
   void*          FCmdID;              // 命令 ID
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCResp - RCK 应答类 */

class TRCResp
{
public:
   // 属性
   static long       State()              { return RCKRespState(); }
   static void*      FuncID()             { return RCKRespFuncObj(); }
   static void*      ConnID()             { return RCKRespConnObj(); }

   // 取各类型返回值的相关方法
   static KYString   RetAsStr();
   static char*      RetAsStr(long& ASize){ return RCKRespRetStr(&ASize); }
   static long       RetAsInt()           { return RCKRespRetInt(NULL); }
   static bool       RetAsBool()          { return RCKRespRetBool(NULL); }
   static double     RetAsFloat()         { return RCKRespRetFloat(NULL); }
   static void*      RetAsStruct()        { return RCKRespRetStruct(NULL); }
   static __int64    RetAsInt64()         { return RCKRespRetInt64(NULL); }
   static TDateTime  RetAsDate()          { return RCKRespRetDate(NULL); }
   static long       RetAsCustom(void* AValue, long ASize)
                     { return RCKRespRetCustom(AValue, ASize); }

   // 取各类型返回字段值的相关方法
   static KYString   FieldAsStr(long AIndex);
   static KYString   FieldAsStr(const char* AName);
   static KYString   FieldAsStr(const KYString& AName)
                     { return FieldAsStr((char*)AName); }
   static char*      FieldAsStr(long AIndex, long& ASize)
                     { return RCKRespFieldStr(AIndex, &ASize); }
   static char*      FieldAsStr(const char* AName, long& ASize)
                     { return RCKRespFieldStr_(AName, &ASize); }

   static long       FieldAsInt(long AIndex)
                     { return RCKRespFieldInt(AIndex, NULL); }
   static long       FieldAsInt(const char* AName)
                     { return RCKRespFieldInt_(AName, NULL); }
   static long       FieldAsInt(const KYString& AName)
                     { return RCKRespFieldInt_((char*)AName, NULL); }

   static bool       FieldAsBool(long AIndex)
                     { return RCKRespFieldBool(AIndex, NULL); }
   static bool       FieldAsBool(const char* AName)
                     { return RCKRespFieldBool_(AName, NULL); }
   static bool       FieldAsBool(const KYString& AName)
                     { return RCKRespFieldBool_((char*)AName, NULL); }

   static double     FieldAsFloat(long AIndex)
                     { return RCKRespFieldFloat(AIndex, NULL); }
   static double     FieldAsFloat(const char* AName)
                     { return RCKRespFieldFloat_(AName, NULL); }
   static double     FieldAsFloat(const KYString& AName)
                     { return RCKRespFieldFloat_((char*)AName, NULL); }

   static void*      FieldAsStruct(long AIndex)
                     { return RCKRespFieldStruct(AIndex, NULL); }
   static void*      FieldAsStruct(const char* AName)
                     { return RCKRespFieldStruct_(AName, NULL); }
   static void*      FieldAsStruct(const KYString& AName)
                     { return RCKRespFieldStruct_((char*)AName, NULL); }

   static __int64    FieldAsInt64(long AIndex)
                     { return RCKRespFieldInt64(AIndex, NULL); }
   static __int64    FieldAsInt64(const char* AName)
                     { return RCKRespFieldInt64_(AName, NULL); }
   static __int64    FieldAsInt64(const KYString& AName)
                     { return RCKRespFieldInt64_((char*)AName, NULL); }

   static TDateTime  FieldAsDate(long AIndex)
                     { return RCKRespFieldDate(AIndex, NULL); }
   static TDateTime  FieldAsDate(const char* AName)
                     { return RCKRespFieldDate_(AName, NULL); }
   static TDateTime  FieldAsDate(const KYString& AName)
                     { return RCKRespFieldDate_((char*)AName, NULL); }

   static long       FieldAsCustom(long AIndex, void* AValue, long ASize)
                     { return RCKRespFieldCustom(AIndex, AValue, ASize); }
   static long       FieldAsCustom(const char* AName, void* AValue, long ASize)
                     { return RCKRespFieldCustom_(AName, AValue, ASize); }
   static long       FieldAsCustom(const KYString& AName, void* AValue, long ASize)
                     { return RCKRespFieldCustom_((char*)AName, AValue, ASize); }

   // 取各类型參数值的相关方法
   static KYString   ParamAsStr(long AIndex);
   static KYString   ParamAsStr(const char* AName);
   static KYString   ParamAsStr(const KYString& AName)
                     { return ParamAsStr((char*)AName); }
   static char*      ParamAsStr(long AIndex, long& ASize)
                     { return RCKRespParamStr(AIndex, &ASize); }
   static char*      ParamAsStr(const char* AName, long& ASize)
                     { return RCKRespParamStr_(AName, &ASize); }

   static long       ParamAsInt(long AIndex)
                     { return RCKRespParamInt(AIndex, NULL); }
   static long       ParamAsInt(const char* AName)
                     { return RCKRespParamInt_(AName, NULL); }
   static long       ParamAsInt(const KYString& AName)
                     { return RCKRespParamInt_((char*)AName, NULL); }

   static bool       ParamAsBool(long AIndex)
                     { return RCKRespParamBool(AIndex, NULL); }
   static bool       ParamAsBool(const char* AName)
                     { return RCKRespParamBool_(AName, NULL); }
   static bool       ParamAsBool(const KYString& AName)
                     { return RCKRespParamBool_((char*)AName, NULL); }

   static double     ParamAsFloat(long AIndex)
                     { return RCKRespParamFloat(AIndex, NULL); }
   static double     ParamAsFloat(const char* AName)
                     { return RCKRespParamFloat_(AName, NULL); }
   static double     ParamAsFloat(const KYString& AName)
                     { return RCKRespParamFloat_((char*)AName, NULL); }

   static void*      ParamAsStruct(long AIndex)
                     { return RCKRespParamStruct(AIndex, NULL); }
   static void*      ParamAsStruct(const char* AName)
                     { return RCKRespParamStruct_(AName, NULL); }
   static void*      ParamAsStruct(const KYString& AName)
                     { return RCKRespParamStruct_((char*)AName, NULL); }

   static __int64    ParamAsInt64(long AIndex)
                     { return RCKRespParamInt64(AIndex, NULL); }
   static __int64    ParamAsInt64(const char* AName)
                     { return RCKRespParamInt64_(AName, NULL); }
   static __int64    ParamAsInt64(const KYString& AName)
                     { return RCKRespParamInt64_((char*)AName, NULL); }

   static TDateTime  ParamAsDate(long AIndex)
                     { return RCKRespParamDate(AIndex, NULL); }
   static TDateTime  ParamAsDate(const char* AName)
                     { return RCKRespParamDate_(AName, NULL); }
   static TDateTime  ParamAsDate(const KYString& AName)
                     { return RCKRespParamDate_((char*)AName, NULL); }

   static long       ParamAsCustom(long AIndex, void* AValue, long ASize)
                     { return RCKRespParamCustom(AIndex, AValue, ASize); }
   static long       ParamAsCustom(const char* AName, void* AValue, long ASize)
                     { return RCKRespParamCustom_(AName, AValue, ASize); }
   static long       ParamAsCustom(const KYString& AName, void* AValue, long ASize)
                     { return RCKRespParamCustom_((char*)AName, AValue, ASize); }

   // 设置各类型返回值的相关方法
   static long       RetByStr(const char* AValue, long ASize = -1)
                     { return RCKRespRetByStr(AValue, ASize); }
   static long       RetByStr(const KYString& AValue)
                     { return RCKRespRetByStr((char*)AValue, AValue.Length()); }
   static long       RetByInt(long AValue)
                     { return RCKRespRetByInt(AValue); }
   static long       RetByBool(bool AValue)
                     { return RCKRespRetByBool(AValue); }
   static long       RetByFloat(double AValue)
                     { return RCKRespRetByFloat(AValue); }
   static long       RetByStruct(void* AValue)
                     { return RCKRespRetByStruct(AValue); }
   static long       RetByInt64(__int64 AValue)
                     { return RCKRespRetByInt64(AValue); }
   static long       RetByDate(TDateTime AValue)
                     { return RCKRespRetByDate(AValue); }
   static long       RetByCustom(const void* AValue, long ASize)
                     { return RCKRespRetByCustom(AValue, ASize); }

   // 设置各类型返回字段值的相关方法
   static long       FieldByStr(long AIndex, const char* AValue, long ASize = -1)
                     { return RCKRespFieldByStr(AIndex, AValue, ASize); }
   static long       FieldByStr(long AIndex, const KYString& AValue)
                     { return RCKRespFieldByStr(AIndex, (char*)AValue, AValue.Length()); }
   static long       FieldByStr(const char* AName, const KYString& AValue)
                     { return RCKRespFieldByStr_(AName, (char*)AValue, AValue.Length()); }
   static long       FieldByStr(const KYString& AName, const KYString& AValue)
                     { return RCKRespFieldByStr_((char*)AName,
                                                (char*)AValue, AValue.Length()); }

   static long       FieldByInt(long AIndex, long AValue)
                     { return RCKRespFieldByInt(AIndex, AValue); }
   static long       FieldByInt(const char* AName, long AValue)
                     { return RCKRespFieldByInt_(AName, AValue); }
   static long       FieldByInt(const KYString& AName, long AValue)
                     { return RCKRespFieldByInt_((char*)AName, AValue); }

   static long       FieldByBool(long AIndex, bool AValue)
                     { return RCKRespFieldByBool(AIndex, AValue); }
   static long       FieldByBool(const char* AName, bool AValue)
                     { return RCKRespFieldByBool_(AName, AValue); }
   static long       FieldByBool(const KYString& AName, bool AValue)
                     { return RCKRespFieldByBool_((char*)AName, AValue); }

   static long       FieldByFloat(long AIndex, double AValue)
                     { return RCKRespFieldByFloat(AIndex, AValue); }
   static long       FieldByFloat(const char* AName, double AValue)
                     { return RCKRespFieldByFloat_(AName, AValue); }
   static long       FieldByFloat(const KYString& AName, double AValue)
                     { return RCKRespFieldByFloat_((char*)AName, AValue); }

   static long       FieldByStruct(long AIndex, void* AValue)
                     { return RCKRespFieldByStruct(AIndex, AValue); }
   static long       FieldByStruct(const char* AName, void* AValue)
                     { return RCKRespFieldByStruct_(AName, AValue); }
   static long       FieldByStruct(const KYString& AName, void* AValue)
                     { return RCKRespFieldByStruct_((char*)AName, AValue); }

   static long       FieldByInt64(long AIndex, __int64 AValue)
                     { return RCKRespFieldByInt64(AIndex, AValue); }
   static long       FieldByInt64(const char* AName, __int64 AValue)
                     { return RCKRespFieldByInt64_(AName, AValue); }
   static long       FieldByInt64(const KYString& AName, __int64 AValue)
                     { return RCKRespFieldByInt64_((char*)AName, AValue); }

   static long       FieldByDate(long AIndex, TDateTime AValue)
                     { return RCKRespFieldByDate(AIndex, AValue); }
   static long       FieldByDate(const char* AName, TDateTime AValue)
                     { return RCKRespFieldByDate_(AName, AValue); }
   static long       FieldByDate(const KYString& AName, TDateTime AValue)
                     { return RCKRespFieldByDate_((char*)AName, AValue); }

   static long       FieldByCustom(long AIndex, const void* AValue, long ASize)
                     { return RCKRespFieldByCustom(AIndex, AValue, ASize); }
   static long       FieldByCustom(const char* AName, const void* AValue, long ASize)
                     { return RCKRespFieldByCustom_(AName, AValue, ASize); }
   static long       FieldByCustom(const KYString& AName, const void* AValue, long ASize)
                     { return RCKRespFieldByCustom_((char*)AName, AValue, ASize); }
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCFields - RCK 字段集类 */

class TRCFields
{
public:
   TRCFields();
   TRCFields(void* AFields);
   virtual ~TRCFields();

   // 属性
   void*          Fields() const       { return FFields; }
   bool           IsBound() const      { return FIsBound; }

   // 字段信息
   long           Count() const        { return RCKFieldCount(FFields); }
   KYString       Name(long AIndex) const
                  { return RCKFieldName(FFields, AIndex); }
   long           Size(long AIndex) const
                  { return RCKFieldSize(FFields, AIndex); }
   long           DefSize(long AIndex) const
                  { return RCKFieldDefSize(FFields, AIndex); }
   long           FieldType(long AIndex) const
                  { return RCKFieldType(FFields, AIndex); }
   bool           IsNull(long AIndex) const
                  { return RCKFieldIsNull(FFields, AIndex, NULL); }
   bool           IsNull_(const KYString& AName) const
                  { return RCKFieldIsNull_(FFields, (char*)AName, NULL); }

   // 字段名索引
   long           IndexOf(const char* AName) const
                  { return RCKFieldIndex(FFields, AName); }
   long           IndexOf(const KYString& AName) const
                  { return RCKFieldIndex(FFields, (char*)AName); }

   // 字段定义方法
   long           AddDef(const char* AName, long AType, long ASize) const
                  { return RCKFieldDefsAdd(FFields, AName, AType, ASize); }
   long           AddDef(const KYString& AName, long AType, long ASize) const
                  { return RCKFieldDefsAdd(FFields, (char*)AName, AType, ASize); }
   void           DeleteDef(long AIndex) const
                  { RCKFieldDefsDelete(FFields, AIndex); }
   void           ClearDefs() const    { RCKFieldDefsClear(FFields); }

   long           AssignDefs(const void* AFieldDefs, long ASize) const
                  { return RCKFieldDefsAssign(FFields, AFieldDefs, ASize); }
   KYString       FieldDefs() const;
   void*          FieldDefs(long& ASize) const
                  { return RCKFieldDefsOutput(FFields, &ASize); }
   long           Defs2Size(const void* AFieldDefs) const
                  { return RCKFieldDefsSize(AFieldDefs); }

   // 字段数据方法
   void           Clear() const        { RCKFieldsClear(FFields); }
   long           Assign(const void* AData, long ASize) const
                  { return RCKFieldsAssign(FFields, AData, ASize); }
   KYString       DataSet() const;
   void*          DataSet(long& ASize) const
                  { return RCKFieldsOutput(FFields, &ASize); }
   long           DataSize() const     { return RCKFieldsSize(FFields); }

   // 取各类型字段值的相关方法
   KYString       AsStr(long AIndex) const;
   KYString       AsStr(const char* AName) const;
   KYString       AsStr(const KYString& AName) const
                  { return AsStr((char*)AName); }
   char*          AsStr(long AIndex, long& ASize) const
                  { return RCKFieldAsStr(FFields, AIndex, &ASize); }
   char*          AsStr(const char* AName, long& ASize) const
                  { return RCKFieldAsStr_(FFields, AName, &ASize); }

   long           AsInt(long AIndex) const
                  { return RCKFieldAsInt(FFields, AIndex, NULL); }
   long           AsInt(const char* AName) const
                  { return RCKFieldAsInt_(FFields, AName, NULL); }
   long           AsInt(const KYString& AName) const
                  { return RCKFieldAsInt_(FFields, (char*)AName, NULL); }

   bool           AsBool(long AIndex) const
                  { return RCKFieldAsBool(FFields, AIndex, NULL); }
   bool           AsBool(const char* AName) const
                  { return RCKFieldAsBool_(FFields, AName, NULL); }
   bool           AsBool(const KYString& AName) const
                  { return RCKFieldAsBool_(FFields, (char*)AName, NULL); }

   double         AsFloat(long AIndex) const
                  { return RCKFieldAsFloat(FFields, AIndex, NULL); }
   double         AsFloat(const char* AName) const
                  { return RCKFieldAsFloat_(FFields, AName, NULL); }
   double         AsFloat(const KYString& AName) const
                  { return RCKFieldAsFloat_(FFields, (char*)AName, NULL); }

   void*          AsStruct(long AIndex) const
                  { return RCKFieldAsStruct(FFields, AIndex, NULL); }
   void*          AsStruct(const char* AName) const
                  { return RCKFieldAsStruct_(FFields, AName, NULL); }
   void*          AsStruct(const KYString& AName) const
                  { return RCKFieldAsStruct_(FFields, (char*)AName, NULL); }

   __int64        AsInt64(long AIndex) const
                  { return RCKFieldAsInt64(FFields, AIndex, NULL); }
   __int64        AsInt64(const char* AName) const
                  { return RCKFieldAsInt64_(FFields, AName, NULL); }
   __int64        AsInt64(const KYString& AName) const
                  { return RCKFieldAsInt64_(FFields, (char*)AName, NULL); }

   TDateTime      AsDate(long AIndex) const
                  { return RCKFieldAsDate(FFields, AIndex, NULL); }
   TDateTime      AsDate(const char* AName) const
                  { return RCKFieldAsDate_(FFields, AName, NULL); }
   TDateTime      AsDate(const KYString& AName) const
                  { return RCKFieldAsDate_(FFields, (char*)AName, NULL); }

   long           AsCustom(long AIndex, void* AValue, long ASize) const
                  { return RCKFieldGetCustom(FFields, AIndex, AValue, ASize); }
   long           AsCustom(const char* AName, void* AValue, long ASize) const
                  { return RCKFieldGetCustom_(FFields, AName, AValue, ASize); }
   long           AsCustom(const KYString& AName, void* AValue, long ASize) const
                  { return RCKFieldGetCustom_(FFields, (char*)AName, AValue, ASize); }

   // 设置各类型字段值的相关方法
   long           ByStr(long AIndex, const char* AValue, long ASize = -1) const
                  { return RCKFieldByStr(FFields, AIndex, AValue, ASize); }
   long           ByStr(long AIndex, const KYString& AValue) const
                  { return RCKFieldByStr(FFields, AIndex, (char*)AValue, AValue.Length()); }
   long           ByStr(const char* AName, const KYString& AValue) const
                  { return RCKFieldByStr_(FFields, AName, (char*)AValue, AValue.Length()); }
   long           ByStr(const KYString& AName, const KYString& AValue) const
                  { return RCKFieldByStr_(FFields, (char*)AName,
                                             (char*)AValue, AValue.Length()); }

   long           ByInt(long AIndex, long AValue) const
                  { return RCKFieldByInt(FFields, AIndex, AValue); }
   long           ByInt(const char* AName, long AValue) const
                  { return RCKFieldByInt_(FFields, AName, AValue); }
   long           ByInt(const KYString& AName, long AValue) const
                  { return RCKFieldByInt_(FFields, (char*)AName, AValue); }

   long           ByBool(long AIndex, bool AValue) const
                  { return RCKFieldByBool(FFields, AIndex, AValue); }
   long           ByBool(const char* AName, bool AValue) const
                  { return RCKFieldByBool_(FFields, AName, AValue); }
   long           ByBool(const KYString& AName, bool AValue) const
                  { return RCKFieldByBool_(FFields, (char*)AName, AValue); }

   long           ByFloat(long AIndex, double AValue) const
                  { return RCKFieldByFloat(FFields, AIndex, AValue); }
   long           ByFloat(const char* AName, double AValue) const
                  { return RCKFieldByFloat_(FFields, AName, AValue); }
   long           ByFloat(const KYString& AName, double AValue) const
                  { return RCKFieldByFloat_(FFields, (char*)AName, AValue); }

   long           ByStruct(long AIndex, void* AValue) const
                  { return RCKFieldByStruct(FFields, AIndex, AValue); }
   long           ByStruct(const char* AName, void* AValue) const
                  { return RCKFieldByStruct_(FFields, AName, AValue); }
   long           ByStruct(const KYString& AName, void* AValue) const
                  { return RCKFieldByStruct_(FFields, (char*)AName, AValue); }

   long           ByInt64(long AIndex, __int64 AValue) const
                  { return RCKFieldByInt64(FFields, AIndex, AValue); }
   long           ByInt64(const char* AName, __int64 AValue) const
                  { return RCKFieldByInt64_(FFields, AName, AValue); }
   long           ByInt64(const KYString& AName, __int64 AValue) const
                  { return RCKFieldByInt64_(FFields, (char*)AName, AValue); }

   long           ByDate(long AIndex, TDateTime AValue) const
                  { return RCKFieldByDate(FFields, AIndex, AValue); }
   long           ByDate(const char* AName, TDateTime AValue) const
                  { return RCKFieldByDate_(FFields, AName, AValue); }
   long           ByDate(const KYString& AName, TDateTime AValue) const
                  { return RCKFieldByDate_(FFields, (char*)AName, AValue); }

   long           ByCustom(long AIndex, const void* AValue, long ASize) const
                  { return RCKFieldSetCustom(FFields, AIndex, AValue, ASize); }
   long           ByCustom(const char* AName, const void* AValue, long ASize) const
                  { return RCKFieldSetCustom_(FFields, AName, AValue, ASize); }
   long           ByCustom(const KYString& AName, const void* AValue, long ASize) const
                  { return RCKFieldSetCustom_(FFields, (char*)AName, AValue, ASize); }

private:
   void*          FFields;             // 字段集句柄
   bool           FIsBound;            // 是否绑定对象
};

/* end namespace */

   }
}

// 命名空间的别名
namespace RCKObjs = RCKernel::__object__;

#endif


封装类的源码(RCKObjs.cpp)

?

// =======================================
// Unit   : RCKernel objects
// Version: 5.0.0.0 (build 2014.07.07)
// Author : Kyee Ye
// Email  : kyee_ye(at)126.com
// Copyright (C) Kyee workroom
// =======================================

#include "RCKObjs.h"

// 使用 RCKernel 的命名空间
namespace RCKernel
{
   namespace __object__
   {

/* begin namespace */

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCKObjs - RCK 对象集类 */

class TRCKObjs
{
private:
   // 映射列表类型
   typedef TKYMapObjKey<TRCAppObj>     TAppList;
   typedef TKYMapObjKey<TRCConnObj>    TConnList;
   typedef TKYMapObjKey<TRCFuncObj>    TFuncList;

public:
   TRCKObjs();
   virtual ~TRCKObjs();

   // 清除
   void           Clear();

   // 加入/删除函数ID
   bool           AddFuncID(void* AFuncID, TRCFuncObj* AFuncObj)
                  { return (FFuncIDs->Add(AFuncID, AFuncObj) != NULL); }
   bool           DeleteFuncID(void* AFuncID)
                  { return FFuncIDs->Delete(AFuncID); }

   // 函数ID -> 对象
   TRCFuncObj*    FuncID2Object(void* AFuncID)
                  {
                     TRCFuncObj* result = NULL;
                     FFuncIDs->Find(AFuncID, result);
                     return result;
                  }

   // 创建/释放应用对象
   TRCAppObj*     CreateAppObj(const char* AName, const char* APassword, long& ARetCode);
   void           FreeAppObj(TRCAppObj* AppObj);

   // 应用对象引用计数增 1
   TRCAppObj*     IncRefAppObj(void* AppID);
   bool           IncRefAppObj(TRCAppObj* AppObj);

   // 创建/释放连接对象(注: AConnID 必须存在)
   TRCConnObj*    CreateConnObj(void* AConnID, bool ANeedFree, long& ARetCode);
   void           FreeConnObj(TRCConnObj* AConnObj);

   // 连接对象引用计数增 1
   TRCConnObj*    IncRefConnObj(void* AConnID);
   bool           IncRefConnObj(TRCConnObj* AConnObj);

   // 创建/释放命令对象
   TRCCmdObj*     CreateCmdObj(void* AConnID = NULL);
   void           FreeCmdObj(TRCCmdObj* ACmdObj);

protected:
   // 当前锁
   void           Lock() const         { FLock->Enter(); }
   void           Unlock() const       { FLock->Leave(); }

private:
   // 拷贝并清除
   bool           DoClearAppObjs(TKYList& AList);
   bool           DoClearConnObjs(TKYList& AList);
   bool           DoClearCmdQueue(TKYList& AList);

   // 事件方法
   void           DoAppDeletion(TRCAppObj* AppObj);
   void           DoCmdDeletion(TRCCmdObj* ACmdObj);
   void           DoConnDeletion(TRCConnObj* AConnObj);

private:
   TKYCritSect*   FLock;               // 当前锁
   TAppList*      FAppIDs;             // 应用 ID 列表
   TAppList*      FAppObjs;            // 应用对象列表
   TFuncList*     FFuncIDs;            // 函数 ID 列表
   TConnList*     FConnIDs;            // 连接 ID 列表
   TConnList*     FConnObjs;           // 连接对象列表
   TKYCycQueue*   FCmdQueue;           // 命令对象队列

public:
   // 全局 RCK 对象集对象
   static TRCKObjs*     _obj()         { return _Object; }

   // 初始化/释放 _Object
   static void          _InitObj();
   static void          _FreeObj(bool AForced = false);

   // 应用对象引用计数增/减 1(注: AppObj 对象必须存在)
   static void          _IncRefAppObj(TRCAppObj* AppObj)
                        { AppObj->IncObjTimes(); }
   static void          _DecRefAppObj(TRCAppObj* AppObj)
                        { if (AppObj->DecObjTimes() == 0) delete AppObj; }

   // 连接对象引用计数增/减 1(注: AConnObj 对象必须存在)
   static void          _IncRefConnObj(TRCConnObj* AConnObj)
                        { AConnObj->IncObjTimes(); }
   static void          _DecRefConnObj(TRCConnObj* AConnObj)
                        { if (AConnObj->DecObjTimes() == 0) delete AConnObj; }

private:
   // 全局 RCK 对象集对象
   static TKYCritSect*  _Lock;
   static TRCKObjs*     _Object;
   static long          _RefCount;

   // TRCKObjs 的静态成员初始化类
   static class TInitialization
   {
   public:
      TInitialization();
      ~TInitialization();
   } _Initialization;

   friend class TInitialization;
};

/* TRCKObjs - RCK 对象集类 */

// ---------------- 静态成员 ----------------

// 全局锁对象
TKYCritSect*   TRCKObjs::_Lock      = NULL;
TRCKObjs*      TRCKObjs::_Object    = NULL;
long           TRCKObjs::_RefCount  = 0;

// TRCKObjs 的静态成员初始化对象
TRCKObjs::TInitialization TRCKObjs::_Initialization;

// ---------------- 静态函数 ----------------
// TRCKObjs 的静态成员初始化类的构造函数
TRCKObjs::TInitialization::TInitialization()
{
   // 初始化
   _RefCount   = 0;
   _Object     = NULL;

   // 创建对象
   _Lock       = new TKYCritSect;
}

// TRCKObjs 的静态成员初始化类的析构函数
TRCKObjs::TInitialization::~TInitialization()
{
   // 释放对象
   FreeAndNil(_Lock);
   FreeAndNil(_Object);
}

// 初始化 _Object
void TRCKObjs::_InitObj()
{
   _Lock->Enter();
   if (_RefCount == 0)
      try
      {
         // 创建对象并引用计数设为 1
         _Object     = new TRCKObjs;
         _RefCount   = 1;

         // 初始化 RCKernel 接口
         RCKInitialize();
      }
      catch (...) {}
   else
      _RefCount++;
   _Lock->Leave();
}

// 释放 _Object
void TRCKObjs::_FreeObj(bool AForced)
{
   _Lock->Enter();
   if (_RefCount > 0)
      try
      {
         // 引用计数减 1
         _RefCount--;

         // 推断是否须要释放
         if ((_RefCount == 0) || AForced)
         {
            // 引用计数设为 0
            _RefCount   = 0;

            // 清除并释放对象
            _Object->Clear();
            FreeAndNil(_Object);

            // 释放 RCKernel 接口
            RCKUninitialize();
         }
      }
      catch (...) {}
   _Lock->Leave();
}

// ---------------- 构造函数和析构函数 ----------------
// 构造函数
TRCKObjs::TRCKObjs()
{
   // 创建对象
   FLock       = new TKYCritSect;
   FAppIDs     = new TAppList(false, false);
   FAppObjs    = new TAppList(false, false);
   FFuncIDs    = new TFuncList(false, true);
   FConnIDs    = new TConnList(false, false);
   FConnObjs   = new TConnList(false, false);
   FCmdQueue   = new TKYCycQueue(0, TKYCycQueue::slPower10);

   // 设置对象属性
   FCmdQueue->SetMaxCount(FCmdQueue->CycCount());
}

// 析构函数
TRCKObjs::~TRCKObjs()
{
   // 清除
   Clear();

   // 释放对象
   FreeAndNil(FLock);
   FreeAndNil(FAppIDs);
   FreeAndNil(FAppObjs);
   FreeAndNil(FFuncIDs);
   FreeAndNil(FConnIDs);
   FreeAndNil(FConnObjs);
   FreeAndNil(FCmdQueue);
}

// ---------------- 私有函数 ----------------
// 拷贝并清除应用对象列表
bool TRCKObjs::DoClearAppObjs(TKYList& AList)
{
   // 初始化
   bool result = false;

   // 检查列表是否非空
   if (FAppObjs->Count() > 0)
      try
      {
         // 更改容量
         AList.ChangeCapacity(FAppObjs->Count());

         // 循环拷贝
         void* pNode = FAppObjs->Next(NULL);
         while (pNode != NULL)
         {
            AList.Add(FAppObjs->Value(pNode));
            pNode = FAppObjs->Next(pNode);
         }

         // 清除
         FAppIDs->Clear();
         FAppObjs->Clear();

         // 返回结果
         result = true;
      }
      catch (...) {}

   // 返回结果
   return result;
}

// 拷贝并清除连接对象列表
bool TRCKObjs::DoClearConnObjs(TKYList& AList)
{
   // 初始化
   bool result = false;

   // 检查列表是否非空
   if (FConnObjs->Count() > 0)
      try
      {
         // 更改容量
         AList.ChangeCapacity(FConnObjs->Count());

         // 循环拷贝
         void* pNode = FConnObjs->Next(NULL);
         while (pNode != NULL)
         {
            AList.Add(FConnObjs->Value(pNode));
            pNode = FConnObjs->Next(pNode);
         }

         // 清除
         FConnIDs->Clear();
         FConnObjs->Clear();

         // 返回结果
         result = true;
      }
      catch (...) {}

   // 返回结果
   return result;
}

// 拷贝并清除命令对象队列
bool TRCKObjs::DoClearCmdQueue(TKYList& AList)
{
   // 初始化
   bool result = false;

   // 检查列表是否非空
   if (FCmdQueue->Count() != 0)
      try
      {
         // 初始化
         void* pData;

         // 更改容量并循环弹出队列
         AList.ChangeCapacity(FCmdQueue->Count());
         while (FCmdQueue->Pop(pData))
            AList.Add(pData);

         // 返回结果
         result = true;
      }
      catch (...) {}

   // 返回结果
   return result;
}

// 列表的 OnDeletion 事件过程
void TRCKObjs::DoAppDeletion(TRCAppObj* AppObj)
{
   _DecRefAppObj(AppObj);
}

// 列表的 OnDeletion 事件过程
void TRCKObjs::DoCmdDeletion(TRCCmdObj* ACmdObj)
{
   delete ACmdObj;
}

// 列表的 OnDeletion 事件过程
void TRCKObjs::DoConnDeletion(TRCConnObj* AConnObj)
{
   _DecRefConnObj(AConnObj);
}

// ---------------- 公有函数 ----------------
// 清除
void TRCKObjs::Clear()
{
   // 初始化
   TKYList  objApps(false);
   TKYList  objCmds(false);
   TKYList  objConns(false);
   bool     boolApp  = false;
   bool     boolCmd  = false;
   bool     boolConn = false;

   // 清除函数列表
   FFuncIDs->Clear();

   // 拷贝并清除列表
   Lock();
   {
      boolApp  = DoClearAppObjs(objApps);
      boolCmd  = DoClearCmdQueue(objCmds);
      boolConn = DoClearConnObjs(objConns);
   }
   Unlock();

   // 推断是否须要清除应用
   if (boolApp)
   {
      // 设置 OnDeletion 事件方法
      objApps.OnDeletion.Object  = this;
      objApps.OnDeletion.Method  = (TKYList::TDoDeletion)&TRCKObjs::DoAppDeletion;

      // 清除
      objApps.Clear();
   }

   // 推断是否须要清除命令
   if (boolCmd)
   {
      // 设置 OnDeletion 事件方法
      objCmds.OnDeletion.Object  = this;
      objCmds.OnDeletion.Method  = (TKYList::TDoDeletion)&TRCKObjs::DoCmdDeletion;

      // 清除
      objCmds.Clear();
   }

   // 推断是否须要清除连接
   if (boolConn)
   {
      // 设置 OnDeletion 事件方法
      objConns.OnDeletion.Object = this;
      objConns.OnDeletion.Method = (TKYList::TDoDeletion)&TRCKObjs::DoConnDeletion;

      // 清除
      objConns.Clear();
   }
}

// 创建应用对象
TRCAppObj* TRCKObjs::CreateAppObj(const char* AName, const char* APassword, long& ARetCode)
{
   // 初始化
   TRCAppObj*  result   = NULL;
   KYString    strName  = AName;
   void*       pAppID   = RCKAppCreate((char*)strName, APassword);

   // 推断是否创建成功
   if (pAppID != NULL)
   {
      ARetCode = krUnknown;

      // 创建对象, 并加入列表
      try
      {
         TRCAppObj* objApp = new TRCAppObj;
         if (objApp != NULL)
         {
            Lock();
            try
            {
               // 加入到 ID 列表
               void* pNode = FAppIDs->Add(pAppID, objApp);
               if (pNode == NULL)
               {
                  pNode = FAppIDs->Find(pAppID);
                  if (pNode != NULL)
                  {
                     FAppObjs->Delete(FAppIDs->Value(pNode));
                     FAppIDs->SetValue(pNode, objApp);
                  }
               }

               // 加入到对象列表
               if (pNode == NULL)
                  ;
               else if ((FAppObjs->Add(objApp, objApp) != NULL)
                     || FAppObjs->Existed(objApp))
               {
                  // 设置对象属性
                  objApp->FName  = strName;
                  objApp->FAppID = pAppID;
                  result         = objApp;
                  ARetCode       = krSuccess;
               }
               else
                  FAppIDs->Remove(pNode);
            }
            catch (...) {}
            Unlock();

            // 若失败则释放对象
            if (result == NULL)
               delete objApp;
         }
      }
      catch (...) {}

      // 若失败则释放
      if (result == NULL)
         RCKAppFree(pAppID);
   }
   else
      ARetCode = RCKGetLastError();

   // 返回结果
   return result;
}

// 释放应用对象
void TRCKObjs::FreeAppObj(TRCAppObj* AppObj)
{
   // 初始化
   TRCAppObj* objApp = NULL;

   // 查找并释放
   Lock();
   try
   {
      void* pNode = FAppObjs->Find(AppObj);
      if (pNode != NULL)
      {
         objApp   = FAppObjs->Value(pNode);
         FAppObjs->Remove(pNode);

         // 释放 ID 项
         pNode    = FAppIDs->Find(objApp->AppID());
         if ((pNode != NULL) && (FAppIDs->Value(pNode) == objApp))
            FAppIDs->Remove(pNode);
      }
   }
   catch (...) {}
   Unlock();

   // 若存在则引用计数减 1
   if (objApp != NULL)
      _DecRefAppObj(objApp);
}

// 应用对象引用计数增 1
TRCAppObj* TRCKObjs::IncRefAppObj(void* AppID)
{
   // 初始化
   TRCAppObj* result = NULL;

   // 查找
   Lock();
   try
   {
      void* pNode = FAppIDs->Find(AppID);
      if (pNode != NULL)
      {
         result   = FAppIDs->Value(pNode);
         result->IncObjTimes();
      }
   }
   catch (...) {}
   Unlock();

   // 返回结果
   return result;
}

// 应用对象引用计数增 1
bool TRCKObjs::IncRefAppObj(TRCAppObj* AppObj)
{
   // 初始化
   bool result = false;

   // 查找
   Lock();
   try
   {
      void* pNode = FAppObjs->Find(AppObj);
      if (pNode != NULL)
      {
         AppObj->IncObjTimes();
         result   = true;
      }
   }
   catch (...) {}
   Unlock();

   // 返回结果
   return result;
}

// 创建连接对象(注: AConnID 必须存在)
TRCConnObj* TRCKObjs::CreateConnObj(void* AConnID, bool ANeedFree, long& ARetCode)
{
   // 初始化
   TRCConnObj* result = NULL;

   // 检查连接是否存在
   RCKConnGetInt(AConnID, kcaState, &ARetCode);
   if (ARetCode == krSuccess)
   {
      ARetCode = krUnknown;

      // 创建对象, 并加入列表
      try
      {
         TRCConnObj* objConn = new TRCConnObj;
         if (objConn != NULL)
         {
            Lock();
            try
            {
               // 加入到 ID 列表
               void* pNode = FConnIDs->Add(AConnID, objConn);
               if (pNode == NULL)
               {
                  if (FConnIDs->Existed(AConnID))
                     ARetCode          = krIsExisted;
               }
               else if ((FConnObjs->Add(objConn, objConn) != NULL)
                     || FConnObjs->Existed(objConn))
               {
                  // 设置对象属性
                  objConn->FConnID     = AConnID;
                  objConn->FCanEvent   = true;
                  objConn->FNeedFree   = ANeedFree;
                  result               = objConn;
                  ARetCode             = krSuccess;
               }
               else
                  FConnIDs->Remove(pNode);
            }
            catch (...) {}
            Unlock();

            // 若失败则释放对象
            if (result == NULL)
               delete objConn;
         }
      }
      catch (...) {}
   }

   // 返回结果
   return result;
}

// 释放连接对象
void TRCKObjs::FreeConnObj(TRCConnObj* AConnObj)
{
   // 初始化
   TRCConnObj* objConn = NULL;

   // 查找并释放
   Lock();
   try
   {
      void* pNode = FConnObjs->Find(AConnObj);
      if (pNode != NULL)
      {
         objConn  = FConnObjs->Value(pNode);
         FConnObjs->Remove(pNode);

         // 释放 ID 项
         pNode    = FConnIDs->Find(objConn->ConnID());
         if ((pNode != NULL) && (FConnIDs->Value(pNode) == objConn))
            FConnIDs->Remove(pNode);
      }
   }
   catch (...) {}
   Unlock();

   // 若存在则引用计数减 1
   if (objConn != NULL)
      _DecRefConnObj(objConn);
}

// 连接对象引用计数增 1
TRCConnObj* TRCKObjs::IncRefConnObj(void* AConnID)
{
   // 初始化
   TRCConnObj* result = NULL;

   // 查找
   Lock();
   try
   {
      void* pNode = FConnIDs->Find(AConnID);
      if (pNode != NULL)
      {
         result   = FConnIDs->Value(pNode);
         result->IncObjTimes();
      }
   }
   catch (...) {}
   Unlock();

   // 返回结果
   return result;
}

// 连接对象引用计数增 1
bool TRCKObjs::IncRefConnObj(TRCConnObj* AConnObj)
{
   // 初始化
   bool result = false;

   // 查找
   Lock();
   try
   {
      void* pNode = FConnObjs->Find(AConnObj);
      if (pNode != NULL)
      {
         AConnObj->IncObjTimes();
         result   = true;
      }
   }
   catch (...) {}
   Unlock();

   // 返回结果
   return result;
}

// 创建命令对象
TRCCmdObj* TRCKObjs::CreateCmdObj(void* AConnID)
{
   // 初始化
   TRCCmdObj* result = NULL;

   // 弹出队列
   Lock();
   try
   {
      result = (TRCCmdObj*)FCmdQueue->Pop();
   }
   catch (...) {}
   Unlock();

   // 推断是否为空
   if (result == NULL)
      result = new TRCCmdObj;

   // 设置连接
   result->SetConnID(AConnID);

   // 返回结果
   return result;
}

// 释放命令对象
void TRCKObjs::FreeCmdObj(TRCCmdObj* ACmdObj)
{
   if (ACmdObj != NULL)
   {
      // 初始化
      bool boolPush = false;

      // 关闭命令
      ACmdObj->End();

      // 压入队列
      Lock();
      try
      {
         boolPush = FCmdQueue->Push(ACmdObj);
      }
      catch (...) {}
      Unlock();

      // 压入队列失败则释放对象
      if (!boolPush)
         delete ACmdObj;
   }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 初始化/释放单元 */

// 初始化 RCKObjs 单元(注: 内部有引用计数)
void InitRCKObjs()
{
   TRCKObjs::_InitObj();
}

// 释放 RCKObjs 单元(注: 内部有引用计数)
void FreeRCKObjs(bool AForced)
{
   TRCKObjs::_FreeObj(AForced);
}

// 命令ID -> 对象
TRCCmdObj* CmdID2Object(void* ACmdID)
{
   return (TRCCmdObj*)RCKCmdGetObj(ACmdID, kmaData, NULL);
}

// 函数ID -> 对象
TRCFuncObj* FuncID2Object(void* AFuncID)
{
   // 初始化
   TRCFuncObj* result = NULL;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->FuncID2Object(AFuncID);

   // 返回结果
   return result;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 应用对象(TRCAppObj)的相关函数 */

// 创建应用对象
TRCAppObj* CreateAppObj(const char* AName, const char* APassword, long* ARetCode)
{
   // 初始化
   TRCAppObj* result  = NULL;
   long       intCode = krNotExist;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->CreateAppObj(AName, APassword, intCode);

   // 设置返回码
   if (ARetCode != NULL)
      *ARetCode = intCode;

   // 返回结果
   return result;
}

// 释放应用对象
void FreeAppObj(TRCAppObj* AppObj)
{
   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      TRCKObjs::_obj()->FreeAppObj(AppObj);
}

// 应用对象引用计数增 1
TRCAppObj* IncRefAppObj(void* AppID)
{
   // 初始化
   TRCAppObj* result = NULL;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->IncRefAppObj(AppID);

   // 返回结果
   return result;
}

// 应用对象引用计数增 1
bool IncRefAppObj_(TRCAppObj* AppObj)
{
   return (TRCKObjs::_obj() != NULL) && TRCKObjs::_obj()->IncRefAppObj(AppObj);
}

// 应用对象引用计数增 1(注: AppObj 对象必须存在)
void IncRefAppObj(TRCAppObj* AppObj)
{
   TRCKObjs::_IncRefAppObj(AppObj);
}

// 应用对象引用计数减 1(注: AppObj 对象必须存在)
void DecRefAppObj(TRCAppObj* AppObj)
{
   TRCKObjs::_DecRefAppObj(AppObj);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 连接对象(TRCConnObj)的相关函数 */

// 创建连接对象(注: AConnID 必须存在)
TRCConnObj* CreateConnObj(void* AConnID, bool ANeedFree, long* ARetCode)
{
   // 初始化
   TRCConnObj* result  = NULL;
   long        intCode = krNotExist;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->CreateConnObj(AConnID, ANeedFree, intCode);

   // 设置返回码
   if (ARetCode != NULL)
      *ARetCode = intCode;

   // 返回结果
   return result;
}

// 释放连接对象
void FreeConnObj(TRCConnObj* AConnObj)
{
   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      TRCKObjs::_obj()->FreeConnObj(AConnObj);
}

// 连接对象引用计数增 1
TRCConnObj* IncRefConnObj(void* AConnID)
{
   // 初始化
   TRCConnObj* result = NULL;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->IncRefConnObj(AConnID);

   // 返回结果
   return result;
}

// 连接对象引用计数增 1
bool IncRefConnObj_(TRCConnObj* AConnObj)
{
   return (TRCKObjs::_obj() != NULL) && TRCKObjs::_obj()->IncRefConnObj(AConnObj);
}

// 连接对象引用计数增 1(注: AConnObj 对象必须存在)
void IncRefConnObj(TRCConnObj* AConnObj)
{
   TRCKObjs::_IncRefConnObj(AConnObj);
}

// 连接对象引用计数减 1(注: AConnObj 对象必须存在)
void DecRefConnObj(TRCConnObj* AConnObj)
{
   TRCKObjs::_DecRefConnObj(AConnObj);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* 命令对象(TRCCmdObj)的相关函数 */

// 创建命令对象
TRCCmdObj* CreateCmdObj(void* AConnID)
{
   // 初始化
   TRCCmdObj* result = NULL;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->CreateCmdObj(AConnID);

   // 返回结果
   return result;
}

// 创建命令对象(注: AConnObj 必须存在)
TRCCmdObj* CreateCmdObj(TRCConnObj* AConnObj)
{
   // 初始化
   TRCCmdObj* result = NULL;

   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      result = TRCKObjs::_obj()->CreateCmdObj(AConnObj->ConnID());

   // 返回结果
   return result;
}

// 释放命令对象
void FreeCmdObj(TRCCmdObj* ACmdObj)
{
   // 推断是否已经初始化
   if (TRCKObjs::_obj() != NULL)
      TRCKObjs::_obj()->FreeCmdObj(ACmdObj);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* TRCAppObj - RCK 应用类 */

// ---------------- 静态函数 ----------------
// 应用的 OnConnLogin 回调事件
void __stdcall TRCAppObj::_RCKOnConnLogin(void* AppID, void* AConnID, bool& AIsRefused)
{
   TRCAppObj* objApp = TRCKObjs::_obj()->IncRefAppObj(AppID);
   if (objApp != NULL)
   {
      objApp->DoConnLogin(AConnID, AIsRefused);
      TRCKObjs::_DecRefAppObj(objApp);
   }
   else
      AIsRefused  = true;
}

// 应用的 OnConnLogout 回调事件
void __stdcall TRCAppObj::_RCKOnConnLogout(void* AppID, void* AConnID)
{
   TRCAppObj* objApp = TRCKObjs::_obj()->IncRefAppObj(AppID);
   if (objApp != NULL)
   {
      objApp->DoConnLogout(AConnID);
      TRCKObjs::_DecRefAppObj(objApp);
   }
}

// 应用的 OnExecResp 回调事件
void __stdcall TRCAppObj::_RCKOnExecResp(void* AppID, void* AFuncID, void* AConnID)
{
   TRCAppObj* objApp = TRCKObjs::_obj()->IncRefAppObj(AppID);
   if (objApp != NULL)
   {
      objApp->DoExecResp(AFuncID, AConnID);
      TRCKObjs::_DecRefAppObj(objApp);
   }
}

// 应用的 OnRespCancel 回调事件
void __stdcall TRCAppObj::_RCKOnRespCancel(void* AppID, void* AFuncID, void* AConnID)
{
   TRCAppObj* objApp = TRCKObjs::_obj()->IncRefAppObj(AppID);
   if (objApp != NULL)
   {
      objApp->DoRespCancel(AFuncID, AConnID);
      TRCKObjs::_DecRefAppObj(objApp);
   }
}

// 应用的 OnRespConfirm 回调事件
void __stdcall TRCAppObj::_RCKOnRespConfirm(void* AppID, void* AFuncID, void* AConnID)
{
   TRCAppObj* objApp = TRCKObjs::_obj()->IncRefAppObj(AppID);
   if (objApp != NULL)
   {
      objApp->DoRespConfirm(AFuncID, AConnID);
      TRCKObjs::_DecRefAppObj(objApp);
   }
}

// ---------------- 构造函数和析构函数 ----------------
// 构造函数
TRCAppObj::TRCAppObj()
{
   // 初始化
   FData       = NULL;
   FAppID      = NULL;
   FObjTimes   = 1;

   // 创建对象
   FLock       = new TKYCritSect;
   FFuncList   = new TObjList(false, false);

   // 运行初始化事件
   DoInitEvent();
}

// 析构函数
TRCAppObj::~TRCAppObj()
{
   // 推断是否非空
   if (FAppID != NULL)
   {
      // 关闭
      Close();

      // 运行清除函数列表
      DoClearFuncObjs();

      // 释放应用对象
      RCKAppFree(FAppID);
   }

   // 释放对象
   FreeAndNil(FLock);
   FreeAndNil(FFuncList);
}

// ---------------- 私有函数 ----------------
// 运行初始化事件
void TRCAppObj::DoInitEvent()
{
   OnFuncFree.Method       = NULL;
   OnFuncFree.Object       = NULL;

   FOnConnLogin.Method     = NULL;
   FOnConnLogin.Object     = NULL;
   FOnConnLogout.Method    = NULL;
   FOnConnLogout.Object    = NULL;

   FOnExecResp.Method      = NULL;
   FOnExecResp.Object      = NULL;
   FOnRespCancel.Method    = NULL;
   FOnRespCancel.Object    = NULL;
   FOnRespConfirm.Method   = NULL;
   FOnRespConfirm.Object   = NULL;
}

// 运行清除函数列表
void TRCAppObj::DoClearFuncObjs()
{
   // 初始化
   TKYList  objIDs(false);
   TKYList  objObjs(false);
   bool     boolNext = false;

   // 拷贝并清除
   Lock();
   if (FFuncList->Count() > 0)
      try
      {
         // 设置容量
         objIDs.ChangeCapacity(FFuncList->Count());
         objObjs.ChangeCapacity(FFuncList->Count());

         // 循环拷贝
         void* pNode = FFuncList->Next(NULL);
         while (pNode != NULL)
         {
            objIDs.Add(FFuncList->Value(pNode));
            objObjs.Add(FFuncList->Key(pNode));
            pNode = FFuncList->Next(pNode);
         }

         // 清除
         FFuncList->Clear();
         boolNext = true;
      }
      catch (...) {}
   Unlock();

   // 推断是否继续
   if (boolNext)
   {
      // 设置 OnDeletion 事件方法
      objIDs.OnDeletion.Object   = this;
      objIDs.OnDeletion.Method   = (TKYList::TDoDeletion)&TRCAppObj::DoIDDeletion;
      objObjs.OnDeletion.Object  = this;
      objObjs.OnDeletion.Method  = (TKYList::TDoDeletion)&TRCAppObj::DoObjDeletion;

      // 清除
      objIDs.Clear();
      objObjs.Clear();
   }
}

// 激发 OnConnLogin 事件
void TRCAppObj::DoConnLogin(void* AConnID, bool& AIsRefused)
{
   try
   {
      AIsRefused = true;
      ((TObject*)FOnConnLogin.Object->*FOnConnLogin.Method)(this, AConnID, AIsRefused);
   }
   catch (...) {}
}

// 激发 OnConnLogout 事件
void TRCAppObj::DoConnLogout(void* AConnID)
{
   try
   {
      ((TObject*)FOnConnLogout.Object->*FOnConnLogout.Method)(this, AConnID);
   }
   catch (...) {}
}

// 激发 OnExecResp 事件
void TRCAppObj::DoExecResp(void* AFuncID, void* AConnID)
{
   try
   {
      ((TObject*)FOnExecResp.Object->*FOnExecResp.Method)(this, AFuncID, AConnID);
   }
   catch (...) {}
}

// 激发 OnRespCancel 事件
void TRCAppObj::DoRespCancel(void* AFuncID, void* AConnID)
{
   try
   {
      ((TObject*)FOnRespCancel.Object->*FOnRespCancel.Method)(this, AFuncID, AConnID);
   }
   catch (...) {}
}

// 激发 OnRespConfirm 事件
void TRCAppObj::DoRespConfirm(void* AFuncID, void* AConnID)
{
   try
   {
      ((TObject*)FOnRespConfirm.Object->*FOnRespConfirm.Method)(this, AFuncID, AConnID);
   }
   catch (...) {}
}

// 列表的 OnDeletion 事件方法
void TRCAppObj::DoIDDeletion(void* AFuncID)
{
   // 从全局列表中删除
   if (TRCKObjs::_obj()->DeleteFuncID(AFuncID))
      RCKFuncFree(AFuncID);
}

// 列表的 OnDeletion 事件方法
void TRCAppObj::DoObjDeletion(TRCFuncObj* AFuncObj)
{
   // 激发 OnFuncFree 事件
   if (OnFuncFree.Method != NULL)
      try
      {
         ((TObject*)OnFuncFree.Object->*OnFuncFree.Method)(this, AFuncObj);
      }
      catch (...) {}

   // 释放
   InterlockedExchangePointer((Pointer*)&AFuncObj->FAppObj, NULL);
   delete AFuncObj;
}

// ---------------- 公有函数 ----------------
// 设置 Name 属性
long TRCAppObj::SetName(const KYString& AName)
{
   // 初始化
   long result = krUnknown;

   // 操作
   Lock();
   if (FAppID != NULL)
   {
      result = RCKAppSetStr(FAppID, kaaName, (char*)AName, AName.Length());
      if (result == krSuccess)
         FName = AName;
   }
   Unlock();

   // 返回结果
   return result;
}

// 新建函数对象
TRCFuncObj* TRCAppObj::NewFuncObj(const char* AName, long ARetType,  long  ARetSize,
                                                     long ACallMode, void* ACallback)
{
   // 初始化
   TRCFuncObj* result   = NULL;
   KYString    strName  = AName;
   void*       pFuncID  = RCKFuncCreate(FAppID, ACallMode, ACallback,
                                        (char*)strName, ARetType, ARetSize);

   // 推断是否创建成功
   if (pFuncID != NULL)
   {
      // 创建对象并加入列表
      try
      {
         TRCFuncObj* objFunc  = new TRCFuncObj;
         if (objFunc != NULL)
         {
            Lock();
            try
            {
               if (FFuncList->Add(objFunc, pFuncID) != NULL)
               {
                  objFunc->FName    = strName;
                  objFunc->FFuncID  = pFuncID;
                  objFunc->FAppObj  = this;
                  result            = objFunc;

                  // 加入列表
                  TRCKObjs::_obj()->AddFuncID(pFuncID, objFunc);
               }
            }
            catch (...) {}
            Unlock();

            // 若失败则释放对象
            if (result == NULL)
               delete objFunc;
         }
      }
      catch (...) {}

      // 若失败则释放函数
      if (result == NULL)
         RCKFuncFree(pFuncID);
   }

   // 返回结果
   return result;
}

// 删除函数对象
bool TRCAppObj::DeleteFuncObj(TRCFuncObj* AFuncObj)
{
   // 初始化
   bool result = false;

   // 检查状态
   if ((FAppID != NULL) && (State() == kasDefining))
   {
      // 初始化
      void* pFuncID  = NULL;

      // 从列表中删除
      Lock();
      try
      {
         void* pNode = FFuncList->Find(AFuncObj);
         if (pNode != NULL)
         {
            pFuncID  = FFuncList->Value(pFuncID);
            result   = true;
            FFuncList->Remove(pNode);
         }
      }
      catch (...) {}
      Unlock();

      // 推断是否成功
      if (result)
      {
         DoIDDeletion(pFuncID);
         DoObjDeletion(AFuncObj);
      }
   }

   // 返回结果
   return result;
}

// 清除函数定义
void TRCAppObj::ClearFuncObjs()
{
   // 检查状态
   if ((FAppID != NULL) && (State() == kasDefining))
      DoClearFuncObjs();
}

// 设置 OnConnLogin 事件
long TRCAppObj::SetOnConnLogin(TDoConnLogin AMethod, void* AObject)
{
   // 初始化
   long result = krUnknown;

   // 操作
   Lock();
   if (FAppID != NULL)
   {
      void* pFunc = (AMethod != NULL) ? &_RCKOnConnLogin : NULL;
      result = RCKAppSetObj(FAppID, kaaOnConnLogin, pFunc);
      if (result != krSuccess)
         ;
      else if (AMethod != NULL)
      {
         FOnConnLogin.Object = AObject;
         FOnConnLogin.Method = AMethod;
      }
      else
      {
         FOnConnLogin.Method = NULL;
         FOnConnLogin.Object = NULL;
      }
   }
   Unlock();

   // 返回结果
   return result;
}

// 设置 OnConnLogout 事件
long TRCAppObj::SetOnConnLogout(TDoConnLogout AMethod, void* AObject)
{
   // 初始化
   long result = krUnknown;

   // 操作
   Lock();
   if (FAppID != NULL)
   {
      void* pFunc = (AMethod != NULL) ?

&_RCKOnConnLogout : NULL; result = RCKAppSetObj(FAppID, kaaOnConnLogout, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnConnLogout.Object = AObject; FOnConnLogout.Method = AMethod; } else { FOnConnLogout.Method = NULL; FOnConnLogout.Object = NULL; } } Unlock(); // 返回结果 return result; } // 设置 OnExecResp 事件 long TRCAppObj::SetOnExecResp(TDoRespEvent AMethod, void* AObject) { // 初始化 long result = krUnknown; // 操作 Lock(); if (FAppID != NULL) { void* pFunc = (AMethod != NULL) ? &_RCKOnExecResp : NULL; result = RCKAppSetObj(FAppID, kaaOnExecResp, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnExecResp.Object = AObject; FOnExecResp.Method = AMethod; } else { FOnExecResp.Method = NULL; FOnExecResp.Object = NULL; } } Unlock(); // 返回结果 return result; } // 设置 OnRespCancel 事件 long TRCAppObj::SetOnRespCancel(TDoRespEvent AMethod, void* AObject) { // 初始化 long result = krUnknown; // 操作 Lock(); if (FAppID != NULL) { void* pFunc = (AMethod != NULL) ? &_RCKOnRespCancel : NULL; result = RCKAppSetObj(FAppID, kaaOnRespCancel, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnRespCancel.Object = AObject; FOnRespCancel.Method = AMethod; } else { FOnRespCancel.Method = NULL; FOnRespCancel.Object = NULL; } } Unlock(); // 返回结果 return result; } // 设置 OnRespConfirm 事件 long TRCAppObj::SetOnRespConfirm(TDoRespEvent AMethod, void* AObject) { // 初始化 long result = krUnknown; // 操作 Lock(); if (FAppID != NULL) { void* pFunc = (AMethod != NULL) ?

&_RCKOnRespConfirm : NULL; result = RCKAppSetObj(FAppID, kaaOnRespConfirm, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnRespConfirm.Object = AObject; FOnRespConfirm.Method = AMethod; } else { FOnRespConfirm.Method = NULL; FOnRespConfirm.Object = NULL; } } Unlock(); // 返回结果 return result; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* TRCFuncObj - RCK 函数类 */ // ---------------- 构造函数和析构函数 ---------------- // 构造函数 TRCFuncObj::TRCFuncObj() { FData = NULL; FFuncID = NULL; FAppObj = NULL; } // 析构函数 TRCFuncObj::~TRCFuncObj() { TRCAppObj* objApp = (TRCAppObj*)InterlockedExchangePointer((Pointer*)&FAppObj, NULL); if (objApp != NULL) objApp->DeleteFuncObj(this); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* TRCConnObj - RCK 连接类 */ // ---------------- 静态函数 ---------------- // 连接的 OnConnect 回调事件 void __stdcall TRCConnObj::_RCKOnConnect(void* AConnID) { TRCConnObj* objConn = TRCKObjs::_obj()->IncRefConnObj(AConnID); if (objConn != NULL) { objConn->DoConnect(); TRCKObjs::_DecRefConnObj(objConn); } } // 连接的 OnDisconnect 回调事件 void __stdcall TRCConnObj::_RCKOnDisconnect(void* AConnID) { TRCConnObj* objConn = TRCKObjs::_obj()->IncRefConnObj(AConnID); if (objConn != NULL) { objConn->DoDisconnect(); TRCKObjs::_DecRefConnObj(objConn); } } // 连接的 OnLogin 回调事件 void __stdcall TRCConnObj::_RCKOnLogin(void* AConnID) { TRCConnObj* objConn = TRCKObjs::_obj()->IncRefConnObj(AConnID); if (objConn != NULL) { objConn->DoLogin(); TRCKObjs::_DecRefConnObj(objConn); } } // 连接的 OnLogout 回调事件 void __stdcall TRCConnObj::_RCKOnLogout(void* AConnID) { TRCConnObj* objConn = TRCKObjs::_obj()->IncRefConnObj(AConnID); if (objConn != NULL) { objConn->DoLogout(); TRCKObjs::_DecRefConnObj(objConn); } } // 连接的 OnCmdReturn 回调事件 void __stdcall TRCConnObj::_RCKOnCmdReturn(void* AConnID, void* ACmdID, long AResult) { TRCConnObj* objConn = TRCKObjs::_obj()->IncRefConnObj(AConnID); if (objConn != NULL) { objConn->DoCmdReturn(ACmdID, AResult); TRCKObjs::_DecRefConnObj(objConn); } } // 连接的 OnRecvData 回调事件 void __stdcall TRCConnObj::_RCKOnRecvData(void* AConnID, const void* AData, long ASize) { TRCConnObj* objConn = TRCKObjs::_obj()->IncRefConnObj(AConnID); if (objConn != NULL) { objConn->DoRecvData(AData, ASize); TRCKObjs::_DecRefConnObj(objConn); } } // ---------------- 构造函数和析构函数 ---------------- // 构造函数 TRCConnObj::TRCConnObj(void* AConnID, bool ANeedFree) { // 初始化 FData = NULL; FConnID = AConnID; FObjTimes = 1; FCanEvent = false; FNeedFree = ANeedFree && (AConnID != NULL); // 运行初始化事件 DoInitEvent(); } // 析构函数 TRCConnObj::~TRCConnObj() { // 推断是否须要释放连接 if (FNeedFree && (FConnID != NULL)) RCKConnFree(FConnID); } // ---------------- 私有函数 ---------------- // 运行初始化事件 void TRCConnObj::DoInitEvent() { FOnConnect.Method = NULL; FOnConnect.Object = NULL; FOnDisconnect.Method = NULL; FOnDisconnect.Object = NULL; FOnLogin.Method = NULL; FOnLogin.Object = NULL; FOnLogout.Method = NULL; FOnLogout.Object = NULL; FOnCmdReturn.Method = NULL; FOnCmdReturn.Object = NULL; FOnRecvData.Method = NULL; FOnRecvData.Object = NULL; } // 激发 OnConnect 事件 void TRCConnObj::DoConnect() { try { ((TObject*)FOnConnect.Object->*FOnConnect.Method)(this); } catch (...) {} } // 激发 OnDisconnect 事件 void TRCConnObj::DoDisconnect() { try { ((TObject*)FOnDisconnect.Object->*FOnDisconnect.Method)(this); } catch (...) {} } // 激发 OnLogin 事件 void TRCConnObj::DoLogin() { try { ((TObject*)FOnLogin.Object->*FOnLogin.Method)(this); } catch (...) {} } // 激发 OnLogout 事件 void TRCConnObj::DoLogout() { try { ((TObject*)FOnLogout.Object->*FOnLogout.Method)(this); } catch (...) {} } // 激发 OnCmdReturn 事件 void TRCConnObj::DoCmdReturn(void* ACmdID, long AResult) { try { ((TObject*)FOnCmdReturn.Object->*FOnCmdReturn.Method)(this, ACmdID, AResult); } catch (...) {} } // 激发 OnRecvData 事件 void TRCConnObj::DoRecvData(const void* AData, long ASize) { try { ((TObject*)FOnRecvData.Object->*FOnRecvData.Method)(this, AData, ASize); } catch (...) {} } // ---------------- 公有函数 ---------------- // 设置 OnConnect 事件 long TRCConnObj::SetOnConnect(TDoNotify AMethod, void* AObject) { // 初始化 long result = krUnknown; // 检查參数 if (FConnID == NULL) ; else if (!FCanEvent) result = krFailure; else { void* pFunc = (AMethod != NULL) ? &_RCKOnConnect : NULL; result = RCKConnSetObj(FConnID, kcaOnConnect, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnConnect.Object = AObject; FOnConnect.Method = AMethod; } else { FOnConnect.Method = NULL; FOnConnect.Object = NULL; } } // 返回结果 return result; } // 设置 OnDisconnect 事件 long TRCConnObj::SetOnDisconnect(TDoNotify AMethod, void* AObject) { // 初始化 long result = krUnknown; // 检查參数 if (FConnID == NULL) ; else if (!FCanEvent) result = krFailure; else { void* pFunc = (AMethod != NULL) ? &_RCKOnDisconnect : NULL; result = RCKConnSetObj(FConnID, kcaOnDisconnect, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnDisconnect.Object = AObject; FOnDisconnect.Method = AMethod; } else { FOnDisconnect.Method = NULL; FOnDisconnect.Object = NULL; } } // 返回结果 return result; } // 设置 OnLogin 事件 long TRCConnObj::SetOnLogin(TDoNotify AMethod, void* AObject) { // 初始化 long result = krUnknown; // 检查參数 if (FConnID == NULL) ; else if (!FCanEvent) result = krFailure; else { void* pFunc = (AMethod != NULL) ? &_RCKOnLogin : NULL; result = RCKConnSetObj(FConnID, kcaOnLogin, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnLogin.Object = AObject; FOnLogin.Method = AMethod; } else { FOnLogin.Method = NULL; FOnLogin.Object = NULL; } } // 返回结果 return result; } // 设置 OnLogout 事件 long TRCConnObj::SetOnLogout(TDoNotify AMethod, void* AObject) { // 初始化 long result = krUnknown; // 检查參数 if (FConnID == NULL) ; else if (!FCanEvent) result = krFailure; else { void* pFunc = (AMethod != NULL) ? &_RCKOnLogout : NULL; result = RCKConnSetObj(FConnID, kcaOnLogout, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnLogout.Object = AObject; FOnLogout.Method = AMethod; } else { FOnLogout.Method = NULL; FOnLogout.Object = NULL; } } // 返回结果 return result; } // 设置 OnCmdReturn 事件 long TRCConnObj::SetOnCmdReturn(TDoCmdReturn AMethod, void* AObject) { // 初始化 long result = krUnknown; // 检查參数 if (FConnID == NULL) ; else if (!FCanEvent) result = krFailure; else { void* pFunc = (AMethod != NULL) ? &_RCKOnCmdReturn : NULL; result = RCKConnSetObj(FConnID, kcaOnCmdReturn, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnCmdReturn.Object = AObject; FOnCmdReturn.Method = AMethod; } else { FOnCmdReturn.Method = NULL; FOnCmdReturn.Object = NULL; } } // 返回结果 return result; } // 设置 OnRecvData 事件 long TRCConnObj::SetOnRecvData(TDoRecvData AMethod, void* AObject) { // 初始化 long result = krUnknown; // 检查參数 if (FConnID == NULL) ; else if (!FCanEvent) result = krFailure; else { void* pFunc = (AMethod != NULL) ? &_RCKOnRecvData : NULL; result = RCKConnSetObj(FConnID, kcaOnRecvData, pFunc); if (result != krSuccess) ; else if (AMethod != NULL) { FOnRecvData.Object = AObject; FOnRecvData.Method = AMethod; } else { FOnRecvData.Method = NULL; FOnRecvData.Object = NULL; } } // 返回结果 return result; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* TRCCmdObj - RCK 命令类 */ // ---------------- 构造函数和析构函数 ---------------- // 构造函数 TRCCmdObj::TRCCmdObj() { FData = NULL; FCmdID = RCKCmdCreate(); // 设置关联对象 if (FCmdID != NULL) RCKCmdSetObj(FCmdID, kmaData, this); } // 析构函数 TRCCmdObj::~TRCCmdObj() { if (FCmdID != NULL) { // 清除关联 RCKCmdSetObj(FCmdID, kmaData, NULL); // 释放对象 RCKCmdFree(FCmdID); } } // ---------------- 公有函数 ---------------- // 取连接对象 TRCConnObj* TRCCmdObj::ConnObj() const { // 初始化 TRCConnObj* result = NULL; void* pConnID = RCKCmdGetObj(FCmdID, kmaConnObj, NULL); // 推断是否非空 if (pConnID != NULL) { result = TRCKObjs::_obj()->IncRefConnObj(pConnID); if (result != NULL) TRCKObjs::_DecRefConnObj(result); } // 返回结果 return result; } // 运行带參数的命令 long TRCCmdObj::ExecByParams(Longword ATimeout, ...) const { // 取參数个数 long intCount = ParamCount(); if (intCount > 0) { // 初始化 const void* pUnknow; // 清除參数 RCKCmdParamClear(FCmdID); // 可变參数初始化 va_list arrArgs; va_start(arrArgs, ATimeout); // 加入參数 for (long intNo = 0; intNo < intCount; intNo++) switch (ParamType(intNo)) { case kdtString: RCKCmdParamByStr(FCmdID, intNo, va_arg(arrArgs, const char*), -1); break; case kdtInteger: RCKCmdParamByInt(FCmdID, intNo, va_arg(arrArgs, int)); break; case kdtBoolean: RCKCmdParamByBool(FCmdID, intNo, va_arg(arrArgs, bool)); break; case kdtDouble: RCKCmdParamByFloat(FCmdID, intNo, va_arg(arrArgs, double)); break; case kdtStruct: RCKCmdParamByStruct(FCmdID, intNo, va_arg(arrArgs, void*)); break; case kdtByte: RCKCmdParamByInt(FCmdID, intNo, va_arg(arrArgs, Byte)); break; case kdtWord: RCKCmdParamByInt(FCmdID, intNo, va_arg(arrArgs, Word)); break; case kdtSmallint: RCKCmdParamByInt(FCmdID, intNo, va_arg(arrArgs, Smallint)); break; case kdtInt64: RCKCmdParamByInt64(FCmdID, intNo, va_arg(arrArgs, __int64)); break; case kdtDateTime: RCKCmdParamByDate(FCmdID, intNo, va_arg(arrArgs, double)); break; case kdtCurrency: RCKCmdParamByFloat(FCmdID, intNo, va_arg(arrArgs, double)); break; default: pUnknow = va_arg(arrArgs, const void*); } // 可变參数释放 va_end(arrArgs); } // 运行 return RCKCmdExecute(FCmdID, ATimeout); } // 取返回值的字符串值 KYString TRCCmdObj::RetAsStr() const { // 初始化 KYString result; long intSize; char* pRet; // 取返回值 pRet = RCKCmdRetStr(FCmdID, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取返回字段的字符串值 KYString TRCCmdObj::FieldAsStr(long AIndex) const { // 初始化 KYString result; long intSize; char* pRet; // 取返回字段值 pRet = RCKCmdFieldStr(FCmdID, AIndex, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取返回字段的字符串值 KYString TRCCmdObj::FieldAsStr(const char* AName) const { // 初始化 KYString result; long intSize; char* pRet; // 取返回字段值 pRet = RCKCmdFieldStr_(FCmdID, AName, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* TRCResp - RCK 应答类 */ // ---------------- 静态函数 ---------------- // 取返回值的字符串值 KYString TRCResp::RetAsStr() { // 初始化 KYString result; long intSize; char* pRet; // 取返回值 pRet = RCKRespRetStr(&intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取返回字段的字符串值 KYString TRCResp::FieldAsStr(long AIndex) { // 初始化 KYString result; long intSize; char* pRet; // 取返回字段值 pRet = RCKRespFieldStr(AIndex, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取返回字段的字符串值 KYString TRCResp::FieldAsStr(const char* AName) { // 初始化 KYString result; long intSize; char* pRet; // 取返回字段值 pRet = RCKRespFieldStr_(AName, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取參数的字符串值 KYString TRCResp::ParamAsStr(long AIndex) { // 初始化 KYString result; long intSize; char* pRet; // 取參数值 pRet = RCKRespParamStr(AIndex, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取參数的字符串值 KYString TRCResp::ParamAsStr(const char* AName) { // 初始化 KYString result; long intSize; char* pRet; // 取參数值 pRet = RCKRespParamStr_(AName, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* TRCFields - RCK 字段集类 */ // ---------------- 构造函数和析构函数 ---------------- // 构造函数 TRCFields::TRCFields() { FFields = RCKFieldsCreate(); FIsBound = (FFields != NULL); } TRCFields::TRCFields(void* AFields) { FFields = AFields; FIsBound = false; } // 析构函数 TRCFields::~TRCFields() { if (FIsBound) RCKFieldsFree(FFields); } // ---------------- 公有函数 ---------------- // 取字段定义集字符串 KYString TRCFields::FieldDefs() const { // 初始化 KYString result; long intSize; char* pRet; // 取字段值 pRet = (char*)RCKFieldDefsOutput(FFields, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取字段集字符串 KYString TRCFields::DataSet() const { // 初始化 KYString result; long intSize; char* pRet; // 取字段值 pRet = (char*)RCKFieldsOutput(FFields, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取字段的字符串值 KYString TRCFields::AsStr(long AIndex) const { // 初始化 KYString result; long intSize; char* pRet; // 取字段值 pRet = RCKFieldAsStr(FFields, AIndex, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } // 取字段的字符串值 KYString TRCFields::AsStr(const char* AName) const { // 初始化 KYString result; long intSize; char* pRet; // 取字段值 pRet = RCKFieldAsStr_(FFields, AName, &intSize); if (pRet != NULL) result.SetString(pRet, intSize); // 返回结果 return result; } /* end namespace */ } }


以上是关于远程调用内核接口的封装类(RCKObjs)的主要内容,如果未能解决你的问题,请参考以下文章

Java 封装

Java 封装

Java 封装

Java 封装

Java 封装

Java 封装