修改组播可以来连接多个
This commit is contained in:
+1
-1
Submodule VehicleManage updated: a49692303b...400d726bd6
+95
-49
@@ -148,16 +148,6 @@ int DLink::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
||||
}
|
||||
}
|
||||
|
||||
if (multicast_sock)
|
||||
{
|
||||
qint64 flag = multicast_sock->writeDatagram((const char *)msg, len,multicast_transmit_addr, multicast_transmit_port);
|
||||
if(flag != -1)
|
||||
{
|
||||
//Byte_Out_ALL += flag;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -366,12 +356,21 @@ void DLink::connectSignal(QVariant m_state,
|
||||
else if(m_Type.toString() == "Multicast")
|
||||
{
|
||||
//断开连接
|
||||
if(multicast_sock)
|
||||
for(int i = 0;i < clientSockets.count();i++)
|
||||
{
|
||||
qWarning("leave multicast Group");
|
||||
delete multicast_sock;
|
||||
multicast_sock = nullptr;
|
||||
if((clientSockets.at(i).addr == QHostAddress(m_Param3.toString()))&&(clientSockets.at(i).port == m_Param4.toInt()))
|
||||
{
|
||||
|
||||
clientSockets.at(i).sock->leaveMulticastGroup(QHostAddress(m_Param3.toString()));
|
||||
clientSockets.at(i).sock->close();
|
||||
qWarning("leave multicast Group");
|
||||
clientSockets.at(i).sock->deleteLater();
|
||||
|
||||
clientSockets.removeAt(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
||||
}
|
||||
}
|
||||
@@ -406,8 +405,6 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
QUdpSocket *clientsock = new QUdpSocket(this);
|
||||
int rst = clientsock->open(QIODevice::ReadWrite);
|
||||
if(rst)
|
||||
@@ -436,8 +433,6 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
|
||||
connect(clientsock, SIGNAL(readyRead()),
|
||||
this, SLOT(readPendingDatagramsClient()));
|
||||
|
||||
//clientsock->disconnectFromHost();
|
||||
|
||||
emit showMessage(tr("UdpSocket open"));
|
||||
}
|
||||
else
|
||||
@@ -466,43 +461,81 @@ bool DLink::setup_multicast(const QString &recieveIP, qint32 recievePort,
|
||||
const QString &transmitIP, qint32 transmitPort)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
multicast_transmit_port = transmitPort;
|
||||
multicast_transmit_addr.setAddress(transmitIP);
|
||||
|
||||
if (multicast_sock)
|
||||
delete multicast_sock;
|
||||
|
||||
multicast_recieve_addr.setAddress(recieveIP);
|
||||
multicast_sock = new QUdpSocket(this);
|
||||
if (multicast_sock->bind(QHostAddress::AnyIPv4, recievePort, QUdpSocket::ShareAddress))
|
||||
{
|
||||
if (multicast_sock->joinMulticastGroup(multicast_recieve_addr))
|
||||
foreach (clientNode node, clientSockets) {
|
||||
if(node.sock)
|
||||
{
|
||||
connect(multicast_sock, SIGNAL(readyRead()),
|
||||
this, SLOT(processPendingMulticastDatagrams()));
|
||||
multicast_recieve_port = recievePort;
|
||||
multicast_sock->setSocketOption(QAbstractSocket::MulticastTtlOption, 3);
|
||||
if((node.addr == QHostAddress(recieveIP))&&(node.port == recievePort))
|
||||
{
|
||||
if(node.sock->isOpen())
|
||||
{
|
||||
node.sock->leaveMulticastGroup(node.addr);
|
||||
node.sock->close();
|
||||
}
|
||||
//断掉所有的连接
|
||||
disconnect(node.sock,nullptr,nullptr,nullptr);
|
||||
|
||||
node.sock->deleteLater();
|
||||
//node.sock = nullptr;
|
||||
|
||||
clientSockets.removeAt(clientSockets.indexOf(node));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QUdpSocket *clientsock = new QUdpSocket(this);
|
||||
if(clientsock->bind(QHostAddress::AnyIPv4,recievePort,QAbstractSocket::ShareAddress))
|
||||
{
|
||||
if (clientsock->joinMulticastGroup(QHostAddress(recieveIP)))
|
||||
{
|
||||
clientsock->setProxy(QNetworkProxy::NoProxy);
|
||||
clientsock->setSocketOption(QAbstractSocket::MulticastTtlOption, 3);
|
||||
|
||||
clientNode node;
|
||||
node.sock = clientsock;
|
||||
node.addr = QHostAddress(transmitIP);
|
||||
node.port = transmitPort;
|
||||
node.type = devtype::datalink;
|
||||
|
||||
clientSockets.append(node);
|
||||
qWarning() << "multicast open:"
|
||||
<< node.addr
|
||||
<< node.port;
|
||||
|
||||
isSuccess = true;
|
||||
emit showMessage(tr("Bind and join the gdt multicast group"));
|
||||
|
||||
|
||||
connect(clientsock, SIGNAL(readyRead()),
|
||||
this, SLOT(processPendingMulticastDatagrams()));
|
||||
|
||||
emit showMessage(tr("Bind and join the multicast group"));
|
||||
}
|
||||
else
|
||||
{
|
||||
clientsock->close();
|
||||
isSuccess = false;
|
||||
delete multicast_sock;
|
||||
multicast_sock = NULL;
|
||||
|
||||
emit showMessage(tr("Fail to join gdt multicast group."));
|
||||
qWarning() << "sock open failure" << clientsock->state();
|
||||
delete clientsock;
|
||||
clientsock = nullptr;
|
||||
emit showMessage(tr("Fail to join multicast group."));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
isSuccess = false;
|
||||
emit showMessage(tr("Fail to bind gdt multicast socket."));
|
||||
delete multicast_sock;
|
||||
multicast_sock = NULL;
|
||||
clientsock->close();
|
||||
qWarning("sock is not binded.");
|
||||
delete clientsock;
|
||||
clientsock = nullptr;
|
||||
emit showMessage(tr("Fail to bind multicast socket."));
|
||||
}
|
||||
|
||||
|
||||
return isSuccess;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -556,14 +589,27 @@ void DLink::readPendingDatagramsClient(void)
|
||||
|
||||
void DLink::processPendingMulticastDatagrams(void)
|
||||
{
|
||||
if(multicast_sock)
|
||||
{
|
||||
while (multicast_sock->hasPendingDatagrams()) {
|
||||
QByteArray datagram;
|
||||
datagram.resize(multicast_sock->pendingDatagramSize());
|
||||
multicast_sock->readDatagram(datagram.data(), datagram.size());
|
||||
QElapsedTimer time;
|
||||
time.start();
|
||||
//轮番查询
|
||||
QObject *obj = sender();
|
||||
|
||||
emit recieveMessage(SourceType::c_sock,datagram);
|
||||
}
|
||||
}
|
||||
if(obj)
|
||||
{
|
||||
QUdpSocket *clientsock = qobject_cast<QUdpSocket *>(obj);
|
||||
if(clientsock)
|
||||
{
|
||||
while (clientsock->hasPendingDatagrams()) {
|
||||
QByteArray datagram;
|
||||
datagram.resize(clientsock->pendingDatagramSize());
|
||||
clientsock->readDatagram(datagram.data(), datagram.size());
|
||||
|
||||
emit recieveMessage(SourceType::c_sock,datagram);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int milsec = time.elapsed();
|
||||
|
||||
//qDebug() << "a loop using time:" << milsec << "ms";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user