void ApplicationCore::slotHandleUniqueApplicationConnection() { QLocalSocket *socket = m_uniqueApplicationServer.nextPendingConnection(); connect(socket, &QLocalSocket::readyRead, socket, [this, socket](){ if (!socket->canReadLine()) return; while (socket->canReadLine()) { const QByteArray data = socket->readLine().trimmed(); if (data.startsWith(StartTaskCommand)) { bool ok = true; const TaskId id = data.mid(StartTaskCommand.length()).toInt(&ok); if (ok) { m_timeTracker.slotStartEvent(id); } else { qWarning() << "Received invalid argument:" << data; } } else if (data.startsWith(RaiseWindowCommand)) { // nothing to do, see below } } socket->deleteLater(); showMainWindow(ApplicationCore::ShowMode::ShowAndRaise); }); }
void PlayerListener::onDataReady() { QLocalSocket* socket = qobject_cast<QLocalSocket*>(sender()); if (!socket) return; while (socket->canReadLine()) { QString const line = QString::fromUtf8( socket->readLine() ); try { PlayerCommandParser parser( line ); QString const id = parser.playerId(); PlayerConnection* connection = 0; if (!m_connections.contains( id )) { connection = m_connections[id] = new PlayerConnection( parser.playerId(), parser.playerName() ); emit newConnection( connection ); } else connection = m_connections[id]; switch (parser.command()) { case CommandBootstrap: emit bootstrapCompleted( parser.playerId() ); break; case CommandTerm: delete connection; m_connections.remove( parser.playerId() ); break; default: connection->handleCommand( parser.command(), parser.track() ); break; } socket->write( "OK\n" ); } catch (std::invalid_argument& e) { qWarning() << e.what(); QString s = "ERROR: " + QString::fromStdString(e.what()) + "\n"; socket->write( s.toUtf8() ); } } }
void SocketApi::slotReadSocket() { QLocalSocket* socket = qobject_cast<QLocalSocket*>(sender()); Q_ASSERT(socket); while(socket->canReadLine()) { QString line = QString::fromUtf8(socket->readLine()).trimmed(); QString command = line.split(":").first(); QString function = QString(QLatin1String("command_")).append(command); QString functionWithArguments = function + QLatin1String("(QString,QLocalSocket*)"); int indexOfMethod = this->metaObject()->indexOfMethod(functionWithArguments.toAscii()); QString argument = line.remove(0, command.length()+1).trimmed(); if(indexOfMethod != -1) { QMetaObject::invokeMethod(this, function.toAscii(), Q_ARG(QString, argument), Q_ARG(QLocalSocket*, socket)); } else {
void QtLocalPeer::receiveMessage() { QLocalSocket *socket = (QLocalSocket*)(sender()); QString msg; while (socket->bytesAvailable() > 0) { if (socket->canReadLine()) { msg = socket->readLine(); } else { msg = socket->readAll(); } // qDebug()<<"local peer reciveing data:"<<msg<<socket->bytesAvailable(); if (this->isClient()) { emit messageReceived(msg); } else { int client_id = this->clients.value(socket); emit messageReceived(msg, client_id); } } }
void RKGraphicsDeviceFrontendTransmitter::newConnection () { RK_TRACE (GRAPHICS_DEVICE); RK_ASSERT (!connection); QLocalSocket *con = local_server->nextPendingConnection (); local_server->close (); // handshake QString token = RKFrontendTransmitter::instance ()->connectionToken (); if (!con->canReadLine ()) con->waitForReadyRead (1000); QString token_c = QString::fromLocal8Bit (con->readLine ()); token_c.chop (1); if (token_c != token) { KMessageBox::detailedError (0, QString ("<p>%1</p>").arg (i18n ("There has been an error while trying to connect the on-screen graphics backend. This means, on-screen graphics using the RKWard device will not work in this session.")), i18n ("Expected connection token %1, but read connection token %2").arg (token).arg (token_c), i18n ("Error while connection graphics backend")); con->close (); return; } connection = con; streamer.setIODevice (con); connect (connection, SIGNAL (readyRead ()), this, SLOT (newData ())); newData (); // might already be available }