void Referee::connectToServer(const QString &ip, int port) { if (!connection) messengerOfTheGods->connectToHost(ip, port); if (messengerOfTheGods->waitForConnected(1000)) { QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << (quint16) 0; out << (quint16) HERMES_CONNECT; out << (quint16) myTeamID; out.device()->seek(0); out << (quint16) (block.size() - sizeof(quint16)); messengerOfTheGods->write(block); Q_EMIT connected(); } else { testMode = true; qDebug() << "Keinen Server gefunden => Testmodus"; qDebug() << "Alle gesendeten Packete werden hier ausgegeben falls verbose=true"; qDebug() << "Um mit dem Server zu kommunizieren, stellt sicher, dass er laeuft (listening) und baut die Verbindung erneut auf"; Q_EMIT connectFailed(); } }
/** Called when the control socket encounters <b>error</b>. */ void ControlConnection::onError(QAbstractSocket::SocketError error) { if (status() == Connecting) { /* If we got a 'connection refused' and we haven't exceeded * MAX_CONNECT_ATTEMPTS, then try to reconnect since Tor is probably * running, but it doesn't have a ControlSocket open yet. */ if (error == QAbstractSocket::ConnectionRefusedError && _connectAttempt < MAX_CONNECT_ATTEMPTS) { tc::debug("Control connection refused. Retrying in %1ms.") .arg(CONNECT_RETRY_DELAY); _connectTimer->start(CONNECT_RETRY_DELAY); } else { /* Exceeded maximum number of connect attempts. Give up. */ QString errstr = ControlSocket::toString(error); tc::error("Vidalia was unable to connect to Tor: %1").arg(errstr); emit connectFailed(tr("Vidalia was unable to connect to Tor. (%1)") .arg(errstr)); setStatus(Disconnected); } } else if (error == QAbstractSocket::RemoteHostClosedError) { /* Tor closed the connection. This is common when we send a 'shutdown' or * 'halt' signal to Tor and doesn't need to be logged as loudly. */ tc::warn("Tor closed the control connection."); } else { /* Some other error. */ /*XXX We may want to be emitting these so the GUI thread can learn about * them and display an error message. */ tc::error("Control socket error: %1").arg(ControlSocket::toString(error)); } }
void SocketConnector::connect( boost::shared_ptr<Poller> poller, const std::string& name, const std::string& host, const std::string& port, ConnectionCodec::Factory* fact, ConnectFailedCallback failed) { // Note that the following logic does not cause a memory leak. // The allocated Socket is freed either by the AsynchConnector // upon connection failure or by the AsynchIO upon connection // shutdown. The allocated AsynchConnector frees itself when it // is no longer needed. Socket* socket = factory(); try { AsynchConnector* c = AsynchConnector::create( *socket, host, port, boost::bind(&establishedOutgoing, poller, options, &timer, _1, fact, name), boost::bind(&connectFailed, _1, _2, _3, failed)); c->start(poller); } catch (std::exception&) { // TODO: Design question - should we do the error callback and also throw? int errCode = socket->getError(); connectFailed(*socket, errCode, strError(errCode), failed); throw; } }
void RfcommClient::socketConnected() { disconnect(socket, SIGNAL(error(QBluetoothAbstractSocket::SocketError)), this, SLOT(connectFailed())); userEntry->setEnabled(true); disconnectAction->setVisible(true); sendAction->setVisible(true); waiter->hide(); logArea->append(QString(tr("Connected to %1")).arg(socket->remoteAddress().toString())); }
void AccessController::onStatusChanged(int status) { qDebug() << "emit " << status; if (NetworkConnectivity::Disconnected == doCheckNetworkConnectivity()) { emit noNetwork(); return; } switch (status) { case ClientStatus::Unintialized: return; case ClientStatus::Started: emit connecting(); return; case ClientStatus::Stopped: emit stopped(); break; case ClientStatus::PageReady: m_client->Connect(m_accessToken); return; case ClientStatus::Connecting: return; case ClientStatus::ConnectOk: emit connected(); break; case ClientStatus::ConnectServerFailed: emit connectFailed(AccessError::ConnectServerFailed); break; case ClientStatus::InvalidToken: emit connectFailed(AccessError::InvalidToken); break; case ClientStatus::Disconnected: emit disconnected(); break; } }
void RfcommClient::connectSocket() { QBluetoothAddress addr = QBluetoothRemoteDeviceDialog::getRemoteDevice(this); if (!addr.isValid()) { return; } connectAction->setVisible(false); waiter->setText(tr("Connecting...")); waiter->setCancelEnabled(true); connect(socket, SIGNAL(error(QBluetoothAbstractSocket::SocketError)), this, SLOT(connectFailed())); socket->connect(QBluetoothAddress::any, addr, 14); waiter->show(); }
void NetHandler::start() { m_bRunning = true; // first let's increase the limit of open files int maxconn = 100000; struct rlimit srl; srl.rlim_cur = maxconn + 10; srl.rlim_max = maxconn + 10; if (setrlimit(RLIMIT_NOFILE, &srl) < 0) { LOG4CXX_FATAL(logger_, "Cannot set RLimit!"); exit(1); } epfd = epoll_create(maxconn); if (epfd < 0) { LOG4CXX_FATAL(logger_, "epoll_create error!"); exit(1); } // Now let's ignore broken pipes struct sigaction sa; memset(&sa, 0, sizeof (sa)); sa.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &sa, NULL) < 0) { LOG4CXX_WARN(logger_, "Error ignoring SIGPIPE!"); } if (initSockets() < 0) { exit(1); } struct epoll_event ev, evs[MAX_EVENTS]; LOG4CXX_INFO(logger_, "Nethandler started."); for (; ; ) { if (!m_bRunning) { break; } int count = epoll_wait(epfd, evs, MAX_EVENTS, 1000); if (count < 0) { LOG4CXX_FATAL(logger_, "epoll_wait error!"); } time_t now = Clock::getCurrentSystemTime(); if (!preNetEvent(now)) { LOG4CXX_ERROR(logger_, "PreNetEvent returned false, terminating..."); break; } for (int i = 0; i < count; i++) { int fd = evs[i].data.fd; if (isListenSocket(fd)) { struct sockaddr_in sa; socklen_t slen = sizeof (sa); int nfd = 0; nfd = accept(fd, (struct sockaddr*) &sa, &slen); if (nfd > 0) { ev.events = EPOLLIN | EPOLLHUP; //| EPOLLRDHUP; ev.data.fd = nfd; if (epoll_ctl(epfd, EPOLL_CTL_ADD, nfd, &ev) < 0) { LOG4CXX_ERROR(logger_, "epoll_ctl error, cannot add client!"); } else { size_t rsize = readCacheSize(fd); //size_t rsize = (fd==wsfd ? NetCache::WEBSERVER_READ_SIZE : NetCache::DEFAULT_READ_SIZE); NetCache *cache = addConnection(nfd, sa, rsize); createProtocolHandler(cache, fd); } } } else // data { NetCache *cache = getCacheFromFd(fd); if (cache != NULL) { __uint32_t events = evs[i].events; bool readError = false; if ((events & EPOLLIN) > 0) { int64 uid = cache->uid; readError = !cache->read(); if (!readError) { string req; while (cache->assemble(req) && !cache->remove) { //LOG4CXX_DEBUG(logger_, "Command Received: \"" << req.c_str() << "\" from uid:" << uid << " fd:" << fd); if (cache->ph != NULL) { cache->ph->handle(uid, req); } else { LOG4CXX_ERROR(logger_, "Protocol handler is NULL for fd:" << fd); } } } } if ((events & EPOLLOUT) > 0) { if ( isConnecting( fd )) { connectSuccess( fd ); } else if (cache->write()) { ev.events = EPOLLIN | EPOLLHUP; // | EPOLLRDHUP; ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev); } } if ((cache->remove && !cache->waitToWrite()) || readError || (events & EPOLLHUP) > 0 || //(events&EPOLLRDHUP)>0 || (events & EPOLLERR) > 0) { int64 uid = cache->uid; if (uid >= 0 && cache->ph != NULL) { cache->ph->leave(uid); } LOG4CXX_DEBUG(logger_, "Client disconnected of fd:" << fd << ", remove: " << cache->remove << ", read error: " << readError << ", hup: " << (events & EPOLLHUP) //<< //", rdhup: " << (events & EPOLLRDHUP) << ", epoll error: " << (events & EPOLLERR)); doCloseConnection(fd); if (isConnectSocket(fd)) { connectFailed(fd); } } } else { LOG4CXX_ERROR(logger_, "Cannot find cache for fd:" << fd); } } } } }
void NetHandler::start() { m_bRunning = true; fd_set readset; FD_ZERO(&master); int listenFd = initSockets(); if (listenFd < 0) { LOG4CXX_FATAL(logger_, "socket create failed!"); exit(1); } if (listenFd > fdmax) { fdmax = listenFd; } LOG4CXX_INFO(logger_, "NetHandler started."); struct timeval timeout; for (; ; ) { if (!m_bRunning) { break; } readset = master; timeout.tv_sec = 1; timeout.tv_usec = 0; int count = select(fdmax + 1, &readset, NULL, NULL, &timeout); if (count < 0) { LOG4CXX_FATAL(logger_, "Select error!"); exit(1); } time_t now = Clock::getCurrentSystemTime(); if (!preNetEvent(now)) { LOG4CXX_ERROR(logger_, "PreNetEvent returned false, terminating..."); break; } if (count > 0) { for (int i = 0; i <= fdmax; i++) { if (FD_ISSET(i, &readset)) { if (isListenSocket(i)) { struct sockaddr_in sa; socklen_t slen = sizeof (sa); int nfd = accept(i, (struct sockaddr*) &sa, &slen); if (nfd < 0) { LOG4CXX_ERROR(logger_, "Accept error!"); } else { FD_SET(nfd, &master); if (nfd > fdmax) { fdmax = nfd; } size_t rsize = readCacheSize(i); NetCache *cache = addConnection(nfd, sa, rsize); createProtocolHandler(cache, i); } } else { NetCache *cache = getCacheFromFd(i); if (cache != NULL) { int64 uid = cache->uid; bool readSucc = cache->read(); if (readSucc) { string req; while (cache->assemble(req) && !cache->remove) { //LOG4CXX_DEBUG(logger_, "Command Received \"" << req.substr(0, 2) << "\" from uid:" << uid << " fd:" << i); if (cache->ph != NULL) { cache->ph->handle(uid, req); } else { LOG4CXX_ERROR(logger_, "Protocol handler is NULL for fd:" << i); } } } if (!readSucc || (cache->remove && !cache->waitToWrite())) { if (uid >= 0 && cache->ph != NULL) { cache->ph->leave(uid); } doCloseConnection(i); LOG4CXX_DEBUG(logger_, "Client disconnected with fd:" << i); if (isConnectSocket(i)) { int newFd = connectFailed(i); fdmax = max(fdmax, newFd); } } } else { LOG4CXX_ERROR(logger_, "Cannot find cache for fd:" << i); } } } } } } }