MainWindow::MainWindow() { setUnifiedTitleAndToolBarOnMac( true ); r = new Comms(); createActions(); createToolBars(); createDocks(); contactsList = new QListWidget(); contactsList->setMinimumWidth( 150 ); contactsList->setMinimumHeight( 250 ); contactsList->setUniformItemSizes( true ); contactsList->setSpacing( 3 ); contactsList->setWordWrap( true ); connectBox = new ConnectBox(); setCentralWidget( connectBox ); QObject::connect( r, SIGNAL( sigConnected() ), this, SLOT( slotConnected() ) ); QObject::connect( r, SIGNAL( sigRoster( QStringList* ) ), this, SLOT( slotRoster( QStringList* ) ) ); QObject::connect( r, SIGNAL( sigRosterPresence(QString, QString) ), this, SLOT( slotRosterPresence(QString, QString) ) ); QObject::connect( r, SIGNAL( sigVCardReceived(QString,QString) ), this, SLOT( slotVCardReceived(QString,QString) ) ); QObject::connect( r, SIGNAL( sigMessage(QString,QString) ), this, SLOT( slotMessage(QString,QString) ) ); QObject::connect( connectBox, SIGNAL( tryConnection(QString,QString) ), this, SLOT( setIdents(QString,QString) ) ); timer = new QTimer( this ); QObject::connect( timer, SIGNAL( timeout() ), r, SLOT( slotReceive() ) ); QObject::connect( contactsList, SIGNAL( itemDoubleClicked(QListWidgetItem*) ), this, SLOT( slotLocalSession(QListWidgetItem*) ) ); }
/***************** * NetworkManager * *****************/ NetworkManager::NetworkManager(QString localPlayerId) : QObject(), m_server(NULL),m_NetworkLinkToServer(NULL),m_disconnectAsked(false),m_connectionState(false),m_localPlayer(NULL),m_audioPlayer(NULL),m_localPlayerId(localPlayerId) { m_reconnect = new QTimer(this); m_preferences = PreferencesManager::getInstance(); //m_thread = new ConnectionRetryThread(); m_dialog = new ConnectionRetryDialog(); connect(m_dialog,SIGNAL(tryConnection()),this,SLOT(startConnection())); connect(m_dialog,SIGNAL(rejected()),this,SIGNAL(stopConnectionTry())); }
//This function will constantly listen for the server sending new data void ListenThread::run() { connect(this, SIGNAL(tryConnection()), NetworkHandler::networkHandler, SLOT(checkConnection())); while(true){ this->sleep(1); //emit a signal to tell the NetworkHandler to check the connection //emit tryConnection(); } }
void Q3Socket::sn_write() { if ( d->state == Connecting ) // connection established? tryConnection(); flush(); }
void Q3Socket::sn_read( bool force ) { Q_LONG maxToRead = 0; if ( d->readBufferSize > 0 ) { maxToRead = d->readBufferSize - d->rba.size(); if ( maxToRead <= 0 ) { if ( d->rsn ) d->rsn->setEnabled( false ); return; } } // Use Q3SocketPrivate::sn_read_alreadyCalled to avoid recursive calls of // sn_read() (and as a result avoid emitting the readyRead() signal in a // slot for readyRead(), if you use bytesAvailable()). if ( !force && Q3SocketPrivate::sn_read_alreadyCalled.findRef(this) != -1 ) return; Q3SocketPrivate::sn_read_alreadyCalled.append( this ); char buf[4096]; Q_LONG nbytes = d->socket->bytesAvailable(); Q_LONG nread; QByteArray *a = 0; if ( state() == Connecting ) { if ( nbytes > 0 ) { tryConnection(); } else { // nothing to do, nothing to care about Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } } if ( state() == Idle ) { Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } if ( nbytes <= 0 ) { // connection closed? // On Windows this may happen when the connection is still open. // This happens when the system is heavily loaded and we have // read all the data on the socket before a new WSAAsyncSelect // event is processed. A new read operation would then block. // This code is also useful when Q3Socket is used without an // event loop. nread = d->socket->readBlock( buf, maxToRead ? QMIN((Q_LONG)sizeof(buf),maxToRead) : sizeof(buf) ); if ( nread == 0 ) { // really closed if ( !d->socket->isOpen() ) { #if defined(Q3SOCKET_DEBUG) qDebug( "Q3Socket (%s): sn_read: Connection closed", name() ); #endif d->connectionClosed(); emit connectionClosed(); } Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } else { if ( nread < 0 ) { if ( d->socket->error() == Q3SocketDevice::NoError ) { // all is fine Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } #if defined(Q3SOCKET_DEBUG) qWarning( "Q3Socket::sn_read (%s): Close error", name() ); #endif if ( d->rsn ) d->rsn->setEnabled( false ); emit error( ErrSocketRead ); Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } a = new QByteArray( nread ); memcpy( a->data(), buf, nread ); } } else { // data to be read #if defined(Q3SOCKET_DEBUG) qDebug( "Q3Socket (%s): sn_read: %ld incoming bytes", name(), nbytes ); #endif if ( nbytes > (int)sizeof(buf) ) { // big a = new QByteArray( nbytes ); nread = d->socket->readBlock( a->data(), maxToRead ? QMIN(nbytes,maxToRead) : nbytes ); } else { a = 0; nread = d->socket->readBlock( buf, maxToRead ? QMIN((Q_LONG)sizeof(buf),maxToRead) : sizeof(buf) ); if ( nread > 0 ) { // ##### could setRawData a = new QByteArray( nread ); memcpy( a->data(), buf, nread ); } } if ( nread == 0 ) { #if defined(Q3SOCKET_DEBUG) qDebug( "Q3Socket (%s): sn_read: Connection closed", name() ); #endif // ### we should rather ask the socket device if it is closed d->connectionClosed(); emit connectionClosed(); Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); delete a; return; } else if ( nread < 0 ) { delete a; if ( d->socket->error() == Q3SocketDevice::NoError ) { // all is fine Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } #if defined(QT_CHECK_RANGE) qWarning( "Q3Socket::sn_read: Read error" ); #endif if ( d->rsn ) d->rsn->setEnabled( false ); emit error( ErrSocketRead ); Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); return; } if ( nread != (int)a->size() ) { // unexpected #if defined(CHECK_RANGE) && !defined(Q_OS_WIN32) qWarning( "Q3Socket::sn_read: Unexpected short read" ); #endif a->resize( nread ); } } d->rba.append( a ); if ( !force ) { if ( d->rsn ) d->rsn->setEnabled( false ); emit readyRead(); if ( d->rsn ) d->rsn->setEnabled( true ); } Q3SocketPrivate::sn_read_alreadyCalled.removeRef( this ); }