LinuxC/C++編程基礎(37) Cumulus::BinaryReader的實現

來源:互聯網
上載者:User

一.Cumulus::BinaryReader.h的定義,如下:

#pragma once

#include "Cumulus.h"
#include "Address.h"
#include "LinyanwenBinaryReader.h"
#include "Poco/Net/SocketAddress.h"

namespace Cumulus {
class BinaryReader : public Linyanwen::BinaryReader {
public:
 BinaryReader(Poco::UInt8* _buffer,Poco::UInt32 _size);
 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;
};

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

inline void BinaryReader::readString8(std::string& value) {
 readRaw(read8(),value);
}
inline void BinaryReader::readString16(std::string& value) {
 readRaw(read16(),value);
}

} // namespace Cumulus

 

二.Cumulus::BinaryReader.cpp的實現,如下:

#include "BinaryReader.h"
#include "Util.h"
using namespace std;
using namespace Poco;

namespace Cumulus {
BinaryReader BinaryReader::BinaryReaderNull(NULL,0);//need to be sure?
BinaryReader::BinaryReader(Poco::UInt8* _buffer,Poco::UInt32 _size) :
 Linyanwen::BinaryReader(_buffer,_size)
{}

BinaryReader::~BinaryReader() {
}

UInt32 BinaryReader::read7BitEncoded() {
 UInt32 id;
 Linyanwen::BinaryReader::read7BitEncoded(id);
 return id;
}

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;
}

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;
}

bool BinaryReader::readAddress(Address& address) {
 UInt8 flag = read8();
 ((vector<UInt8>&)address.host).resize(4);
 if((flag&0x0F)==0x8F)
  ((vector<UInt8>&)address.host).resize(16);
 for(int i=0;i<address.host.size();++i)
  ((vector<UInt8>&)address.host)[i] = read8();
 (UInt16&)address.port = read16();
 return flag==0x02;
}

} // namespace Cumulus

 

轉載請註明出處:山水間部落格,http://blog.csdn.net/linyanwen99/article/details/8599871

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.