summaryrefslogtreecommitdiffstats
path: root/resource/listener.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'resource/listener.cpp')
-rw-r--r--resource/listener.cpp44
1 files changed, 37 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()
35void Listener::closeAllConnections() 35void 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
40void Listener::acceptConnection() 47void 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
57void Listener::clientDropped() 66void 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
95void 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}