UavUdpSocket

该模块主要用于和无人机地面站通信单元进行数据的收发,基于udpsocket.

cpp 复制代码
#pragma once
#include <QObject>
#include "libCore.h"
#include "LkCommonStruct.h"

class LkUavUdpSocket : public LkUdpSocket
{
	Q_OBJECT
public:
	LkUavUdpSocket(QObject *parent = 0);
	~LkUavUdpSocket();	

	//向无人机发送命令
	template <class T>
	int cmdToUav(T cmd, MsgHeader type)
	{
		int len = cmd.ByteSize();
		char* buff;
		buff = new char[len];
		memset(buff, 0, len);
		cmd.SerializeToArray(buff, len);
		QByteArray sendTo;
		LittleEndianStream(out, sendTo);

		out << int(type);
		out.writeRawData(buff, len);
		delete buff;
		//qDebug() << QString("cmdToUav:Type %1: %2").arg(type).arg(QDateTime::currentDateTime().toString("hh:mm:ss:.zzz"));
		int r =  mSendSocket->writeDatagram(sendTo.data(), sendTo.size(), QHostAddress(mUavComIp), mUavSendPort);
		//qDebug() << QString("cmdToUav:End %1: %2").arg(type).arg(QDateTime::currentDateTime().toString("hh:mm:ss:.zzz"));
		return r;
	}
	void init(QString ip, int port);
private:
	void parse(QByteArray &ba);
	
public slots:
	virtual void readPendingDatagrams();
signals:

public:
	QString mUavComIp;
	int mUavSendPort;
	QSharedPointer<LkUdpSocket> mSendSocket;
private:
};


```cpp
#include "LkUavUdpSocket.h"

LkUavUdpSocket::LkUavUdpSocket(QObject *parent)
	: LkUdpSocket(parent)
{
	connect(this, &LkUavUdpSocket::readyRead, this, &LkUavUdpSocket::readPendingDatagrams);
	mSendSocket = QSharedPointer<LkUdpSocket>::create();
}

LkUavUdpSocket::~LkUavUdpSocket()
{
}

void LkUavUdpSocket::init(QString ip, int port)
{	
	bool b = bind(QHostAddress::Any, port);
	mUavComIp = ip;	
}

void LkUavUdpSocket::readPendingDatagrams()
{
	while (hasPendingDatagrams())
	{
		QByteArray datagram;
		datagram.resize(pendingDatagramSize());
		QNetworkDatagram netDgm = receiveDatagram();
		QString senderIP = netDgm.senderAddress().toString();
		datagram = netDgm.data();
		parse(datagram);
	}
}

void LkUavUdpSocket::parse(QByteArray &ba)
{
	LittleEndianStream(in, ba);
	int msgId;
	in >> msgId;
	if (msgId > 0)
	{
		LkMessageEvent me(QString::number(msgId));
		me.setData(ba.size() - sizeof(int), ba.data() + sizeof(int));
		gNetListener->dispatch(me);
	}
}
复制代码
相关推荐
brrdg_sefg23 分钟前
WEB 漏洞 - 文件包含漏洞深度解析
前端·网络·安全
lqqjuly3 小时前
特殊的“Undefined Reference xxx“编译错误
c语言·c++
冰红茶兑滴水3 小时前
云备份项目--工具类编写
linux·c++
刘好念4 小时前
[OpenGL]使用 Compute Shader 实现矩阵点乘
c++·计算机图形学·opengl·glsl
酒鬼猿4 小时前
C++进阶(二)--面向对象--继承
java·开发语言·c++
姚先生974 小时前
LeetCode 209. 长度最小的子数组 (C++实现)
c++·算法·leetcode
Quz4 小时前
Wireshark协议相关功能:过滤、启用/禁用、导出和统计查看
网络·测试工具·wireshark
安全方案5 小时前
如何增强网络安全意识?(附培训PPT资料)
网络·安全·web安全
小王爱吃月亮糖5 小时前
QT开发【常用控件1】-Layouts & Spacers
开发语言·前端·c++·qt·visual studio