int readBytes( PAPIConnection * inConnection, char * inBuffer, int inLen ) { int theTotalByteCount = 0; while ( inLen > 0 ) { int theByteCount = -1; if ( haveMessage( inConnection ) != -1 ) { theByteCount = recv(inConnection->mFD, inBuffer + theTotalByteCount, inLen, 0); if ( theByteCount == -1 && errno == EINTR ) { theByteCount = 0; } } if ( theByteCount == SOCKET_ERROR ) { theTotalByteCount = -1; break; } else { inLen -= theByteCount; theTotalByteCount += theByteCount; } } return theTotalByteCount; }
void MumbleClient::connectToServer(QString address, unsigned port) { _telnet->connectHost(address,port); QObject::connect(_telnet,SIGNAL(connectedToHost()),this,SLOT(sendVersion())); QObject::connect(_telnet,SIGNAL(haveMessage(QByteArray)),this,SLOT(processProtoMessage(QByteArray))); QObject::connect(_telnet,SIGNAL(haveUDPData(QByteArray)),this,SLOT(processUDPData(QByteArray))); }
void MumbleClient::disconnectFromServer() { if(_authenticated) { _telnet->disconnectHost(); QObject::disconnect(_telnet,SIGNAL(connectedToHost()),this,SLOT(sendVersion())); QObject::disconnect(_telnet,SIGNAL(haveMessage(QByteArray)),this,SLOT(processProtoMessage(QByteArray))); QObject::disconnect(_telnet,SIGNAL(haveUDPData(QByteArray)),this,SLOT(processUDPData(QByteArray))); _encryption_set = false; _authenticated = false; _synchronized = false; _session_id = -1; } }
void TelnetClient::processData() { if (_status !=1) return; QByteArray data; bool endOfLine = false; while ((!endOfLine)) { if(_status==1) { char ch; if(_socket->bytesAvailable()>0) { int bytesRead = _socket->read(&ch, sizeof(ch)); if (bytesRead == sizeof(ch)) { //cnt++; if (_socket->bytesAvailable()==0) { endOfLine = true; } data.append( ch ); } } else { break; } } } qDebug() << "Good message from " << _socket->peerAddress().toString(); emit haveMessage(data); }
void * listener( void * inConnection ) { #ifndef WNT sigset_t theSigset; #endif PAPIConnection * theConnection = ( PAPIConnection * )inConnection; #ifndef WNT sigemptyset( &theSigset ); sigaddset( &theSigset, SIGHUP ); pthread_sigmask( SIG_BLOCK, &theSigset, 0 ); #endif setListenerThreadStatus( inConnection, RUNNING ); while ( getListenerThreadStatus( inConnection ) == RUNNING ) { PAPIStatus theStatus; PAPIMessage * theMessage; if ( theConnection->mFD != INVALID_SOCKET ) { int bHaveMessage = haveMessage( theConnection ); if ( bHaveMessage == 0 ) { continue; } else if ( bHaveMessage == -1 ) { papiClose( theConnection->mFD ); theConnection->mFD = INVALID_SOCKET; if ( lockMutex( theConnection->mConnectionStateChangeMutex ) == 0 ) { theConnection->mConnectionListener( PAPIDisconnected, theConnection->mUserData ); unlockMutex( theConnection->mConnectionStateChangeMutex ); } if ( isDaemonEnabled() != 1 ) { break; } continue; } theMessage = getResponseMessage( theConnection, &theStatus ); if ( theMessage != 0 ) { if ( theMessage->mName == PAPImodifyNotification || theMessage->mName == PAPIaddNotification || theMessage->mName == PAPIremoveNotification ) { sendInternalNotification( theConnection, theMessage ); sendNotification( theConnection, theMessage ); deleteMessage( theMessage ); } else if ( theMessage->mName == PAPIRespSuccessContinueSASLAuth&& theConnection->mAuthHandler != 0 ) { handleSASLAuth( theConnection, theMessage ); } else { if ( lockMutex( theConnection->mSavedMessageMutex ) == 0 ) { theConnection->mSavedMessage = theMessage; unlockMutex( theConnection->mSavedMessageMutex ); } } } } else { if ( lockMutex( theConnection->mConnectionStateChangeMutex ) == 0 ) { theConnection->mFD = daemonConnect( 1 ); setBlocking( theConnection, 0 ); if ( theConnection->mFD != INVALID_SOCKET ) { theConnection->mConnectionListener( PAPIConnected, theConnection->mUserData ); } unlockMutex( theConnection->mConnectionStateChangeMutex ); if ( theConnection->mFD == INVALID_SOCKET ) { papiSleep( 10 ); } } } } setListenerThreadStatus( inConnection, NOT_RUNNING ); return 0; }