bool TCPServer::handleIO( PSocketContext pSocketContext, PIOContext pIOContext, BOOL status) { if(!status) { if(!handleError(pSocketContext)) { return false; } return true; } if( (0 == pIOContext->overlapped.InternalHigh) && (pIOContext->opType & (OPT_READ + OPT_WRITE))) { handleClose(pSocketContext); return true; } switch(pIOContext->opType) { case OPT_ACCEPT: handleAccept(pSocketContext, pIOContext); break; case OPT_READ: handleRead(pSocketContext, pIOContext); break; case OPT_WRITE: handleWrite(pSocketContext, pIOContext); break; default: LOG("操作类型参数异常"); break; } return true; }
std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf( const CommunicateFlags& flags, IOBufQueue data) { std::pair<IOBufQueue, IOBufQueue> out; auto readCallback = [&] (int pfd, int cfd) -> bool { if (cfd == 1 && flags.readStdout_) { return handleRead(pfd, out.first); } else if (cfd == 2 && flags.readStderr_) { return handleRead(pfd, out.second); } else { // Don't close the file descriptor, the child might not like SIGPIPE, // just read and throw the data away. return discardRead(pfd); } }; auto writeCallback = [&] (int pfd, int cfd) -> bool { if (cfd == 0 && flags.writeStdin_) { return handleWrite(pfd, data); } else { // If we don't want to write to this fd, just close it. return false; } }; communicate(std::move(readCallback), std::move(writeCallback)); return out; }
void ServerInstance::run() { while(true) { PacketBase pb; if(N->recv(pb) == 0) { continue; } if(pb.type == TYPE_SERVER) { continue; } switch(state) { case Idle: handleIdle(pb); break; case Write: handleWrite(pb); break; case CommitReady: handleCommitReady(pb); break; default: throw FSException("Unknown server state"); } } }
void Connection::onHandler(IOLoop* loop, int events) { //to prevent ref decr to 0 ConnectionPtr_t conn = shared_from_this(); if(events & TNET_READ) { handleRead(); } if(events & TNET_WRITE) { if(m_status == Connecting) { handleConnect(); } else { handleWrite(); } } if(events & TNET_ERROR) { handleError(); } }
static void _as_activateAs(AS* h, int sd, uint32_t events) { struct partner_s *partner; int lsd = sd; partner = g_hash_table_lookup(h->hashmap, &lsd); if (partner == NULL && !IS_NEW_CONNECTION(sd)) { h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__, "Null pointer in lookup of %d (partner).", sd); _exit(EXIT_FAILURE); } h->slogf(SHADOW_LOG_LEVEL_MESSAGE, __FUNCTION__, "In the activate %d.", sd); if(events & EPOLLIN) { int res = 0; h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__, "EPOLLIN is set %d", sd); if (IS_NEW_CONNECTION(sd)) handleNewConnection(h, sd); else res = handleRead(h, sd, partner); if (res > 0) { /* close the connection, some error (like EPIPE) * occurred in the read. */ closeConnections(h, sd, partner); } else if (res < 0) { /* Fatal error occurred. */ _exit(EXIT_FAILURE); } } if(events & EPOLLOUT) { struct partner_s *me = g_hash_table_lookup(h->hashmap, &partner->sd); if (me == NULL) { if (h->isDone == 1){ /* If it reaches this point, it means that the other end point closed its socket descriptior (connection closed). Thus this socked descriptor must be closed too. */ closeConnections(h, sd, partner); }else{ h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__, "Null pointer in lookup of %d (me) (connection closed).", sd); _exit(EXIT_FAILURE); } }else{ h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__, "EPOLLOUT is set %d", sd); handleWrite(h, sd, partner->sd, me); } } }
int handleGenericOperation(int *fd, const char *relpath, GenericOperation *genop) { int ret; char fpath[PATH_MAX]; // optimistic approach: we are writing the changes right in root directory // in case of a failure, we revert back to a snapshot getAbsolutePath(fpath, config.rootdir, relpath); //printOp(relpath, fpath, genop); switch (genop->type) { case GENERIC_OPERATION__TYPE__CREATE: ret = handleCreate(fpath, fd, genop->create_op); break; case GENERIC_OPERATION__TYPE__MKNOD: ret = handleMknod(fpath, genop->mknod_op); break; case GENERIC_OPERATION__TYPE__MKDIR: ret = handleMkdir(fpath, genop->mkdir_op); break; case GENERIC_OPERATION__TYPE__SYMLINK: ret = handleSymlink(fpath, genop->symlink_op); break; case GENERIC_OPERATION__TYPE__LINK: ret = handleLink(fpath, genop->link_op); break; case GENERIC_OPERATION__TYPE__WRITE: ret = handleWrite(fpath, fd, genop->write_op); break; case GENERIC_OPERATION__TYPE__UNLINK: ret = handleUnlink(fpath, fd, genop->unlink_op); break; case GENERIC_OPERATION__TYPE__RMDIR: ret = handleRmdir(fpath, genop->rmdir_op); break; case GENERIC_OPERATION__TYPE__TRUNCATE: ret = handleTruncate(fpath, genop->truncate_op); break; case GENERIC_OPERATION__TYPE__CHMOD: ret = handleChmod(fpath, genop->chmod_op); break; case GENERIC_OPERATION__TYPE__CHOWN: ret = handleChown(fpath, genop->chown_op); break; case GENERIC_OPERATION__TYPE__RENAME: ret = handleRename(fpath, genop->rename_op); break; case GENERIC_OPERATION__TYPE__SETXATTR: case GENERIC_OPERATION__TYPE__REMOVEXATTR: break; } return ret; }
int Connection::send(const string& data) { if(m_status != Connected) { LOG_INFO("send error"); return -1; } handleWrite(data); return 0; }
bool FtpServerDataConnection::flush() { if(_direction==Direction::DOWNLOAD) { while(!_outputStreams.completed()) if(!handleWrite()) return false; } return true; }
SimpleHttpResult* SimpleHttpClient::request (int method, const string& location, const char* body, size_t bodyLength, const map<string, string>& headerFields) { _result = new SimpleHttpResult; _errorMessage = ""; // set body to all connections setRequest(method, location, body, bodyLength, headerFields); double endTime = now() + _requestTimeout; double remainingTime = _requestTimeout; while (isWorking() && remainingTime > 0.0) { switch (_state) { case (IN_CONNECT): handleConnect(); break; case (IN_WRITE): handleWrite(remainingTime); break; case (IN_READ_HEADER): case (IN_READ_BODY): case (IN_READ_CHUNKED_HEADER): case (IN_READ_CHUNKED_BODY): handleRead(remainingTime); break; default: break; } remainingTime = endTime - now(); } if (isWorking() && _errorMessage=="" ) { if (_warn) { LOGGER_WARNING << "Request timeout reached."; } _errorMessage = "Request timeout reached."; } // set result type in getResult() SimpleHttpResult* result = getResult(); _result = 0; return result; }
void IOServiceLibEvent::doWrite(ConnectorChannelContext* ctx) { std::list<mxcore::SharedPtr<mxcore::ByteBuffer> > writtenMsgList; try { ctx->writeMessage(writtenMsgList); handleWrite(ctx, writtenMsgList); } catch (mxcore::IOException& e) { handleWriteError(ctx, writtenMsgList.front(), e.getErrorCode()); } }
void NetMessageSocket::io(ev::io&, int revents) { debug("I/O event received: 0x%04x", revents); socketTimer_.stop(); if (revents & ev::READ) if (!handleRead()) return; if (revents & ev::WRITE) if (!handleWrite()) return; socketTimer_.start(30, 0); }
bool parseI2CMessage(int size, const byte *msg, CONTEXT *context) { switch (msg[0] ) { case I2C_CMD_CONFIG: return handleConfig(size, msg, context); break; case I2C_CMD_READ: return handleRead(size, msg, context); break; case I2C_CMD_WRITE: return handleWrite(size, msg, context); break; } return false; }
int OepFinder::IsCurrentInOEP(INS ins){ int writeItemIndex=-1; BOOL checkWxorX = TRUE; ADDRINT curEip = INS_Address(ins); BOOL isLib = filterLib(curEip); if(isLib){ return INLIB; // we are inside a library } if(isWriteINS(ins)){ handleWrite(ins); } if(checkWxorX){ writeItemIndex = getWxorXindex(ins); if(writeItemIndex != -1 ){ checkWxorX = FALSE; BOOL isOEP = heuristics(ins); if(isOEP){ return FOUND_OEP; } return NOT_FOUND_OEP; } } else{ if(checkEIPInWriteitem(curEip , writeItemIndex)){ return EIP_IN_CUR_WITEM; } else checkWxorX = TRUE; return EIP_NOT_IN_CUR_WITEM; } }
void newConnThread::run() { //this is where the thread actually starts. socket = new QTcpSocket; if(!socket->setSocketDescriptor(this->socketDescriptor)) { exit(1); } //cool. we've got a connection. //setup timer timer = new QTimer; timer->start(500); //time to connect some signals and slots. connect(socket,SIGNAL(readyRead()),this,SLOT(handleReadyRead())); connect(socket,SIGNAL(disconnected()),this,SLOT(handleDisconnected())); connect(timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); connect(this,SIGNAL(timeSig()),this,SLOT(handleWrite())); //make the thread run even when this run function goes out of scope. exec(); }
void AsyncPipeWriter::write(unique_ptr<folly::IOBuf> buf, AsyncWriter::WriteCallback* callback) { if (closed()) { if (callback) { AsyncSocketException ex(AsyncSocketException::NOT_OPEN, "attempt to write to closed pipe"); callback->writeErr(0, ex); } return; } bool wasEmpty = (queue_.empty()); folly::IOBufQueue iobq; iobq.append(std::move(buf)); std::pair<folly::IOBufQueue, AsyncWriter::WriteCallback*> p( std::move(iobq), callback); queue_.emplace_back(std::move(p)); if (wasEmpty) { handleWrite(); } else { CHECK(!queue_.empty()); CHECK(isHandlerRegistered()); } }
std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf( IOBufQueue input) { // If the user supplied a non-empty input buffer, make sure // that stdin is a pipe so we can write the data. if (!input.empty()) { // findByChildFd() will throw std::invalid_argument if no pipe for // STDIN_FILENO exists findByChildFd(STDIN_FILENO); } std::pair<IOBufQueue, IOBufQueue> out; auto readCallback = [&] (int pfd, int cfd) -> bool { if (cfd == STDOUT_FILENO) { return handleRead(pfd, out.first); } else if (cfd == STDERR_FILENO) { return handleRead(pfd, out.second); } else { // Don't close the file descriptor, the child might not like SIGPIPE, // just read and throw the data away. return discardRead(pfd); } }; auto writeCallback = [&] (int pfd, int cfd) -> bool { if (cfd == STDIN_FILENO) { return handleWrite(pfd, input); } else { // If we don't want to write to this fd, just close it. return true; } }; communicate(std::move(readCallback), std::move(writeCallback)); return out; }
void AsyncPipeWriter::handlerReady(uint16_t events) noexcept { CHECK(events & EventHandler::WRITE); handleWrite(); }
static void selectPass(void) { int const fdSetCount = maxfd / FD_SETSIZE + 1; # define FD_ZERO_EXT(ar) for (int i = 0; i < fdSetCount; ++i) { FD_ZERO(&(ar)[i]); } # define FD_SET_EXT(fd, ar) FD_SET((fd) % FD_SETSIZE, &(ar)[(fd) / FD_SETSIZE]) # define FD_ISSET_EXT(fd, ar) FD_ISSET((fd) % FD_SETSIZE, &(ar)[(fd) / FD_SETSIZE]) fd_set readfds[fdSetCount], writefds[fdSetCount]; FD_ZERO_EXT(readfds); FD_ZERO_EXT(writefds); /* Server sockets */ for (int i = 0; i < seTotal; ++i) { if (seInfo[i].fd != INVALID_SOCKET) { FD_SET_EXT(seInfo[i].fd, readfds); } } /* Connection sockets */ for (int i = 0; i < coTotal; ++i) { ConnectionInfo *cnx = &coInfo[i]; if (cnx->local.fd != INVALID_SOCKET) { /* Accept more output from the local server if there's room */ if (cnx->local.recvPos < RINETD_BUFFER_SIZE) { FD_SET_EXT(cnx->local.fd, readfds); } /* Send more input to the local server if we have any, or if we’re closing */ if (cnx->local.sentPos < cnx->remote.recvPos || cnx->coClosing) { FD_SET_EXT(cnx->local.fd, writefds); } } if (cnx->remote.fd != INVALID_SOCKET) { /* Get more input if we have room for it */ if (cnx->remote.recvPos < RINETD_BUFFER_SIZE) { FD_SET_EXT(cnx->remote.fd, readfds); } /* Send more output if we have any, or if we’re closing */ if (cnx->remote.sentPos < cnx->local.recvPos || cnx->coClosing) { FD_SET_EXT(cnx->remote.fd, writefds); } } } select(maxfd + 1, readfds, writefds, 0, 0); for (int i = 0; i < coTotal; ++i) { ConnectionInfo *cnx = &coInfo[i]; if (cnx->remote.fd != INVALID_SOCKET) { if (FD_ISSET_EXT(cnx->remote.fd, readfds)) { handleRead(cnx, &cnx->remote, &cnx->local); } } if (cnx->remote.fd != INVALID_SOCKET) { if (FD_ISSET_EXT(cnx->remote.fd, writefds)) { handleWrite(cnx, &cnx->remote, &cnx->local); } } if (cnx->local.fd != INVALID_SOCKET) { if (FD_ISSET_EXT(cnx->local.fd, readfds)) { handleRead(cnx, &cnx->local, &cnx->remote); } } if (cnx->local.fd != INVALID_SOCKET) { if (FD_ISSET_EXT(cnx->local.fd, writefds)) { handleWrite(cnx, &cnx->local, &cnx->remote); } } } /* Handle servers last because handleAccept() may modify coTotal */ for (int i = 0; i < seTotal; ++i) { ServerInfo *srv = &seInfo[i]; if (srv->fd != INVALID_SOCKET) { if (FD_ISSET_EXT(srv->fd, readfds)) { handleAccept(srv); } } } }
void handleRequest(char *arg) { clientHandle_t *ch = (clientHandle_t *) arg; selcmd_t sc; struct stat sbuf; int code = 0; int c_errno = 0; while (1) { Log("(handleRequest) going to sleep on 0x%x\n", &ch->ch_state); LWP_WaitProcess(&ch->ch_state); assert(ch->ch_state == CH_INUSE); FD_ZERO(&(ch->ch_read)); FD_ZERO(&(ch->ch_write)); FD_ZERO(&(ch->ch_except)); FD_SET(ch->ch_fd, &(ch->ch_read)); FD_SET(ch->ch_fd, &(ch->ch_except)); code = IOMGR_Select(ch->ch_fd + 1, &(ch->ch_read), &(ch->ch_write), &(ch->ch_except), (struct timeval *)NULL); if (FD_ISSET(ch->ch_fd, &(ch->ch_except))) { Log("Received expception. Read fd_set shows %d\n", FD_ISSET(ch->ch_fd, &(ch->ch_read))); assertNullFDSet(ch->ch_fd, &(ch->ch_read)); assertNullFDSet(-1, &(ch->ch_write)); assertNullFDSet(ch->ch_fd, &(ch->ch_except)); goto done; } assert(code > 0); if (read(ch->ch_fd, (char *)&sc, sizeof(sc)) != sizeof(sc)) { Die(1, "(handleRequest) read command"); } Log("(handleRequest)cmd=%d\n", sc.sc_cmd); fflush(stdout); switch (sc.sc_cmd) { case SC_PROBE: Log("Probed from client at %s\n", inet_ntoa(ch->ch_addr.sin_addr)); break; #ifdef notdef case SC_OOB: nThreads--; ch->ch_fd = 0; ch->ch_state = CH_FREE; return; #endif case SC_WRITE: handleWrite(ch, &sc); break; case SC_END: Log("Ending ungracefully in server.\n"); exit(1); default: Log("%d: bad command to handleRequest.\n", sc.sc_cmd); break; } done: /* We're done now, so we can be re-used. */ nThreads--; close(ch->ch_fd); ch->ch_fd = 0; ch->ch_state = CH_FREE; } }
/****************************************************************************************************** * Makes call to state machine ******************************************************************************************************/ void stateMachine( struct reference* ref ) { // Find which L1 cache to access struct cache L1_cache = cacheType( ref ); // Define initial state struct state state; state.type = ref->type; state.iteration = 0; // Check instruction count if( setFlush == TRUE ) { state.next = FLUSH; } else { state.next = QUERY_L1; } // Define initial L1 reference decomposeAddress( ref, L1 ); state.L1_Index = ref->index[0]; state.L1_Tag = ref->tag[0]; #ifdef PRINT printf( "Tag: %13llx \n", ref->tag[0] ); printf( "Index: %6llx \n", ref->index[0] ); #endif // Start state machine while( TRUE ) { switch( state.next ) { case IDLE: // Increment L1 reference #ifdef PRINT printf( "Increment Reference \n" ); #endif incrementL1( &state, ref ); break; case QUERY_L1: // Query L1 cache for tag #ifdef PRINT printf( "Query L1 \n" ); #endif queryL1( &state, &L1_cache ); break; case QUERY_L2: // Query L2 cache for tag #ifdef PRINT printf( "Query L2 \n" ); #endif queryL2( &state ); break; case ADD_L1: // Add tag to L1 cache #ifdef PRINT printf( "Add L1 \n" ); #endif addL1( &state, &L1_cache ); break; case ADD_L2: // Add tag to L2 cache #ifdef PRINT printf( "Add L2 \n" ); #endif addL2( &state ); break; case HANDLE_WRITE: // Set dirty bit in L1 cache #ifdef PRINT printf( "Handle write case \n" ); #endif handleWrite( &state, &L1_cache ); break; case FLUSH: // Flush and invalidate all caches #ifdef PRINT printf( "Flush \n" ); #endif flushCaches( &state ); break; case TERMINATE: // Break out of state machine #ifdef PRINT printf( "**********************************************************************************************\n" ); #endif terminate( ref ); return; } } }
void Connection::handleWrite() { handleWrite(string()); }
void DataflowAnalyzer::execute(const Statement *statement, ReachingDefinitions &definitions) { switch (statement->kind()) { case Statement::INLINE_ASSEMBLY: /* * To be completely correct, one should clear reaching definitions. * However, not doing this usually leads to better code. */ break; case Statement::ASSIGNMENT: { auto assignment = statement->asAssignment(); computeValue(assignment->right(), definitions); handleWrite(assignment->left(), computeMemoryLocation(assignment->left(), definitions), definitions); break; } case Statement::JUMP: { auto jump = statement->asJump(); if (jump->condition()) { computeValue(jump->condition(), definitions); } if (jump->thenTarget().address()) { computeValue(jump->thenTarget().address(), definitions); } if (jump->elseTarget().address()) { computeValue(jump->elseTarget().address(), definitions); } break; } case Statement::CALL: { auto call = statement->asCall(); computeValue(call->target(), definitions); break; } case Statement::HALT: { break; } case Statement::TOUCH: { auto touch = statement->asTouch(); switch (touch->accessType()) { case Term::READ: computeValue(touch->term(), definitions); break; case Term::WRITE: handleWrite(touch->term(), computeMemoryLocation(touch->term(), definitions), definitions); break; case Term::KILL: handleKill(computeMemoryLocation(touch->term(), definitions), definitions); break; default: unreachable(); } break; } case Statement::CALLBACK: { statement->asCallback()->function()(); break; } case Statement::REMEMBER_REACHING_DEFINITIONS: { dataflow_.getDefinitions(statement) = definitions; break; } default: log_.warning(tr("%1: Unknown statement kind: %2.").arg(Q_FUNC_INFO).arg(statement->kind())); break; } }