Encapsulation of iOS-binary Protocol

Source: Internet
Author: User
Tags htons

Encapsulation of iOS-binary Protocol

 

Encapsulation of iOS-binary Protocol 

 

 

For SDK socket communication, there will be a binary communication mode. Based on the previous work content, we will summarize:

First, we can use the string and binary protocols in socket communication to achieve communication. For the string protocol, the standard communication mode is "string"-"value", and the network transmission is achieved through XML or json, parsing encapsulation also extracts information based on XML or json.

 

For the binary protocol, the C language encapsulates the Protocol through struct, And the OC is used in iOS. In the OC, you can also encapsulate the binary protocol through the C language, however, mixed C language and OC will feel uncomfortable. So today we will talk about binary protocol encapsulation through OC.

 

First, the C language decomposes the protocol encapsulation. A Struct is nothing more than the Protocol value layout, the number of bytes occupied by the Protocol value, the package size, and the memory block of the Struct. Through one-to-one correspondence, we can map the struct to the OC class. The following is a simple protocol encapsulation structure and OC protocol encapsulation:

 

For example, the C language structure of a protocol is as follows:

 

 

Struct {char one; // character unsigned short two; // short type unsigned int three; // int type char * four; // string} BinaryProtocolStruct;


 

Uses the OC object-oriented method to encapsulate the protocol.

 

Header file:

 

@interface BinaryProtocolTest : NSObject-(id)initWithOne:(int)one andTwo:(int)two andThree:(int)three andFour:(NSString *)string;-(id)initWithData:(NSData *)data;-(void)setOne:(int)one;-(void)setTwo:(int)two;-(void)setThree:(int)three;-(void)setFour:(NSString *)four;-(int)getOne;-(int)getTwo;-(int)getThree;-(NSString *)getFour;-(NSMutableData *)getProtocolData;-(int)getProtocolSize;+(int)getProtocolSize;@end

Implementation file:

 

 

 

// Fixed protocol size # define ProtocolSize 39 @ implementation BinaryProtocolTest {NSMutableData * _ protocolData; // protocol memory block int _ protocolSize; // protocol memory block size struct {unsigned short one_offset: 8; unsigned short one_len: 8; unsigned short two_offset: 8; unsigned short two_len: 8; unsigned short three_offset: 8; unsigned short three_len: 8; unsigned short four_offset: 8; unsigned short four_len: 8 ;}_ protocolLayout; // protocol memory block layout, Master Determined by offset and size}-(void) initProtocolLayout {_ protocolLayout. one_offset = 0; _ protocolLayout. one_len = 1; _ protocolLayout. two_offset = 1; _ protocolLayout. two_len = 2; _ protocolLayout. three_offset = 3; _ protocolLayout. three_len = 4; _ protocolLayout. four_offset = 7; _ protocolLayout. four_len = 32; _ protocolSize = 39;}/** this method is mainly used when you want to use this Protocol to encapsulate your own data. */-(id) initWithOne :( int) one andTwo :( int) two andThree :( int) t Hree andFour :( NSString *) four {self = [super init]; if (self) {[self initProtocolLayout]; // initialize the Protocol Memory layout first _ protocolData = [[NSMutableData alloc] init]; // initialize the Protocol memory block [_ protocolData resetBytesInRange: NSMakeRange (0, _ protocolSize)]; // set the memory block size. // if one is of the char type, you do not need to convert the transmission mode of the network host. Write the value of one to the memory block. unsigned char tempOne = one; [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. one_offset, _ protocolLayout. on E_len) withBytes: & tempOne length: _ protocolLayout. one_len]; // two is of the unsigned short type. Therefore, you need to convert the transmission byte sequence of the network host to htons> short type Host Storage> network storage, and write the memory block unsigned short tempTwo = two; tempTwo = htons (tempTwo); [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. two_offset, _ protocolLayout. two_len) withBytes: & tempTwo length: _ protocolLayout. two_len]; // three is of the int type. Therefore, you need to convert the bytes of the network host to the htonl-> short type.> Network storage, and write the memory block unsigned int tempThree = three; tempThree = htonl (tempThree); [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. three_offset, _ protocolLayout. three_len) withBytes: & tempThree length: _ protocolLayout. three_len]; // if four is a string, you do not need to store or convert NSData * tempFour = [four dataUsingEncoding: NSUTF8StringEncoding]; [_ protocolData replaceBytesInRange: NSMakeRange. four_of Fset, _ protocolLayout. four_len) withBytes: tempFour. bytes length: _ protocolLayout. four_len];} return self;}-(id) init {self = [super init]; if (self) {[self initProtocolLayout]; _ protocolData = [[NSMutableData alloc] init]; [_ protocolData resetBytesInRange: NSMakeRange (0, _ protocolSize)];} return self;}-(id) initWithData :( NSData *) data {self = [super init]; if (self) {[self initProtocolLayout]; if (Data. length! = _ ProtocolSize) {return nil; // parameter filtering. If the returned data packet size is incorrect,} _ protocolData = [[NSMutableData alloc] init] is returned; [_ protocolData resetBytesInRange: NSMakeRange (0, _ protocolSize)]; [_ protocolData replaceBytesInRange: NSMakeRange (0, _ protocolSize) withBytes: data. bytes length: _ protocolSize];} return self;} // set char-(void) setOne :( int) one {if (_ protocolData. length! = _ ProtocolSize) {// if one is of the char type, you do not need to convert the transmission mode of the network host. Write the value of one into the memory block unsigned char tempOne = one; [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. one_offset, _ protocolLayout. one_len) withBytes: & tempOne length: _ protocolLayout. one_len] ;}}// two setting unsigned short-(void) setTwo :( int) two {if (_ protocolData. length! = _ ProtocolSize) {// two is of the unsigned short type. Therefore, you need to convert the transmission byte sequence of the network host to htons> short type Host Storage> network storage, and write the memory block unsigned short tempTwo = two; tempTwo = htons (tempTwo); [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. two_offset, _ protocolLayout. two_len) withBytes: & tempTwo length: _ protocolLayout. two_len] ;}}// set int-(void) setThree for three :( int) three {if (_ protocolData. length! = _ ProtocolSize) {// three is of the int type. Therefore, you need to convert the bytes of the network host to htonl> short type Host Storage> network storage, and write the memory block unsigned int tempThree = three; tempThree = htonl (tempThree); [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. three_offset, _ protocolLayout. three_len) withBytes: & tempThree length: _ protocolLayout. three_len] ;}}// set string-(void) setFour for four :( NSString *) four {if (_ protocolData. length! = _ ProtocolSize) {// you do not need to store and convert NSData * tempFour = [four dataUsingEncoding: NSUTF8StringEncoding]; [_ protocolData replaceBytesInRange: NSMakeRange (_ protocolLayout. four_offset, _ protocolLayout. four_len) withBytes: tempFour. bytes length: _ protocolLayout. four_len] ;}}// get one-(int) getOne {if (_ protocolData. length! = _ ProtocolSize) {unsigned char temp; [_ protocolData getBytes: & temp range: NSMakeRange (_ protocolLayout. one_offset, _ protocolLayout. one_len)]; return temp;} return 0;} // get two-(int) getTwo {if (_ protocolData. length! = _ ProtocolSize) {unsigned short temp; [_ protocolData getBytes: & temp range: NSMakeRange (_ protocolLayout. two_offset, _ protocolLayout. two_len)]; // return ntohs (temp);} return 0;} // get three-(int) getThree {if (_ protocolData. length! = _ ProtocolSize) {unsigned char temp; [_ protocolData getBytes: & temp range: NSMakeRange (_ protocolLayout. three_offset, _ protocolLayout. three_len)]; // int network storage to local storage return ntohl (temp);} return 0;} // get four-(NSString *) getFour {if (_ protocolData. length! = _ ProtocolSize) {NSData * temp = [_ protocolData subdataWithRange: NSMakeRange (_ protocolLayout. four_offset, _ protocolLayout. four_len)]; NSString * tempStr = [[NSString alloc] initwithuf8string: temp. bytes]; return tempStr;} return nil;}-(NSMutableData *) getProtocolData {return _ protocolData;}-(int) getProtocolSize {return _ protocolSize;} + (int) getProtocolSize {return ProtocolSize;} @ end

Summary: object-oriented ideas are encapsulated to connect data entities and operations on data entities

 


 

Related Article

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.