本文整理汇总了C++中QLocalSocket::waitForBytesWritten方法的典型用法代码示例。如果您正苦于以下问题:C++ QLocalSocket::waitForBytesWritten方法的具体用法?C++ QLocalSocket::waitForBytesWritten怎么用?C++ QLocalSocket::waitForBytesWritten使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket::waitForBytesWritten方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//a.removeLibraryPath(a.libraryPaths().first());
qRegisterMetaType<Song>("Song");
qRegisterMetaTypeStreamOperators<Song>("Song");
MainWindow w;
QStringList args = a.arguments();
QLocalSocket socket;
socket.connectToServer("iniTunes");
QTextStream stream(&socket);
if(socket.waitForConnected(3000))
{
stream <<QString::number(PLAY)<<endl;
stream << args.at(1) <<endl;
socket.waitForBytesWritten();
exit(0);
}
else
{
qDebug()<<"Launch";
w.show();
w.createRemote();
if(args.count()==2)
{
qDebug()<<"Open";
w.open(args.at(1));
}
}
return a.exec();
}
示例2: ipcSendCommandLine
//
// Sending to the server is done synchronously, at startup.
// If the server isn't already running, startup continues,
// and the items in savedPaymentRequest will be handled
// when uiReady() is called.
//
bool PaymentServer::ipcSendCommandLine()
{
bool fResult = false;
for (const QString& r : savedPaymentRequests)
{
QLocalSocket* socket = new QLocalSocket();
socket->connectToServer(ipcServerName(), QIODevice::WriteOnly);
if (!socket->waitForConnected(BITCOIN_IPC_CONNECT_TIMEOUT))
{
delete socket;
socket = nullptr;
return false;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << r;
out.device()->seek(0);
socket->write(block);
socket->flush();
socket->waitForBytesWritten(BITCOIN_IPC_CONNECT_TIMEOUT);
socket->disconnectFromServer();
delete socket;
socket = nullptr;
fResult = true;
}
return fResult;
}
示例3: 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)
}
示例4: 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;
}
示例5: sendMessages
bool KNSingletonApplication::sendMessages(const QString &uniqueKey,
const QStringList &messages)
{
//Create sender client.
QLocalSocket client;
//Link to the server which is listening to the unique key.
client.connectToServer(uniqueKey, QIODevice::WriteOnly);
//If connecting failed, return false.
if(!client.waitForConnected(TimeoutLimit))
{
qDebug("Cannot connect to the local server.");
//Disconnect from the server.
client.disconnectFromServer();
return false;
}
//Generate the message data.
QByteArray messageData;
QDataStream dataWriter(&messageData, QIODevice::WriteOnly);
dataWriter << messages;
//Send the data to local server.
client.write(messageData);
//Check sending status.
if(!client.waitForBytesWritten(TimeoutLimit))
{
qDebug("Send arguments failed.");
client.disconnectFromServer();
return false;
}
//Send the arguments success.
client.disconnectFromServer();
return true;
}
示例6: IsAlreadyRunning
bool Application::IsAlreadyRunning () const
{
QLocalSocket socket;
socket.connectToServer (GetSocketName ());
if (socket.waitForConnected () ||
socket.state () == QLocalSocket::ConnectedState)
{
QDataStream out (&socket);
out << Arguments_;
if (socket.waitForBytesWritten ())
return true;
if (socket.error() == QLocalSocket::UnknownSocketError)
return true;
}
else
{
switch (socket.error ())
{
case QLocalSocket::ServerNotFoundError:
case QLocalSocket::ConnectionRefusedError:
break;
default:
qWarning () << Q_FUNC_INFO
<< "socket error"
<< socket.error ();
return true;
}
}
// Clear any halted servers and their messages
QLocalServer::removeServer (GetSocketName ());
return false;
}
示例7: saveCommand
// Process incoming IPC command. First check if monero-wallet-gui is
// already running. If it is, send it to that instance instead, if not,
// queue the command for later use inside our QML engine. Returns true
// when queued, false if sent to another instance, at which point we can
// kill the current process.
bool IPC::saveCommand(QString cmdString){
qDebug() << QString("saveCommand called: %1").arg(cmdString);
QLocalSocket ls;
QByteArray buffer;
buffer = buffer.append(cmdString);
QString socketFilePath = this->socketFile().filePath();
ls.connectToServer(socketFilePath, QIODevice::WriteOnly);
if(ls.waitForConnected(1000)){
ls.write(buffer);
if (!ls.waitForBytesWritten(1000)){
qDebug() << QString("Could not send command \"%1\" over IPC %2: \"%3\"").arg(cmdString, socketFilePath, ls.errorString());
return false;
}
qDebug() << QString("Sent command \"%1\" over IPC \"%2\"").arg(cmdString, socketFilePath);
return false;
}
if(ls.isOpen())
ls.disconnectFromServer();
// Queue for later
this->SetQueuedCmd(cmdString);
return true;
}
示例8: pqdbg_send_message
void pqdbg_send_message(int lvl, const QString &msg, const QString &title)
{
#ifdef PQDEBUG
static QMutex mutex;
mutex.lock();
QLocalSocket *debugSocket = PHPQt5::debugSocket();
if(debugSocket->isOpen()) {
/*
QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << reinterpret_cast<quint64>(PQEngine::pqeEngine);
out << PQEngine::pqeCoreName;
out << lvl;
out << title;
out << msg;
out.device()->reset();
// = QString("%1:::%2:::%3:::%4:::%5:::%")
// .arg(reinterpret_cast<quint64>(PQEngine::pqeEngine))
// .arg(PQEngine::pqeCoreName)
// .arg(lvl)
// .arg(title)
// .arg(msg).toUtf8();
debugSocket->write(data);
debugSocket->waitForBytesWritten();
*/
QString str = QString("%1:::%2:::%3:::%4:::%5:::%6:::%")
.arg(reinterpret_cast<quint64>(PQEngine::pqeEngine))
.arg(PQEngine::pqeCoreName)
.arg(reinterpret_cast<quint64>(QThread::currentThread()))
.arg(lvl)
.arg(title)
.arg(msg).toUtf8();
/*
QByteArray block;
QDataStream sendStream(&block, QIODevice::ReadWrite);
sendStream << quint16(0) << str;
sendStream.device()->seek(0);
sendStream << (quint16)(block.size() - sizeof(quint16));
*/
debugSocket->write(str.toUtf8());
debugSocket->waitForBytesWritten();
}
mutex.unlock();
#else
Q_UNUSED(lvl);
Q_UNUSED(msg);
Q_UNUSED(title);
#endif
}
示例9: stream
BrowserApplication::BrowserApplication(int &argc, char **argv, char *styleSheet,QSharedDataPointer<ProcessOptions> processOptions)
: QApplication(argc, argv)
, m_localServer(0)
, m_privateProfile(0)
, sawStyleSheetCommandLineOption(false)
, nextSessionNameIndex(1)
{
nameTemplate = QLatin1String("domterm-%1");
QCoreApplication::setOrganizationName(QLatin1String("DomTerm"));
QCoreApplication::setApplicationName(QLatin1String("QtDomTerm"));
QCoreApplication::setApplicationVersion(QLatin1String(QTDOMTERM_VERSION));
QString serverName = QCoreApplication::applicationName()
+ QString::fromLatin1(QT_VERSION_STR).remove('.') + QLatin1String("webengine");
if (styleSheet != NULL) {
m_stylesheetFilename = QString(styleSheet);
sawStyleSheetCommandLineOption = true;
}
m_fileSystemWatcher = new QFileSystemWatcher(this);
QLocalSocket socket;
socket.connectToServer(serverName);
if (socket.waitForConnected(500)) {
QDataStream stream(&socket);
stream << *processOptions;
socket.waitForBytesWritten();
return;
}
#if defined(Q_OS_OSX)
QApplication::setQuitOnLastWindowClosed(false);
#else
QApplication::setQuitOnLastWindowClosed(true);
#endif
m_localServer = new QLocalServer(this);
connect(m_localServer, SIGNAL(newConnection()),
this, SLOT(newLocalSocketConnection()));
if (!m_localServer->listen(serverName)
&& m_localServer->serverError() == QAbstractSocket::AddressInUseError) {
QLocalServer::removeServer(serverName);
if (!m_localServer->listen(serverName))
qWarning("Could not create local socket %s.", qPrintable(serverName));
}
#ifndef QT_NO_OPENSSL
if (!QSslSocket::supportsSsl()) {
QMessageBox::information(0, "Demo Browser",
"This system does not support OpenSSL. SSL websites will not be available.");
}
#endif
QString localSysName = QLocale::system().name();
installTranslator(QLatin1String("qt_") + localSysName);
QTimer::singleShot(0, this, SLOT(postLaunch()));
}
示例10: 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);
}
}
示例11: sendQuit
bool LH_QtPlugin_WebKit::sendQuit()
{
QLocalSocket sock;
sock.connectToServer("LCDHost_WebKitServer");
if( !sock.waitForConnected(100) ) return false;
WebKitCommand('Q').write(&sock);
sock.waitForBytesWritten(100);
sock.disconnectFromServer();
return true;
}
示例12: stream
QSingleApplication::QSingleApplication(int argc, char **argv):QApplication(argc,argv)
{
_isRunning=false;
QCoreApplication::setApplicationName("localserver");
QString serverName=QCoreApplication::applicationName();
QLocalSocket socket;
socket.connectToServer(serverName);
if(socket.waitForConnected(500))
{
QTextStream stream(&socket);
QStringList args=QCoreApplication::arguments();
if(args.count()>1)
{
stream<<args.last();
}else
{
stream<<QString();
}
stream.flush();
qDebug()<<"Connected server,program will quit";
socket.waitForBytesWritten();
/**
*qApp->quit(); 此代码是用来退出事件循环的;在构造函数中,事件循环
*尚未启动,因此就无法退出. 最好就是设置标志判断在外部判断
*/
_isRunning=true;
return;
}
qDebug()<<"Can't connect to server,build a server";
server=new QLocalServer(this);
connect(server,SIGNAL(newConnection()),this,SLOT(newLocalConnection()));
if(server->listen(serverName))
{
//防止程序崩溃时,残留进程服务,移除之
if(server->serverError()==QAbstractSocket::AddressInUseError&&QFile::exists(server->serverName()))
{
QFile::remove(server->serverName());
server->listen(serverName);
}
}
}
示例13: main
int main(int argc, char *argv[])
{
//_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF );
//oldHandler = qInstallMsgHandler(msgHandler);
QApplication a(argc, argv);
QTranslator qtTranslator;
qtTranslator.load(":/lang/qt_" + QLocale::system().name(),
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
a.installTranslator(&qtTranslator);
QTranslator myappTranslator;
myappTranslator.load(":/lang/xdcc_" + QLocale::system().name());
a.installTranslator(&myappTranslator);
_singular = new QSharedMemory("DCClient");
if(_singular->attach(QSharedMemory::ReadWrite))
{
_singular->detach();
if(argc >= 2)
{
QLocalSocket socket;
socket.connectToServer("DCClientIPC");
if(socket.waitForConnected())
{
QDataStream out(&socket);
out << QString(argv[1]);
socket.waitForBytesWritten();
}
}
return 0;
}
_singular->create(1);
QApplication::setQuitOnLastWindowClosed(false);
XDCC xdcc;
int ret = a.exec();
delete _singular;
return ret;
}
示例14: sendMessage
bool SingleApplication::sendMessage(const QString &message)
{
QLocalSocket socket;
socket.connectToServer(serverName());
if (socket.waitForConnected(500)) {
QTextStream stream(&socket);
stream << message;
stream.flush();
if (socket.waitForBytesWritten())
return true;
}
return false;
}
示例15: serverSendMessage
bool QtLocalPeer::serverSendMessage(const QString &message, int client_id, int timeout)
{
qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<client_id;
QLocalSocket *socket = NULL;
QHash<QLocalSocket*, int>::iterator it;
if (client_id == -1) {
// broadcast
for (it = this->clients.begin(); it != this->clients.end(); it++) {
socket = it.key();
// QByteArray uMsg(message.toUtf8());
// QDataStream ds(socket);
// ds.writeBytes(uMsg.constData(), uMsg.size());
socket->write(message.toAscii());
bool res = socket->waitForBytesWritten(timeout);
}
} else {
for (it = this->clients.begin(); it != this->clients.end(); it++) {
if (it.value() == client_id) {
socket = it.key();
}
}
if (socket) {
// qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<client_id;
// QByteArray uMsg(message.toUtf8());
// QDataStream ds(socket);
// ds.writeBytes(uMsg.constData(), uMsg.size());
socket->write(message.toAscii());
bool res = socket->waitForBytesWritten(timeout);
} else {
qDebug()<<"can not find socket for client:"<<client_id;
}
}
return true;
}