gusucode.com > 通用协议编解码模块C#源码程序 > 通用协议编解码模块/codec_src/Codec_src/MsgCodec/CollMsgNode.cpp

    // CollMsgNode.cpp: implementation of the CCollMsgNode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CollMsgNode.h"


DEFINE_NODE_IMPLEMENTATION(CollMsgNode)

CCollMsgNode::CCollMsgNode()
{
}

CCollMsgNode::~CCollMsgNode()
{
}


BOOL GetParamElem(CString &strElem, CString &strParam, BOOL bRemoveElem = TRUE);

BOOL CCollMsgNode::ParseParam(LPCTSTR szParam)
{
	BOOL bResult = TRUE;
	CString str, strParam(szParam);

	//	Node Type: 
	GetParamElem(str, strParam);
	if (str.CompareNoCase("Number") == 0)
		m_mntNodeType = mntNumber;
	else if (str.CompareNoCase("Octet") == 0)
		m_mntNodeType = mntOctet;
	else if (str.CompareNoCase("ASCII") == 0)
		m_mntNodeType = mntASCII;
	else if (str.CompareNoCase("BCD") == 0)
		m_mntNodeType = mntBCD;
	else
		m_mntNodeType = mntOctet;

	//	Node Length: 
	GetParamElem(str, strParam);
	m_bFixLength = TRUE;
	m_PosInfo.nBitLen = ::atol(str);
	if (str.IsEmpty())
	{
		m_bFixLength = FALSE;
	}
	else
	{
		m_bFixLength = TRUE;

		if (str.Right(3).CompareNoCase("bit") == 0)
			;
		else if (str.Right(4).CompareNoCase("byte") == 0)
			m_PosInfo.nBitLen *= 8;
		else if (str.Right(4).CompareNoCase("word") == 0)
			m_PosInfo.nBitLen *= 16;
		else if (str.Right(5).CompareNoCase("dword") == 0)
			m_PosInfo.nBitLen *= 32;
		else
			m_PosInfo.nBitLen *= 8;
	}

	//	Children Nodes: 
	GetParamElem(str, strParam);
	while (! str.IsEmpty())
	{
		m_ChildrenIndList.AddTail(str);
		GetParamElem(str, strParam);
	}

	return TRUE;
}

BOOL CCollMsgNode::GetValue(DWORD &nValue)
{
	return CMsgNode::GetValue(nValue);
}

BOOL CCollMsgNode::GetValue(const BYTE **pszValue, LONG &nByteLength)
{
	return CMsgNode::GetValue(pszValue, nByteLength);
}

BOOL CCollMsgNode::SetValue(DWORD nValue)
{
	return CMsgNode::SetValue(nValue);
}

BOOL CCollMsgNode::SetValue(const BYTE *szValue, LONG nByteLength)
{
	return CMsgNode::SetValue(szValue, nByteLength);
}

UINT CCollMsgNode::PrepareDecode(LONG &nByteStart, LONG &nBitStart)
{
	UINT nResult = CMsgNode::PrepareDecode(nByteStart, nBitStart);

	if (nResult == CDC_OK && (m_ChildrenList.GetCount() == 0))
	{
		//	Create Children
		CString strChildInd;
		IMsgNode *pNode;
		IPrivateNode *pPrivate;
		POSITION pos = m_ChildrenIndList.GetHeadPosition();
		while (pos)
		{
			strChildInd = m_ChildrenIndList.GetNext(pos);
			pNode = CodecPrivateServ::CreateNode(strChildInd);
			if (pNode)
			{
				pNode->GetInterface(IN_IPrivateNode, (void**)&pPrivate);
				pPrivate->InitInstance(m_pRoot, this);
				m_ChildrenList.AddTail(pNode);
			}
			else
			{
				nResult = CDC_UNKNOWN_MSG;
			}
		}
	}

	return nResult;
}

UINT CCollMsgNode::Decoding(LONG &nByteStart, LONG &nBitStart)
{
	UINT nResult = CDC_OK;
	IMsgNode *pNode;
	IPrivateNode *pPrivate;
	POSITION pos = m_ChildrenList.GetHeadPosition();
	while (pos)
	{
		pNode = m_ChildrenList.GetNext(pos);
		pNode->GetInterface(IN_IPrivateNode, (void**)&pPrivate);
		nResult = pPrivate->RefreshNode(nByteStart, nBitStart);
	}
	return nResult;
}

UINT CCollMsgNode::FinishDecode(LONG &nByteStart, LONG &nBitStart)
{
	if (m_bFixLength)
	{
#ifdef _DEBUG
		if (m_PosInfo.nBitLen != ((nByteStart - m_PosInfo.nByteStart) * 8 + nBitStart - m_PosInfo.nBitStart))
			TRACE("Error: Length not matched when decode the Fix Length node - %s @%d:%d\n", m_strNodeName, nByteStart, nBitStart);
#endif //_DEBUG
	}
	else
	{
		m_PosInfo.nBitLen = (nByteStart - m_PosInfo.nByteStart) * 8 + nBitStart - m_PosInfo.nBitStart;
	}

	return CMsgNode::FinishDecode(nByteStart, nBitStart);
}