RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C++中组解数据包(序列化反序列化方式)-创新互联
class ExBodyHead
{public:
	quint8 m_no; 
	quint8 m_id; 
	quint16 m_length; 
	quint32 m_timestamp; 
	quint16 m_spare; 
	quint16 m_secondid; 

	ExBodyHead()
	{m_no = 0;        m_id = 0;    m_length = 0;
		m_timestamp = 0; m_spare = 0; m_secondid = 0;
	}
	ExBodyHead(quint8 _no, quint8 _id, quint16 _length, quint16 _timestamp, quint16 _spare, quint16 _secondid)
		: m_no(_no), m_id(_id), m_length(_length), m_timestamp(_timestamp), m_spare(_spare), m_secondid(_secondid)
	{}

	friend QDataStream& operator<<(QDataStream& os, const ExBodyHead& data)
	{os<< data.m_no<< data.m_id<< data.m_length<< data.m_timestamp<< data.m_spare<< data.m_secondid;
		return os;
	}

	friend QDataStream& operator>>(QDataStream& is, ExBodyHead& data)
	{is >>data.m_no >>data.m_id >>data.m_length >>data.m_timestamp >>data.m_spare >>data.m_secondid;
		return is;
	}
};

//对数据流包头组包
templateQByteArray PackProStruct(const T& data, bool bLittleEndian = false)
{QByteArray headBytes;
	QBuffer head_buffer(&headBytes);
	head_buffer.open(QIODevice::WriteOnly);
	QDataStream head_stream(&head_buffer);
	head_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
	head_stream<< data;
	head_buffer.close();
	return headBytes;
}

//对数据流进行组包
templateQByteArray PackProStruct(const T& data, ExPckgHead pckgHead, bool bLittleEndian = false)
{QByteArray serial_array;
	QBuffer serial_buffer(&serial_array);
	serial_buffer.open(QIODevice::WriteOnly);
	QDataStream out_stream(&serial_buffer);
	out_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
	out_stream<< data;
	serial_buffer.close();

	pckgHead.m_length = serial_array.size() + 16;
	ExPckgHead head(pckgHead);
	QByteArray headBytes;
	QBuffer head_buffer(&headBytes);
	head_buffer.open(QIODevice::WriteOnly);
	QDataStream head_stream(&head_buffer);
	head_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
	head_stream<< head;
	head_buffer.close();
	serial_array.prepend(headBytes);

	return serial_array;
}

//对数据流进行解包
templatevoid UnPackProStruct(const QByteArray& msg, T& data, bool bLittleEndian = false)
{QByteArray deserial_array(msg);
	QBuffer deserial_buffer(&deserial_array);
	deserial_buffer.open(QIODevice::ReadOnly);
	QDataStream in_stream(&deserial_buffer);
	in_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);

	in_stream >>data;
	deserial_buffer.close();
}

//对字符流进行解包
templatevoid UnPackProStruct(const std::string& msg, T& data, bool bLittleEndian = false)
{QByteArray deserial_array(msg.data(), msg.size());
	QBuffer deserial_buffer(&deserial_array);
	deserial_buffer.open(QIODevice::ReadOnly);
	QDataStream in_stream(&deserial_buffer);
	in_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);

	in_stream >>data;
	deserial_buffer.close();
}
class timeSysInfo : public ExBodyHead //BCD码
{public:
		quint8 m_second;    //秒
		quint8 m_minute;    //分
		quint8 m_hour;      //时
		quint8 m_timeZoneCorrect;  //时区修正
		quint8 m_day;     //日
		quint8 m_month;   //月
		quint8 m_lowByteYear;   //年的低字节
		quint8 m_highByteYear;  //年的高字节

		friend QDataStream& operator<< (QDataStream& os, const timeSysInfo& data)
		{	os<< data.m_no<< data.m_id<< data.m_length<< data.m_timestamp<< data.m_spare<< data.m_secondid
			<< data.m_second<< data.m_minute<< data.m_hour<< data.m_timeZoneCorrect<< data.m_day
			<< data.m_month<< data.m_lowByteYear<< data.m_highByteYear;

			return os;
		}
		friend QDataStream& operator >>(QDataStream& is, timeSysInfo& data)
		{	is >>data.m_no >>data.m_id >>data.m_length >>data.m_timestamp >>data.m_spare >>data.m_secondid
			   >>data.m_second >>data.m_minute >>data.m_hour >>data.m_timeZoneCorrect >>data.m_day
			   >>data.m_month >>data.m_lowByteYear >>data.m_highByteYear;

			return is;
		}
};
QByteArray datagram = EVDATA::PackProStruct(m_timeSysInfo, pckgHead);		

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

作为一家“创意+整合+营销”的成都网站建设机构,我们在业内良好的客户口碑。成都创新互联公司提供从前期的网站品牌分析策划、网站设计、成都做网站、网站建设、外贸营销网站建设、创意表现、网页制作、系统开发以及后续网站营销运营等一系列服务,帮助企业打造创新的互联网品牌经营模式与有效的网络营销方法,创造更大的价值。
当前名称:C++中组解数据包(序列化反序列化方式)-创新互联
转载注明:http://lswzjz.com/article/idhpe.html