1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00
LibQQt/examples/qqtudpexample/cedianudpprotocol.cpp

64 lines
1.6 KiB
C++
Raw Normal View History

#include "cedianudpprotocol.h"
CedianUdpProtocol::CedianUdpProtocol(QObject *parent) : QQtUdpProtocol(parent)
{
}
bool CedianUdpProtocol::dispatcher(const QNetworkDatagram & dg)
{
mDG = dg;
mAddress = dg.senderAddress();
mPort = dg.senderPort();
mLocalPort = dg.destinationPort();
QByteArray data = dg.data();
mMsg.parser(data);
emit msgIncoming();
}
/*依赖已经初始化好的端口*/
CedianUdpProtocol *cedianUdpServer(QObject *parent)
{
static CedianUdpServer* s0[9] = {NULL};
static CedianUdpProtocol* p0 = NULL;
if(!s0[0] && !p0)
{
p0 = new CedianUdpProtocol(parent);
for(int i =0; i < 9; i++)
{
int port = cedianUdpPort(i+1);
pline() << i+1 << port;
s0[i] = new CedianUdpServer(parent);
/*通讯端口:绑定本地端口*/
s0[i]->bind(port);
/*通讯端口:给测点协议*/
s0[i]->installProtocol(p0);
}
}
return p0;
}
/*
* Port列表设置函数mainwindow调用
* port列表传进去
*
* 9
* 1-9
*/
qint32 cedianUdpPort(int siteID, qint32* portList)
{
if(siteID < 1 || siteID > 9)
return 0;
static qint32 sPort[9] = {0};
if(portList)
{
for (int i =0; i < 9; i++)
{
sPort[i] = portList[i];
}
}
return sPort[siteID-1];
}