bool SocketPollerImpl::wait(const Duration& timeout) { if(!m_sockets.empty()) { return poll(m_sockets, static_cast<int>(timeout.asMilliseconds())) > 0; } return false; }
void TcpClient::connect(const IpAddress& address, NetPort port, const Duration& timeout, const Duration& pause) { Expect(address.isValid(), Throw(InvalidParameter, "Invalid IpAddress")); Expect(port != NetPort_Any, Throw(InvalidParameter, "Invalid NetPort")); int ret; Clock clock; Socket::create(address.getProtocol()); prv::SockAddrBuffer buffer(address, port); clock.start(); do { ret = ::connect(getHandler(), buffer.getSockAddr(), buffer.getLength()); if(ret != 0) { std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<long long>(pause.asMilliseconds()))); } }while(ret != 0 && clock.getElapsedTime() < timeout); Expect(ret == 0, Throw(InternalError, "Failed to connect to the remote host")); }
Duration IcmpImpl::ping(const IpAddressV4& address, const Duration& timeout) { HANDLE icmpHandle = IcmpCreateFile(); Expect(icmpHandle != INVALID_HANDLE_VALUE, Throw(Win32Error, "Failed to create ICMP handler")); ByteArray request(32); IPAddr addr = address.toInt(); ByteArray response(sizeof(ICMP_ECHO_REPLY) + request.getCapacity()); DWORD retval = IcmpSendEcho(icmpHandle, addr, &request[0], request.getCapacity(), nullptr, &response[0], response.getCapacity(), timeout.asMilliseconds()); Expect(retval > 0, Throw(Win32Error, "Failed to send ICMP request")); const ICMP_ECHO_REPLY* reply = reinterpret_cast<const ICMP_ECHO_REPLY*>(response.getBuffer()); Duration duration = Duration::milliseconds(reply->RoundTripTime); IcmpCloseHandle(icmpHandle); return duration; }