//------------------------------------------------------------------------ void TcpServer::onReceive(Header hdr, Cbor& cbor) { PT_BEGIN() ; WIFI_DISCONNECT: { while (true) { PT_YIELD(); if (hdr.is(self(), left(), REPLY(CONNECT), 0)) { LOGF("TcpServer started. %x", this); listen(); goto CONNECTING; } } } CONNECTING: { while (true) { setReceiveTimeout(2000); PT_YIELD(); if (hdr.is(self(), left(), REPLY(CONNECT), 0)) { listTcp(); goto WIFI_DISCONNECT; } } } PT_END() }
bool STidPRGReaderUnit::waitRemoval(unsigned int maxwait) { ElapsedTimeCounter etc; BuzzerModeGuard guard(this); auto stidprgdt = std::dynamic_pointer_cast<STidPRGDataTransport>(getDataTransport()); stidprgdt->setReceiveTimeout(100); do { auto chip = getCurrentChip(); if (!chip) { d_insertedChip = nullptr; return true; } if (chip && d_insertedChip && chip->getChipIdentifier() != d_insertedChip->getChipIdentifier()) { d_insertedChip = chip; return true; } std::this_thread::sleep_for(std::chrono::milliseconds(200)); } while (etc.elapsed() < maxwait); return false; }
ICMPSocketImpl::ICMPSocketImpl(IPAddress::Family family, int dataSize, int ttl, int timeout): RawSocketImpl(family, IPPROTO_ICMP), _icmpPacket(family, dataSize) { setOption(IPPROTO_IP, IP_TTL, ttl); setReceiveTimeout(Timespan(timeout)); }
CUnixSocket::CUnixSocket(bool isServerSocket, IUnixSocketSubscriber* pSubscriber, int connectedSocket, bool askForReadiness) : CThread("CUnixSocket"), mpSubscriber(pSubscriber), mIsServerSocket(isServerSocket), mAskForReadiness(askForReadiness), mSock(connectedSocket), mRemoteSock(-1), mDestroyed(false), mIncomingListen(false) { setReceiveTimeout(1, 0); start(); }
void TcpClient::onReceive(Header hdr, Cbor& cbor) { switch(state()) { case S_INIT: { if ( hdr.is(INIT) ) { state(S_WIFI_DISCONNECTED); } break; } case S_WIFI_DISCONNECTED :{ if (hdr.is(self(), left(), REPLY(CONNECT), 0)) { connect(); state(S_TCP_DISCONNECTED); } break; } case S_TCP_DISCONNECTED :{ if (hdr.is(self(), self(), REPLY(CONNECT),0)) { // TCP connect established right().tell(self(), REPLY(CONNECT), 0); state(S_TCP_CONNECTED); } else if (hdr.is(self(), left(), REPLY(DISCONNECT), 0)) { // WIFI disconnected state(S_WIFI_DISCONNECTED); }; break; } case S_TCP_CONNECTED :{ if (hdr.is(right(),TXD)) { Bytes data(256); cbor.get(data); write(data); } else if (hdr.is(right(),DISCONNECT)) { // listener demands disconnect disconnect(); state(S_RECONNECT_WAIT); setReceiveTimeout(2000); } else if (hdr.is(left(),REPLY(DISCONNECT))) { // WIFI disconnected state(S_WIFI_DISCONNECTED); } break; } case S_RECONNECT_WAIT:{ if ( hdr.is(TIMEOUT)) { connect(); state(S_TCP_DISCONNECTED); } break; } } return; }
//____________________________________________________________ // // //____________________________________________________________ // void Tcp::onReceive(Header hdr, Cbor& cbor) { PT_BEGIN() ; WIFI_DISCONNECTED: { while (true) { PT_YIELD_UNTIL(hdr.is(left(), self(), REPLY(CONNECT), ANY)); // LOGF("Tcp started. %x", this); goto CONNECTING; } } CONNECTING: { LOGF("CONNECTING"); while (true) { PT_YIELD(); if (hdr.is(self(), self(), REPLY(CONNECT), 0)) { right().tell(self(), REPLY(CONNECT), 0); _state=READY; goto CONNECTED; } else if (hdr.is(self(), self(), REPLY(DISCONNECT), 0)) { right().tell(self(), REPLY(DISCONNECT), 0); goto WIFI_DISCONNECTED; } } } CONNECTED: { LOGF("CONNECTED"); while (true) { setReceiveTimeout(5000); PT_YIELD(); if (hdr.is(self(), REPLY(DISCONNECT))) { // tcp link gone, callback was called right().tell(self(), REPLY(DISCONNECT), 0); goto CONNECTING; } else if (hdr.is(left(), REPLY(DISCONNECT))) { // wifi link gone right().tell(self(), REPLY(DISCONNECT), 0); goto WIFI_DISCONNECTED; } else if (hdr.is(right(), DISCONNECT)) { // wifi link gone disconnect(); goto CONNECTING; } else if (timeout()) { LOGF("%d:%d %d", _lastRxd + 5000, Sys::millis()); if ((_lastRxd + 5000) < Sys::millis()) { LOGF(" timeout - disconnect %X:%X", this, _conn); disconnect(); } } } } ; PT_END() }
// Called in the forkit after forking the kit virtual void launchedKit(int /* pid */) override { // Open websocket connection between the child process and WSD. Poco::Net::HTTPClientSession cs("127.0.0.1", MasterPortNumber); cs.setTimeout(0); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, std::string(UNIT_URI)); Poco::Net::HTTPResponse response; auto ws = std::make_shared<Poco::Net::WebSocket>(cs, request, response); ws->setReceiveTimeout(0); Log::info("Fetching font list from forkit"); std::string fontListMsg = getFontList() + "\n"; ws->sendFrame(fontListMsg.c_str(), fontListMsg.length()); }
Optional<Telegram> Connection::get(std::chrono::milliseconds timeout) { if (socket_ == 0) throw std::logic_error("Cannot receive if connection was not opened before."); setReceiveTimeout(timeout); struct sockaddr_in sa; socklen_t fromlen = sizeof(sa); memset(&sa, 0, sizeof(sa)); ssize_t bytesReceived = recvfrom(socket_, buffer_, bufferSize_, 0, (struct sockaddr*) &sa, &fromlen); if (bytesReceived < 0) if (errno == EAGAIN) return Optional<Telegram>(); else throw std::runtime_error("Receiving telegram failed."); else return Optional<Telegram>(sa.sin_addr.s_addr, ntohs(sa.sin_port), std::vector<uint8_t>(buffer_, buffer_+bytesReceived)); }
bool STidPRGReaderUnit::waitInsertion(unsigned int maxwait) { ElapsedTimeCounter etc; EXCEPTION_ASSERT_WITH_LOG(getDataTransport(), LibLogicalAccessException, "No data transport."); auto stidprgdt = std::dynamic_pointer_cast<STidPRGDataTransport>(getDataTransport()); EXCEPTION_ASSERT_WITH_LOG(stidprgdt, LibLogicalAccessException, "Invalid data transport."); stidprgdt->setReceiveTimeout(100); select_chip_type(); do { if (d_insertedChip) { return true; } d_insertedChip = getCurrentChip(); std::this_thread::sleep_for(std::chrono::milliseconds(200)); } while (etc.elapsed() < maxwait); return !!d_insertedChip; }
status_t HTTPStream::connect(const char *server, int port) { Mutex::Autolock autoLock(mLock); status_t err = OK; if (mState == CONNECTED) { return ERROR_ALREADY_CONNECTED; } if (port < 0 || port > (int) USHRT_MAX) { return UNKNOWN_ERROR; } char service[sizeof("65536")]; sprintf(service, "%d", port); struct addrinfo hints, *ai; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; hints.ai_socktype = SOCK_STREAM; int ret = getaddrinfo(server, service, &hints, &ai); if (ret) { return ERROR_UNKNOWN_HOST; } CHECK_EQ(mSocket, -1); mState = CONNECTING; status_t res = -1; struct addrinfo *tmp; for (tmp = ai; tmp; tmp = tmp->ai_next) { mSocket = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); if (mSocket < 0) { continue; } setReceiveTimeout(30); // Time out reads after 30 secs by default. int s = mSocket; mLock.unlock(); res = MyConnect(s, tmp->ai_addr, tmp->ai_addrlen); mLock.lock(); if (mState != CONNECTING) { close(s); freeaddrinfo(ai); return UNKNOWN_ERROR; } if (res == OK) { break; } close(s); } freeaddrinfo(ai); if (res != OK) { close(mSocket); mSocket = -1; mState = READY; return res; } mState = CONNECTED; return OK; }
status_t HTTPStream::connect(const char *server, int port, bool https) { if (port < 0) { port = https ? 443 : 80; } Mutex::Autolock autoLock(mLock); status_t err = OK; if (mState == CONNECTED) { return ERROR_ALREADY_CONNECTED; } if (port < 0 || port > (int) USHRT_MAX) { return UNKNOWN_ERROR; } char service[sizeof("65536")]; sprintf(service, "%d", port); struct addrinfo hints, *ai; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; hints.ai_socktype = SOCK_STREAM; int ret = getaddrinfo(server, service, &hints, &ai); if (ret) { return ERROR_UNKNOWN_HOST; } CHECK_EQ(mSocket, -1); mState = CONNECTING; status_t res = -1; struct addrinfo *tmp; for (tmp = ai; tmp; tmp = tmp->ai_next) { mSocket = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); if (mSocket < 0) { continue; } if (mUIDValid) { RegisterSocketUser(mSocket, mUID); } setReceiveTimeout(30); // Time out reads after 30 secs by default. int s = mSocket; mLock.unlock(); res = MyConnect(s, tmp->ai_addr, tmp->ai_addrlen); mLock.lock(); if (mState != CONNECTING) { close(s); freeaddrinfo(ai); return UNKNOWN_ERROR; } if (res == OK) { break; } close(s); } freeaddrinfo(ai); if (res != OK) { close(mSocket); mSocket = -1; mState = READY; return res; } if (https) { CHECK(mSSL == NULL); if (mSSLContext == NULL) { SSL_library_init(); mSSLContext = SSL_CTX_new(TLSv1_client_method()); if (mSSLContext == NULL) { LOGE("failed to create SSL context"); mState = READY; return ERROR_IO; } } mSSL = SSL_new((SSL_CTX *)mSSLContext); if (mSSL == NULL) { LOGE("failed to create SSL session"); mState = READY; return ERROR_IO; } int res = SSL_set_fd((SSL *)mSSL, mSocket); if (res == 1) { res = SSL_connect((SSL *)mSSL); } if (res != 1) { SSL_free((SSL *)mSSL); mSSL = NULL; LOGE("failed to connect over SSL"); mState = READY; return ERROR_IO; } } mState = CONNECTED; return OK; }
bool CUnixSocket::connect() { //socklen_t addrLen = strlen(mAddr.sun_path) + sizeof(mAddr.sun_family); socklen_t addrLen = sizeof(mAddr); if (mIsServerSocket) { unlink(mAddr.sun_path); if (bind(mSock, (sockaddr *) &mAddr, addrLen) == -1) { LOG4CPLUS_ERROR(logger, "CUnixSocket::connect() " + string(CThread::getName()) + "bind error"); //perror("CUnixSocket::connect() bind"); return false; } else { if (::listen(mSock, 1) == -1) { LOG4CPLUS_ERROR(logger, "CUnixSocket::connect() " + string(CThread::getName()) + " listen error"); //perror("CUnixSocket::connect() listen"); return false; } else { if (mIncomingListen) { start(); return true; } else { addrLen = (socklen_t) sizeof(mRemoteAddr); mRemoteSock = accept(mSock, (sockaddr*) &mRemoteAddr, &addrLen); if (mRemoteSock == -1) { LOG4CPLUS_ERROR(logger, "CUnixSocket::connect() accept error"); //perror("CUnixSocket::connect() accept"); } } } } ::close(mSock); mSock = mRemoteSock; mRemoteSock = -1; } else { if (::connect(mSock, (sockaddr*)&mAddr, addrLen) == -1) { mSock = -1; LOG4CPLUS_ERROR(logger, "CUnixSocket::connect(): " + string(CThread::getName()) + " connect error"); //perror("CUnixSocket::connect() connect"); return false; } } setReceiveTimeout(1, 0); start(); return true; }