-
Notifications
You must be signed in to change notification settings - Fork 2
/
itom.cpp
310 lines (292 loc) · 9.48 KB
/
itom.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <string>
#include "common.h"
#include "friendbutton.h"
#include "friendlist.h"
#include "itom.h"
#include <QByteArray>
#include <QDataStream>
#include <QHostInfo>
#include <QList>
#include <QMessageBox>
#include <QNetworkInterface>
#include <QString>
#include <QTcpServer>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QVBoxLayout>
#include <QWidget>
iTom::iTom(const QString &uname, QWidget *parent) :
userName(uname), QMainWindow(parent)
{
gotBytes = fileBytes = nameSize = 0;
file = Q_NULLPTR;
ip = get_ip(); // 本机IP
ipset.insert(ip_str2dig(ip)); // 添加本机IP进ipset
// 好友列表
friendList = new FriendList(this);
resize(ITOM_SIZE);
// 默认添加广播按钮
FriendButton *fb = new FriendButton(
QString("Broadcast"), // 好友名字
QHostAddress("255.255.255.255"), // 好友IP(广播)
userName, // 自己名字
QHostAddress("255.255.255.255"), // 自己IP(也是广播)
friendList);
friendList->add_item((QWidget*)fb);
fset.append(fb);
ipset.insert(ip_str2dig("255.255.255.255")); // 添加广播地址进set
QWidget *mainWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
// 退出按钮
QPushButton *leaveBtn = new QPushButton(ip);
connect(leaveBtn, SIGNAL(clicked()),
this, SLOT(bye_world()));
mainLayout->addWidget(friendList);
mainLayout->addWidget(leaveBtn);
mainWidget->setLayout(mainLayout);
setCentralWidget(mainWidget);
// TCP
tServer = new QTcpServer(this);
connect(tServer, SIGNAL(newConnection()),
this, SLOT(accept_connect()));
// 开始监听
if(!tServer->listen(QHostAddress::Any, TCP_PORT))
qDebug() << tServer->errorString();
// UDP
uSocket = new QUdpSocket(this);
uSocket->bind(UDP_PORT, QUdpSocket::ShareAddress);
connect(uSocket, SIGNAL(readyRead()),
this, SLOT(read_datagram()));
// 广播上线消息
hello_world();
}
iTom::~iTom()
{
delete friendList;
delete tServer;
delete uSocket;
}
/*--- 获取本机IP ---*/
QString iTom::get_ip()
{
QString localhost = QHostInfo::localHostName();
QHostInfo hostinfo = QHostInfo::fromName(localhost);
QList<QHostAddress> al = hostinfo.addresses();
foreach(QHostAddress ad, al)
if(ad.protocol() == QAbstractSocket::IPv4Protocol)
return ad.toString();
return QString("255.255.255.255");
}
/*--- UDP 广播上线消息 ---*/
void iTom::hello_world()
{
QByteArray buf;
QDataStream out(&buf, QIODevice::WriteOnly);
out.setVersion(DATA_STREAM_VERSION);
out << (int)OnLine << userName << ip; // 名字 + IP
uSocket->writeDatagram(buf.data(), buf.size(),
QHostAddress("255.255.255.255"), UDP_PORT);
}
/*--- UDP 广播下线消息 ---*/
void iTom::bye_world()
{
QByteArray buf;
QDataStream out(&buf, QIODevice::WriteOnly);
out.setVersion(DATA_STREAM_VERSION);
out << (int)OffLine << ip; // 只发IP
uSocket->writeDatagram(buf.data(), buf.size(),
QHostAddress("255.255.255.255"), UDP_PORT);
close(); // 关主界面
}
/*--- UDP 读广播报文 ---*/
void iTom::read_datagram()
{
QByteArray buf;
QDataStream ds(&buf, QIODevice::ReadWrite);
ds.setVersion(DATA_STREAM_VERSION);
FriendButton *fb = Q_NULLPTR;
int type; // 信息类型
quint32 peerIPdig; // IP地址压进一个整数
QString peerName, peerIP, message;
while(uSocket->hasPendingDatagrams())
{
buf.resize(uSocket->pendingDatagramSize());
uSocket->readDatagram(buf.data(), buf.size());
ds >> type;
switch(type)
{
case OnLine: //*** 别人上线
// 添加好友到列表中
ds >> peerName >> peerIP; // 名字 + IP
peerIPdig = ip_str2dig(peerIP);
if(ipset.count(peerIPdig) > 0) // 已存在列表中
break;
else // 添加IP到ipset
ipset.insert(peerIPdig);
fb = new FriendButton(
peerName, QHostAddress(peerIP),
userName, QHostAddress(ip),
friendList);
// 加进朋友列表
friendList->add_item((QWidget*)fb);
// 保存好友名片指针
fset.append(fb);
// 对上线信息的回复
buf.clear();
ds << (int)OnLine_Reply << userName << ip;
uSocket->writeDatagram(buf.data(), buf.size(),
QHostAddress(peerIP), UDP_PORT);
break;
case OnLine_Reply: //*** 别人对自己上线的回复
ds >> peerName >> peerIP; // 名字 + IP
peerIPdig = ip_str2dig(peerIP);
if(ipset.count(peerIPdig) > 0) // 已存在列表中
break;
else // 添加IP到ipset
ipset.insert(peerIPdig);
fb = new FriendButton(
peerName, QHostAddress(peerIP),
userName, QHostAddress(ip),
friendList);
friendList->add_item((QWidget*)fb);
fset.append(fb);
break;
case OffLine: //*** 好友下线
ds >> peerIP; // 只接IP
peerIPdig = ip_str2dig(peerIP);
if(ipset.count(peerIPdig) < 1) // 本身不存在
break;
else
ipset.erase(peerIPdig);
// 从好友列表删除
friendList->sub_item(peerIP);
// 从指针集删除
foreach(FriendButton *f, fset)
if(ip_str2dig(f->peer_ip()) == peerIPdig)
{
peerName = f->peer_name();
fset.removeAt(fset.indexOf(f));
}
QMessageBox::warning(this, "Friend Left",
QString("%1 Logout!").arg(peerName),
QMessageBox::Ok);
break;
case Message: //*** 信息
ds >> peerName >> peerIP >> message; // 名字+IP+信息
peerIPdig = ip_str2dig(peerIP);
if(ipset.count(peerIPdig) <= 0)
{
fb = new FriendButton(
peerName, QHostAddress(peerIP),
userName, QHostAddress(ip),
friendList);
friendList->add_item((QWidget*)fb);
fset.append(fb);
ipset.insert(peerIPdig);
}
// 发送源是自己(广播时)就不再显示
if(peerName == userName)
break;
// 找相应的FriendButton转传文件
foreach(FriendButton *f, fset)
if(ip_str2dig(f->peer_ip()) == peerIPdig)
{
f->relay_msg(message);
break;
}
break;
} // end swhich
buf.clear();
} // end while
}
/*--- TCP 接受链接请求 ---*/
void iTom::accept_connect()
{
tSocket = tServer->nextPendingConnection();
connect(tSocket, SIGNAL(readyRead()),
this, SLOT(receive_file()));
connect(tSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(show_error(QAbstractSocket::SocketError)));
gotBytes = 0;
fileName.clear();
}
/*--- TCP 接收文件 ---*/
void iTom::receive_file()
{
QDataStream in(tSocket);
in.setVersion(DATA_STREAM_VERSION);
// 首部未接收/未接收完
if(gotBytes <= 2 * sizeof(qint64))
{
if(!nameSize) // 前两个长度字段未接收
{
if(tSocket->bytesAvailable() >= 2 * sizeof(qint64))
{
in >> fileBytes >> nameSize;
gotBytes += 2 * sizeof(qint64);
}
else // 数据不足,等下次
return;
}
else if(tSocket->bytesAvailable() >= nameSize)
{
in >> fileName;
gotBytes += nameSize;
}
else // 数据不足文件名长度,等下次
return;
}
// 已读文件名、文件未打开 -> 尝试打开文件
if(!fileName.isEmpty() && file == Q_NULLPTR)
{
fileName = "C:/" + fileName;
file = new QFile(fileName);
if(!file->open(QFile::WriteOnly)) // 打开失败
{
delete file;
file = Q_NULLPTR;
return;
}
}
// 文件未打开,不能进行后续操作
if(file == Q_NULLPTR)
return;
// 文件未接收完
if(gotBytes < fileBytes)
{
gotBytes += tSocket->bytesAvailable();
file->write(tSocket->readAll());
}
// 文件已接收完
if(gotBytes == fileBytes)
{
tSocket->close(); // 关socket
file->close(); // 关文件
delete file;
file = Q_NULLPTR;
gotBytes = fileBytes = nameSize = 0;
fileName.clear();
QMessageBox::warning(this, "File Received",
QString("%1 接收成功!").arg(fileName),
QMessageBox::Ok);
}
}
/*--- TCP 出错处理 ---*/
void iTom::show_error(QAbstractSocket::SocketError)
{
qDebug() << tSocket->errorString();
tSocket->close(); // 关cocket
tSocket = Q_NULLPTR;
file = Q_NULLPTR;
fileName.clear(); // 清空文件名
fileBytes = gotBytes = nameSize = 0;
}
/*--- 断交处理 ---*/
void iTom::disconnect_slot()
{
tSocket->close(); // 关cocket
tSocket = Q_NULLPTR;
file = Q_NULLPTR;
fileName.clear(); // 清空文件名
fileBytes = gotBytes = nameSize = 0;
}