QTfrontend/net/netudpwidget.cpp
changeset 6616 f77bb02b669f
parent 6060 fdfc01419815
child 6700 e04da46ee43c
equal deleted inserted replaced
6615:65602f1ef0f8 6616:f77bb02b669f
    20 #include <QUdpSocket>
    20 #include <QUdpSocket>
    21 
    21 
    22 #include "netudpwidget.h"
    22 #include "netudpwidget.h"
    23 
    23 
    24 HWNetUdpModel::HWNetUdpModel(QObject* parent) :
    24 HWNetUdpModel::HWNetUdpModel(QObject* parent) :
    25   HWNetServersModel(parent)
    25     HWNetServersModel(parent)
    26 {
    26 {
    27     pUdpSocket = new QUdpSocket(this);
    27     pUdpSocket = new QUdpSocket(this);
    28 
    28 
    29     pUdpSocket->bind();
    29     pUdpSocket->bind();
    30     connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
    30     connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
    39     pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
    39     pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
    40 }
    40 }
    41 
    41 
    42 void HWNetUdpModel::onClientRead()
    42 void HWNetUdpModel::onClientRead()
    43 {
    43 {
    44     while (pUdpSocket->hasPendingDatagrams()) {
    44     while (pUdpSocket->hasPendingDatagrams())
       
    45     {
    45         QByteArray datagram;
    46         QByteArray datagram;
    46         datagram.resize(pUdpSocket->pendingDatagramSize());
    47         datagram.resize(pUdpSocket->pendingDatagramSize());
    47         QHostAddress clientAddr;
    48         QHostAddress clientAddr;
    48         quint16 clientPort;
    49         quint16 clientPort;
    49 
    50 
    50         pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    51         pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    51 
    52 
    52         QString packet = QString::fromUtf8(datagram.data());
    53         QString packet = QString::fromUtf8(datagram.data());
    53         if(packet.startsWith("hedgewars server")) {
    54         if(packet.startsWith("hedgewars server"))
       
    55         {
    54             QStringList sl;
    56             QStringList sl;
    55             sl << packet.remove(0, 17) << clientAddr.toString() << "46631";
    57             sl << packet.remove(0, 17) << clientAddr.toString() << "46631";
    56             games.append(sl);
    58             games.append(sl);
    57         }
    59         }
    58     }
    60     }
    62 
    64 
    63 QVariant HWNetUdpModel::data(const QModelIndex &index,
    65 QVariant HWNetUdpModel::data(const QModelIndex &index,
    64                              int role) const
    66                              int role) const
    65 {
    67 {
    66     if (!index.isValid() || index.row() < 0
    68     if (!index.isValid() || index.row() < 0
    67         || index.row() >= games.size()
    69             || index.row() >= games.size()
    68         || role != Qt::DisplayRole)
    70             || role != Qt::DisplayRole)
    69     return QVariant();
    71         return QVariant();
    70 
    72 
    71     return games[index.row()][index.column()];
    73     return games[index.row()][index.column()];
    72 }
    74 }