-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
56 lines (46 loc) · 1.32 KB
/
Client.cpp
File metadata and controls
56 lines (46 loc) · 1.32 KB
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
#include "Client.h"
#include <utility>
Client::Client(QString program, QTcpSocket* sock, QObject* parent): QObject(parent), sock(sock),
program(std::move(program))
{
}
void Client::interact()
{
// Merge StdErr with StdOut
process.setProcessChannelMode(QProcess::MergedChannels);
// Start Process
process.start(program);
// process.waitForStarted();
// When we have something from the server, pipe it to process
connect(sock, &QTcpSocket::readyRead, this, [&]()
{
const QByteArray command = sock->readAll();
if (command.endsWith('\n'))
process.write(command);
else
process.write((command + "\r\n"));
process.waitForBytesWritten();
/*
if (command.trimmed() == "exit")
{
sock->close();
a.exit(0);
}
*/
});
// When we have something from process pipe it to socket
connect(&process, &QProcess::readyRead, this, [&]()
{
const QByteArray processOut = process.readAll();
sock->write(processOut);
sock->waitForBytesWritten();
});
// Exit app and close socket
connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [&](int exitCode, QProcess::ExitStatus /*exitStatus*/)
{
sock->close();
QApplication::exit(exitCode);
return exitCode;
});
}