ecpacket.cpp

来自「wxWidgets写的电驴」· C++ 代码 · 共 846 行 · 第 1/2 页

CPP
846
字号
	if (m_tagList.back().m_error == 0) {		return true;	} else {		m_error = m_tagList.back().m_error;#ifndef KEEP_PARTIAL_PACKETS		m_tagList.pop_back();#endif		return false;	}}bool CECTag::ReadFromSocket(CECSocket& socket){	if (m_state == bsName) {		ec_tagname_t tmp_tagName;		if (!socket.ReadNumber(&tmp_tagName, sizeof(ec_tagname_t))) {			m_tagName = 0;			return false;		} else {			m_tagName = tmp_tagName >> 1;			if (tmp_tagName & 0x01) {				m_state = bsLengthChld;			} else {				m_state = bsLength;			}		}	}	if (m_state == bsLength || m_state == bsLengthChld) {		ec_taglen_t tagLen;		if (!socket.ReadNumber(&tagLen, sizeof(ec_taglen_t))) {			return false;		} else {			m_dataLen = tagLen;			if (m_state == bsLength) {				m_state = bsData1;			} else {				m_state = bsChildCnt;			}		}	}	if (m_state == bsChildCnt || m_state == bsChildren) {		if (!ReadChildren(socket)) {			return false;		}	}	if (m_state == bsData1) {		unsigned int tmp_len = m_dataLen;		m_dataLen = 0;		m_dataLen = tmp_len - GetTagLen();		if (m_dataLen > 0) {			m_tagData = malloc(m_dataLen);			m_state = bsData2;		} else {			m_tagData = NULL;			m_state = bsFinished;		}	}	if (m_state == bsData2) {		if (m_tagData != NULL) {			if (!socket.ReadBuffer((void *)m_tagData, m_dataLen)) {				return false;			} else {				m_state = bsFinished;			}		} else {			m_error = 1;			return false;		}	}	return true;}bool CECTag::WriteTag(CECSocket& socket) const{	ec_taglen_t tagLen = GetTagLen();	ec_tagname_t tmp_tagName = (m_tagName << 1) | (m_tagList.empty() ? 0 : 1);		if (!socket.WriteNumber(&tmp_tagName, sizeof(ec_tagname_t))) return false;	if (!socket.WriteNumber(&tagLen, sizeof(ec_taglen_t))) return false;	if (!m_tagList.empty()) {		if (!WriteChildren(socket)) return false;	}	if (m_dataLen > 0) {		if (m_tagData != NULL) {	// This is here only to make sure everything, it should not be NULL at this point			if (!socket.WriteBuffer(m_tagData, m_dataLen)) return false;		}	}	return true;}bool CECTag::ReadChildren(CECSocket& socket){	if (m_state == bsChildCnt) {		uint16 tmp_tagCount;		if (!socket.ReadNumber(&tmp_tagCount, 2)) {			return false;		} else {			m_tagList.clear();			if (tmp_tagCount > 0) {				m_tagList.reserve(tmp_tagCount);				for (int i=0; i<tmp_tagCount; i++) {					m_tagList.push_back(CECTag(socket));				}				m_state = bsChildren;			} else {				m_state = bsData1;			}		}	}	if (m_state == bsChildren) {		for (unsigned int i=0; i<m_tagList.size(); i++) {			CECTag& tag = m_tagList[i];			if (!tag.IsOk()) {				if (!tag.ReadFromSocket(socket)) {					if (tag.m_error != 0) {						m_error = tag.m_error;					}					return false;				}			}		}		m_state = bsData1;	}	return true;}bool CECTag::WriteChildren(CECSocket& socket) const{    uint16 tmp = m_tagList.size();	if (!socket.WriteNumber(&tmp, sizeof(tmp))) return false;	if (!m_tagList.empty()) {		for (TagList::size_type i=0; i<m_tagList.size(); i++) {			if (!m_tagList[i].WriteTag(socket)) return false;		}	}	return true;}/** * Finds the (first) child tag with given name. * * @param name TAG name to look for. * @return the tag found, or NULL. */const CECTag* CECTag::GetTagByName(ec_tagname_t name) const{	for (TagList::size_type i=0; i<m_tagList.size(); i++)		if (m_tagList[i].m_tagName == name) return &m_tagList[i];	return NULL;}/** * Finds the (first) child tag with given name. * * @param name TAG name to look for. * @return the tag found, or NULL. */CECTag* CECTag::GetTagByName(ec_tagname_t name){	for (TagList::size_type i=0; i<m_tagList.size(); i++)		if (m_tagList[i].m_tagName == name) return &m_tagList[i];	return NULL;}/** * Finds the (first) child tag with given name. * * @param name TAG name to look for. * @return the tag found, or a special null-valued tag otherwise. * * @see s_theNullTag */const CECTag* CECTag::GetTagByNameSafe(ec_tagname_t name) const{	const CECTag* result = GetTagByName(name);	if (result == NULL)		result = &s_theNullTag;	return result;}/** * Query TAG length that is suitable for the TAGLEN field (i.e.\  * without it's own header size). * * @return Tag length, containing its childs' length. */uint32 CECTag::GetTagLen(void) const{	uint32 length = m_dataLen;	for (TagList::size_type i=0; i<m_tagList.size(); i++) {		length += m_tagList[i].GetTagLen();		length += sizeof(ec_tagname_t) + sizeof(ec_taglen_t) + ((m_tagList[i].GetTagCount() > 0) ? 2 : 0);	}	return length;}/** * Returns an EC_IPv4_t class. * * This function takes care of the enadianness of the port number. * * @return EC_IPv4_t class. * * @see CECTag(ec_tagname_t, const EC_IPv4_t&) */EC_IPv4_t CECTag::GetIPv4Data(void) const{	EC_IPv4_t p;	RawPokeUInt32( p.m_ip, RawPeekUInt32( ((EC_IPv4_t *)m_tagData)->m_ip ) );	p.m_port = ENDIAN_NTOHS(((EC_IPv4_t *)m_tagData)->m_port);	return p;}/** * Returns a double value. * * @note The returned value is what we get by converting the string form * of the number to a double. * * @return The double value of the tag. * * @see CECTag(ec_tagname_t, double) */double CECTag::GetDoubleData(void) const{	wxString str = GetStringData();	struct lconv *lc = localeconv();	str.Replace(wxT("."), char2unicode(lc->decimal_point));	double tmp;	if (!str.ToDouble(&tmp)) {		double x = 0.0;		// let's fool g++, it'd error out on 0.0 / 0.0		tmp = 0.0 / x;		// intentionally generate nan	}	return tmp;}/*! * \fn CMD4Hash CECTag::GetMD4Data(void) const * * \brief Returns a CMD4Hash class. * * This function takes care of converting from MSB to LSB as necessary. * * \return CMD4Hash class. * * \sa CECTag(ec_tagname_t, const CMD4Hash&) *//*! * \fn CECTag *CECTag::GetTagByIndex(unsigned int index) const * * \brief Finds the indexth child tag. * * \param index 0-based index, 0 <= \e index < GetTagCount() * * \return The child tag, or NULL if index out of range. *//*! * \fn CECTag *CECTag::GetTagByIndexSafe(unsigned int index) const * * \brief Finds the indexth child tag. * * \param index 0-based index, 0 <= \e index < GetTagCount() * * \return The child tag, or a special null-valued tag if index out of range. *//*! * \fn uint16 CECTag::GetTagCount(void) const * * \brief Returns the number of child tags. * * \return The number of child tags. *//*! * \fn const void *CECTag::GetTagData(void) const * * \brief Returns a pointer to the TAG DATA. * * \return A pointer to the TAG DATA. (As specified with the data field of the constructor.)*//*! * \fn uint16 CECTag::GetTagDataLen(void) const * * \brief Returns the length of the data buffer. * * \return The length of the data buffer. *//*! * \fn ec_tagname_t CECTag::GetTagName(void) const * * \brief Returns TAGNAME. * * \return The name of the tag. *//*! * \fn wxString CECTag::GetStringData(void) const * * \brief Returns the string data of the tag. * * Returns a wxString created from TAGDATA. It is automatically * converted from UTF-8 to the internal application encoding. * Should be used with care (only on tags created with the * CECTag(ec_tagname_t, const wxString&) constructor), * becuse it does not perform any check to see if the tag really contains a * string object. * * \return The string data of the tag. * * \sa CECTag(ec_tagname_t, const wxString&) *//*! * \fn uint8 CECTag::GetInt8Data(void) const * * \brief Returns the uint8 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint8 data of the tag. * * \sa CECTag(ec_tagname_t, uint8) *//*! * \fn uint16 CECTag::GetInt16Data(void) const * * \brief Returns the uint16 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint16 data of the tag. * * \sa CECTag(ec_tagname_t, uint16) *//*! * \fn uint32 CECTag::GetInt32Data(void) const * * \brief Returns the uint32 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint32 data of the tag. * * \sa CECTag(ec_tagname_t, uint32) *//*! * \fn uint64 CECTag::GetInt64Data(void) const * * \brief Returns the uint64 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint64 data of the tag. * * \sa CECTag(ec_tagname_t, uint64) *//********************************************************** *							  * *	CECPacket class					  * *							  * **********************************************************/bool CECPacket::ReadFromSocket(CECSocket& socket){	if (m_state == bsName) {		if (!socket.ReadNumber(&m_opCode, sizeof(ec_opcode_t))) {			return false;		} else {			m_state = bsChildCnt;		}	}	if (m_state == bsChildCnt || m_state == bsChildren) {		if (!ReadChildren(socket)) {			return false;		}	}	m_state = bsFinished;	return true;}bool CECPacket::WritePacket(CECSocket& socket) const{	if (!socket.WriteNumber(&m_opCode, sizeof(ec_opcode_t))) return false;	if (!WriteChildren(socket)) return false;	return true;}/*! * \fn CECPacket::CECPacket(ec_opcode_t opCode, EC_DETAIL_LEVEL detail_level) * * \brief Creates a new packet with given OPCODE. *//*! * \fn ec_opcode_t CECPacket::GetOpCode(void) const * * \brief Returns OPCODE. * * \return The OpCode of the packet. *//*! * \fn uint32 CECPacket::GetPacketLength(void) const * * \brief Returns the length of the packet. * * \return The length of the packet. */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?