void MainWindow::on_checkBoxConnect_toggled(bool checked) { if (checked) { quint16 port = ui->lineEditPort->text().toUInt(); tcpSocket = new QTcpSocket(this); connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnect())); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onRead())); tcpSocket->connectToHost(ui->lineEditIp->text(), port); } else { tcpSocket->disconnect(); disconnect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnect())); disconnect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onRead())); delete tcpSocket; } }
int FileAction::onOpenRead(ServerContext& ctxt, FileServer& server, handle_t **handle, const char *path) { int status = 0; if (!canRead(ctxt, server, path)) { return -EPERM; } *handle = ctxt.getSession().createHandle(HDL_FILE); TAG_HANDLE(*handle); _DEBUG("onOpenRead: created handle for %s", path); if (*handle == NULL) { return -EIO; } HANDLE_PATH(**handle) = path; (*handle)->h_special_data = true; (*handle)->h_action = this; status = onRead(ctxt, server, translate(path).c_str(), HANDLE_CONTENTS(**handle)); if (status >= 0) { status = 0; (*handle)->h_count = status; } return status; }
void TcpGateDialog::InitConnect() { m_ndConnect.group = ui->gbConnect; m_ndConnect.cbIp = ui->cbCIP; m_ndConnect.spinPort = ui->spinCPort; m_ndConnect.lnSend = ui->lnCSendBytes; m_ndConnect.lnRecv = ui->lnCRecvBytes; m_ndConnect.leStatus = ui->leCStatus; m_ndConnect.btnStart = ui->btnConnect; connect(m_ndConnect.btnStart, SIGNAL(clicked()), this, SLOT(onClkBtnConnect())); m_ndConnect.bConnected = false; m_ndConnect.bStarted = false; m_ndConnect.nSendBytes = 0; m_ndConnect.nRecvBytes = 0; QSettings setting; m_ndConnect.latestIpList = setting.value("TcpGateTest/Connect/LatestIP", "").toStringList(); int nCount = m_ndConnect.latestIpList.count(); for (int i = nCount-1; i >= 0; i--) { if (!m_ndConnect.latestIpList[i].isEmpty()) { m_ndConnect.cbIp->addItem(m_ndConnect.latestIpList[i]); } } m_ndConnect.tcpServer = NULL; m_ndConnect.tcpSocket = new QTcpSocket(this); m_ndConnect.spinPort->setValue(setting.value("TcpGateTest/Connect/LatestPort", 5100).toInt()); connect(m_ndConnect.tcpSocket, SIGNAL(connected()), this, SLOT(onConnected())); connect(m_ndConnect.tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisConnected())); connect(m_ndConnect.tcpSocket, SIGNAL(readyRead()), this, SLOT(onRead())); connect(m_ndConnect.tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onNdcError(QAbstractSocket::SocketError))); }
void http_session::do_read() { std::weak_ptr<http_session> wp(shared_Derived_from_this<http_session>()); //开始异步读取 socket_->async_read_some(boost::asio::buffer(data_ + pos_, max_length - pos_), [wp](const boost::system::error_code &ec, std::size_t length){ auto sp(wp.lock()); if (sp) { sp->onRead(ec, length); } }); }
ZTPManager::ZTPManager(quint16 port, QHostAddress groupAddress, QObject *parent): QObject(parent) { _Socketlistener.bind(QHostAddress::Any,port,QUdpSocket::ShareAddress); if(groupAddress.toIPv4Address()>0xe0000000 && groupAddress.toIPv4Address()<0xf0000000) { _Socketlistener.joinMulticastGroup(groupAddress); //_Socketlistener.setSocketOption(QAbstractSocket::MulticastTtlOption, 5); } connect(&_Socketlistener,SIGNAL(readyRead()),this,SLOT(onRead()),Qt::AutoConnection); MTU = 60000; _timeout = 3000; }
void Client::onSocketStateChanged(QAbstractSocket::SocketState socketState) { //qDebug() << "client: socket state" << socketState; ClientRoom* room = (ClientRoom*)sender(); switch (socketState) { case QAbstractSocket::ConnectedState: rooms.insert(room->getName(), room); this->sendMessage(room->getSocket(), instMessage(nickName, room->getName(), MessageType::CONNECT_TO_ROOM)); connect(room->getSocket(), SIGNAL(readyRead()), this, SLOT(onRead())); break; case QAbstractSocket::UnconnectedState: emit addNewMessage("{System}: Room offline", room->getName()); break; } }
void TcpGateDialog::onNewConnection() { if(m_ndListen.tcpSocket) { m_ndListen.tcpSocket->close(); delete m_ndListen.tcpSocket; } m_ndListen.tcpSocket = m_ndListen.tcpServer->nextPendingConnection(); connect(m_ndListen.tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisConnected())); connect(m_ndListen.tcpSocket, SIGNAL(readyRead()), this, SLOT(onRead())); connect(m_ndListen.tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); Log2Main(tr("receive new connect%1:%2").arg(m_ndListen.tcpSocket->peerAddress().toString()).arg(m_ndListen.tcpSocket->peerPort())); m_ndListen.bConnected = true; }
void NuCachedSource2::onMessageReceived(const sp<AMessage> &msg) { switch (msg->what()) { case kWhatFetchMore: { onFetch(); break; } case kWhatRead: { onRead(msg); break; } default: TRESPASS(); } }
int ExtConn::onError() { int error = errno; int ret = getSockError(&error); if ((ret == -1) || (error != 0)) { if (ret != -1) errno = error; } LS_DBG_L(this, "ExtConn::onError()"); if (error != 0) { m_iState = CLOSING; doError(error); } else onRead(); return LS_FAIL; }
int EdStream::handleEvents(short event) { int ret = 0; LS_DBG_L("EdStream::handleEvent(), fd: %d, event: %hd", getfd(), event); if (event & POLLIN) { ret = onRead(); if (!getAssignedRevent()) goto EVENT_DONE; } if (event & POLLHUP) { if ((ret != -1) || (getHupCounter() > 50)) ret = onHangup(); else if (getHupCounter() > 100) abort(); if (!getAssignedRevent()) goto EVENT_DONE; } if ((ret != -1) && (event & POLLHUP)) { ret = onHangup(); if (!getAssignedRevent()) return 0; } if ((ret != -1) && (event & POLLOUT)) { ret = onWrite(); if (!getAssignedRevent()) goto EVENT_DONE; } if ((ret != -1) && (event & POLLERR)) { ret = onError(); if (!getAssignedRevent()) return 0; } EVENT_DONE: if (ret != -1) onEventDone(event); return 0; }
void WsConnection::onConnEvent(const ConnectionPtr_t& conn, ConnEvent event, const void* context) { switch(event) { case Conn_ReadEvent: { const StackBuffer* buffer = (const StackBuffer*)context; onRead(conn, buffer->buffer, buffer->count); } break; case Conn_WriteCompleteEvent: { m_sendCallback(); m_sendCallback = std::bind(&dummyCallback); } break; default: break; } }
void ZTPManager::commonInit(const QHostAddress& groupAddress) { #if QT_VERSION < QT_VERSION_CHECK(5,0,0) QThreadPool::globalInstance()->setMaxThreadCount(QThreadPool::globalInstance()->maxThreadCount()+1); #endif ztpThreadPool = new QThreadPool; ztpThreadPool->setMaxThreadCount(1); if(groupAddress.toIPv4Address()>0xe0000000 && groupAddress.toIPv4Address()<0xf0000000) { _Socketlistener.joinMulticastGroup(groupAddress); //_Socketlistener.setSocketOption(QAbstractSocket::MulticastTtlOption, 5); } connect(&_Socketlistener,SIGNAL(readyRead()),this,SLOT(onRead()),Qt::DirectConnection); MTU = 60000; _timeout = 3000; moveToThread(&ztpTask); connect(this,SIGNAL(registTask(const QByteArray &,const QString &,quint16)),this, SLOT(procPkg(const QByteArray &,const QString &,quint16)),Qt::QueuedConnection); }
/*! Creates and connects server and its signals */ void WlanConnection::startServer() { if (!mServer) { mServer = new WlanServer(this); QObject::connect(mServer, SIGNAL(read(QByteArray)), this, SLOT(onRead(QByteArray))); QObject::connect(mServer, SIGNAL(clientConnected(QString)), this, SLOT(onClientConnected(QString))); QObject::connect(mServer, SIGNAL(clientDisconnected(int)), this, SLOT(onClientDisconnected(int))); QObject::connect(mServer, SIGNAL(reconnectToNetwork()), this, SLOT(onReconnect())); QObject::connect(mServer, SIGNAL(socketError(int)), this, SLOT(onSocketError(int))); WlanNetworkMgr& mgr = Network::networkManager(); QObject::connect(&mgr, SIGNAL(stateChanged(QNetworkSession::State)), mServer, SLOT(onNetworkStateChanged(QNetworkSession::State))); QObject::connect(&mgr, SIGNAL(ipChanged(QString)), mServer, SLOT(onIpChanged(QString))); }
void StemmedFileProcessingDialog::processFiles(const QString &inFile, const QString &outFile) { ui->cbxReading->setText(ui->cbxReading->text() + " " + inFile); ui->cbxWriting->setText(ui->cbxWriting->text() + " " + outFile); this->in = inFile; this->out = outFile; QThread* t = new QThread(this); StemmedFileParserController* controller = new StemmedFileParserController(this); controller->moveToThread(t); t->start(); QObject::connect(this, SIGNAL(read(QString)), controller, SLOT(onRead(QString)), Qt::QueuedConnection); QObject::connect(this, SIGNAL(process()), controller, SLOT(onProcess()), Qt::QueuedConnection); QObject::connect(this, SIGNAL(write(QString)), controller, SLOT(onWrite(QString)), Qt::QueuedConnection); QObject::connect(this, SIGNAL(generateHistograms(QString)), controller, SLOT(onGenerateHistograms(QString)), Qt::QueuedConnection); QObject::connect(controller, SIGNAL(readDone(bool)), this, SLOT(onReadDone(bool)), Qt::QueuedConnection); QObject::connect(controller, SIGNAL(processDone()), this, SLOT(onProcessDone()), Qt::QueuedConnection); QObject::connect(controller, SIGNAL(writeDone(bool)), this, SLOT(onWriteDone(bool)), Qt::QueuedConnection); QObject::connect(controller, SIGNAL(generateHistogramsDone(bool, QString)), this, SLOT(onGenerateHistogramsDone(bool, QString)), Qt::QueuedConnection); emit read(this->in); this->show(); }
int SkSocket::readPacket(void (*onRead)(int, const void*, size_t, DataType, void*), void* context) { if (!fConnected || !fReady || NULL == onRead || NULL == context || fReadSuspended) return -1; int totalBytesRead = 0; char packet[PACKET_SIZE]; for (int i = 0; i <= fMaxfd; ++i) { if (!FD_ISSET (i, &fMasterSet)) continue; memset(packet, 0, PACKET_SIZE); SkDynamicMemoryWStream stream; int attempts = 0; bool failure = false; int bytesReadInTransfer = 0; int bytesReadInPacket = 0; header h; h.done = false; h.bytes = 0; while (!h.done && fConnected && !failure) { int retval = read(i, packet + bytesReadInPacket, PACKET_SIZE - bytesReadInPacket); ++attempts; if (retval < 0) { #ifdef NONBLOCKING_SOCKETS if (errno == EWOULDBLOCK || errno == EAGAIN) { if (bytesReadInPacket > 0 || bytesReadInTransfer > 0) continue; //incomplete packet or frame, keep tring else break; //nothing to read } #endif //SkDebugf("Read() failed with error: %s\n", strerror(errno)); failure = true; break; } if (retval == 0) { //SkDebugf("Peer closed connection or connection failed\n"); failure = true; break; } SkASSERT(retval > 0); bytesReadInPacket += retval; if (bytesReadInPacket < PACKET_SIZE) { //SkDebugf("Read %d/%d\n", bytesReadInPacket, PACKET_SIZE); continue; //incomplete packet, keep trying } SkASSERT((bytesReadInPacket == PACKET_SIZE) && !failure); memcpy(&h.done, packet, sizeof(bool)); memcpy(&h.bytes, packet + sizeof(bool), sizeof(int)); memcpy(&h.type, packet + sizeof(bool) + sizeof(int), sizeof(DataType)); if (h.bytes > CONTENT_SIZE || h.bytes <= 0) { //SkDebugf("bad packet\n"); failure = true; break; } //SkDebugf("read packet(done:%d, bytes:%d) from fd:%d in %d tries\n", // h.done, h.bytes, fSockfd, attempts); stream.write(packet + HEADER_SIZE, h.bytes); bytesReadInPacket = 0; attempts = 0; bytesReadInTransfer += h.bytes; } if (failure) { onRead(i, NULL, 0, h.type, context); this->onFailedConnection(i); continue; } if (bytesReadInTransfer > 0) { SkData* data = stream.copyToData(); SkASSERT(data->size() == bytesReadInTransfer); onRead(i, data->data(), data->size(), h.type, context); data->unref(); totalBytesRead += bytesReadInTransfer; } } return totalBytesRead; }
void Conn::setupReadHandler() { onRead( boost::system::error_code(), std::make_shared< boost::asio::streambuf >() ); }
bool TCPNetwork::init() { QObject::connect(&_sock, SIGNAL(readyRead()), this, SLOT(onRead())); QObject::connect(&_sock, SIGNAL(disconnected()), this, SLOT(on_disconnect())); return (true); }
int main () { int rc; // generic return code from various functions const char* configFile = "movieWatcher.ini"; config cfg; rc = ini_parse(configFile, configHandler, &cfg); if (rc < 0) { perror("Error on ini_parse()"); exit(EXIT_FAILURE); } // Watch for movie file being created int fd = inotify_init(); if (fd == -1) { perror("Error with inotify_init()"); exit(EXIT_FAILURE); } printf("watchedPath=%s\n", cfg.watchedPath); int wd = inotify_add_watch(fd, cfg.watchedPath, IN_CREATE | IN_MODIFY); if (wd == -1) { perror("Error with inotify_add_watch()"); exit(EXIT_FAILURE); } while (1) { // poll until there is something left to read. // This is necessary because we will be reading a variable number of bytes // (eg the name of the file) struct pollfd pfd; pfd.fd = fd; pfd.events = POLLIN; rc = poll(&pfd, 1, -1); if (rc == -1) { perror("Error with poll()"); exit(EXIT_FAILURE); } // Since there is something to read, get the byte count int nToRead; rc = ioctl(pfd.fd, FIONREAD, &nToRead); if (rc == -1) { perror("Error with ioctl()"); exit(EXIT_FAILURE); } onRead(pfd.fd, nToRead, &cfg); } rc = inotify_rm_watch(fd, wd); if (rc == -1) { perror("Error with inotify_add_watch"); exit(EXIT_FAILURE); } close(fd); }