void FeatureProxyConnector::initialize() { if (mProcessInitialized == true) { return; } long pid = -1; #if defined(WIN_API) pid = GetCurrentProcessId(); #endif mpConnectionTimer = new QTimer(this); mpConnectionTimer->setSingleShot(true); int connectionTimeout = FeatureProxyConnector::getSettingConnectionTimeout(); mpConnectionTimer->setInterval(connectionTimeout); VERIFYNR(connect(mpConnectionTimer, SIGNAL(timeout()), this, SLOT(abortConnection()))); if (mpProcess->state() != QProcess::NotRunning) { terminate(); } mpServer = new QLocalServer(this); if (!mpServer->listen("OpticksFeatureProxyConnector" + QString::number(pid))) { terminate(); return; } QStringList args; if (pid >= 0) { args << "-pid" << QString::number(pid); } mpProcess->start(mExecutable, args); mProcessInitialized = true; if (!mpProcess->waitForStarted()) { terminate(); return; } if (!mpServer->waitForNewConnection(3000)) { terminate(); return; } mpSocket = mpServer->nextPendingConnection(); mpServer->close(); mStream.setDevice(mpSocket); connect(mpSocket, SIGNAL(readyRead()), this, SLOT(processReply())); mStream << APP_VERSION_NUMBER << endl; }
void Server::incomingConnection(int id) { QThread* thread = new QThread(); ServerSocket* socket = new ServerSocket(id); socket->moveToThread(thread); thread->start(); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(socket, SIGNAL(logGenerated(QString)), this, SLOT(on_socket_logGenerated(QString))); connect(socket, SIGNAL(imageRead(QByteArray)), this, SLOT(on_socket_imageRead(QByteArray))); connect(this, SIGNAL(serverTerminated()), socket, SLOT(disconnectFromHostImplementation())); connect(this, SIGNAL(serverTerminated()), socket, SLOT(abortConnection())); connect(socket, SIGNAL(disconnected()), thread, SLOT(quit())); //Seems risky }
status_t SimpleSocketConnection::notifyReadable() { uint_t dataSize = 0; ulong_t curResponseSize; if (sending_) Log(eLogDebug, _T("notifyReadable(): called while sending data, probably some connection error occured"), true); status_t status = errNone; status_t error = getSocketErrorStatus(status); if (errNone == error) error = status; else { LogStrUlong(eLogInfo, _T("notifyReadable(): getSocketErrorStatus() returned error (ignored): "), error); error = errNone; } if (errNone != error) goto Exit; curResponseSize = responseLen_; if (curResponseSize >= maxResponseSize_ - chunkSize_) { error = errResponseTooLong; goto Exit; } if (NULL == chunk_) chunk_ = (char*)malloc(chunkSize_); if (NULL == chunk_) { error = memErrNotEnoughSpace; goto Exit; } dataSize = 0; error = socket().receive(dataSize, chunk_, chunkSize_, transferTimeout()); if (errNone != error) goto Exit; totalReceived_ += dataSize; assert(dataSize <= chunkSize_); response_ = StrAppend(response_, responseLen_, chunk_, dataSize); if (NULL == response_) { error = memErrNotEnoughSpace; goto Exit; } responseLen_ += dataSize; if (0 == dataSize) { // Log(eLogInfo, _T("notifyReadable(): 0 == dataSize (server shut socket down?)"), true); // TODO: if we use chunkSize_ != dataSize condition, we also need // to notifyProgress(); //error=notifyProgress(); //if (error) // goto Exit; error = notifyFinished(); abortConnection(); } else { registerEvent(SocketSelector::eventRead); error = notifyProgress(); } Exit: if (errNone!=error) LogStrUlong(eLogError, _T("notifyReadable(): Socket::receive() returned error: "), error); return error; }