一.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