-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNetwork.cpp
207 lines (179 loc) · 4.15 KB
/
Network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "Network.h"
#include <QDataStream>
#include <QNetworkInterface>
#include <QStringList>
Network::Network(QObject *parent) : QObject(parent)
{
init();
}
Network::~Network()
{
}
void Network::init()
{
m_isHost = false;
m_tcpPort = 50010;
foreach(const QNetworkInterface& interface, QNetworkInterface::allInterfaces())
{
if (interface.flags() & (QNetworkInterface::IsUp | QNetworkInterface::IsRunning) && interface.isValid())
{
foreach(const QNetworkAddressEntry& entry, interface.addressEntries())
{
QString ip = entry.ip().toString();
m_myAddresses << entry.ip();
}
}
}
m_tcpServer = new QTcpServer(this);
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(onClientArrives()));
m_tcpServer->listen(QHostAddress::Any, m_tcpPort);
m_mySocket = new QTcpSocket(this);
connect(m_mySocket, SIGNAL(readyRead()), this, SLOT(onTcpRead()));
/*
m_udpSocket = new QUdpSocket(this);
m_udpSocket->bind(QHostAddress::Any, m_udpPort, QUdpSocket::ShareAddress);
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(onUdpRead()));
*/
m_clientSocket = NULL;
}
void Network::onClientArrives()
{
m_isHost = true;
m_clientSocket = m_tcpServer->nextPendingConnection();
connect(m_clientSocket, SIGNAL(readyRead()), this, SLOT(onTcpRead()));
emit otherConnected();
}
void Network::onTcpRead()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
QByteArray data = socket->readAll();
QDataStream ds(data);
QByteArray content;
ds >> content;
if (content == "start")
{
Qizi qizi;
bool firstDrop;
ds >> (int&)qizi >> firstDrop;
emit joinGame(qizi, firstDrop);
}
else if (content == "drop")
{
Step step;
ds >> step.pos.column >> step.pos.row >> (int&)step.qizi;
emit otherDrops(step);
}
else if (content == "regret")
{
emit otherAsksRegret();
}
else if (content == "agree regret")
{
emit otherRespondsRegret(true);
}
else if (content == "disagree regret")
{
emit otherRespondsRegret(false);
}
}
/*
void Network::onUdpRead()
{
if (m_udpSocket->hasPendingDatagrams())
{
QByteArray data;
data.resize(m_udpSocket->pendingDatagramSize());
QHostAddress senderAddr;
quint16 senderPort;
m_udpSocket->readDatagram(data.data(), data.size(), &senderAddr, &senderPort);
if (m_myAddresses.contains(senderAddr))
return;
QDataStream ds(data);
QByteArray content;
ds >> content;
if (content == "host")
emit receiveHost(senderAddr);
}
}
*/
/*
void Network::broadcastToHost()
{
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
ds << QByteArray("host");
foreach(const QHostAddress& addr, m_broadcasts)
{
if (!addr.isNull())
{
qDebug() << addr.toString();
m_udpSocket->writeDatagram(data, addr, m_udpPort);
}
}
}
*/
/*
void Network::join(QHostAddress addr)
{
m_mySocket->connectToHost(addr, m_tcpPort);
m_mySocket->waitForConnected(1000);
}*/
void Network::join(const QString& ip)
{
m_mySocket->connectToHost(QHostAddress(ip), m_tcpPort);
m_mySocket->waitForConnected(1000);
}
void Network::askRegret()
{
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
ds << QByteArray("regret");
if (m_isHost)
m_clientSocket->write(data);
else
m_mySocket->write(data);
}
void Network::respondRegret(bool agree)
{
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
if (agree)
ds << QByteArray("agree regret");
else
ds << QByteArray("disagree regret");
if (m_isHost)
m_clientSocket->write(data);
else
m_mySocket->write(data);
}
void Network::notifyStart(Qizi otherQizi, bool firstDrop)
{
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
ds << QByteArray("start") << (int)otherQizi << firstDrop;
m_clientSocket->write(data);
}
void Network::notifyDrop(Step step)
{
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
ds << QByteArray("drop") << step.pos.column << step.pos.row << (int)step.qizi;
if (m_isHost)
m_clientSocket->write(data);
else
m_mySocket->write(data);
}
void Network::close()
{
m_mySocket->close();
if (m_clientSocket)
{
m_clientSocket->close();
m_clientSocket->deleteLater();
m_clientSocket = NULL;
}
}
QList<QHostAddress> Network::localAddresses()
{
return m_myAddresses;
}