-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalSocketIpcClient.cpp
66 lines (51 loc) · 1.88 KB
/
LocalSocketIpcClient.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
#include "LocalSocketIpcClient.h"
#include <QDebug>
#include <QDataStream>
LocalSocketIpcClient::LocalSocketIpcClient(QString remoteServername, QObject *parent) :
QObject(parent)
{
m_socket = new QLocalSocket(this);
m_serverName = remoteServername;
connect(m_socket, SIGNAL(connected()), this, SLOT(socket_connected()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected()));
m_socket->connectToServer(m_serverName, QLocalSocket::ReadWrite);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(receiveMessage()));
connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(socket_error(QLocalSocket::LocalSocketError)));
}
LocalSocketIpcClient::~LocalSocketIpcClient() {
m_socket->abort();
delete m_socket;
m_socket = NULL;
}
void LocalSocketIpcClient::send_MessageToServer(QString message) {
if (m_socket->state() != QLocalSocket::ConnectedState) {
m_socket->connectToServer(m_serverName, QLocalSocket::ReadWrite);
}
if (message.isEmpty()) {
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << message;
out.device()->seek(0);
m_socket->write(block);
m_socket->flush();
}
void LocalSocketIpcClient::receiveMessage() {
QDataStream in(m_socket);
QString data;
in >> data;
qDebug() << "recved " << data;
emit messageReceived(data);
}
void LocalSocketIpcClient::socket_connected() {
emit messageReceived("Disconnected from server");
}
void LocalSocketIpcClient::socket_disconnected() {
emit messageReceived("Disconnected from server");
}
void LocalSocketIpcClient::socket_error(QLocalSocket::LocalSocketError) {
emit messageReceived(QString("socket_error ").append(m_socket->errorString()));
}