如何添加QDataStream读写类型long long long

时间:2019-04-01 02:12:33

标签: c++ qt

如何在此类中长时间添加类型

OctetsStream.h

#ifndef OCTETSSTREAM_H
#define OCTETSSTREAM_H

#include <QObject>
#include <QByteArray>
#include <QDataStream>
#include <QDebug>
#include <stdlib.h>
#include <QtEndian>
#include <QTcpSocket>

#include <Network/Octets.h>
#include <Utils.h>

class OctetsStream
{
public:
    OctetsStream() { init(); }
    OctetsStream(QTcpSocket *socket) { socket = socket; init(); }
    OctetsStream(QByteArray data, bool header = true) { setData(data, header); }
    void init();
    void setData(QByteArray data, bool header = true)
    {
        this->data = QByteArray(data);
        init();
        if (header)
        {
            uncompact_uint32();
            uncompact_uint32();
        }
    }
    uint calcSession();
    void Send(uint opcode);
    void Reset();
    virtual void Unmarshal() {}
    virtual void Marshal() {}

    //Read&Write
    uint uncompact_uint32();
    uint readUInt32();
    int readInt32();
    short readInt16();
    char readByte();
    quint8 readUByte();
    Octets readOctet();
    void compact_uint32(const uint &value);
    static QByteArray scompact_uint32(const uint &value);
    void writeUInt32(const uint &value);
    void writeInt32(const int &value);
    void writeInt16(const short &value);
    void writeByte(const char &value);
    void writeUByte(const quint8 &value);
    void writeOctet(const Octets &value);

    //Operator overload
    OctetsStream &operator >>(uint &value);
    OctetsStream &operator >>(int &value);
    OctetsStream &operator >>(short &value);
    OctetsStream &operator >>(char &value);
    OctetsStream &operator >>(quint8 &value);
    OctetsStream &operator >>(Octets &value);
    OctetsStream &operator <<(uint value);
    OctetsStream &operator <<(int value);
    OctetsStream &operator <<(short value);
    OctetsStream &operator <<(char value);
    OctetsStream &operator <<(quint8 value);
    OctetsStream &operator <<(Octets value);

    QTcpSocket *socket;
    uint session;

private:
    QByteArray data;
    QDataStream *qds;
};

#endif // OCTETSSTREAM_H

OctetsStream.cpp

#include "OctetsStream.h"

void OctetsStream::init()
{
    qds = new QDataStream(&data, QIODevice::ReadWrite);
    qds->setByteOrder(QDataStream::LittleEndian);
    qds->setFloatingPointPrecision(QDataStream::SinglePrecision);
}

uint OctetsStream::calcSession()
{
    return session - 2147483648;
}

void OctetsStream::Send(uint opcode)
{
    QByteArray out;
    out.append(OctetsStream::scompact_uint32(opcode));
    out.append(OctetsStream::scompact_uint32(data.length()));
    out.append(data);
    if (socket->isOpen())
        socket->write(out);
}

void OctetsStream::Reset()
{
    data.clear();
    init();
}

uint OctetsStream::uncompact_uint32()
{
    char byte = readByte();
    switch (byte & 0xE0)
    {
    case 0x0E:
        return readInt32();
    case 0xC0:
        qds->device()->seek(qds->device()->pos() - 1);
        return readInt32() & 0x1FFFFFFF;
    case 0x80:
    case 0xA0:
        qds->device()->seek(qds->device()->pos() - 1);
        return readInt16() & 0x3FFF;
    default:
        return byte;
    }
}

uint OctetsStream::readUInt32()
{
    uint value;
    this->operator >>(value);
    return value;
}

int OctetsStream::readInt32()
{
    int value;
    this->operator >>(value);
    return value;
}

short OctetsStream::readInt16()
{
    short value;
    this->operator >>(value);
    return value;
}

char OctetsStream::readByte()
{
    char value;
    this->operator >>(value);
    return value;
}

quint8 OctetsStream::readUByte()
{
    quint8 value;
    this->operator >>(value);
    return value;
}

Octets OctetsStream::readOctet()
{
    Octets value;
    this->operator >>(value);
    return value;
}

void OctetsStream::compact_uint32(const uint &value)
{
    if (value < 127)
        writeByte(value);
    else if (value < 16383)
        writeInt16(value | 0x8000);
}

QByteArray OctetsStream::scompact_uint32(const uint &value)
{
    QByteArray out;
    QDataStream stream(&out, QIODevice::WriteOnly);
    if (value < 127)
        stream << (int8_t)value;
    else if (value < 16383)
        stream << (short)(value | 0x8000);
    return out;
}

void OctetsStream::writeUInt32(const uint &value)
{
    this->operator <<(value);
}

void OctetsStream::writeInt32(const int &value)
{
    this->operator <<(value);
}

void OctetsStream::writeInt16(const short &value)
{
    this->operator <<(value);
}

void OctetsStream::writeByte(const char &value)
{
    this->operator <<(value);
}

void OctetsStream::writeUByte(const quint8 &value)
{
    this->operator <<(value);
}

void OctetsStream::writeOctet(const Octets &value)
{
    this->operator <<(value);
}

OctetsStream &OctetsStream::operator >>(uint &value)
{
    qds->operator >>(value);
    value = qbswap(value);
    return *this;
}

OctetsStream &OctetsStream::operator >>(int &value)
{
    qds->operator >>(value);
    value = qbswap(value);
    return *this;
}

OctetsStream &OctetsStream::operator >>(short &value)
{
    qds->operator >>(value);
    value = qbswap(value);
    return *this;
}

OctetsStream &OctetsStream::operator >>(char &value)
{
    qds->readRawData(&value, 1);
    return *this;
}

OctetsStream &OctetsStream::operator >>(quint8 &value)
{
    qds->operator >>(value);
    return *this;
}

OctetsStream &OctetsStream::operator>>(Octets &value)
{
    uint lenght = uncompact_uint32();
    value.lenght = lenght;
    char data[lenght];
    if (lenght > 1)
        qds->readRawData(data, lenght);
    value.text = QByteArray(data);
    return *this;
}

OctetsStream &OctetsStream::operator <<(uint value)
{
    qds->operator <<(qbswap(value));
    return *this;
}

OctetsStream &OctetsStream::operator <<(int value)
{
    qds->operator <<(qbswap(value));
    return *this;
}

OctetsStream &OctetsStream::operator <<(short value)
{
    qds->operator <<(qbswap(value));
    return *this;
}

OctetsStream &OctetsStream::operator <<(char value)
{
    qds->writeRawData(&value, 1);
    return *this;
}

OctetsStream &OctetsStream::operator <<(quint8 value)
{
    //qds->writeRawData(&value, 1);
    qds->operator <<(value);
    return *this;
}

OctetsStream &OctetsStream::operator <<(Octets value)
{
    compact_uint32(value.lenght);
    if (value.lenght > 1)
        qds->writeRawData(value.text, value.lenght);
    return *this;
}

0 个答案:

没有答案