现在的位置: 首页 > 综合 > 正文

OpenRTMFP/Cumulus Primer(9)AMF解析之BinaryReader/Writer

2013年01月02日 ⁄ 综合 ⁄ 共 5392字 ⁄ 字号 评论关闭

OpenRTMFP/Cumulus Primer(9)AMF解析之BinaryReader/Writer

  • Author: 柳大·Poechant(钟超)
  • Email: zhongchao.ustc#gmail.com (#->@)
  • Blog: Blog.CSDN.net/Poechant
  • Date: April 24th, 2012

本文介绍 CumulusServer 中如何对 AMF 数据格式进行序列化与反序列化。

1 AMF3 数据格式基础

首先介绍一下变长整数(Variable Length Integer),比如 UInt32 如下。

Resize icon

上图摘自 Adobe AMF3 官方文档,这是一种压缩方式的整数存储,且每一字节都对后面的数据具有预知作用。那么字符串如何处理呢?下面是字符串的处理方式,AMF0 和 AMF3 都才用 UTF-8 编码方式,并做如下压缩处理:

Resize icon

上图摘自 Adobe AMF3 官方文档。

2 序列化

序列化包括 8 位、16 位、32 位,以及 UTF-8 和 UTF-16(I guess)编码的 String,还有原生数据(Raw Data)、变长无符号整数(Variable Length Unsigned Integer)以及 IP 地址。所谓序列化就是按照指定格式编写各种对象、基础数据类型值。

class BinaryWriter : public Poco::BinaryWriter {
public:
    BinaryWriter(std::ostream& ostr);
    virtual ~BinaryWriter();

    void writeRaw(const Poco::UInt8* value,Poco::UInt32 size);
    void writeRaw(const char* value,Poco::UInt32 size);
    void writeRaw(const std::string& value);

    void write8(Poco::UInt8 value);
    void write16(Poco::UInt16 value);
    void write32(Poco::UInt32 value);

    void writeString8(const std::string& value);
    void writeString8(const char* value,Poco::UInt8 size);
    void writeString16(const std::string& value);
    void writeString16(const char* value,Poco::UInt16 size);

    void write7BitValue(Poco::UInt32 value);
    void write7BitLongValue(Poco::UInt64 value);

    void writeAddress(const Address& address,bool publicFlag);
    void writeAddress(const Poco::Net::SocketAddress& address,bool publicFlag);

    static BinaryWriter BinaryWriterNull;
};

请注意其中名为 BinaryWriterNull 的成员。构造函数定义为:

BinaryWriter::BinaryWriter(ostream& ostr):
    Poco::BinaryWriter(ostr,BinaryWriter::NETWORK_BYTE_ORDER) {
}


BinaryWriter::~BinaryWriter() {
    flush();
}

其中 writeRaw 是简单地封装 Poco::BinaryWriter::writeRaw(),如下:

inline void BinaryWriter::writeRaw(const Poco::UInt8* value,Poco::UInt32 size) {
    Poco::BinaryWriter::writeRaw((char*)value,size);
}
inline void BinaryWriter::writeRaw(const char* value,Poco::UInt32 size) {
    Poco::BinaryWriter::writeRaw(value,size);
}
inline void BinaryWriter::writeRaw(const std::string& value) {
    Poco::BinaryWriter::writeRaw(value);
}

写入整数实现如下,用的是从 Poco::BinaryReader 继承来的重载运算符操作:

inline void BinaryWriter::write8(Poco::UInt8 value) {
    (*this) << value;
}   
inline void BinaryWriter::write16(Poco::UInt16 value) {
    (*this) << value;
}
inline void BinaryWriter::write32(Poco::UInt32 value) {
    (*this) << value;
}

写入字符串:

void BinaryWriter::writeString8(const char* value,UInt8 size) {
    write8(size);
    writeRaw(value,size);
}
void BinaryWriter::writeString8(const string& value) {
    write8(value.size());
    writeRaw(value);
}
void BinaryWriter::writeString16(const char* value,UInt16 size) {
    write16(size);
    writeRaw(value,size);
}
void BinaryWriter::writeString16(const string& value) {
    write16(value.size());
    writeRaw(value);
}

写入变长整数,这段代码含义也一目了然,就是读取变长无符号 32 位整数、64 位整数。

void BinaryWriter::write7BitValue(UInt32 value) {
    UInt8 shift = (Util::Get7BitValueSize(value)-1)*7;
    bool max = false;
    if(shift>=21) { // 4 bytes maximum
        shift = 22;
        max = true;
    }

    while(shift>=7) {
        write8(0x80 | ((value>>shift)&0x7F));
        shift -= 7;
    }
    write8(max ? value&0xFF : value&0x7F);
}

