Source Analysis Series PomeloForEgret

I travel on a project in hand, using a front-end egret engine, the rear end of the frame using a pomelo, front-end network library using the pomelo provided to egrets, you can get this source code on git, or should pomelo the official website can be found.

This paper is an analysis of this PomeloForEgret source. A complete library should include a client network initialization handshake, send heartbeat packets, protocol for transmitting and receiving data. PomeloForEgret based WebSocket, contains the complete functionality described above, the transmission and reception of data and support data according protobuf json protocol data structure of a sequence of binary data. This paper focuses on the sending and receiving portions of the protocol data interpretation.

First introduced to the PomeloForEgret major categories included: Pomelo, Package, Message, Protocol, Protobuf. Pomelo is the interaction with the outside world of business logic classes, and Package, Message, Protocol and Protobuf class as Pomelo's tools.

Message types include data transfer TYPE_REQUEST, TYPE_NOTIFY, TYPE_RESPONSE, TYPE_PUSH.

Package types include service TYPE_HANDSHAKE, TYPE_HANDSHAKE, TYPE_HEARTBEAT, TYPE_DATA, TYPE_KICK.

A data transmitting data protocol
data transmission protocol need to go through the following three processes:

  1. Method Pomelo request class.
    requet (route, msg, cg) has the following three steps:
    . A reqId ++
    . B the sendMessage (reqId, route, MSG)
    . C the callbacks [reqId] = CB, RouteMap [reqId] = route, the callback store protocol transmitted, this callback for processing same data with a protocol (routing) returned.
  2. Pomelo is sendMessage method is a method 1.b above.
    sendMessage (reqId, route, msg) has the following steps:
    A byte:. egrete.ByteArray
    . this._message.encode B (reqId, route, MSG), using the Message tools into the binary data sequence of data transmission (the need to use Protocol Protobuf tools or tools).
    c. this._package.encode (Package.TYPE_DATA, byte) , the use of tools Package, together with service type Package (see above), followed by the step 2.b binary data byte stream.
  3. Pomelo的send方法。
    a. this.socket.writeByte(byte)
    b. this.socket.flush

In particular, steps 2.b and 2.c above as follows:
2.b Message encode the transmitted data protocol method serialized:
the Message type data transfer (see above) into the byte stream.
The reqId byte stream.
The route to the byte stream.
Protobuf Protocol using tools or data into the byte stream protocol, as follows [tag, type] [value] [tag, type] [value] [tag, type] [value] .......
Therefore, the message byte stream should be such that: [Transmission type] [reqId] [route] [ protobuf serialized data]
encode method step 2.c Package as follows:
write Package service type (see above).
2.b write binary data generated in length, 3 bytes.
Write binary 2.b generated.

Therefore, the byte stream should such a package: [service type] [message data length of 3 bytes] [the Message Data]

  1. Receiving data protocol.
    Receiving protocol data includes the following steps:
  2. this._package.decode (byte):
    A. Read the Package type of business (see above).
    b. Read the message length
    c. Read the message.
    return {type: type, body: body, length: len}

  3. this._message.decode (Data):
    A. Message type data transfer (see above).
    b. Read reqId.
    c. Reads the routing
    d. The use of the Protocol class 2.c Protobuf or the message data into the desired object or data.

Data protocol of sending and receiving process is in turn one to one.

Finally paste under Protobuf class serialization and de-serialization of writing unsigned int, I feel quite clever:
Source Analysis Series PomeloForEgret
Source Analysis Series PomeloForEgret

Finish.

Guess you like

Origin blog.51cto.com/zhangzhao/2445772