QTfrontend/netudpwidget.cpp
changeset 2948 3f21a9dc93d0
parent 1066 1f1b3686a2b0
child 3236 4ab3917d7d44
equal deleted inserted replaced
2947:803b277e4894 2948:3f21a9dc93d0
    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()));
    31 }
    31 }
    32 
    32 
    33 void HWNetUdpModel::updateList()
    33 void HWNetUdpModel::updateList()
    34 {
    34 {
    35 	games.clear();
    35     games.clear();
    36 
    36 
    37 	reset();
    37     reset();
    38 
    38 
    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 		QByteArray datagram;
    45         QByteArray datagram;
    46 		datagram.resize(pUdpSocket->pendingDatagramSize());
    46         datagram.resize(pUdpSocket->pendingDatagramSize());
    47 		QHostAddress clientAddr;
    47         QHostAddress clientAddr;
    48 		quint16 clientPort;
    48         quint16 clientPort;
    49 
    49 
    50 		pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    50         pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    51 
    51 
    52 		if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
    52         if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
    53 			QStringList sl;
    53             QStringList sl;
    54 			sl << "-" << clientAddr.toString() << "46631";
    54             sl << "-" << clientAddr.toString() << "46631";
    55 			games.append(sl);
    55             games.append(sl);
    56 		}
    56         }
    57 	}
    57     }
    58 
    58 
    59 	reset();
    59     reset();
    60 }
    60 }
    61 
    61 
    62 QVariant HWNetUdpModel::data(const QModelIndex &index,
    62 QVariant HWNetUdpModel::data(const QModelIndex &index,
    63                              int role) const
    63                              int role) const
    64 {
    64 {
    65 	if (!index.isValid() || index.row() < 0
    65     if (!index.isValid() || index.row() < 0
    66 		|| index.row() >= games.size()
    66         || index.row() >= games.size()
    67 		|| role != Qt::DisplayRole)
    67         || role != Qt::DisplayRole)
    68 	return QVariant();
    68     return QVariant();
    69 
    69 
    70 	return games[index.row()][index.column()];
    70     return games[index.row()][index.column()];
    71 }
    71 }