Пример #1
0
void Network::onReceipt()
{
    if (socket())
    {
        if (commandStarted == false) {
            /* There it's a new message we are receiving.
               To start receiving it we must know its length, i.e. the 2 first bytes */
            if (socket()->bytesAvailable() < 2) {
                return;
            }
            /* Ok now we can start */
            commandStarted=true;
            /* getting the length of the message */
            char c1, c2;
            socket()->getChar(&c1), socket()->getChar(&c2);
            remainingLength=uchar(c1)*256+uchar(c2);

            /* Just a little check :p */
            if (!AntiDos::obj()->transferBegin(myid, remainingLength, ip())) {
                return;
            }

            /* Recursive call to write less code =) */
            onReceipt();
        } else {
            /* Checking if the command is complete! */
            if (socket()->bytesAvailable() >= remainingLength) {
                emit isFull(socket()->read(remainingLength));
                commandStarted = false;
                /* Recursive call to spare code =), there may be still data pending */
                onReceipt();
            }
        }
    }
}
Пример #2
0
void Network::onReceipt()
{
    if (commandStarted == false) {
        /* There it's a new message we are receiving.
           To start receiving it we must know its length, i.e. the 2 first bytes */
        if (this->bytesAvailable() < 4) {
            return;
        }
        /* Ok now we can start */
        commandStarted=true;
        /* getting the length of the message */
        char c1, c2, c3, c4;
        this->getChar(&c1), this->getChar(&c2); this->getChar(&c3), this->getChar(&c4);
        remainingLength= (uchar(c1) << 24) + (uchar(c2) << 16) + (uchar(c3) << 8) + uchar(c4);
        /* Recursive call to write less code =) */
        onReceipt();
    } else {
        /* Checking if the command is complete! */
        if (this->bytesAvailable() >= remainingLength) {
            emit isFull(read(remainingLength));
            commandStarted = false;
            /* Recursive call to spare code =), there may be still data pending */
            onReceipt();
        }
    }
}
Пример #3
0
Network::Network(QTcpSocket *sock, int id) : mysocket(sock), commandStarted(false), myid(id)
{
    connect(socket(), SIGNAL(readyRead()), this, SLOT(onReceipt()));
    connect(socket(), SIGNAL(disconnected()), this, SIGNAL(disconnected()));
    connect(socket(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(manageError(QAbstractSocket::SocketError)));
    /* SO THE SOCKET IS SAFELY DELETED LATER WHEN DISCONNECTED! */
    connect(socket(), SIGNAL(disconnected()), socket(), SLOT(deleteLater()));
    connect(this, SIGNAL(destroyed()), socket(), SLOT(deleteLater()));
}
Пример #4
0
Network::Network(const QString &host, quint16 port) : commandStarted(false)
{
    if (port == 0) {
        //Default argument, not worth going further
    } else {
        connectToHost(host, port);
    }

    connect(this, SIGNAL(readyRead()), SLOT(onReceipt()));
}
Пример #5
0
Socket::Socket(QObject *parent) :
    QObject(parent),
    dataSize(0),
    historySize(0),
    commandStarted(false),
    compressed_(true)
{
    socket = new QTcpSocket(this);
    connect(socket,SIGNAL(readyRead()),
            this,SLOT(onReceipt()));
    connect(socket, SIGNAL(connected()),
            this,SIGNAL(connected()));
    connect(socket, SIGNAL(disconnected()),
            this,SIGNAL(disconnected()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
            this,SIGNAL(error(QAbstractSocket::SocketError)));
}