qint64 QtLocalPeer::getRunningPid() { if (!isClient()) return 0; QLocalSocket socket; bool connOk = false; for (int i = 0; i < 2; i++) { // Try twice, in case the other instance is just starting up socket.connectToServer(socketName); connOk = socket.waitForConnected(5000/2); if (connOk || i) break; Sleep(250); } if (!connOk) return -1; const char* msg = "qbt://pid"; QDataStream ds(&socket); ds.writeBytes(msg, qstrlen(msg)); bool res = socket.waitForBytesWritten(5000) && socket.waitForReadyRead(5000); if (!res) return -1; DWORD pid; qint64 pid_size = sizeof pid; while (socket.bytesAvailable() < pid_size) socket.waitForReadyRead(); if (socket.read((char *)&pid, pid_size) < pid_size) return -1; return pid; }
void QtLocalPeer::receiveConnection() { QLocalSocket* socket = server->nextPendingConnection(); if (!socket) return; while (socket->bytesAvailable() < (int)sizeof(quint32)) socket->waitForReadyRead(); QDataStream ds(socket); QByteArray uMsg; quint32 remaining; ds >> remaining; uMsg.resize(remaining); int got = 0; char* uMsgBuf = uMsg.data(); do { got = ds.readRawData(uMsgBuf, remaining); remaining -= got; uMsgBuf += got; } while (remaining && got >= 0 && socket->waitForReadyRead(2000)); if (got < 0) { qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData()); delete socket; return; } QString message(QString::fromUtf8(uMsg)); socket->write(ack, qstrlen(ack)); socket->waitForBytesWritten(1000); delete socket; emit messageReceived(message); //### (might take a long time to return) }
void ControlPeer::receiveConnection() { QLocalSocket* socket = p->server->nextPendingConnection(); if (!socket) { return; } while (socket->bytesAvailable() < (int)sizeof(quint32)) { socket->waitForReadyRead(); } QDataStream ds(socket); QByteArray uMsg; quint32 remaining; ds >> remaining; uMsg.resize(remaining); int got = 0; char* uMsgBuf = uMsg.data(); do { got = ds.readRawData(uMsgBuf, remaining); remaining -= got; uMsgBuf += got; } while (remaining && got >= 0 && socket->waitForReadyRead(2000)); if (got < 0) { qWarning("Guzum.ControlPeer: Message reception failed %s", socket->errorString().toLatin1().constData()); delete socket; return; } QString message(QString::fromUtf8(uMsg)); socket->write(ACK, qstrlen(ACK)); socket->waitForBytesWritten(1000); delete socket; // split message into the tokens, the format is the following: // <method_name>\n<arg0>\n<arg1> etc QStringList tokens = message.split("\n"); QString methodName = tokens[0]; if (methodName == SHOW_DIALOG_METHOD) { showFileSelectorDialog(); } else if (methodName == OPEN_FILE_METHOD) { if (tokens.size() == 2) { // just open file using default gnupg home QString filename = tokens[1]; editFile(filename); } else if (tokens.size() == 3) { // use file and custom gnupg home QString filename = tokens[1]; QString gnupgHome = tokens[2]; editFile(filename, gnupgHome); } QString filename = message.mid(qstrlen(OPEN_FILE_METHOD)+1); } }
void Application::newConnection() { QLocalSocket *socket = m_localServer->nextPendingConnection(); if (!socket) { return; } socket->waitForReadyRead(1000); MainWindow *window = NULL; QString data; QStringList arguments; QTextStream stream(socket); stream >> data; QByteArray byteArray = QByteArray::fromBase64(data.toUtf8()); QDataStream in(&byteArray, QIODevice::ReadOnly); in >> arguments; QCommandLineParser *parser = getParser(); parser->parse(arguments); if (!SettingsManager::getValue(QLatin1String("Browser/OpenLinksInNewTab")).toBool() && !parser->isSet(QLatin1String("privatesession"))) { window = createWindow(parser->isSet(QLatin1String("privatesession"))); } else { window = getWindow(); } if (window) { if (parser->positionalArguments().isEmpty()) { window->openUrl(); } else { QStringList urls = parser->positionalArguments(); for (int i = 0; i < urls.count(); ++i) { window->openUrl(QUrl(urls.at(i))); } } } delete socket; if (window) { window->raise(); window->activateWindow(); } delete parser; }
Application::Application(const SearchQuery &query, QObject *parent) : QObject(parent) { // Ensure only one instance of Application Q_ASSERT(!m_instance); m_instance = this; m_settings = new Settings(this); m_localServer = new QLocalServer(this); m_networkManager = new QNetworkAccessManager(this); m_extractorThread = new QThread(this); m_extractor = new Extractor(); m_docsetRegistry = new DocsetRegistry(); m_docsetRegistry->init(m_settings->docsetPath); m_mainWindow = new MainWindow(this); // Server for detecting already running instances connect(m_localServer, &QLocalServer::newConnection, [this]() { QLocalSocket *connection = m_localServer->nextPendingConnection(); // Wait a little while the other side writes the bytes connection->waitForReadyRead(); if (connection->bytesAvailable()) { QDataStream in(connection); Zeal::SearchQuery query; in >> query; m_mainWindow->bringToFront(query); } else {
void BrowserApplication::newLocalSocketConnection() { QLocalSocket *socket = m_localServer->nextPendingConnection(); if (!socket) return; socket->waitForReadyRead(1000); QDataStream stream(socket); QSharedDataPointer<ProcessOptions> processOptions(new ProcessOptions()); stream >> *processOptions; QString url = processOptions->url; if (!url.isEmpty()) { QSettings settings; settings.beginGroup(QLatin1String("general")); int openLinksIn = settings.value(QLatin1String("openLinksIn"), 0).toInt(); settings.endGroup(); if (openLinksIn == 1) newMainWindow(processOptions); else { mainWindow()->tabWidget()->newTab(processOptions); } openUrl(url); } delete socket; mainWindow()->raise(); mainWindow()->activateWindow(); }
bool QtLocalPeer::sendMessage(const QString &message, int timeout) { if (!isClient()) return false; QLocalSocket socket; bool connOk = false; for (int i = 0; i < 2; i++) { // Try twice, in case the other instance is just starting up socket.connectToServer(socketName); connOk = socket.waitForConnected(timeout/2); if (connOk || i) break; int ms = 250; #if defined(Q_OS_WIN) Sleep(DWORD(ms)); #else struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; nanosleep(&ts, NULL); #endif } if (!connOk) return false; QByteArray uMsg(message.toUtf8()); QDataStream ds(&socket); ds.writeBytes(uMsg.constData(), uMsg.size()); bool res = socket.waitForBytesWritten(timeout); res &= socket.waitForReadyRead(timeout); // wait for ack res &= (socket.read(qstrlen(ack)) == ack); return res; }
int QmKeysPrivate::getKeyValue(const struct input_event &query) { // Try to connect to qmkeyd. QLocalSocket socket; socket.connectToServer(SERVER_NAME); if (!socket.waitForConnected(1000)) { return -1; } // Query for the given key if (socket.write((char*)&query, sizeof(query)) != sizeof(query)) { return -1; } if (!socket.waitForReadyRead(1000)) { return -1; } struct input_event response; int ret = 0; // A loop because we might receive other events as well. do { ret = socket.read((char*)&response, sizeof(response)); if (ret == sizeof(response)) { if (response.type == query.type && response.code == query.code) { break; } } } while (ret == sizeof(response)); socket.disconnect(); return response.value; }
void SocketListener::onNewConnection() { JUFFENTRY; QLocalSocket* socket = server_->nextPendingConnection(); if ( !socket->waitForReadyRead(1000) ) { qDebug() << "Couldn't read data:" << socket->errorString(); return; } QByteArray data = socket->readAll(); JUFFDEBUG(QString::fromLocal8Bit(data)); if ( data.isEmpty() ) { return; } QStringList list = QString::fromLocal8Bit(data).split(";"); foreach (QString arg, list) { if ( arg[0] == '-' ) { if ( arg.compare("--newfile") == 0 ) { emit newFileRequested(); } } else { if ( !arg.isEmpty() ) emit fileRecieved(QFileInfo(arg).absoluteFilePath()); } } }
//////////////////////////////////////////////////////////////////////////////// // 通过socket通讯实现程序单实例运行,监听到新的连接时触发该函数 //////////////////////////////////////////////////////////////////////////////// void QSingleApplication::_newLocalConnection() { QLocalSocket *socket = _localServer->nextPendingConnection(); if(socket) { socket->waitForReadyRead(2*TIME_OUT); delete socket; // 其他处理,如:读取启动参数 _activateWindow(); } }
void QtLocalPeer::receiveConnection() { QLocalSocket* socket = server->nextPendingConnection(); if (!socket) return; while (socket->bytesAvailable() < (int)sizeof(quint32)) socket->waitForReadyRead(); QDataStream ds(socket); QByteArray uMsg; quint32 remaining; ds >> remaining; uMsg.resize(remaining); int got = 0; char* uMsgBuf = uMsg.data(); do { got = ds.readRawData(uMsgBuf, remaining); remaining -= got; uMsgBuf += got; } while (remaining && got >= 0 && socket->waitForReadyRead(2000)); if (got < 0) { qWarning() << "QtLocalPeer: Message reception failed" << socket->errorString(); delete socket; return; } QString message(QString::fromUtf8(uMsg)); #ifdef Q_OS_WIN if (message == "qbt://pid") { qint64 pid = GetCurrentProcessId(); socket->write((const char *)&pid, sizeof pid); } else { socket->write(ack, qstrlen(ack)); } #else socket->write(ack, qstrlen(ack)); #endif socket->waitForBytesWritten(1000); delete socket; #ifdef Q_OS_WIN if (message == "qbt://pid") return; #endif emit messageReceived(message); //### (might take a long time to return) }
void MainWidget::newLocalConnection() { QLocalSocket *socket = server->nextPendingConnection(); if(!socket) return; socket->waitForReadyRead(1000); delete socket; }
/** * @brief Executed when the showUp command is sent to LocalServer */ void SingleApplication::slotConnectionEstablished() { QLocalSocket *socket = d_ptr->server->nextPendingConnection(); QByteArray data; if (socket->waitForReadyRead()) data = socket->readAll(); socket->close(); delete socket; emit otherInstanceDataReceived(data); }
void TSBrowserApplication::newLocalSocketConnection() { QLocalSocket *socket = m_localServer->nextPendingConnection(); if (!socket) return; socket->waitForReadyRead(1000); QTextStream stream(socket); QString url; stream >> url; delete socket; }
void SingleApplication::newConnection() { QLocalSocket *socket = m_localServer->nextPendingConnection(); if (!socket) return; socket->waitForReadyRead(); QTextStream stream(socket); QString message; stream >> message; emit messageRecieved(message); delete socket; }
void Genesis::establishConnection() { // qDebug() << "establishConnection"; QLocalSocket *clientConnection = nextPendingConnection(); connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater())); connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClientData())); _blockSize = 0; clientConnection->waitForReadyRead(-1); }
void SingleApp::receiveMessage() { QLocalSocket *localSocket = localServer->nextPendingConnection(); if (!localSocket->waitForReadyRead(timeout)) { qDebug("%s", qPrintable(localSocket->errorString().toLatin1())); return; } QByteArray byteArray = localSocket->readAll(); QString message = QString::fromUtf8(byteArray.constData()); emit messageReceived(message); localSocket->disconnectFromServer(); }
void QtLocalPeer::receiveConnection() { QLocalSocket* socket = server->nextPendingConnection(); if (!socket) return; // Why doesn't Qt have a blocking stream that takes care of this shait??? while (socket->bytesAvailable() < static_cast<int>(sizeof(quint32))) { if (!socket->isValid()) // stale request return; socket->waitForReadyRead(1000); } QDataStream ds(socket); QByteArray uMsg; quint32 remaining; ds >> remaining; uMsg.resize(remaining); int got = 0; char* uMsgBuf = uMsg.data(); //qDebug() << "RCV: remaining" << remaining; do { got = ds.readRawData(uMsgBuf, remaining); remaining -= got; uMsgBuf += got; //qDebug() << "RCV: got" << got << "remaining" << remaining; } while (remaining && got >= 0 && socket->waitForReadyRead(2000)); //### error check: got<0 if (got < 0) { qWarning() << "QtLocalPeer: Message reception failed" << socket->errorString(); delete socket; return; } // ### async this QString message = QString::fromUtf8(uMsg.constData(), uMsg.size()); socket->write(ack, qstrlen(ack)); socket->waitForBytesWritten(1000); emit messageReceived(message, socket); // ##(might take a long time to return) }
void QtLocalPeer::receiveConnection() { QLocalSocket* socket = server->nextPendingConnection(); if (!socket) return; int client_id = this->client_seq ++; this->clients.insert(socket, client_id); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(receiveMessage())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); return; // multi client long connection support while (socket->bytesAvailable() < (int)sizeof(quint32)) socket->waitForReadyRead(); QDataStream ds(socket); QByteArray uMsg; quint32 remaining; ds >> remaining; uMsg.resize(remaining); int got = 0; char* uMsgBuf = uMsg.data(); do { got = ds.readRawData(uMsgBuf, remaining); remaining -= got; uMsgBuf += got; } while (remaining && got >= 0 && socket->waitForReadyRead(2000)); if (got < 0) { qWarning() << "QtLocalPeer: Message reception failed" << socket->errorString(); delete socket; return; } QString message(QString::fromUtf8(uMsg)); socket->write(ack, qstrlen(ack)); socket->waitForBytesWritten(1000); // delete socket; emit messageReceived(message); //### (might take a long time to return) }
void mASocketManager::newConnection() { //fprintf(stderr, "[miniAudicle]: received connection from remote\n"); //fflush(stderr); QLocalSocket * socket = m_server->nextPendingConnection(); QByteArray data; QString path; int timeouts = 0; while(timeouts < MAX_TIMEOUTS) { if(socket->bytesAvailable() <= 0 && !socket->waitForReadyRead(MAX_TIMEOUT_MS/MAX_TIMEOUTS)) timeouts++; else { QByteArray bytes = socket->readAll(); data.append(bytes); bytes.append('\0'); //fprintf(stderr, "[miniAudicle]: received data '%s'\n", bytes.constData()); // check for line ending if(data.at(data.length()-1) == '\n') { path = QString(data); // remove trailing \n path.remove(path.length()-1, 1); socket->close(); socket = NULL; break; } } } if(path.length()) { if(QFileInfo(path).exists()) { //fprintf(stderr, "[miniAudicle]: received path '%s' from remote\n", path.toUtf8().constData()); //fflush(stderr); m_mainWindow->openFile(path); m_mainWindow->activateWindow(); m_mainWindow->raise(); m_mainWindow->show(); } } }
void MdCharmApplication::newConnectionSlot() { qDebug( "new connection" ); QLocalSocket *socket = localServer->nextPendingConnection(); if ( !socket ) { return; } socket->waitForReadyRead( 1000 ); QTextStream stream( socket ); QString args = stream.readAll(); delete socket; emit openFiles( args.split( "," ) ); }
void IPC::handleConnection(){ QLocalSocket *clientConnection = this->m_server->nextPendingConnection(); connect(clientConnection, &QLocalSocket::disconnected, clientConnection, &QLocalSocket::deleteLater); clientConnection->waitForReadyRead(2); QByteArray cmdArray = clientConnection->readAll(); QString cmdString = QTextCodec::codecForMib(106)->toUnicode(cmdArray); // UTF-8 qDebug() << cmdString; this->parseCommand(cmdString); clientConnection->close(); delete clientConnection; }
void UniqueApp::newLocalConnection() { QLocalSocket *socket = m_server->nextPendingConnection(); if(!socket) return; socket->waitForReadyRead(1000); //显示传入参数值 QTextStream in(socket); QString vl; in >> vl; qDebug()<<"The value is: "<<vl; delete socket; }
void WebBrowserApp::newConnection() { QLocalSocket *socket = m_pLocalServer->nextPendingConnection(); if (!socket) return; socket->waitForReadyRead(1000); QTextStream stream(socket); QString url; stream >> url; QMessageBox::information(NULL,QString("url"),url); delete socket; mainWindow()->raise(); mainWindow()->activateWindow(); }
//New messages detected void LSingleApplication::newInputsAvailable(){ while(lserver->hasPendingConnections()){ QLocalSocket *sock = lserver->nextPendingConnection(); QByteArray bytes; sock->waitForReadyRead(); while(sock->bytesAvailable() > 0){ //if(sock->waitForReadyRead()){ //qDebug() << "Info Available"; bytes.append( sock->readAll() ); } sock->disconnectFromServer(); QStringList inputs = QString::fromLocal8Bit(bytes).split("::::"); //qDebug() << " - New Inputs Detected:" << inputs; emit InputsAvailable(inputs); } }
void Application::receiveMessage() { QLocalSocket *localSocket = d_ptr->localServer.nextPendingConnection(); if (!localSocket->waitForReadyRead(GUI_APPLICATION_LOCAL_SOCKET_TIMEOUT)) return; QByteArray byteArray = localSocket->readAll(); const QString message = QString::fromUtf8(byteArray.constData()); if (message == "raise") emit raiseRequested(); localSocket->disconnectFromServer(); }
void MainWindow::newConnection() { QLocalSocket *socket = server->nextPendingConnection(); socket->waitForReadyRead(2000); QByteArray array = socket->readLine(); QString target(array); socket->close(); delete socket; initialize(target); on_txtArgument_textEdited(); // showNormal(); // activateWindow(); }
void GUISingleApplication::receiveConnection() { QLocalSocket *socket = d_ptr->server->nextPendingConnection(); if (!socket) return; connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); if (socket->waitForReadyRead()) { QDataStream in(socket); if (!in.atEnd()) { d_ptr->timer->stop(); QByteArray message; in >> message; Base::Console().Log("Received message: %s\n", message.constData()); d_ptr->messages.push_back(message); d_ptr->timer->start(1000); }
int qt_waitforinput(void) { #ifdef USE_MOUSE fd_set read_fds; int stdin_fd = fileno(stdin); int socket_fd = qt_socket.socketDescriptor(); if (!qt_initialized || (socket_fd < 0) || (qt_socket.state() != QLocalSocket::ConnectedState)) return getchar(); // Gnuplot event loop do { // Watch file descriptors FD_ZERO(&read_fds); FD_SET(socket_fd, &read_fds); if (!paused_for_mouse) FD_SET(stdin_fd, &read_fds); // Wait for input if (select(socket_fd+1, &read_fds, NULL, NULL, NULL) < 0) { fprintf(stderr, "Qt terminal communication error: select() error\n"); break; } // Terminal event coming if (FD_ISSET(socket_fd, &read_fds)) { qt_socket.waitForReadyRead(-1); while (qt_socket.bytesAvailable() >= sizeof(gp_event_t)) { struct gp_event_t event; qt_socket.read((char*) &event, sizeof(gp_event_t)); /// @todo don't process mouse move events if others are in the queue if (qt_processTermEvent(&event)) return '\0'; // exit from paused_for_mouse } } } while (paused_for_mouse || (!paused_for_mouse && !FD_ISSET(stdin_fd, &read_fds))); #endif return getchar(); }
void PaymentServer::handleURIConnection() { QLocalSocket *clientConnection = uriServer->nextPendingConnection(); while (clientConnection->bytesAvailable() < (int)sizeof(quint32)) clientConnection->waitForReadyRead(); connect(clientConnection, &QLocalSocket::disconnected, clientConnection, &QLocalSocket::deleteLater); QDataStream in(clientConnection); in.setVersion(QDataStream::Qt_4_0); if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) { return; } QString msg; in >> msg; handleURIOrFile(msg); }