Commit 9f3cebd5 authored by Dorian Zedler's avatar Dorian Zedler

added the ability for the client to surround sent messages with "<message>"...

added the ability for the client to surround sent messages with "<message>" and "</message>" to understand split-up messages
parent ab1e65f4
......@@ -33,6 +33,8 @@ public:
QTcpSocket * socket;
bool usingTerminationKeys;
QString readBuffer;
};
QList<socketClient> clients;
......@@ -51,6 +53,7 @@ public slots:
qint64 sendToClient(QTcpSocket *socket, const QString &str);
// --- helper functions ---
void processSocketMessage(socketClient * socket, QString message);
QList<socketClient> getClients();
private:
......
......@@ -40,6 +40,8 @@ void SocketServer::newConnection()
socketClient client;
client.socket = clientSocket;
client.sessionActive = false;
client.usingTerminationKeys = false;
client.readBuffer = "";
clients.append(client);
......@@ -53,7 +55,12 @@ void SocketServer::readClient()
// find sender in clients list
for(int i = 0; i < this->clients.length(); i++) {
if(this->clients[i].socket == sender()) {
emit gotCommand(&this->clients[i], this->clients[i].socket->readAll());
if(!this->clients[i].usingTerminationKeys) {
emit gotCommand(&this->clients[i], this->clients[i].socket->readAll());
}
else {
this->processSocketMessage(&this->clients[i], this->clients[i].socket->readAll());
}
return;
}
}
......@@ -85,6 +92,58 @@ void SocketServer::gotDisconnection()
// --- helper functions ---
// ------------------------
void SocketServer::processSocketMessage(socketClient * socket, QString message) {
QString startKey = "<message>";
QString endKey = "</message>";
//qWarning() << "... processing message now ... : " << message;
if(message == ""){
return;
}
if((message.startsWith(startKey) && message.endsWith(endKey)) && (message.count(startKey) == 1 && message.count(endKey) == 1)){
// non-split message ( e.g.: <message>123456789</message>
}
else if(!message.contains(endKey) && (!socket->readBuffer.isEmpty() || message.startsWith(startKey))){
// begin of a split message ( e.g.: <message>123 )
// or middle of a split message ( e.g.: 456 )
//qWarning() << "this is a begin or middle of split a message";
socket->readBuffer += message;
return;
}
else if(!message.contains(startKey) && message.endsWith(endKey)) {
// end of a split message ( e.g.: 789</message> )
if(!socket->readBuffer.isEmpty()){
message = socket->readBuffer + message;
socket->readBuffer.clear();
}
}
else if((message.count(startKey) > 1 || message.count(endKey) > 1) || (message.contains(endKey) && !message.endsWith(endKey) && message.contains(startKey) && !message.startsWith(startKey))) {
// multiple messages in one packet ( e.g.: <message>123456789</message><message>987654321</message> )
// or multiple message fragments in one message ( e.g.: 56789</message><message>987654321</message> or 56789</message><message>98765 )
//qDebug() << "detected multiple messages";
int startOfSecondMessage = message.lastIndexOf(startKey);
// process first part of message
QString firstMessage = message.left(startOfSecondMessage);
this->processSocketMessage(socket, firstMessage);
// process second part of message
QString secondMessage = message.right(message.length() - startOfSecondMessage);
this->processSocketMessage(socket, secondMessage);
return;
}
else {
// invalid message
return;
}
//qWarning() << "... done processing, message: " << message;
emit gotCommand(socket, message.replace(startKey, "").replace(endKey, ""));
}
QList<SocketServer::socketClient> SocketServer::getClients()
{
return clients;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment