Exemplo n.º 1
0
void mumlib::Transport::pingTimerTick(const boost::system::error_code &e) {
    if (state == ConnectionState::CONNECTED) {

        sendSslPing();

        if (not noUdp) {
            using namespace std::chrono;

            sendUdpPing();

            if (udpActive) {
                const int lastUdpReceivedMilliseconds = duration_cast<milliseconds>(
                        system_clock::now() - lastReceivedUdpPacketTimestamp).count();

                if (lastUdpReceivedMilliseconds > PING_INTERVAL.total_milliseconds() + 1000) {
                    udpActive = false;
                    logger.warn("Didn't receive UDP ping in %d ms, falling back to TCP.", lastUdpReceivedMilliseconds);
                }
            }
        }

        pingTimer.expires_at(pingTimer.expires_at() + PING_INTERVAL);
        pingTimer.async_wait(boost::bind(&Transport::pingTimerTick, this, _1));
    }
}
Exemplo n.º 2
0
void bithorde::Keepalive::run()
{
	if (_stale) {
		cerr << "WARNING: " << _client.peerName() << " did not respond to ping. Disconnecting..." << endl;
		return _client.close();
	} else {
		Ping ping;
		ping.set_timeout(MAX_PING_RESPONSE_TIME.total_milliseconds());

		if (_client.sendMessage(Connection::Ping, ping, Message::NEVER, true)) {
			_stale = true;
			_timer.clear();
			_timer.arm(boost::posix_time::seconds(MAX_PING_RESPONSE_TIME.total_seconds() * 1.5));
		} else {
			cerr << "WARNING: " << _client.peerName() << " without input, failed to send prioritized ping. Disconnecting..." << endl;
			return _client.close();
		}
	}
}
Exemplo n.º 3
0
int mumlib::Audio::encodeAudioPacket(int target, mumlib::SourceAudioPacketType srcType, void *inputPcmBuffer, int inputLength, uint8_t *outputBuffer,
                                     int outputBufferSize) {

    using namespace std::chrono;

    const int lastAudioPacketSentInterval = duration_cast<milliseconds>(
            system_clock::now() - lastEncodedAudioPacketTimestamp).count();

    if (lastAudioPacketSentInterval > RESET_SEQUENCE_NUMBER_INTERVAL.total_milliseconds() + 1000) {
        logger.debug("Last audio packet was sent %d ms ago, resetting encoder.", lastAudioPacketSentInterval);
        resetEncoder();
    }

    std::vector<uint8_t> header;

    header.push_back(0x80 | target);

    auto sequenceNumberEnc = VarInt(outgoingSequenceNumber).getEncoded();
    header.insert(header.end(), sequenceNumberEnc.begin(), sequenceNumberEnc.end());

    uint8_t tmpOpusBuffer[1024];
    int outputSize=0;
    switch(srcType) {
        case mumlib::SourceAudioPacketType::FLOAT:
            outputSize = opus_encode_float(opusEncoder,
                                           (float*)inputPcmBuffer,
                                           inputLength,
                                           tmpOpusBuffer,
                                           min(outputBufferSize, 1024));

            break;

        case mumlib::SourceAudioPacketType::INT16_T:
        default:
            outputSize = opus_encode(opusEncoder,
                                     (opus_int16*)inputPcmBuffer,
                                       inputLength,
                                       tmpOpusBuffer,
                                       min(outputBufferSize, 1024)
    );
            break;
    }

    if (outputSize <= 0) {
        throw AudioException((boost::format("failed to encode %d B of PCM data: %s") % inputLength %
                              opus_strerror(outputSize)).str());
    }

    auto outputSizeEnc = VarInt(outputSize).getEncoded();
    header.insert(header.end(), outputSizeEnc.begin(), outputSizeEnc.end());

    memcpy(outputBuffer, &header[0], header.size());
    memcpy(outputBuffer + header.size(), tmpOpusBuffer, outputSize);

    int incrementNumber = 100 * inputLength / SAMPLE_RATE;

    outgoingSequenceNumber += incrementNumber;

    lastEncodedAudioPacketTimestamp = std::chrono::system_clock::now();

    return outputSize + header.size();
}