Linuxc/C ++ programming BASICS (37) cumulus: binaryreader implementation

Source: Internet
Author: User

1. Cumulus: binaryreader. h definition, as follows:

# 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

 

II. Implementation of cumulus: binaryreader. cpp:

# Include "binaryreader. H"
# Include "util. H"
Using namespace STD;
Using namespace poco;

Namespace cumulus {
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

 

Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8599871

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.