diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-11-21 13:52:47 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-11-21 13:52:47 +0100 |
commit | 4adae7c3711abbeaa7f03c699a7d366c78f776bc (patch) | |
tree | 99f5a6b181f4ba6704823b6b1b362c5c3dc50e7c | |
parent | e0a2b14d3f52efb22c819e01645b208ded1052fa (diff) | |
download | sink-4adae7c3711abbeaa7f03c699a7d366c78f776bc.tar.gz sink-4adae7c3711abbeaa7f03c699a7d366c78f776bc.zip |
a read buffer for client communication
-rw-r--r-- | resource/listener.cpp | 44 | ||||
-rw-r--r-- | resource/listener.h | 2 |
2 files changed, 39 insertions, 7 deletions
diff --git a/resource/listener.cpp b/resource/listener.cpp index ae16da7..f5920b7 100644 --- a/resource/listener.cpp +++ b/resource/listener.cpp | |||
@@ -35,29 +35,38 @@ Listener::~Listener() | |||
35 | void Listener::closeAllConnections() | 35 | void Listener::closeAllConnections() |
36 | { | 36 | { |
37 | //TODO: close all client connectionsin m_connections | 37 | //TODO: close all client connectionsin m_connections |
38 | for (Client &client: m_connections) { | ||
39 | if (client.socket) { | ||
40 | client.socket->close(); | ||
41 | delete client.socket; | ||
42 | client.socket = 0; | ||
43 | } | ||
44 | } | ||
38 | } | 45 | } |
39 | 46 | ||
40 | void Listener::acceptConnection() | 47 | void Listener::acceptConnection() |
41 | { | 48 | { |
42 | Console::main()->log(QString("Accepting connection")); | 49 | Console::main()->log(QString("Accepting connection")); |
43 | QLocalSocket *connection = m_server->nextPendingConnection(); | 50 | QLocalSocket *socket = m_server->nextPendingConnection(); |
44 | 51 | ||
45 | if (!connection) { | 52 | if (!socket) { |
46 | return; | 53 | return; |
47 | } | 54 | } |
48 | 55 | ||
49 | Console::main()->log("Got a connection"); | 56 | Console::main()->log("Got a connection"); |
50 | Client client("Unknown Client" /*fixme: actual names!*/, connection); | 57 | Client client("Unknown Client" /*fixme: actual names!*/, socket); |
58 | connect(socket, &QIODevice::readyRead, | ||
59 | this, &Listener::readFromSocket); | ||
51 | m_connections << client; | 60 | m_connections << client; |
52 | connect(connection, &QLocalSocket::disconnected, | 61 | connect(socket, &QLocalSocket::disconnected, |
53 | this, &Listener::clientDropped); | 62 | this, &Listener::clientDropped); |
54 | 63 | ||
55 | } | 64 | } |
56 | 65 | ||
57 | void Listener::clientDropped() | 66 | void Listener::clientDropped() |
58 | { | 67 | { |
59 | QLocalSocket *connection = qobject_cast<QLocalSocket *>(sender()); | 68 | QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender()); |
60 | if (!connection) { | 69 | if (!socket) { |
61 | return; | 70 | return; |
62 | } | 71 | } |
63 | 72 | ||
@@ -65,7 +74,7 @@ void Listener::clientDropped() | |||
65 | QMutableListIterator<Client> it(m_connections); | 74 | QMutableListIterator<Client> it(m_connections); |
66 | while (it.hasNext()) { | 75 | while (it.hasNext()) { |
67 | const Client &client = it.next(); | 76 | const Client &client = it.next(); |
68 | if (client.socket == connection) { | 77 | if (client.socket == socket) { |
69 | Console::main()->log(QString(" dropped... %1").arg(client.name)); | 78 | Console::main()->log(QString(" dropped... %1").arg(client.name)); |
70 | it.remove(); | 79 | it.remove(); |
71 | break; | 80 | break; |
@@ -82,3 +91,24 @@ void Listener::checkConnections() | |||
82 | emit noClients(); | 91 | emit noClients(); |
83 | } | 92 | } |
84 | } | 93 | } |
94 | |||
95 | void Listener::readFromSocket() | ||
96 | { | ||
97 | QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender()); | ||
98 | if (!socket) { | ||
99 | return; | ||
100 | } | ||
101 | |||
102 | Console::main()->log("Reading from socket..."); | ||
103 | QMutableListIterator<Client> it(m_connections); | ||
104 | while (it.hasNext()) { | ||
105 | Client &client = it.next(); | ||
106 | if (client.socket == socket) { | ||
107 | Console::main()->log(QString(" Client: %1").arg(client.name)); | ||
108 | client.commandBuffer += socket->readAll(); | ||
109 | Console::main()->log(QString(" Command: %1").arg(QString(client.commandBuffer))); | ||
110 | break; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | } | ||
diff --git a/resource/listener.h b/resource/listener.h index 1d41467..536ba39 100644 --- a/resource/listener.h +++ b/resource/listener.h | |||
@@ -17,6 +17,7 @@ public: | |||
17 | 17 | ||
18 | QString name; | 18 | QString name; |
19 | QLocalSocket *socket; | 19 | QLocalSocket *socket; |
20 | QByteArray commandBuffer; | ||
20 | bool hasSentCommand; | 21 | bool hasSentCommand; |
21 | }; | 22 | }; |
22 | 23 | ||
@@ -38,6 +39,7 @@ private Q_SLOTS: | |||
38 | void acceptConnection(); | 39 | void acceptConnection(); |
39 | void clientDropped(); | 40 | void clientDropped(); |
40 | void checkConnections(); | 41 | void checkConnections(); |
42 | void readFromSocket(); | ||
41 | 43 | ||
42 | private: | 44 | private: |
43 | QLocalServer *m_server; | 45 | QLocalServer *m_server; |