当前位置: 首页>>代码示例>>C++>>正文


C++ QUdpSocket::setReadBufferSize方法代码示例

本文整理汇总了C++中QUdpSocket::setReadBufferSize方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::setReadBufferSize方法的具体用法?C++ QUdpSocket::setReadBufferSize怎么用?C++ QUdpSocket::setReadBufferSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QUdpSocket的用法示例。


在下文中一共展示了QUdpSocket::setReadBufferSize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: onServerUdpSocketReadyRead

void UdpRelay::onServerUdpSocketReadyRead()
{
    if (listenSocket.pendingDatagramSize() > RecvSize) {
        emit info("[UDP] Datagram is too large. discarded.");
        return;
    }

    QByteArray data;
    data.resize(listenSocket.pendingDatagramSize());
    QHostAddress r_addr;
    quint16 r_port;
    qint64 readSize = listenSocket.readDatagram(data.data(),
                                                RecvSize,
                                                &r_addr,
                                                &r_port);
    emit bytesRead(readSize);

    if (isLocal) {
        if (static_cast<int>(data[2]) != 0) {
            emit info("[UDP] Drop a message since frag is not 0");
            return;
        }
        data.remove(0, 3);
    } else {
        if (autoBan && Common::isAddressBanned(r_addr)) {
            emit debug(QString("[UDP] A banned IP %1 "
                               "attempted to access this server")
                       .arg(r_addr.toString()));
            return;
        }
        data = encryptor->decryptAll(data);
    }

    Address destAddr, remoteAddr(r_addr, r_port);//remote == client
    int header_length = 0;
    bool at_auth = false;
    Common::parseHeader(data, destAddr, header_length, at_auth);
    if (header_length == 0) {
        emit info("[UDP] Can't parse header. "
                  "Wrong encryption method or password?");
        if (!isLocal && autoBan) {
            Common::banAddress(r_addr);
        }
        return;
    }

    QUdpSocket *client = cache.value(remoteAddr, nullptr);
    QString dbg;
    if (!client) {
        client = new QUdpSocket(this);
        client->setReadBufferSize(RecvSize);
        client->setSocketOption(QAbstractSocket::LowDelayOption, 1);
        cache.insert(remoteAddr, client);
        connect(client, &QUdpSocket::readyRead,
                this, &UdpRelay::onClientUdpSocketReadyRead);
        connect(client, &QUdpSocket::disconnected,
                this, &UdpRelay::onClientDisconnected);
        QDebug(&dbg) << "[UDP] cache miss:" << destAddr << "<->" << remoteAddr;
    } else {
        QDebug(&dbg) << "[UDP] cache hit:" << destAddr << "<->" << remoteAddr;
    }
    emit debug(dbg);

    if (isLocal) {
        if (auth || at_auth) {
            /*
             * shadowsocks UDP Request (OTA-enabled, unencrypted)
             * +------+----------+----------+----------+-------------+
             * | ATYP | DST.ADDR | DST.PORT |   DATA   |  HMAC-SHA1  |
             * +------+----------+----------+----------+-------------+
             * |  1   | Variable |    2     | Variable |     10      |
             * +------+----------+----------+----------+-------------+
             */
            encryptor->addHeaderAuth(data);
        }
        data = encryptor->encryptAll(data);
        destAddr = serverAddress;
    } else {
        if (auth || at_auth) {
            if (!encryptor->verifyHeaderAuth(data,
                                             data.length() - Cipher::AUTH_LEN))
            {
                emit info("[UDP] One-time message authentication "
                          "for header failed.");
                if (autoBan) {
                    Common::banAddress(r_addr);
                }
                return;
            }
        }
        data = data.mid(header_length,
                        data.length() - header_length - Cipher::AUTH_LEN);
    }

    if (!destAddr.isIPValid()) {//TODO async dns
        destAddr.blockingLookUp();
    }
    client->writeDatagram(data, destAddr.getFirstIP(), destAddr.getPort());
}
开发者ID:Brainiac5000,项目名称:libQtShadowsocks,代码行数:99,代码来源:udprelay.cpp


注:本文中的QUdpSocket::setReadBufferSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。