void BinaryWriter::write7BitLongValue(UInt64 value) {
    UInt8 shift = (Util::Get7BitValueSize(value)-1)*7;
    bool max = shift>=63; // Can give 10 bytes!
    if(max)
        ++shift;

    while(shift>=7) {
        write8(0x80 | ((value>>shift)&0x7F));
        shift -= 7;
    }
    write8(max ? value&0xFF : value&0x7F);
}

写入 IP 地址的两个函数暂略。

3 反序列化

反序列化就是从指定格式的数据中读出各类型的数据值。

class BinaryReader : public Poco::BinaryReader {
public:
    BinaryReader(std::istream& istr);
    virtual ~BinaryReader();

    Poco::UInt32    read7BitValue();
    Poco::UInt64    read7BitLongValue();
    Poco::UInt32    read7BitEncoded();
    void            readString(std::string& value);
    void            readRaw(Poco::UInt8* value,Poco::UInt32 size);
    void            readRaw(char* value,Poco::UInt32 size);
    void            readRaw(Poco::UInt32 size,std::string& value);
    void            readString8(std::string& value);
    void            readString16(std::string& value);
    Poco::UInt8     read8();
    Poco::UInt16    read16();
    Poco::UInt32    read32();
    bool            readAddress(Address& address);

    static BinaryReader BinaryReaderNull;
};

构造与析构函数都很简单:

BinaryReader::BinaryReader(istream& istr) :         Poco::BinaryReader(istr,BinaryReader::NETWORK_BYTE_ORDER) {
}

BinaryReader::~BinaryReader() {
}

读取原生数据(Raw Data):

inline void BinaryReader::readRaw(Poco::UInt8* value,Poco::UInt32 size) {
    Poco::BinaryReader::readRaw((char*)value,size);
}
inline void BinaryReader::readRaw(char* value,Poco::UInt32 size) {
    Poco::BinaryReader::readRaw(value,size);
}
inline void BinaryReader::readRaw(Poco::UInt32 size,std::string& value) {
    Poco::BinaryReader::readRaw(size,value);
}

写整数,用的是 Poco::BinaryWriter 的重载运算符:

inline void BinaryWriter::write8(Poco::UInt8 value) {
    (*this) << value;
}

inline void BinaryWriter::write16(Poco::UInt16 value) {
    (*this) << value;
}

inline void BinaryWriter::write32(Poco::UInt32 value) {
    (*this) << value;
}

读写整数依旧使用从 Poco::BinaryReader 继承来的运算符操作:

UInt8 BinaryReader::read8() {
    UInt8 c;
    (*this) >> c;
    return c;
}

UInt16 BinaryReader::read16() {
    UInt16 c;
    (*this) >> c;
    return c;
}

UInt32 BinaryReader::read32() {
    UInt32 c;
    (*this) >> c;
    return c;
}

写字符串:

void BinaryWriter::writeString8(const char* value,UInt8 size) {
    write8(size);
    writeRaw(value,size);
}
void BinaryWriter::writeString8(const string& value) {
    write8(value.size());
    writeRaw(value);
}
void BinaryWriter::writeString16(const char* value,UInt16 size) {
    write16(size);
    writeRaw(value,size);
}
void BinaryWriter::writeString16(const string& value) {
    write16(value.size());
    writeRaw(value);
}

读取变长整数,分别针对 UInt32 和 UInt64,要理解 AMF3 的变长整数才能理解这个:

UInt32 BinaryReader::read7BitValue() {
    UInt8 n = 0;
    UInt8 b = read8();
    UInt32 result = 0;
    while ((b&0x80) && n < 3) {
        result <<= 7;
        result |= (b&0x7F);
        b = read8();
        ++n;
    }
    result <<= ((n<3) ? 7 : 8); // Use all 8 bits from the 4th byte
    result |= b;
    return result;
}

UInt64 BinaryReader::read7BitLongValue() {
    UInt8 n = 0;
    UInt8 b = read8();
    UInt64 result = 0;
    while ((b&0x80) && n < 8) {
        result <<= 7;
        result |= (b&0x7F);
        b = read8();
        ++n;
    }
    result <<= ((n<8) ? 7 : 8); // Use all 8 bits from the 4th byte
    result |= b;
    return result;
}

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

抱歉!评论已关闭.