本文整理汇总了C++中QLocalSocket::bytesAvailable方法的典型用法代码示例。如果您正苦于以下问题:C++ QLocalSocket::bytesAvailable方法的具体用法?C++ QLocalSocket::bytesAvailable怎么用?C++ QLocalSocket::bytesAvailable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket::bytesAvailable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendFortune
void GuiManager::sendFortune()
{
qDebug()<<__FUNCTION__;
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_4);
out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
QLocalSocket *clientConnection = server->nextPendingConnection();
qDebug()<<"===================="<<clientConnection->isOpen();
QLocalSocket* socket = clientConnection;
QDataStream in(socket);
in.setVersion(QDataStream::Qt_5_4);
quint16 blockSize = 0;
if (blockSize == 0) {
qDebug()<< (socket->bytesAvailable() < (int)sizeof(quint16));
if (socket->bytesAvailable() < (int)sizeof(quint16))
qDebug()<<"socket->bytesAvailable()"<<socket->bytesAvailable() ;
in >> blockSize;
}
示例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: 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 {
示例4: 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;
}
示例5: 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);
}
}
示例6: handleURIConnection
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);
}
示例7: socket_new_connection
void LocalSocketIpcServer::socket_new_connection() {
QLocalSocket *clientConnection = m_server->nextPendingConnection();
while (clientConnection->bytesAvailable() < (int)sizeof(quint32))
clientConnection->waitForReadyRead();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
QString message;
in >> message;
emit messageReceived(message);
}
示例8: GetData
void CLocalSvrCommunication::GetData( )
{
QLocalSocket* pSocket = qobject_cast< QLocalSocket* >( sender( ) );
quint64 nDataSize = pSocket->bytesAvailable( );
if ( 0 == nDataSize ) {
return;
}
QByteArray byData = pSocket->readAll( );
QString strMsg( byData );
emit NotifyMsg( strMsg );
}
示例9: newConnection
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();
}
}
}
示例10: newInputsAvailable
//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);
}
}
示例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: in
Server::Server(QString servername, QObject *parent) : QObject(parent){
qDebug() << "Starting server...";
m_server = new QLocalServer(this);
while(!m_server->listen(servername)){
if(m_server->serverError() == QAbstractSocket::AddressInUseError){
qDebug() << m_server->serverError() << "\nCalling removeServer...";
m_server->removeServer(servername);
}else{
qDebug() << "Not able to start the server!\n" << m_server->serverError();
}
}
if (m_server->isListening())
qDebug() << "Server started.\n\n";
clientID = 0;
connect(m_server, &QLocalServer::newConnection, [this](){
clientID++;
QLocalSocket *sock = m_server->nextPendingConnection();
clients.insert(clientID, sock);
sock->setProperty("ID", QVariant(clientID));
sock->setProperty("BTR", QVariant(0));
QObject::connect(sock, &QLocalSocket::readyRead, [this, sock](){
QDataStream in(sock);
in.setVersion(QDataStream::Qt_5_0);
if(sock->property("BTR").toInt() == 0){
quint64 btr;
in >> btr;
sock->setProperty("BTR", QVariant(btr));
}
if(sock->bytesAvailable() < sock->property("BTR").toInt())
return;
QString message;
in >> message;
sock->setProperty("BTR", QVariant(0));
emit newMessageFromClient(sock->property("ID").toInt(), message);
});
示例13: qt_waitforinput
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();
}
示例14: _q_readData
void MRmiServerPrivateSocket::_q_readData()
{
QLocalSocket* socket = (QLocalSocket*) sender();
uint sz = socket->bytesAvailable();
QDataStream stream(socket);
stream.setVersion(QDataStream::Qt_4_0);
if (method_size == 0) {
if (sz < (int)sizeof(quint16))
return;
stream >> method_size;
}
if (sz < method_size)
return;
invoke(stream);
}
示例15: receiveMessage
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);
}
}
}