三、08年2月29日
作者:青青子衿
email:[email protected]
1、在utility.cpp文件中定義了
bool recv_line ( int sSocket , char * szBuffer , int iBufSize , CSSLSocket * pSSLSocket = NULL );
bool recv_line_irc ( int sSocket , char * szBuffer , int iBufSize , CSSLSocket * pSSLSocket = NULL );
兩個函數,可能和數據通訊有關係。
2、 main函數中 CCommands m_cCommands 變量在 m_cCVar . Init () 函數中被調用,
對 CCommands 做詳細分析:
定義:
class CCommands : public CCommandHandler
{
public :
CCommands ();
void RegisterCommand ( command * pCommand , const char * szName , const char * szDescription , CCommandHandler * pHandler );
command * FindCommandByName ( const char * szName , bool bExact );
bool HandleCommand ( CMessage * pMsg );
command m_cmdList ; //構造函數中用,在構造函數中將m_cmdList,裝入到m_lCommands命令列表中,作爲列表中的第一個元素
protected :
list < command *> m_lCommands ; //各種指令的名稱和描述符,保存到數據結構command中,放到list列表中
};
有2個成員變量:
public : command m_cmdList ; //構造函數中用,在構造函數中將m_cmdList, 裝入到m_lCommands命令列表中,作爲列表 中的第一個元素
list < command *> m_lCommands ; //各種指令的名稱和描述符,保存到數據結構 command中,放到list列表中
共4個成員函數:
(1)、 CCommands :: CCommands ()
{
RegisterCommand (& m_cmdList , "commands.list" , "Lists all available commands" , this );
}
調用 RegisterCommand 函數,將指令加入到列表中,
(2) RegisterCommand 函數
//////////////////////////////////////////////////////////////////////////////////////
//
//函數功能:註冊指令
//參數:command *pCommand 指令
// const char *szName 名字
// const char *szDescription 描述
// CCommandHandler *pHandler
//返回值:void
//
////////////////////////////////////////////////////////////////////////////////////////
void CCommands :: RegisterCommand ( command * pCommand , const char * szName , const char * szDescription , CCommandHandler * pHandler )
{
pCommand -> sName . Assign ( szName ); / // 指令的名稱
pCommand -> sDescription . Assign ( szDescription ); / // 指令的描述
pCommand -> pHandler = pHandler ; / // ???指令的處理方法,還不確定
m_lCommands . push_back ( pCommand );
}
即將要添加的指令加入到 m_lCommands 列表中
(3) FindCommandByName
//////////////////////////////////////////////////////////////////////////////////////
//
//函數功能:通過指令名稱,在指令列表中查找一條指令
//參數:const char *szName 指令的名稱
// bool bExact 是否直接執行該指令
//返回值:爲指令結構體指針變量
//
////////////////////////////////////////////////////////////////////////////////////////
command * CCommands :: FindCommandByName ( const char * szName , bool bExact )
{
list < command *>:: iterator ic ; // Cycle through commands and find the string
for ( ic = m_lCommands . begin (); ic != m_lCommands . end (); ++ ic )
{
//從列表的第一個元素到最後一個元素依次比較,找到名字相符的,返回該command指針變量
if (!(* ic )-> sName . Compare ( szName ))
{
return (* ic );
}
}
return NULL ;
}
(4) bool CCommands :: HandleCommand ( CMessage * pMsg )
該函數是從 CCommandHandler 類中的純虛函數中繼承過來的, 猜測該函數的作用是執行具體的指令,具體細節還不清楚, 特別是參數 CMessage *pMsg 不清楚
結論:所有關於指令操作的類,都是 有基類 CCommandHandler 擴展而來的,包括正在分析的 CCommands 類, CCommandHandler 不實現任何功能,只是定義的虛函數 virtual bool HandleCommand ( CMessage * pMsg )=0; 而具體的功能由繼承它的子類來實現。
3、 main函數中
m_cCVar . Init (); 代碼的關注。
通過分析 class CCVar : public CCommandHandler 類的主要功能來實現。
類的申明:
class CCommandHandler ;
typedef struct cvar_s
{
CString sValue ;
float fValue ;
bool bValue ;
int iValue ;
CString sName ;
CString sDescription ;
bool bSave ;
} cvar ;
class CCVar : public CCommandHandler
{
public :
CCVar ();
~ CCVar ();
void Init ();
void RegisterCvar ( cvar * pCvar , const CString & sName , const CString & sValue , const CString & sDescription , bool bSave , bool bEncrypted , int iKey );
void RegisterCvar ( cvar * pCvar , const char * szName , const char * szValue , const char * szDescription , bool bSave , bool bEncrypted , int iKey );
cvar * FindCvarByName ( const CString & sName , bool bExact );
cvar * FindCvarByName ( const char * szName , bool bExact );
bool ParseConfig ( const CString & sFilename );
bool ParseConfig ( const char * szFilename );
bool SaveConfig ( const CString & sFilename );
bool SaveConfig ( const char * szFilename );
void SetCVar ( cvar * pCvar , const char * szValue );
void SetCVar ( cvar * pCvar , const CString & sValue );
void SetCVar ( cvar * pCvar , float fValue );
void SetCVar ( cvar * pCvar , bool bValue );
void SetCVar ( cvar * pCvar , int iValue );
bool HandleCommand ( CMessage * pMsg ); //##從父函數的虛函數中繼承而來。
command m_cmdList , m_cmdGet , m_cmdSet , m_cmdLoadConfig , m_cmdSaveConfig ;
protected :
list < cvar *> m_lCvars ;
};
成員變量:
command m_cmdList , m_cmdGet , m_cmdSet , m_cmdLoadConfig , m_cmdSaveConfig ;
list < cvar *> m_lCvars ; //cvar* 類型的列表
成員函數:
(1) 、構造函數與析構函數
///////////////////////////////////////////
//
//函數功能:構造函數,清除m_lCvars列表
//
//////////////////////////////////////////
CCVar :: CCVar ()
{
m_lCvars . clear ();
}
///////////////////////////////////////////
//
//函數功能:析構函數,清除m_lCvars列表
//
//////////////////////////////////////////
CCVar ::~ CCVar ()
{
m_lCvars . clear ();
}
(2) 、 Init () 函數
////////////////////////////////////////////////////////////////////
//
//函數功能:初始化函數,註冊有本類負責處理的一系列的指令
//參數: 無
//返回值: 無
//
////////////////////////////////////////////////////////////////////
void CCVar :: Init ()
{
//command m_cmdList, m_cmdGet, m_cmdSet, m_cmdLoadConfig, m_cmdSaveConfig;
//都是結構體command類型的變量,註冊由CCVar類來處理的指令
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdList , "cvar.list" , "prints a list of all cvars" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdGet , "cvar.get" , "gets the content of a cvar" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdSet , "cvar.set" , "sets the content of a cvar" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdLoadConfig , "cvar.loadconfig" , "loads config from a file" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdSaveConfig , "cvar.saveconfig" , "saves config to a file" , this );
}
(3) 、 RegisterCvar 註冊函數
/////////////////////////////////////////////////////////////////
//
//函數功能:註冊 cvar結構體數據
//參數:cvar *pCvar cvar結構體指針
// const CString &sName 名稱
// const CString &sValue 值
// const CString &sDescription 描述
// bool bSave 是否保存
// bool bEncrypted 是否加密
// int iKey 密鑰???不確定
//
/////////////////////////////////////////////////////////////////
void CCVar :: RegisterCvar ( cvar * pCvar , const CString & sName , const CString & sValue , const CString & sDescription , bool bSave , bool bEncrypted , int iKey )
{
/*
typedef struct cvar_s
{
CString sValue;
float fValue;
bool bValue;
int iValue;
CString sName;
CString sDescription;
bool bSave;
} cvar;
*/
pCvar -> sName . Assign ( sName );
pCvar -> sDescription . Assign ( sDescription );
pCvar -> bSave = bSave ;
pCvar -> sValue . m_bIsCryptStr = bEncrypted ; //sValue是一個CString類型的字符串變量,CString並不是MFC中的字符串類,而是程序自己定義的一個類,可能包括,加密功能
pCvar -> sValue . m_iCryptKey = iKey ;
pCvar -> sValue . Assign ( sValue );
SetCVar ( pCvar , sValue ); //調用另一個成員函數,之後再分析
m_lCvars . push_back ( pCvar );
}
//另一個重載的 RegisterCvar 函數
void CCVar :: RegisterCvar ( cvar * pCvar , const char * szName , const char * szValue , const char * szDescription , bool bSave , bool bEncrypted , int iKey )
{
RegisterCvar ( pCvar , CString ( szName ), CString ( szValue ), CString ( szDescription ), bSave , bEncrypted , iKey );
}
//主要參數有CString變爲 const char *szName 功能不變
(4) 、 FindCvarByName 函數
///////////////////////////////////////////////////////////////
//
//函數功能:通過名字從CVar列表中檢索指定的元素
//參數: const CString &sName 名稱
// bool bExact 是否執行,該參數並不起作用
//返回值: cvar * 類型的指針
//
//////////////////////////////////////////////////////////////
cvar * CCVar :: FindCvarByName ( const CString & sName , bool bExact )
{
list < cvar *>:: iterator ic ;
int iCount =0;
for ( ic = m_lCvars . begin (); ic != m_lCvars . end (); ++ ic )
if (!(* ic )-> sName . CompareNoCase ( sName ))
{
return (* ic );
}
return NULL ;
}
///////////////////////////////////////////////////////////////
//
//函數功能:通過名字從CVar列表中檢索指定的元素
// 上一個函數的重載函數。
//
//////////////////////////////////////////////////////////////
cvar * CCVar :: FindCvarByName ( const char * szName , bool bExact )
{
return FindCvarByName ( CString ( szName ), bExact );
}
(5) ParseConfig 函數,保存配置,沒有實現任何代碼
bool CCVar :: ParseConfig ( const CString & sFilename )
{
return true ;
}
bool CCVar :: ParseConfig ( const char * szFilename )
{
return ParseConfig ( CString ( szFilename ));
}
(6) SaveConfig 函數,保存配置,沒有實現任何代碼
bool CCVar :: SaveConfig ( const CString & sFilename )
{
return true ;
}
bool CCVar :: SaveConfig ( const char * szFilename )
{
return SaveConfig ( CString ( szFilename ));
}
(7) 、 SetCVar 函數, RegisterCvar 函數中會調用本函數
////////////////////////////////////////////////////
//
//函數功能:cvar *pCvar 指向cvar結構體類型的指針
// const char *szValue 準備設置的數值
//
/////////////////////////////////////////////////////
void CCVar :: SetCVar ( cvar * pCvar , const char * szValue )
{
pCvar -> sValue . Assign ( szValue );
pCvar -> fValue =( float ) atof ( pCvar -> sValue . CStr ());
pCvar -> iValue = atoi ( pCvar -> sValue . CStr ());
pCvar -> bValue = false ;
if (! pCvar -> sValue . Compare ( "true" )) pCvar -> bValue = true ; //如果szValue字符串不是true,則pCvar->bValue設爲true
}
void CCVar :: SetCVar ( cvar * pCvar , const CString & sValue )
{
pCvar -> sValue . Assign ( sValue );
pCvar -> fValue =( float ) atof ( pCvar -> sValue . CStr ());
pCvar -> iValue = atoi ( pCvar -> sValue . CStr ());
pCvar -> bValue = false ;
if (! pCvar -> sValue . Compare ( "true" )) pCvar -> bValue = true ;
}
void CCVar :: SetCVar ( cvar * pCvar , float fValue )
{
pCvar -> sValue . Format ( "%f" , fValue );
pCvar -> fValue = fValue ;
pCvar -> iValue = atoi ( pCvar -> sValue . CStr ());
pCvar -> bValue = false ;
if ( fValue >=1) pCvar -> bValue = true ; //如果fValue大於1,pCvar->bValue的值設爲true
}
void CCVar :: SetCVar ( cvar * pCvar , bool bValue )
{
if ( bValue ) //如果bValue是true
{
pCvar -> sValue . Assign ( "true" );
pCvar -> fValue =1.0f;
pCvar -> iValue =1;
}
else
{
pCvar -> sValue . Assign ( "false" );
pCvar -> fValue =0.0f;
pCvar -> iValue =0;
}
pCvar -> bValue = bValue ;
}
void CCVar :: SetCVar ( cvar * pCvar , int iValue )
{
pCvar -> sValue . Format ( "%d" , iValue );
pCvar -> fValue =( float ) iValue ;
pCvar -> iValue = iValue ;
pCvar -> bValue = false ;
if ( iValue >=1) //如果iValue大於等於1,pCvar->bValue值設爲true
{
pCvar -> bValue = true ;
}
}
(8)、 bool HandleCommand ( CMessage * pMsg ); //##從父函數的虛函數中繼承而來。
具體作用還不清楚,或許需要看了 CMessage 類的實現之後,才能明白