Compatibility mode implemented

This commit is contained in:
Roman
2014-11-02 23:28:21 +03:00
parent c07318c325
commit 5f4f72dac8
9 changed files with 51 additions and 30 deletions

View File

@ -55,7 +55,7 @@ configure_file(
IMMEDIATE @ONLY)
set(QMSGPACK_MAJOR "0")
set(QMSGPACK_MINOR "0")
set(QMSGPACK_MINOR "1")
set(QMSGPACK_VERSION "0")
set(MSGPACK_QT_LIB_VERSION_STRING "${QMSGPACK_MAJOR}.${QMSGPACK_MINOR}.${QMSGPACK_VERSION}")

View File

@ -11,15 +11,19 @@ QT -= gui
TARGET = qmsgpack
CONFIG -= app_bundle
TEMPLATE = app
#DEFINES += MSGPACK_MAKE_LIB
target.path = ../build
TEMPLATE = lib
DEFINES += MSGPACK_MAKE_LIB
DESTDIR = $$_PRO_FILE_PWD_/../bin
INSTALLS += target
QMAKE_CXXFLAGS += -fPIC
CONFIG += debug_and_release
CONFIG(debug, debug|release) {
TARGET = $$join(TARGET,,,d)
}
SOURCES += main.cpp \
msgpack.cpp \
SOURCES += msgpack.cpp \
msgpack_common.cpp \
private/pack_p.cpp \
private/unpack_p.cpp

View File

@ -1,5 +1,4 @@
#include "msgpack.h"
#include <QDebug>
#include "private/unpack_p.h"
#include "private/pack_p.h"
#include <QByteArray>
@ -12,7 +11,6 @@ QVariant MsgPack::unpack(const QByteArray &data)
return MsgPackPrivate::unpack(p, end);
}
QByteArray MsgPack::pack(const QVariant &variant)
{
quint8 *p = 0;
@ -27,14 +25,17 @@ QByteArray MsgPack::pack(const QVariant &variant)
return arr;
}
bool MsgPack::registerPacker(QMetaType::Type qType, qint8 msgpackType, MsgPack::pack_user_f packer)
{
return MsgPackPrivate::register_packer(qType, msgpackType, packer);
}
bool MsgPack::registerUnpacker(qint8 msgpackType, MsgPack::unpack_user_f unpacker)
{
return MsgPackPrivate::register_unpacker(msgpackType, unpacker);
}
void MsgPack::setCompatibilityModeEnabled(bool enabled)
{
MsgPackPrivate::compatibilityMode = enabled;
}

View File

@ -12,6 +12,8 @@ namespace MsgPack
MSGPACK_EXPORT QByteArray pack(const QVariant &variant);
MSGPACK_EXPORT bool registerPacker(QMetaType::Type qType, qint8 msgpackType, pack_user_f packer);
MSGPACK_EXPORT void setCompatibilityModeEnabled(bool enabled);
}
#endif // MSGPACK_H

View File

@ -6,4 +6,4 @@ QString MsgPack::version()
.arg(MSGPACK_MAJOR)
.arg(MSGPACK_MINOR)
.arg(MSGPACK_VERSION);
}
}

View File

@ -4,7 +4,7 @@
#include <QVariant>
#define MSGPACK_MAJOR 0
#define MSGPACK_MINOR 0
#define MSGPACK_MINOR 1
#define MSGPACK_VERSION 0
namespace MsgPack {
@ -15,9 +15,14 @@ namespace MsgPack {
* return type size
*/
typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write);
/**
* unpack user type to QVariant
*/
typedef QVariant (*unpack_user_f)(const QByteArray &data);
/**
* @brief version
* @return current version
*/
QString version();
}

View File

@ -15,9 +15,14 @@ namespace MsgPack {
* return type size
*/
typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write);
/**
* unpack user type to QVariant
*/
typedef QVariant (*unpack_user_f)(const QByteArray &data);
/**
* @brief version
* @return current version
*/
QString version();
}

View File

