bool DoConnect() { sql = PQconnectStart(GetDSN().c_str()); if (!sql) return false; if(PQstatus(sql) == CONNECTION_BAD) return false; if(PQsetnonblocking(sql, 1) == -1) return false; /* OK, we've initalised the connection, now to get it hooked into the socket engine * and then start polling it. */ this->fd = PQsocket(sql); if(this->fd <= -1) return false; if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_WRITE | FD_WANT_NO_READ)) { ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BUG: Couldn't add pgsql socket to socket engine"); return false; } /* Socket all hooked into the engine, now to tell PgSQL to start connecting */ return DoPoll(); }
bool wxEpollDispatcher::HasPending() const { epoll_event event; // NB: it's not really clear if epoll_wait() can return a number greater // than the number of events passed to it but just in case it can, use // >= instead of == here, see #10397 return DoPoll(&event, 1, 0) >= 1; }
void DoEvent() { if((status == CREAD) || (status == CWRITE)) { DoPoll(); } else if((status == RREAD) || (status == RWRITE)) { DoResetPoll(); } else { DoConnectedPoll(); } }
bool DoEvent() { bool ret; if((status == CREAD) || (status == CWRITE)) { ret = DoPoll(); } else if((status == RREAD) || (status == RWRITE)) { ret = DoResetPoll(); } else { ret = DoConnectedPoll(); } return ret; }
bool DoResetPoll() { switch(PQresetPoll(sql)) { case PGRES_POLLING_WRITING: ServerInstance->SE->WantWrite(this); status = CWRITE; return DoPoll(); case PGRES_POLLING_READING: status = CREAD; return true; case PGRES_POLLING_FAILED: return false; case PGRES_POLLING_OK: status = WWRITE; return DoConnectedPoll(); default: return true; } }
int wxEpollDispatcher::Dispatch(int timeout) { epoll_event events[16]; const int rc = DoPoll(events, WXSIZEOF(events), timeout); if ( rc == -1 ) { wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"), m_epollDescriptor); return -1; } int numEvents = 0; for ( epoll_event *p = events; p < events + rc; p++ ) { wxFDIOHandler * const handler = (wxFDIOHandler *)(p->data.ptr); if ( !handler ) { wxFAIL_MSG( wxT("NULL handler in epoll_event?") ); continue; } // note that for compatibility with wxSelectDispatcher we call // OnReadWaiting() on EPOLLHUP as this is what epoll_wait() returns // when the write end of a pipe is closed while with select() the // remaining pipe end becomes ready for reading when this happens if ( p->events & (EPOLLIN | EPOLLHUP) ) handler->OnReadWaiting(); else if ( p->events & EPOLLOUT ) handler->OnWriteWaiting(); else if ( p->events & EPOLLERR ) handler->OnExceptionWaiting(); else continue; numEvents++; } return numEvents; }
bool DoResetPoll() { switch(PQresetPoll(sql)) { case PGRES_POLLING_WRITING: ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ); status = CWRITE; return DoPoll(); case PGRES_POLLING_READING: ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE); status = CREAD; return true; case PGRES_POLLING_FAILED: return false; case PGRES_POLLING_OK: ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE); status = WWRITE; DoConnectedPoll(); default: return true; } }
int PerformOperation(short port) { int acceptFd = 0; struct sockaddr_in sin; socklen_t sinlen = sizeof(sin); int fd = 0; int blocking = 1; int reuseaddr = 1; int devpoll = 0; int pollresult = 0; struct pollfd dp_fds[MAXSET]; struct dvpoll pollset; int procClient = 0; int i = 0; /* create devpoll fd */ if ((devpoll = open("/dev/poll", O_RDWR)) == -1) { printf("devpoll can't be opened: %s(%d)\n", strerror(errno), errno); exit(0); } /* create the accept socket */ acceptFd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (acceptFd == -1) { /* Error creating accept fd */ printf("Error creating socket: %s\n", strerror(errno)); exit(0); } /* set socket opt to reuse address */ if (setsockopt(acceptFd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) { printf("Error w/ setsockopt: %s\n", strerror(errno)); close(acceptFd); exit(0); } /* setup the accept fd as nonblocking */ if (ioctl(acceptFd, FIONBIO, &blocking) == -1) { /* error setting the nonblocking of fd */ printf("Error setting FIONBIO: %s\n", strerror(errno)); close(acceptFd); exit(0); } /* bind the socket to the port */ memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = INADDR_ANY; if (bind(acceptFd, (struct sockaddr*)&sin, sizeof(sin))==-1) { printf("Error binding socket: %s\n", strerror(errno)); close(acceptFd); exit(0); } /* setup the listen queue */ if (listen(acceptFd, 1) == -1) { printf("Error listening: %s\n", strerror(errno)); close(acceptFd); exit(0); } /* Add to dev poll queue */ Add(devpoll, acceptFd); while (1) { /* Get and service any active fd */ pollset.dp_fds = dp_fds; pollset.dp_nfds = MAXSET; pollset.dp_timeout = -1; if ((pollresult = DoPoll(devpoll, &pollset)) == -1) { printf("Error w/ the polling: %s(%d)\n", strerror(errno), errno); exit(0); } for (i = 0; i < pollresult; i++) { /* if accept then accept the conn and add it to the devpoll */ if (pollset.dp_fds[i].fd == acceptFd) { fd = Accept(acceptFd); Add(devpoll, fd); } else { /* if a client fd then get the data and echo back */ procClient = ProcessClient(pollset.dp_fds[i].fd); if (procClient == 0) { /* client closed conn remove it from devpoll */ Remove(devpoll, fd); } } } } }