void cereal::CerealPort::readThread() { char data[MAX_LENGTH]; int ret; struct pollfd ufd[1]; ufd[0].fd = fd_; ufd[0].events = POLLIN; while(!stream_stopped_) { if(!stream_paused_) { if(poll(ufd, 1, 10) > 0) { if(!(ufd[0].revents & POLLERR)) { ret = ::read(fd_, data, MAX_LENGTH); if(ret>0) { readCallback(data, ret); } } } } } }
void SerialPort::readThread() { char data[128]; int ret; struct pollfd ufd[1]; ufd[0].fd = fd_; ufd[0].events = POLLIN; while (!stream_stopped_) { if (!stream_paused_) { if (poll(ufd, 1, 10) > 0) { if (!(ufd[0].revents & POLLERR)) { ret = ::read(fd_, data, sizeof(data)); if (ret > 0) { readCallback(data, ret); } } } } } }
void ClientSocket::onRead(const error_code &err, size_t bytes) { logger->info("% bytes were read", bytes); if (err) { stop(); return; } std::string msg(read_buffer_, bytes); msg.erase(msg.begin(), msg.begin() + msg.find(delimeter) + 2); logger->info("Message: %", msg); switch (protobuf.getMessageType(msg)) { case MessageType::Login: { logger->info("Get login/pwd from client"); std::pair<std::string, std::string> loginPair = protobuf.getLogin(msg); this->login = loginPair.first; loginCallback(shared_from_this(), loginPair.first, loginPair.second); break; } case MessageType::Message: { logger->info("Get message from client"); readCallback(msg); break; } } read(); }
void Channel::handleEvent(){ if(mRevents & (POLLIN | POLLPRI)){ if(readCallback) readCallback(); } else if(mRevents & (POLLOUT)){ if(writeCallback) writeCallback(); } }
/** * Test passing contexts between threads */ TEST(AsyncSSLSocketTest2, AttachDetachSSLContext) { // Start listening on a local port WriteCallbackBase writeCallback; ReadCallback readCallback(&writeCallback); HandshakeCallback handshakeCallback(&readCallback); SSLServerAcceptCallbackDelay acceptCallback(&handshakeCallback); TestSSLServer server(&acceptCallback); EventBase eventBase; EventBaseAborter eba(&eventBase, 3000); std::shared_ptr<AttachDetachClient> client( new AttachDetachClient(&eventBase, server.getAddress())); client->connect(); eventBase.loop(); }
void Tcp_Connexion::handle_read(const boost::system::error_code& err, size_t bytesTransferred) { if(!err&&bytesTransferred>0) { rbuf.commit(bytesTransferred); std::istream is(&rbuf); std::string s; is>>s; std::cout<<bytesTransferred<<" Read : "<<s<<std::endl; readCallback(s); boost::asio::async_read(socket, rbuf, boost::asio::transfer_at_least(1), boost::bind(&Tcp_Connexion::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); }
// converse is called by PAM to interact with the user. Interaction means // either writing something to stdout and stderr or reading something from // stdin. int converse(int n, const struct pam_message **msg, struct pam_response **resp, void *data) { int i; struct pam_response *aresp; // If no messages arrived, or the number of messages is greater than // allowed, something is wrong with the caller. if (n <= 0 || n > PAM_MAX_NUM_MSG) { return PAM_CONV_ERR; } // According to pam_conv(3): "It is the caller's responsibility to release // both, this array and the responses themselves, using free(3)." The // caller in this situation is the PAM module and the array and the // responses refer to aresp and aresp[i].resp. aresp = calloc(n, sizeof *aresp); if (aresp == NULL) { return PAM_BUF_ERR; } // Loop over all messages and process them. for (i = 0; i < n; ++i) { aresp[i].resp_retcode = 0; aresp[i].resp = NULL; switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_OFF: // Read back response from user. What the user writes should not // be echoed to the screen. aresp[i].resp = readCallback((uintptr_t)data, 0); if (aresp[i].resp == NULL) { goto fail; } break; case PAM_PROMPT_ECHO_ON: // First write the message to stderr. writeCallback((uintptr_t)data, STDERR_FILENO, (char *)(msg[i]->msg)); // Read back response from user. What the user writes will be // echoed to the screen. aresp[i].resp = readCallback((uintptr_t)data, 1); if (aresp[i].resp == NULL) { goto fail; } break; case PAM_ERROR_MSG: // Write message to stderr. writeCallback((uintptr_t)data, STDERR_FILENO, (char *)(msg[i]->msg)); if (strlen(msg[i]->msg) > 0 && msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n') { writeCallback((uintptr_t)data, STDERR_FILENO, (char *)"\n"); } break; case PAM_TEXT_INFO: // Write message to stdout. writeCallback((uintptr_t)data, STDOUT_FILENO, (char *)(msg[i]->msg)); if (strlen(msg[i]->msg) > 0 && msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n') { writeCallback((uintptr_t)data, STDOUT_FILENO, (char *)"\n"); } break; default: goto fail; } } *resp = aresp; return PAM_SUCCESS; fail: for (i = 0; i < n; ++i) { if (aresp[i].resp != NULL) { memset(aresp[i].resp, 0, strlen(aresp[i].resp)); free(aresp[i].resp); } } memset(aresp, 0, n * sizeof *aresp); *resp = NULL; return PAM_CONV_ERR; }
/* * We keep on reading as long as we have something to read and a buffer to put * it in */ void SslIO::readable(DispatchHandle& h) { AbsTime readStartTime = AbsTime::now(); do { // (Try to) get a buffer if (!bufferQueue.empty()) { // Read into buffer BufferBase* buff = bufferQueue.front(); assert(buff); bufferQueue.pop_front(); errno = 0; int readCount = buff->byteCount-buff->dataCount; int rc = socket.read(buff->bytes + buff->dataCount, readCount); if (rc > 0) { buff->dataCount += rc; threadReadTotal += rc; readCallback(*this, buff); if (rc != readCount) { // If we didn't fill the read buffer then time to stop reading break; } // Stop reading if we've overrun our timeslot if (Duration(readStartTime, AbsTime::now()) > threadMaxIoTimeNs) { break; } } else { // Put buffer back (at front so it doesn't interfere with unread buffers) bufferQueue.push_front(buff); assert(buff); // Eof or other side has gone away if (rc == 0 || errno == ECONNRESET) { eofCallback(*this); h.unwatchRead(); break; } else if (errno == EAGAIN) { // We have just put a buffer back so we know // we can carry on watching for reads break; } else { // Report error then just treat as a socket disconnect QPID_LOG(error, "Error reading socket: " << getErrorString(PR_GetError())); eofCallback(*this); h.unwatchRead(); break; } } } else { // Something to read but no buffer if (emptyCallback) { emptyCallback(*this); } // If we still have no buffers we can't do anything more if (bufferQueue.empty()) { h.unwatchRead(); break; } } } while (true); ++threadReadCount; return; }