[Hand Tour new Project history] 9th day-websocket Baotou: string 129, Binary 130__web

Source: Internet
Author: User

Package Unpack function sharing

int Websocketprotocol::trydeframeincomingpacket (pushframework::D atabuffer& buffer, pushframework::i  ncomingpacket*& Ppacket, int& serviceid, unsigned int&, nextractedbytes connectioncontext*) {if (buffer.

	Getdatasize () = = 0) return protocol::eincompletepacket;

	websocketconnectioncontext* pcxt = (websocketconnectioncontext*) pContext; if (pcxt->getstage () = = Websocketconnectioncontext::handshakestage) {websockethandshakemessage* pMessage = new Webs Ockethandshakemessage (buffer. GetBuffer (), buffer.
		Getdatasize ());
		Serviceid = 0; nextractedbytes = buffer.
		Getdatasize ();
		Ppacket = PMessage;
	return protocol::success;
    }//in The other cases, we should expect a data Message:int = 6; if (buffer).

    Getdatasize () < nminexpectedsize) return protocol::eincompletepacket;
	BYTE payloadflags = buffer.getat (0);
	if ((payloadflags >> 7) = = 0) {return protocol::eundefinedfailure; }/* IF (Payloadflags!= 129) return protocol::eundefinedfailure;
    */BYTE Basicsize = Buffer.getat (1) & 0x7F;
    unsigned __int64 payloadsize;

    int masksoffset;
        if (basicsize <=) {payloadsize = basicsize;
    Masksoffset = 2;
        else if (basicsize = = 126) {nminexpectedsize = 2; if (buffer).
        Getdatasize () < nminexpectedsize) return protocol::eincompletepacket; Payloadsize = Ntohs (* (u_short*) (buffer.
        GetBuffer () + 2));
    Masksoffset = 4;
        else if (basicsize = = 127) {nminexpectedsize = 8; if (buffer).
        Getdatasize () < nminexpectedsize) return protocol::eincompletepacket; Payloadsize = Ntohl (* (u_long*) (buffer.
        GetBuffer () + 2));
    Masksoffset = 10;

    else return protocol::eundefinedfailure;
    Nminexpectedsize + = Payloadsize; if (buffer). Getdatasize () < nminexpectedsize) return Protocol::eincompletepacKet	BYTE Len = 4;
    Length of the mask BYTE masks[4]; memcpy (masks, buffer.
	
    GetBuffer () + Masksoffset, 4);
    char* payload = new Char[payloadsize + 1]; memcpy (payload, buffer.
	
    GetBuffer () + Masksoffset + Len, payloadsize);
    For (unsigned __int64 i = 0; i < payloadsize; i++) {Payload[i] = (Payload[i] ^ masks[i%4]);

	} Payload[payloadsize] = ';
	websocketdatamessage* pMessage = new Websocketdatamessage (payload);
	Serviceid = 1;
	Nextractedbytes = nminexpectedsize;

	Ppacket = PMessage;
	Delete payload;
return protocol::success; int Websocketprotocol::frameoutgoingpacket (pushframework::outgoingpacket& packet, pushframework::D atabuffer & Buffer, unsigned int& nwrittenbytes, unsigned int size) {websocketmessage& message = (websocketmessage&amp

	;) packet; if (message. GetType () = = Websocketmessage::handshake) {websockethandshakemessage& handshake = (websockethandshakemessage

        &) message; String Strraw = handshake. SerializE ();

        if (Strraw.size () > Buffer.getremainingsize ()) return protocol::einsufficientbuffer; Buffer.
        Append ((char*) strraw.c_str (), strraw.size ());
        Nwrittenbytes = Strraw.size ();
	return protocol::success; } if (message. GetType () = = Websocketmessage::D atamessage) {websocketdatamessage& datamessage = (websocketdatamessage&) mess
        Age
		
		String strdata = Datamessage.getencodeddata ();		Stream stream;
		Binary write int nsize = Strdata.size ();
		if (size!= 0) {nsize = size;
		} unsigned __int64 payloadsize = nsize; /* int expectedsize = payloadsize + 1;
		Statistic packet Length if (payloadsize <= && payloadsize <= 65535) expectedsize + = 1;
		else if (payloadsize > Payloadsize && <= 65535) Expectedsize + = 3;
		else Expectedsize = 9;
		if (Expectedsize > Buffer.getremainingsize ()) return protocol::einsufficientbuffer; * *//create the flags byte unsigned char payloadflags = 130;/Header: string 129, Binary 130 if (size = = 0) {payloadflags = 129; } if (size = = 0) {buffer.
		WriteByte (Payloadflags); else {stream.
		Write (Payloadflags);			int expectedsize = 1;
			The length of the statistic packet is//create the long byte if (payloadsize <=) {unsigned char basicsize = payloadsize; if (size = = 0) {buffer.
			WriteByte (basicsize); else {stream.
			Write (basicsize);
		} expectedsize = 1; else if (/*payloadsize > &&*/payloadsize <= 65535)//by Byfei 2017/02/20 {unsigned char basics
			ize = 126;
			unsigned char len[2];
			Len[0] = (payloadsize >> 8) & 255;
			LEN[1] = (payloadsize) & 255; if (size = = 0) {buffer.
				WriteByte (basicsize); Buffer.
				WriteByte (Len[0]); Buffer.
			WriteByte (len[1]); else {stream.
				Write (basicsize); Stream.
				Write (Len[0]); Stream.
			Write (len[1]);
		} expectedsize = 3;
			else {unsigned char basicsize = 127;
			Char Len[8]; Len[0] = (payloadsize >>) & 255;
			LEN[1] = (payloadsize >>) & 255;
			LEN[2] = (payloadsize >>) & 255;
			LEN[3] = (payloadsize >>) & 255;
			LEN[4] = (payloadsize >>) & 255;
			LEN[5] = (payloadsize >>) & 255;
			LEN[6] = (payloadsize >> 8) & 255;
			LEN[7] = (payloadsize) & 255; if (size = = 0) {buffer.
				WriteByte (basicsize); Buffer.
			Append (len, 8); else {stream.
				Write (basicsize); Stream.
			WriteBuffer (len, 8);
		} expectedsize = 9; }//buffer.
		Append ((char*) strdata.c_str (), size); if (size!= 0) {buffer.
			Append (Stream.getbuf (), expectedsize); Buffer.
		Append (Datamessage.getstream (). Getbuf (), nsize); else {buffer.
		Append ((char*) strdata.c_str (), nsize); } expectedsize = Payloadsize + expectedsize;
        The length of the statistic packet nwrittenbytes = expectedsize;
	return protocol::success; Return protocol::eundefinedfailure; }

Google protobuf Use of small traps click on the Open link

A brief analysis of Google Protocol buffers Click Open link

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.