QTfrontend/net/netudpwidget.cpp
changeset 6060 fdfc01419815
parent 5147 a2ae7702bb75
child 6616 f77bb02b669f
equal deleted inserted replaced
6059:ddf020d0941a 6060:fdfc01419815
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2007 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 #include <QUdpSocket>
       
    21 
       
    22 #include "netudpwidget.h"
       
    23 
       
    24 HWNetUdpModel::HWNetUdpModel(QObject* parent) :
       
    25   HWNetServersModel(parent)
       
    26 {
       
    27     pUdpSocket = new QUdpSocket(this);
       
    28 
       
    29     pUdpSocket->bind();
       
    30     connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
       
    31 }
       
    32 
       
    33 void HWNetUdpModel::updateList()
       
    34 {
       
    35     games.clear();
       
    36 
       
    37     reset();
       
    38 
       
    39     pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
       
    40 }
       
    41 
       
    42 void HWNetUdpModel::onClientRead()
       
    43 {
       
    44     while (pUdpSocket->hasPendingDatagrams()) {
       
    45         QByteArray datagram;
       
    46         datagram.resize(pUdpSocket->pendingDatagramSize());
       
    47         QHostAddress clientAddr;
       
    48         quint16 clientPort;
       
    49 
       
    50         pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
       
    51 
       
    52         QString packet = QString::fromUtf8(datagram.data());
       
    53         if(packet.startsWith("hedgewars server")) {
       
    54             QStringList sl;
       
    55             sl << packet.remove(0, 17) << clientAddr.toString() << "46631";
       
    56             games.append(sl);
       
    57         }
       
    58     }
       
    59 
       
    60     reset();
       
    61 }
       
    62 
       
    63 QVariant HWNetUdpModel::data(const QModelIndex &index,
       
    64                              int role) const
       
    65 {
       
    66     if (!index.isValid() || index.row() < 0
       
    67         || index.row() >= games.size()
       
    68         || role != Qt::DisplayRole)
       
    69     return QVariant();
       
    70 
       
    71     return games[index.row()][index.column()];
       
    72 }