コード例 #1
0
void DefaultOperationTcpChannel::onPingTimeout(const boost::system::error_code& err)
{
    if (!err) {
        sendPingRequest();
    } else {
        KAA_MUTEX_LOCKING("channelGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
        KAA_MUTEX_LOCKED("channelGuard_");
        if (err != boost::asio::error::operation_aborted && isConnected_) {
            KAA_MUTEX_UNLOCKING("channelGuard_");
            KAA_UNLOCK(channelLock);
            KAA_MUTEX_UNLOCKED("channelGuard_")

            KAA_LOG_ERROR(boost::format("Channel [%1%] failed to process PING: %2%") % getId() % err.message());
            onServerFailed();
            return;
        } else {
            KAA_LOG_DEBUG(boost::format("Channel [%1%] PING timer aborted") % getId());
            return;
        }
    }

    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    if (isConnected_) {
        setPingTimer();
    }
}
コード例 #2
0
bool OnionClient::loop() {
	if (isConnected) {
		unsigned long t = millis;
		if ((t - lastInActivity > ONION_KEEPALIVE * 1000UL) || (t - lastOutActivity > ONION_KEEPALIVE * 1000UL)) {
			if (pingOutstanding) {
				close();
				return false;
			} else {
			    sendPingRequest();
				lastOutActivity = t;
				lastInActivity = t;
				pingOutstanding = true;
			}
		}
        //OnionPacket* pkt = getPacket();
		if (available) {
		    
			lastInActivity = t;
			uint8_t type = recvPkt->getType();
			if (type == ONIONCONNACK) {
			    if (recvPkt->getPayload()[0] == 0) {
    			    subscribe();
    			} else {
    			    close();
                            available = false;
    			    //delete pkt;
    			    return false;
    			}

		    } else if (type == ONIONSUBACK) {
				isOnline = true;
        		//publish(publishMap,2);
				lastOutActivity = t;
			} else if (type == ONIONPUBLISH) {
			    parsePublishData(recvPkt);
			} else if (type == ONIONPINGREQ) {
			    // Functionize this
				sendPingResponse();
				lastOutActivity = t;
			} else if (type == ONIONPINGRESP) {
				pingOutstanding = false;
			} 
	        available = false;
			//delete pkt;
		}
		return true;
	} else {
	    // not connected
	    unsigned long t = millis;
	    if (socketConnected) {
    		if (t - lastOutActivity > ONION_RETRY * 1000UL) {
    		    this->begin();
    	    }
    	}
	    return true;
	}
}