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

鍾超發表於2012-04-24

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

-

相關文章