bool MjpegClient::connectTo(const QString& host, int port, QString url, const QString& user, const QString& pass) { if(url.isEmpty()) url = "/"; m_host = host; m_port = port > 0 ? port : 80; m_url = url; m_user = user; m_pass = pass; if(m_socket) { m_socket->abort(); delete m_socket; m_socket = 0; } m_socket = new QTcpSocket(this); connect(m_socket, SIGNAL(readyRead()), this, SLOT(dataReady())); connect(m_socket, SIGNAL(disconnected()), this, SLOT(lostConnection())); connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(socketDisconnected())); connect(m_socket, SIGNAL(connected()), this, SIGNAL(socketConnected())); connect(m_socket, SIGNAL(connected()), this, SLOT(connectionReady())); connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(socketError(QAbstractSocket::SocketError))); connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(lostConnection(QAbstractSocket::SocketError))); m_socket->connectToHost(host,port); m_socket->setReadBufferSize(1024 * 1024); return true; }
void MjpegClient::lostConnection(QAbstractSocket::SocketError error) { qDebug() << "MjpegClient::lostConnection("<<error<<"):" << m_socket->errorString(); if(error == QAbstractSocket::ConnectionRefusedError) lostConnection(); }
void NetServerCom::startCommunication() { socket = server->nextPendingConnection(); connect(socket, SIGNAL(disconnected()), this, SLOT(closeSocket())); while (comActive && socket->isOpen()) { //read queued commands and send to host if (writeCmd.size()>0) { NetCommand cmd = writeCmd.dequeue(); QString m = cmd.toString().append("\n"); socket->write(m.toAscii()); socket->flush(); cout << "queued cmd sent: " << cmd.toString().toStdString() << endl; } //read command from host if (socket->waitForReadyRead(100)) { QByteArray data = socket->readAll(); NetCommand cmd = NetCommand(QString(data.constData())); emit commandReceived(cmd); } } socket->disconnect(); delete socket; emit finished(); emit lostConnection(comActive); }
void CGroupClient::linkSignals() { connect(this, SIGNAL(disconnected()), this, SLOT(lostConnection() ) ); connect(this, SIGNAL(connected()), this, SLOT(connectionEstablished() ) ); connect(this, SIGNAL(error(QAbstractSocket::SocketError )), this, SLOT(errorHandler(QAbstractSocket::SocketError) ) ); connect(this, SIGNAL(readyRead()), this, SLOT( dataIncoming() ) ); buffer = ""; currentMessageLen = 0; }
void WebSocketsClient::sendBinMessage(ConstBin msg) { Synchronized<FastLockR> _(lock); if (stream != null) try { Super::sendBinMessage(msg,true); stream->flush(); return; } catch (NetworkException &) { onReconnect().thenCall(Action::create(this,&WebSocketsClient::sendBinMessage, StringB(msg))); lostConnection(naturalNull); } else { onReconnect().thenCall(Action::create(this,&WebSocketsClient::sendBinMessage, StringB(msg))); } }
void WebSocketsClient::rearmStream() { closeCode = 0; while (!closeCode && stream->dataReady()) { if (!onRawDataIncome()) { if (closeCode==0) closeCode = naturalNull; } } if (closeCode) { lostConnection(closeCode); } else if (listener != null) { listener->add(stream, this, INetworkResource::waitForInput, naturalNull, 0); } }
/// Server operators /// @{ /// @brief Server received request, auto accept void MainWindow::acceptConnection() { tcpServerConnection = tcpServer->nextPendingConnection(); if( (tcpServerConnection->peerAddress() != tcpServer->serverAddress()) && (user_settings.server.localOnly)) { // Reject tcpServerConnection->close(); }else{ // Valid tcpServerConnectionValid = true; tcpServer->close(); connect(tcpServerConnection, SIGNAL(readyRead()), this, SLOT(newData())); connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); connect(tcpServerConnection, SIGNAL(disconnected()), this, SLOT(lostConnection())); connect(tcpServerConnection, SIGNAL(disconnected()), tcpServerConnection, SLOT(deleteLater())); } }
void Client::setupProtocolClient() { if (protocolClient != nullptr) { MessageCenter::getInstance()->setProtocolClient(nullptr); if (protocolClient->getIsConnected()) { protocolClient->disconnectFromServer(); } QMetaObject::invokeMethod(protocolClient, "teardown", Qt::QueuedConnection); protocolClient->deleteLater(); protocolClient = nullptr; } QString const nickname = contactRegistry->getNickname(clientConfiguration->getClientIdentity()); if (nickname.compare(QStringLiteral("You"), Qt::CaseInsensitive) == 0) { LOGGER()->info("Using only ID as PushFromID token (for iOS Push Receivers)."); protocolClient = new ProtocolClient(KeyRegistry(clientConfiguration->getClientLongTermKeyPair(), serverConfiguration->getServerLongTermPublicKey(), contactRegistry->getKnownIdentitiesWithPublicKeys()), GroupRegistry(contactRegistry->getKnownGroupsWithMembersAndTitles()), MessageCenter::getInstance()->getUniqueMessgeIdGenerator(), *serverConfiguration, *clientConfiguration, MessageCenter::getInstance(), PushFromId(clientConfiguration->getClientIdentity())); } else { protocolClient = new ProtocolClient(KeyRegistry(clientConfiguration->getClientLongTermKeyPair(), serverConfiguration->getServerLongTermPublicKey(), contactRegistry->getKnownIdentitiesWithPublicKeys()), GroupRegistry(contactRegistry->getKnownGroupsWithMembersAndTitles()), MessageCenter::getInstance()->getUniqueMessgeIdGenerator(), *serverConfiguration, *clientConfiguration, MessageCenter::getInstance(), PushFromId(nickname)); LOGGER()->info("Using nickname \"{}\" as PushFromID token (for iOS Push Receivers).", nickname.toStdString()); } protocolClient->moveToThread(&protocolClientThread); OPENMITTSU_CONNECT(protocolClient, connectToFinished(int, QString), this, protocolClientOnConnectToFinished(int, QString)); OPENMITTSU_CONNECT(protocolClient, readyConnect(), this, protocolClientOnReadyConnect()); OPENMITTSU_CONNECT(protocolClient, lostConnection(), this, protocolClientOnLostConnection()); OPENMITTSU_CONNECT(protocolClient, duplicateIdUsageDetected(), this, protocolClientOnDuplicateIdUsageDetected()); QEventLoop eventLoop; OPENMITTSU_CONNECT(protocolClient, setupDone(), &eventLoop, quit()); QMetaObject::invokeMethod(protocolClient, "setup", Qt::QueuedConnection); eventLoop.exec(); // blocks until "finished()" has been called MessageCenter::getInstance()->setProtocolClient(protocolClient); }
void NetClientCom::startCommunication() { bool autoReconnect = true; // Connect to Host socket=new QTcpSocket; socket->connectToHost(host, port); connect(socket, SIGNAL(disconnected()), this, SLOT(closeSocket())); if (!socket->waitForConnected(5000)) { cout << "Error: Could not connect to " << host.toStdString() << ":" << port << endl; emit couldNotConnect(); return; } cout << "Connected to " << host.toStdString() << ":" << port << endl; isconnected=true; int i=0; while (comActive && socket->isOpen()) { /*if (autoReconnect && (!socket->isOpen())) { socket->connectToHost(host, port); cout << "RECONNECTING..." << endl; if (!socket->waitForConnected()) { isconnected=false; comActive=false; cout << "Error: Could not reconnect to " << host.toStdString() << ":" << port << endl; } }*/ //read queued commands and send to host if (writeCmd.size()>0) { NetCommand cmd = writeCmd.dequeue(); QString m = cmd.toString().append("\n"); socket->write(m.toAscii()); socket->flush(); cout << "CLIENT CMD OUT: " << cmd.toString().toStdString() << endl; } //read command from host if (socket->waitForReadyRead(100)) { QByteArray data = socket->readAll(); cout << "----" << endl << QString(data.constData()).toStdString() << "----" << endl; QStringList list = QString(data.constData()).trimmed().split("\n"); for (int i=0; i<list.size(); i++) { NetCommand cmd = NetCommand(list.at(i)); cout << "CLIENT CMD IN : " << cmd.toString().toStdString() << endl; emit commandReceived(cmd); } } } isconnected=false; socket->disconnect(); cout << "client: " << "socket->disconnect();" << endl; delete socket; emit finished(); emit lostConnection(comActive); cout << "client: " << "delete socket, emit finished()" << endl; }