short TcpSocket::poll(short events) const { struct pollfd fds; fds.fd = _impl->fd(); fds.events = events; int timeout = getTimeout().ceil(); log_debug("poll timeout " << timeout); #ifdef __MINGW32__ int p = WSAPoll(&fds, 1, timeout); #else int p = ::poll(&fds, 1, timeout); #endif log_debug("poll returns " << p << " revents " << fds.revents); if (p < 0) { log_error("error in poll; errno=" << errno); throw SystemError("poll"); } else if (p == 0) { log_debug("poll timeout (" << timeout << ')'); throw IOTimeout(); } return fds.revents; }
void TcpSocketImpl::endConnect() { log_trace("ending connect"); if(_pfd && ! _socket.wbuf()) { _pfd->events &= ~POLLOUT; } checkPendingError(); if( _isConnected ) return; try { while (true) { pollfd pfd; pfd.fd = this->fd(); pfd.revents = 0; pfd.events = POLLOUT; log_debug("wait " << timeout() << " ms"); bool avail = this->wait(this->timeout(), pfd); if (avail) { // something has happened int sockerr = checkConnect(); if (_isConnected) return; if (++_addrInfoPtr == _addrInfo.impl()->end()) { // no more addrInfo - propagate error throw IOError(connectFailedMessage(_addrInfo, sockerr)); } } else if (++_addrInfoPtr == _addrInfo.impl()->end()) { log_debug("timeout"); throw IOTimeout(); } close(); _connectResult = tryConnect(); if (_isConnected) return; checkPendingError(); } } catch(...) { close(); throw; } }
void RpcClientImpl::wait(std::size_t msecs) { if (!_socket.selector()) throw std::logic_error("cannot run async rpc request without a selector"); Clock clock; if (msecs != RemoteClient::WaitInfinite) clock.start(); std::size_t remaining = msecs; while (activeProcedure() != 0) { if (_socket.selector()->wait(remaining) == false) throw IOTimeout(); if (msecs != RemoteClient::WaitInfinite) { std::size_t diff = static_cast<std::size_t>(clock.stop().totalMSecs()); remaining = diff >= msecs ? 0 : msecs - diff; } } }
void RpcClientImpl::wait(Timespan timeout) { if (_socket.selector() == 0) throw std::logic_error("cannot run async rpc request without a selector"); Clock clock; if (timeout >= Timespan(0)) clock.start(); Timespan remaining = timeout; while (activeProcedure() != 0) { if (_socket.selector()->wait(remaining) == false) throw IOTimeout(); if (timeout >= Timespan(0)) { remaining = timeout - clock.stop(); if (remaining < Timespan(0)) remaining = Timespan(0); } } }