author | koda |
Sat, 24 Oct 2009 13:50:31 +0000 | |
changeset 2579 | e5e4ebf528b5 |
parent 1066 | 1f1b3686a2b0 |
child 2948 | 3f21a9dc93d0 |
permissions | -rw-r--r-- |
486 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
486 | 3 |
* Copyright (c) 2007 Ulyanov Igor <iulyanov@gmail.com> |
883 | 4 |
* Copyright (c) 2007, 2008 Andrey Korotaev <unC0Rr@gmail.com> |
486 | 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 |
||
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
20 |
#include <QUdpSocket> |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
21 |
|
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
22 |
#include "netudpwidget.h" |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
23 |
|
667 | 24 |
HWNetUdpModel::HWNetUdpModel(QObject* parent) : |
25 |
HWNetServersModel(parent) |
|
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
26 |
{ |
667 | 27 |
pUdpSocket = new QUdpSocket(this); |
28 |
||
29 |
pUdpSocket->bind(); |
|
30 |
connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead())); |
|
31 |
} |
|
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
32 |
|
667 | 33 |
void HWNetUdpModel::updateList() |
34 |
{ |
|
35 |
games.clear(); |
|
36 |
||
37 |
reset(); |
|
38 |
||
39 |
pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631); |
|
416 | 40 |
} |
41 |
||
667 | 42 |
void HWNetUdpModel::onClientRead() |
416 | 43 |
{ |
667 | 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 |
if(QString("%1").arg(datagram.data())==QString("hedgewars server")) { |
|
53 |
QStringList sl; |
|
54 |
sl << "-" << clientAddr.toString() << "46631"; |
|
55 |
games.append(sl); |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
reset(); |
|
413
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
60 |
} |
523f1769f2bc
udp server detecting experimental version (added files)
displacer
parents:
diff
changeset
|
61 |
|
667 | 62 |
QVariant HWNetUdpModel::data(const QModelIndex &index, |
63 |
int role) const |
|
64 |
{ |
|
65 |
if (!index.isValid() || index.row() < 0 |
|
66 |
|| index.row() >= games.size() |
|
67 |
|| role != Qt::DisplayRole) |
|
68 |
return QVariant(); |
|
69 |
||
70 |
return games[index.row()][index.column()]; |
|
71 |
} |