gusucode.com > 通用协议编解码模块C#源码程序 > 通用协议编解码模块/codec_src/Codec_src/MsgCodec/PrivateCodecServ.cpp
// PrivateCodecServ.cpp: implementation of the PrivateCodecServ class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "MsgCodec.h" #include <MsgCodec/MsgCodecs.h> #include "PrivateCodecServ.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif // Enumeration Table Functions CMapStringToPtr *gx_pEnumTableMap = NULL; IEnumTable* CodecPrivateServ::GetEnumTable(LPCTSTR szName) { if (szName) { void *pEnumTable = NULL; if (gx_pEnumTableMap->Lookup(szName, pEnumTable)) return (IEnumTable*)pEnumTable; TRACE("Error: Enumeration Table \"%s\" Not Found\n", szName); } return NULL; } BOOL CodecPrivateServ::RegisterEnumTable(IEnumTable* pEnumTable) { static CMapStringToPtr x_EnumTableMap; gx_pEnumTableMap = &x_EnumTableMap; if (pEnumTable) { gx_pEnumTableMap->SetAt(pEnumTable->GetEnumTableName(), pEnumTable); return TRUE; } return FALSE; } // Message Node Information Functions CMapStringToPtr *gx_pNodeInfoMap; const TNodeInfo* CodecPrivateServ::GetNodeInfoStrings(LPCTSTR szNodeInd) { if (szNodeInd) { void *pNodeInfo = NULL; if (gx_pNodeInfoMap->Lookup(szNodeInd, pNodeInfo)) return (TNodeInfo*)pNodeInfo; TRACE("Error: Node Information \"%s\" Not Found\n", szNodeInd); } return NULL; } LPCTSTR CodecPrivateServ::GetNodeImp(LPCTSTR szNodeInd) { const TNodeInfo *pNodeInfo = GetNodeInfoStrings(szNodeInd); if (pNodeInfo) return pNodeInfo->szImpName; return NULL; } LPCTSTR CodecPrivateServ::GetNodeParam(LPCTSTR szNodeInd) { const TNodeInfo *pNodeInfo = GetNodeInfoStrings(szNodeInd); if (pNodeInfo) return pNodeInfo->szImpParam; return NULL; } const BYTE* CodecPrivateServ::GetNodeDefaultTemplate(LPCTSTR szNodeInd, LONG &nLength) { const TNodeInfo *pNodeInfo = GetNodeInfoStrings(szNodeInd); if (pNodeInfo) { nLength = pNodeInfo->DefaultTemplate.nLength; return pNodeInfo->DefaultTemplate.szTemplate; } return NULL; } BOOL CodecPrivateServ::RegisterNodeInfoStrings(LPCTSTR szNodeInd, const TNodeInfo* pNodeInfo) { static CMapStringToPtr gx_NodeInfoMap; gx_pNodeInfoMap = &gx_NodeInfoMap; if (szNodeInd && pNodeInfo) { gx_NodeInfoMap.SetAt(szNodeInd, (void*)pNodeInfo); return TRUE; } return FALSE; } // Message Node Implementation Functions CMapStringToPtr *gx_pNodeImpMap = NULL; OBN CodecPrivateServ::GetNodeClass(LPCTSTR szImpName) { if (szImpName) { void *pNodeImp = NULL; if (gx_pNodeImpMap->Lookup(szImpName, pNodeImp)) return (OBN)pNodeImp; TRACE("Error: Node Implementation \"%s\" Not Found\n", szImpName); } return NULL; } BOOL CodecPrivateServ::RegisterNodeClass(LPCTSTR szImpName, OBN obnClassID) { static CMapStringToPtr x_NodeImpMap; gx_pNodeImpMap = &x_NodeImpMap; if (szImpName && obnClassID) { gx_pNodeImpMap->SetAt(szImpName, obnClassID); return TRUE; } return FALSE; } // Functions for Dynamic Creations DECLARE_OBJECT(MsgBlock) ICodec* CodecPrivateServ::CreateBlock(LPCTSTR szNodeInd) { ICodec *pCodec = NULL; if (GetNodeInfoStrings(szNodeInd)) { IPrivateBlock *pBlock = NULL; if (ObjServ::CreateInstance(ON_MsgBlock, IN_IPrivateBlock, (void**)&pBlock)) { pBlock->InitBlock(szNodeInd); pBlock->GetInterface(IN_ICodec, (void**)&pCodec); } } return pCodec; } IMsgNode* CodecPrivateServ::CreateNode(LPCTSTR szNodeInd) { IMsgNode *pNode = NULL; const TNodeInfo* pNodeInfo = GetNodeInfoStrings(szNodeInd); if (pNodeInfo) { OBN obnClass; if (obnClass = GetNodeClass(pNodeInfo->szImpName)) { IPrivateNode *pPrivateNode = NULL; if (ObjServ::CreateInstance(obnClass, IN_IPrivateNode, (void**)&pPrivateNode)) { pPrivateNode->GetInterface(IN_IMsgNode, (void**)&pNode); pPrivateNode->InstantNode(szNodeInd, pNodeInfo->szImpParam); } } } return pNode; } //IEnumTable* CodecServ::GetEnumTable(LPCTSTR szName) IEnumTable* g_GetEnumTable(LPCTSTR szName) { return CodecPrivateServ::GetEnumTable(szName); } //ICodec* CodecServ::CreateBlock(LPCTSTR szNodeInd) ICodec* g_CreateBlock(LPCTSTR szNodeInd) { return CodecPrivateServ::CreateBlock(szNodeInd); } //UINT CodecServ::DecodeMsg(BYTE *pMsg, LONG &nLen, IMsgNode*& pRootNode, LPCTSTR szRootName) UINT g_DecodeMsg(BYTE *pMsg, LONG &nLen, IMsgNode*& pRootNode, LPCTSTR szRootName) { UINT nResult = CDC_INNER_ERROR; // ICodec *pCodec = CodecServ::CreateBlock(szRootName); ICodec *pCodec = g_CreateBlock(szRootName); if (pCodec) { nResult = pCodec->DecodeMsg(pMsg, nLen); IMsgNode *pNode = NULL; pCodec->GetInterface(IN_IMsgNode, (void**)&pNode); pRootNode = pNode; } return nResult; }