-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageClient.cpp
116 lines (93 loc) · 3.26 KB
/
imageClient.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
#include "imageClient.h"
imageClient::imageClient(QObject *parent) : QObject(parent)
{
// ********** Connect Signal & Slot ------>>
connect(&client , SIGNAL(connected()) , this, SLOT(startTransfer()));
connect(&client , SIGNAL(bytesWritten(qint64)) , this, SLOT(updateProgress(qint64)));
connect(&client , SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(&client , SIGNAL(readyRead()) , this, SLOT(recieveResult()));
}
void imageClient::setHostAddressAndPort(QString address, quint16 port)
{
hostAddress.setAddress(address);
this->hostPort = port;
}
void imageClient::start(QString path)
{
// ********** Set server address --------->>
address = "163.20.133.59"; // School
//address = "172.20.10.3";
setHostAddressAndPort(address, 37);
imPath = path;
if(imPath.contains("file:///"))
imPath = imPath.split("file:///").at(1);
// ********** Check File Path ------------>>
if(imPath == NULL) {
emit sendWarning("檔案不存在");
return;
}
emit sendStateInfo("連結伺服器...");
file = new QFile(imPath);
client.connectToHost(hostAddress, hostPort);
client.waitForConnected();
// QApplication::setOverrideCursor(Qt::WaitCursor);
}
void imageClient::startTransfer()
{
// ********** Check File ----------------->>
if (!file->open(QIODevice::ReadOnly)) {
qDebug() << "無法讀取檔案!";
emit sendWarning("無法讀取檔案!");
delete file;
file = 0;
return;
}
// ********** Write File Information ----->>
QByteArray fileInfo;
QDataStream sendOut(&fileInfo, QIODevice::WriteOnly);
fileSize = file->size() + sizeof(qint64); // totatl send size
emit sendMaxFileSize(fileSize); // assign maximum progress bar value
sendOut << (qint64)fileSize; // write file size
fileInfo.append(file->readAll()); // write file data
// sendOut << file->readAll(); // can't use this (have extra bytes)
// ********** Send File Information ------>>
sendSize = 0;
client.write(fileInfo); // send file information
}
void imageClient::updateProgress(qint64 numBytes)
{
// ********** Update Progress Value ------>>
sendSize = sendSize + (int)numBytes;
emit sendProgressValue(sendSize);
// ********** Finish Sending ------------->>
if(sendSize == fileSize) {
emit sendStateInfo("圖片傳送完成!");
}
}
void imageClient::closeConnection()
{
// Client
client.close();
// File
file->close();
delete file;
emit sendProgressValue(0); // reset progress bar
// QApplication::restoreOverrideCursor();
}
void imageClient::displayError(QAbstractSocket::SocketError socketError)
{
emit sendWarning("網路錯誤!");
closeConnection();
}
void imageClient::recieveResult()
{
emit sendStateInfo("接收辨識結果!");
if(client.bytesAvailable() > 0){
// for *char data
//prosResult = client.readAll();
// for byteArray
prosResult = QString::fromUtf8(client.readAll());
emit sendResult(prosResult);
closeConnection();
}
}