@ -4,6 +4,7 @@
#include <QDebug>
QHash<QMetaType::Type, MsgPackPrivate::packer_t> MsgPackPrivate::user_packers;
bool MsgPackPrivate::compatibilityMode = false;
quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
{
@ -17,7 +18,7 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
else if (t == QMetaType::QString)
p = pack_string(v.toString(), p, wr);
else if (t == QMetaType::QVariantList)
p = pack_list(v.toList(), p, wr);
p = pack_array(v.toList(), p, wr);
else if (t == QMetaType::QStringList)
p = pack_stringlist(v.toStringList(), p, wr);
else if (t == QMetaType::LongLong)
@ -27,7 +28,7 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
else if (t == QMetaType::Double)
p = pack_double(v.toDouble(), p, wr);
else if (t == QMetaType::QByteArray)
p = pack_array(v.toByteArray(), p, wr);
p = pack_bin(v.toByteArray(), p, wr);
else if (t == QMetaType::QVariantMap)
p = pack_map(v.toMap(), p, wr);
else {
@ -122,7 +123,7 @@ quint8 *MsgPackPrivate::pack_bool(const QVariant &v, quint8 *p, bool wr)
return p + 1;
}
quint8 *MsgPackPrivate::pack_listlen(quint32 len, quint8 *p, bool wr)
quint8 *MsgPackPrivate::pack_arraylen(quint32 len, quint8 *p, bool wr)
{
if (len <= 15) {
if (wr) *p = 0x90 | len;
@ -141,10 +142,10 @@ quint8 *MsgPackPrivate::pack_listlen(quint32 len, quint8 *p, bool wr)
return p;
}
quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr)
quint8 *MsgPackPrivate::pack_array(const QVariantList &list, quint8 *p, bool wr)
{
int len = list.length();
p = pack_listlen(len, p, wr);
p = pack_arraylen(len, p, wr);
foreach (QVariant item, list)
p = pack(item, p, wr);
return p;
@ -153,7 +154,7 @@ quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr)
quint8 *MsgPackPrivate::pack_stringlist(const QStringList &list, quint8 *p, bool wr)
{
int len = list.length();
p = pack_listlen(len, p, wr);
p = pack_arraylen(len, p, wr);
foreach (QString item, list)
p = pack_string(item, p, wr);
return p;
@ -165,7 +166,8 @@ quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr)
if (len <= 31) {
if (wr) *p = 0xa0 | len;
p++;
} else if (len <= std::numeric_limits<quint8>::max()) {
} else if (len <= std::numeric_limits<quint8>::max() &&
compatibilityMode == false) {
if (wr) *p = 0xd9;
p++;
if (wr) *p = len;
@ -203,21 +205,22 @@ quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr)
return p + 8;
}
quint8 *MsgPackPrivate::pack_array(const QByteArray &arr, quint8 *p, bool wr)
quint8 *MsgPackPrivate::pack_bin(const QByteArray &arr, quint8 *p, bool wr)
{
int len = arr.length();
if (len <= std::numeric_limits<quint8>::max()) {
if (len <= std::numeric_limits<quint8>::max() &&
compatibilityMode == false) {
if (wr) *p = 0xc4;
p++;
if (wr) *p = len;
p++;
} else if (len <= std::numeric_limits<quint16>::max()) {
if (wr) *p = 0xc5;
if (wr) *p = compatibilityMode ? 0xc5 : 0xda;
p++;
if (wr) _msgpack_store16(p, len);
p += 2;
} else {
if (wr) *p = 0xc6;
if (wr) *p = compatibilityMode ? 0xc6 : 0xdb;
p++;
if (wr) _msgpack_store32(p, len);
p += 4;

View File

@ -13,6 +13,7 @@ typedef struct {
} packer_t;
bool register_packer(QMetaType::Type q_type, qint8 msgpack_type, MsgPack::pack_user_f packer);
extern QHash<QMetaType::Type, packer_t> user_packers;
extern bool compatibilityMode;
quint8 * pack(const QVariant &v, quint8 *p, bool wr);
@ -23,13 +24,13 @@ quint8 * pack_ulonglong(quint64 i, quint8 *p, bool wr);
quint8 * pack_bool(const QVariant &v, quint8 *p, bool wr);
quint8 * pack_listlen(quint32 len, quint8 *p, bool wr);
quint8 * pack_list(const QVariantList &list, quint8 *p, bool wr);
quint8 * pack_arraylen(quint32 len, quint8 *p, bool wr);
quint8 * pack_array(const QVariantList &list, quint8 *p, bool wr);
quint8 * pack_stringlist(const QStringList &list, quint8 *p, bool wr);
quint8 * pack_string(const QString &str, quint8 *p, bool wr);
quint8 * pack_double(double i, quint8 *p, bool wr);
quint8 * pack_array(const QByteArray &arr, quint8 *p, bool wr);
quint8 * pack_bin(const QByteArray &arr, quint8 *p, bool wr);
quint8 * pack_map(const QVariantMap &map, quint8 *p, bool wr);
quint8 * pack_user(const QVariant &v, quint8 *p, bool wr);
}