Esempio n. 1
0
void QMQTT::ClientPrivate::onNetworkConnected()
{
    Q_Q(Client);
    sendConnect();
    startKeepAlive();
    emit q->connected();
}
Esempio n. 2
0
    void Sender::handleRead(const std::vector<uint8_t>& newData)
    {
        data.insert(data.end(), newData.begin(), newData.end());

#ifdef DEBUG
        std::cout << "Sender got " << std::to_string(newData.size()) << " bytes" << std::endl;
#endif
        
        uint32_t offset = 0;
        
        while (offset < data.size())
        {
            if (state == rtmp::State::VERSION_SENT)
            {
                if (data.size() - offset >= sizeof(uint8_t))
                {
                    // S0
                    uint8_t version = static_cast<uint8_t>(*data.data() + offset);
                    offset += sizeof(version);

#ifdef DEBUG
                    std::cout << "Got version " << static_cast<uint32_t>(version) << std::endl;
#endif
                    
                    if (version != 0x03)
                    {
                        std::cerr << "Unsuported version, disconnecting sender" << std::endl;
                        socket.disconnect();
                        break;
                    }
                    
                    state = rtmp::State::VERSION_RECEIVED;
                }
                else
                {
                    break;
                }
            }
            else if (state == rtmp::State::VERSION_RECEIVED)
            {
                if (data.size() - offset >= sizeof(rtmp::Challenge))
                {
                    // S1
                    rtmp::Challenge* challenge = reinterpret_cast<rtmp::Challenge*>(data.data() + offset);
                    offset += sizeof(*challenge);

#ifdef DEBUG
                    std::cout << "Got challenge message, time: " << challenge->time <<
                        ", version: " << static_cast<uint32_t>(challenge->version[0]) << "." <<
                        static_cast<uint32_t>(challenge->version[1]) << "." <<
                        static_cast<uint32_t>(challenge->version[2]) << "." <<
                        static_cast<uint32_t>(challenge->version[3]) << std::endl;
#endif
                    
                    // C2
                    rtmp::Ack ack;
                    ack.time = challenge->time;
                    memcpy(ack.version, challenge->version, sizeof(ack.version));
                    memcpy(ack.randomBytes, challenge->randomBytes, sizeof(ack.randomBytes));
                    
                    std::vector<uint8_t> ackData;
                    ackData.insert(ackData.begin(),
                                   reinterpret_cast<uint8_t*>(&ack),
                                   reinterpret_cast<uint8_t*>(&ack) + sizeof(ack));
                    socket.send(ackData);
                    
                    state = rtmp::State::ACK_SENT;
                }
                else
                {
                    break;
                }
            }
            else if (state == rtmp::State::ACK_SENT)
            {
                if (data.size() - offset >= sizeof(rtmp::Ack))
                {
                    // S2
                    rtmp::Ack* ack = reinterpret_cast<rtmp::Ack*>(data.data() + offset);
                    offset += sizeof(*ack);

#ifdef DEBUG
                    std::cout << "Got Ack message, time: " << ack->time <<
                        ", version: " << static_cast<uint32_t>(ack->version[0]) << "." <<
                        static_cast<uint32_t>(ack->version[1]) << "." <<
                        static_cast<uint32_t>(ack->version[2]) << "." <<
                        static_cast<uint32_t>(ack->version[3]) << std::endl;
                    std::cout << "Handshake done" << std::endl;
#endif
                    
                    state = rtmp::State::HANDSHAKE_DONE;

                    sendConnect();
                }
                else
                {
                    break;
                }
            }
            else if (state == rtmp::State::HANDSHAKE_DONE)
            {
                rtmp::Packet packet;
                
                uint32_t ret = rtmp::decodePacket(data, offset, inChunkSize, packet, receivedPackets);

                if (ret > 0)
                {
#ifdef DEBUG
                    std::cout << "Total packet size: " << ret << std::endl;
#endif

                    offset += ret;

                    handlePacket(packet);
                }
                else
                {
                    break;
                }
            }
        }

        if (offset > data.size())
        {
            std::cout << "Reading outside of the buffer, buffer size: " << static_cast<uint32_t>(data.size()) << ", data size: " << offset << std::endl;
        }

        data.erase(data.begin(), data.begin() + offset);

#ifdef DEBUG
        std::cout << "Remaining data " << data.size() << std::endl;
#endif
    }
void QMQTT::ClientPrivate::onNetworkConnected()
{
    sendConnect();
    startKeepAlive();
}
void DefaultOperationTcpChannel::openConnection()
{
    if (isConnected_) {
        KAA_LOG_WARN(boost::format("Channel [%1%] connection is already opened") % getId());
        return;
    }

    KAA_LOG_TRACE(boost::format("Channel [%1%] opening connection to %2%:%3%")
                                                        % getId()
                                                        % currentServer_->getHost()
                                                        % currentServer_->getPort());

    boost::system::error_code errorCode;

    boost::asio::ip::tcp::endpoint ep = HttpUtils::resolveEndpoint(currentServer_->getHost(),
                                                                   currentServer_->getPort(),
                                                                   errorCode);
    if (errorCode) {
        KAA_LOG_ERROR(boost::format("Channel [%1%] failed to resolve endpoint: %2%")
                                                                    % getId()
                                                                    % errorCode.message());
        onServerFailed();
        return;
    }

    responseBuffer_.reset(new boost::asio::streambuf());
    sock_.reset(new boost::asio::ip::tcp::socket(io_));
    sock_->open(ep.protocol(), errorCode);

    if (errorCode) {
        KAA_LOG_ERROR(boost::format("Channel [%1%] failed to open socket: %2%")
                                                            % getId()
                                                            % errorCode.message());
        onServerFailed();
        return;
    }

    sock_->connect(ep, errorCode);

    if (errorCode) {
        KAA_LOG_ERROR(boost::format("Channel [%1%] failed to connect to %2%:%3%: %4%")
                                                                % getId()
                                                                % ep.address().to_string()
                                                                % ep.port()
                                                                % errorCode.message());
        onServerFailed();
        return;
    }

    channelManager_.onConnected({sock_->local_endpoint().address().to_string(), ep.address().to_string(), getServerType()});

    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_LOCK(channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    isConnected_ = true;

    KAA_MUTEX_UNLOCKING("channelGuard_");
    KAA_UNLOCK(channelGuard_);
    KAA_MUTEX_UNLOCKED("channelGuard_");

    sendConnect();
    setConnAckTimer();
    readFromSocket();
    setPingTimer();
}