blob: ad581a881756d21fba1350149457f42beeb62247 (
plain)
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
|
/******************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Debug Bridge.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
******************************************************************************/
#include "filepullexecutor.h"
#include "../utils/make_unique.h"
#include "filepullcommon.h"
#include "protocol/services.h"
#include "stream.h"
#include <QtCore/qdatastream.h>
#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
FilePullExecutor::FilePullExecutor(Stream *stream)
: m_stream{stream},
m_source{nullptr},
m_transferring{false}
{
if (m_stream)
connect(m_stream, &Stream::packetAvailable, this, &Executor::receive);
}
void FilePullExecutor::receive(StreamPacket packet)
{
uint32_t typeValue;
packet >> typeValue;
auto type = toFilePullPacketType(typeValue);
switch (type) {
case FilePullOpen: {
QString sourcePath;
packet >> sourcePath;
if (openSource(sourcePath))
transferBlock();
break;
}
case FilePullWasRead:
qDebug() << "File pull read acknowledged.";
if (m_transferring)
transferBlock();
else
closeSource();
break;
case FilePullError:
qDebug() << "FilePullError from host";
closeSource();
break;
default:
qFatal("Unsupported FilePushPacketType %d in ProcessExecutor::receive", type);
}
}
bool FilePullExecutor::openSource(const QString &path)
{
qDebug() << "Opening source file" << path;
m_source = make_unique<QFile>(path);
bool opened = m_source->open(QIODevice::ReadOnly);
StreamPacket packet;
if (!opened) {
packet << FilePullError
<< QString{"Could not open \"%1\" on device"}.arg(m_source->fileName());
} else {
packet << FilePullOpened;
}
return m_stream->write(packet) && opened;
}
void FilePullExecutor::transferBlock()
{
QByteArray block = m_source->read(fileTransferBlockSize);
m_transferring = !m_source->atEnd();
StreamPacket packet;
packet << FilePullRead << block;
m_stream->write(packet);
}
void FilePullExecutor::closeSource()
{
qDebug() << "Closing source file" << m_source->fileName();
m_source->close();
StreamPacket packet;
packet << FilePullEnd;
m_stream->write(packet);
}
|