-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpFileSender.cpp
105 lines (88 loc) · 2.51 KB
/
tcpFileSender.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
//
// Created by kurum on 2022/06/11.
//
#include "tcpFileSender.h"
#include <utility>
#define MB1 10485760
tcpSock::tcpSock(QString fileName, QObject *parent) : QObject(parent) {
fileNa = fileName;
tcp = new QTcpServer();
connect(tcp, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
void tcpSock::onNewConnection() {
QTcpSocket *a = tcp->nextPendingConnection();
printf_s("new connected service;");
if (a == NULL) {
qInfo() << "wrong";
return;
}
connect(a, SIGNAL(disconnected()), this, SLOT(deal_with()));
connect(a, SIGNAL(readyRead()), this, SLOT(nptBusy()));
//auto dd=new std::thread(sendF,this,a,fileNa);
SendFile(a, fileNa);
}
void tcpSock::SendFile(QTcpSocket *a, QString fileName) {
qint32 Send = 1;
QFile file(fileName);
file.open(QIODevice::ReadOnly);
uchar *fpr = file.map(0, MB1);
long long starPOint = 0;
QByteArray bbb /* =QByteArray::fromRawData((char *) fpr, MB1)*/;
auto persize = fileInfo.size() / MB1;
while ((starPOint + MB1) < file.size()) {
qInfo() << ++Send << "MB";
fpr = file.map(starPOint, MB1);
starPOint += MB1;
bbb = QByteArray::fromRawData((char *) fpr, MB1);
qInfo() << bbb.left(10);
a->write(bbb);
pers->setValue(pers->value() + persize);
a->waitForBytesWritten(100);
file.unmap(fpr);
//a->write("1Pack");
}
fpr = file.map(starPOint, file.size() - starPOint);
bbb = QByteArray::fromRawData((char *) fpr, file.size() - starPOint);
a->write(bbb);
qInfo() << ++Send;
qInfo() << starPOint;
//delete fpr;
pers->setValue(100);
file.unmap(fpr);
file.close();
a->write("end");
a->setProperty("id", 1);
}
long long tcpSock::star(int port = 7001) {
busy = true;
QHostAddress add("localhost");
qInfo() << "line 72: " + fileNa << endl;
fileInfo = QFileInfo(fileNa);
auto size = fileInfo.size();
qInfo() << size;
tcp->listen(add, port);
qInfo() << "creat host succed";
return size;
}
void tcpSock::deal_with() {
auto a = qobject_cast<QTcpSocket *>(sender());
disconnect(a);
a->close();
tcp->close();
qInfo() << "over";
busy = false;
emit next();
}
void tcpSock::nptBusy() {
auto a = qobject_cast<QTcpSocket *>(sender());
while (a->canReadLine()) {
QString bb(a->readLine());
qInfo() << bb;
if (bb.left(2) == "OK") {
busy = false;
}
}
}
bool tcpSock::isBusy() const {
return busy;
}