Encapsulation of the ios-binary protocol

Source: Internet
Author: User
Tags htons

Encapsulation of the ios-binary protocol


For the communication mode of the binary protocol in the SDK socket communication, this paper summarizes the previous work content:

First in the socket communication can have a string protocol and binary Protocol, through the Protocol to achieve the purpose of communication. For the string protocol is the standard mode of communication through string is "string"-"value" mode, through XML or JSON to achieve network transmission, parsing encapsulation is based on XML or JSON for information extraction.


In the case of binary protocols, the C language is encapsulated by a struct, using OC in iOS, and in OC you can encapsulate the binary protocol in C, but the C-and OC-mix turns out to be cool. So today we talk about binary protocol encapsulation through OC.


First of all, C language to the package of the decomposition of the protocol, a struct structure is nothing more than the layout of the protocol value, the value of the number of bytes of the protocol, the size of the packet, the structure of the memory block. With the one by one correspondence, we can map the struct to the OC class. The following is understood by a simple protocol encapsulation structure problem and the OC's protocol encapsulation:


For example, there is now a protocol for the C language structure as follows


struct {    char one;    Character    unsigned short;  Short type    unsigned int three;    int type    char * four;    String}binaryprotocolstruct;


Encapsulation of the protocol using OC object-oriented thinking


Header file:

@interface binaryprotocoltest:nsobject-(ID) initwithone: (int) one andtwo: (int) and 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 size of the protocol # protocolsize 39@implementation binaryprotocoltest{nsmutabledata *_protocoldata;//protocol memory block int _prot      Ocolsize;        The memory block size of the protocol 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;    The memory block layout of the protocol, mainly 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;} /* * The main function of this method is to use */-(ID) initwithone when you want to encapsulate your own data using the protocol: (int) one andtwo: (int)--andthree: (int) Three andfour: (NSString *) four{self = [super inIt];  if (self) {[self initprotocollayout]; First initialize the memory layout of the Protocol _protocoldata = [[Nsmutabledata alloc]init];//the memory block of the initialization protocol [_protocoldata Resetbytesinrange:nsmak        Erange (0, _protocolsize)];//sets the size of the memory block//one the char type does not require a network host transfer mode conversion, the value of one is written to the memory block unsigned char tempone = one; [_protocoldata Replacebytesinrange:nsmakerange (_protocollayout.one_offset, _protocollayout.one_len) withBytes:&        Amp;tempone Length:_protocollayout.one_len]; Two is the unsigned short type, so the transfer byte order of the network host is converted Htons->short type of host storage--network storage, and write memory block unsigned short temptwo = TW        O        Temptwo = htons (temptwo); [_protocoldata Replacebytesinrange:nsmakerange (_protocollayout.two_offset, _protocollayout.two_len) WithBytes:                        &temptwo Length:_protocollayout.two_len];        Three is the int type, so the transfer byte order of the network host is converted Htonl->short type of host storage, network storage, and write memory block unsigned int tempthree = three;        Tempthree = htonl (Tempthree); [_protocoldata ReplacebytEsinrange:nsmakerange (_protocollayout.three_offset, _protocollayout.three_len) Withbytes:&tempthree length:_                Protocollayout.three_len];        Four is a string that does not require a storage conversion nsdata *tempfour = [four datausingencoding:nsutf8stringencoding]; [_protocoldata Replacebytesinrange:nsmakerange (_protocollayout.four_offset, _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 filter, if the size of the returned packet is incorrect, return} _protocoldata =        [[Nsmutabledata alloc] init]; [_protocoldata Resetbytesinrange:Nsmakerange (0, _protocolsize)];            [_protocoldata replacebytesinrange:nsmakerange (0, _protocolsize) withBytes:data.bytes length:_protocolsize]; } return self;} One of the settings char-(void) Setone: (int) one{if (_protocoldata.length!=_protocolsize) {//one for char type does not require network Host transfer mode conversion, put on        The value of E is written to the memory block unsigned char tempone = one; [_protocoldata Replacebytesinrange:nsmakerange (_protocollayout.one_offset, _protocollayout.one_len) WithBytes:    &tempone Length:_protocollayout.one_len]; }}//two Settings unsigned short-(void) Settwo: (int) two{if (_protocoldata.length!=_protocolsize) {//two to unsigned Shor        T type, so to convert the network host's transmission byte sequence htons->short type host storage--network storage, and write 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 settings int-(void) Setthree: (int) three{if (_protocoldAta.length!=_protocolsize) {//three is an int type so the transfer byte order of the network host is converted Htonl->short type of host storage--network storage, and write memory block        unsigned int tempthree = three;        Tempthree = htonl (Tempthree); [_protocoldata Replacebytesinrange:nsmakerange (_protocollayout.three_offset, _protocollayout.three_len) withBytes    : &tempthree Length:_protocollayout.three_len]; }}//four settings string-(void) Setfour: (NSString *) four{if (_protocoldata.length!=_protocolsize) {//four As String does not need to be stored        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)];    Short networked storage to local storage 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 networked storage to local storage return Ntohl (temp); } return 0;} Get four-(NSString *) getfour{if (_protocoldata.length!=_protocolsize) {NSData *temp = [_protocoldata subdat        Awithrange:nsmakerange (_protocollayout.four_offset, _protocollayout.four_len)];        NSString *tempstr = [[NSString alloc]initwithutf8string:temp.bytes];    return tempstr; } return nil;} -(Nsmutabledata *) getprotocoldata{return _protocoldata;} -(int) getprotocolsize{return _protocolsize;} + (int) getprotocolsize{return protocolsize;} @end

Summary: Object-oriented thought encapsulation that enables data entities to be connected to the operation of data entities



Encapsulation of the ios-binary protocol

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.