本文整理汇总了C++中QLocalSocket::waitForReadyRead方法的典型用法代码示例。如果您正苦于以下问题:C++ QLocalSocket::waitForReadyRead方法的具体用法?C++ QLocalSocket::waitForReadyRead怎么用?C++ QLocalSocket::waitForReadyRead使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket::waitForReadyRead方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getRunningPid
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;
}
示例2: receiveConnection
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)
}
示例3: receiveConnection
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);
}
}
示例4: newConnection
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;
}
示例5: in
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 {
示例6: newLocalSocketConnection
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();
}
示例7: sendMessage
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;
}
示例8: getKeyValue
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;
}
示例9: onNewConnection
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());
}
}
}
示例10: _newLocalConnection
////////////////////////////////////////////////////////////////////////////////
// 通过socket通讯实现程序单实例运行,监听到新的连接时触发该函数
////////////////////////////////////////////////////////////////////////////////
void QSingleApplication::_newLocalConnection() {
QLocalSocket *socket = _localServer->nextPendingConnection();
if(socket) {
socket->waitForReadyRead(2*TIME_OUT);
delete socket;
// 其他处理,如:读取启动参数
_activateWindow();
}
}
示例11: receiveConnection
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)
}
示例12: newLocalConnection
void MainWidget::newLocalConnection()
{
QLocalSocket *socket = server->nextPendingConnection();
if(!socket)
return;
socket->waitForReadyRead(1000);
delete socket;
}
示例13: slotConnectionEstablished
/**
* @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);
}
示例14: newLocalSocketConnection
void TSBrowserApplication::newLocalSocketConnection()
{
QLocalSocket *socket = m_localServer->nextPendingConnection();
if (!socket)
return;
socket->waitForReadyRead(1000);
QTextStream stream(socket);
QString url;
stream >> url;
delete socket;
}
示例15: newConnection
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;
}