Summary of the invention
The objective of the invention is to existing network bytes packing and the method complicacy that unpacks, complicated at programming model; Protocol fields needs under the situation of often change; Use inconvenience; Can not satisfy simple, as to be prone to expansion requirement problem, propose a kind of protocol Data Unit PDU implementation method that is used for the network bytes packing and unpacks that solves the complex network programming.
Technical scheme of the present invention is:
A kind of protocol Data Unit PDU implementation method that is used for the network bytes packing and unpacks, it may further comprise the steps:
(a). define the expandable mark language XML field configuration file of required host-host protocol, comprise order, length and the data type of field in the host-host protocol;
(b). packing: read the field configuration file of this agreement, obtain the data type and the data length of each field, the java fundamental type numerical value of this each field of agreement is set then, read all fields of this agreement successively:
If this field is the java fundamental type, convert the java fundamental type numerical value of this field into byte data; If this field is a son bag type, read all fields of son bag, be packaged into byte data successively; Travel through all fields of this agreement, obtain final byte arrays;
(c). unpack: the byte arrays that acquisition need unpack, according to the protocol fields configuration file that reads in advance, obtain the data length of an only field:
If the java basic data type that this field is converts byte data into predefined java basic data type; If the type of this field is a son bag type, resolve all fields of this child bag successively, obtain the java basic data type; Travel through all fields of this agreement, resolve successively and obtain the java basic data type.
Beneficial effect of the present invention:
It is complicated to the invention solves the programming model that exists in the legacy network programming, and protocol fields changes the problem of inconvenience, provides a kind of network bytes simple, that be prone to expansion to flow packing, unpacking method.
The present invention improves the efficient of network bytes stream programming, make the network bytes packing, unpack and become simple; Shielded the difference of high low byte, good Debugging message is provided and unified traffic monitoring is provided.
Embodiment
Below in conjunction with accompanying drawing and embodiment the present invention is further described.
Define order, length, the data type of field in the agreement through the XML configuration file.The complicacy read-write of shielding original data type is automatically converted to intelligible basic data type with it, realizes that high low byte preface arbitrarily changes, and can adapt to protocol configuration flexibly.
Shown in figure one, the relation in each object in the PDU implementation method has been described, relevant noun is explained as follows:
Protocol object (Protocol): a byte stream by one or more PDU objects are formed is abstract, can represent a complete agreement, such as the MML agreement, and the SMPP agreement
Son bag (PduBean): for the distincter protocol object of organizing, perhaps reuse protocol definition, can use the form of son bag to organize protocol fields
Protocol fields object (PduField): not subdivisible PDU object, the Field Definition of an atom of expression
PDU: interface definition, protocol object, Zi Bao, protocol fields all realize the IPdu interface, can realize self-contained between such object like this, can think a kind of realization of component pattern.
During practical implementation:
Definition, the exploitation how the example explanation is used the network packing, separated packet technology realization agreement that be embodied as with a MML agreement.
One, as shown in the table like the byte that requires in the agreement:
The message opening flag |
Verification with |
Operation information |
The affairs head |
Can the thread of a conversation |
Message header |
Message-length |
The message opening flag |
←
The message sending order
Two, definition protocol configuration file is following:
<?xml?version=″1.0″encoding=″UTF-8″?>
<zsmart>
<protocol>
<bufferDefine?name=″MML″>
!--defining main message body-->
<buffer?id=″MML_REQ″seq=″1″fieldname=″MsgBeginFlag″type=″5″
Length=" 4 " defaultValue=" ' SC ' " comments=" message opening flag " />
<buffer id=″MML_REQ″ seq=″2″fieldname=″TotalLen″ type=″5″
Length=" 4 " defaultValue=" 0000 " comments=" total length field " />
<buffer id=″MML_REQ″ seq=″3″fieldname=″msgHeader″ type=″0″
Length=" 4 " defaultValue=" " comments=" message header, son bag " />
<buffer id=″MML_REQ″seq=″4″fieldname=″MML_SessionHeader″
Type=" 0 " length=" 4 " defaultValue=" ' SC ' " comments=" the meeting thread of a conversation, son bag " />
<buffer id=″MML_REQ″seq=″5″fieldname=″MML_transHeader″
Type=" 0 " length=" 4 " defaultValue=" ' SC ' " comments=" affairs head, son bag " />
<buffer?id=″MML_REQ″seq=″6″fieldname=″MML_CMD″ type=″5″
Length=" 0 " defaultValue=" " comments=" MML instruction " />
<buffer id=″MML_REQ″seq=″7″fieldname=″CheckSum″ type=″5″
Length=" 8 " defaultValue=" 9B94EDA4 " comments=" verification with " />
!--definition message chieftain bag-->
<buffer id=″msgHeader″ seq=″1″ fieldname=″version″ type=″5″
Length=" 4 " defaultValue=" 1.00 " comments=" version number " />
<buffer id=″msgHeader″ seq=″2″?fieldname=″endFlag″ type=″5″
Length=" 8 " defaultValue=" JS123456 " comments=" end mark " />
<buffer id=″msgHeader″?seq=″3″?fieldname=″serviceName″ type=″5″
Length=" 8 " defaultValue=" PPS " comments=" Service name " />
!--definition session chieftain bag-->
<buffer id=″MML_SessionHeader″ seq=″1″fieldname=″session_id″
Type=" 5 " length=" 8 " defaultValue=" 00000000 " comments=" session id " />
<buffer id=″MML_SessionHeader″ seq=″2″
fieldname=″session_control_word″type=″5″length=″6″defaultValue=″DlgCon″
Comments=" session control word " />
<buffer id=″MML_SessionHeader″?seq=″3″fieldname=″session?reserved″
Type=" 5 " length=" 4 " defaultValue=" FFFF " comments=" session reserved word " />
!--definition affairs chieftain bag-->
<buffer id=″MML_transHeader″?seq=″1″fieldname=″trans_id″?type=″5″
Length=" 8 " defaultValue=" 00000001 " comments=" affairs ID " />
<buffer id=″MML_transHeader″seq=″2″fieldname=″trans_control_word″
Type=" 5 " length=" 6 " defaultValue=" TxBeg " comments=" affairs control word " />
<buffer id=″MML_transHeader″ ?seq=″3″fieldname=″trans_reserved″
Type=" 5 " length=" 4 " defaultValue=" FFFF " comments=" affairs reserved word " />
!--definition heartbeat message-->
<buffer id=″MML_HEART″ seq=″1″fieldname=″MsgBeginFlag″
Type=" 5 " length=" 4 " defaultValue=" ' SC " ' comments=" message opening flag " />
<buffer?id=″MML_HEART″ seq=″2″?fieldname=″TotalLen″ type=″5″
Length=" 4 " defaultValue=" 0004 " comments=" total length field " />
<buffer id=″MML_HEART″ seq=″3″fieldname=″msgBody″ type=″5″
Length=" 4 " defaultValue=" HBHB " comments=" total length field " />
<buffer?id=″MML_HEART″seq=″4″fieldname=″CheckSum″type=″5″
Length=" 8 " defaultValue=" 00000000 " comments=" verification with " />
</bufferDefine>
</protocol>
</zsmart>
Profile Description:
1, bufferDesine: define a procotol bag
2, buffer: define a single protocol fields
3, Id: define this field and belong to that sub-packet
4, Seq: the serial number in the child bag (in a sub-packet, sequence number can not repeat)
5, Fieldname: field name, in a sub-packet, field name can not repeat
Type: corresponding java data type 1:Byte 2:Short 3:Int 4:Long 5:String (variable length) 6:byte [] array 7:CString, with the character string of NULL end
7, length: field length, if the character string of variable length is made as 0 with length, if field type is Byte, Short, Int, Long can use the default-length of j ava
8, defaultValue: default value, can the field default value be set through this field, if do not give this field settings, will use predefined default value during packing.
Owing to when protocol definition, specified the length and the order of each field, all PDU can accomplish according to order and pack automatically and unpack.
Three, in program, quote protocol object, accomplish protocol packing, unpack.
Packing process is following:
Read configuration file, obtain data type, the data length of a field; The java fundamental type numerical value of each field is set; Java fundamental type numerical value with each field converts byte data into successively; If this field is a son bag type, obtain all fields that change the son bag, be packaged into byte data successively.Travel through all fields, obtain final byte arrays.
It is following to separate packet procedures:
The byte arrays that acquisition need unpack according to the protocol fields configuration of reading in advance, obtains the data length of an only field, converts byte data into predefined java basic data type; Likewise,, resolve all fields of this child bag successively, obtain the java basic data type if the type of this field is a son bag field.Travel through all fields, resolve successively and obtain the java basic data type.
Four, the PDU interface programming for example
The simple protocol packing
// acquisition MML heartbeat protocol definition
Protocol?mmlReq=ProtocolTools.getProtocol(″MML.MML_HEART″,1);
// the msgBody field value is set, its residual value uses the default configuration of configuration file
mmlReq.getPduField(″msgBody″).setValue(″HBHB″);
// packetizes messages
byte[]heartBytes=mmlReq.pack();
logger.info(″MML Heart ″+heartBytes.length+″Bytes″+PubFunc.hexToString(heartBytes));
// print protocol bag details
logger.info(mmlReq);
Top code has been demonstrated the process of using the PDU programming model to pack, can see, and utilizes PDU programming model framework, can realize protocol packing very easily.Promote the efficient of programming.
Execution result
-MML?Heart?16Bytes?27?53?43?27?30?30?30?30?48?42?48?42?31?32?32?32
-mmlHeart:
,[MsgBeginFlag,S,′SC′,,len=4,Hex=27 53 4327 ],[TotalLen,S,0000,,len=4,Hex=30 30 ?3030 ],[msgBody,S,HBHB,,len=4,Hex=48 42 ?4842],[CheckSum,S,1222,,len=4,Hex=31323232]
Top output result has shown title, length, corresponding java value, 16 hex value of each protocol fields, contrasts 16 binary information of whole bag, can obtain needed Debugging message very easily.
The present invention does not relate to all identical with the prior art prior art that maybe can adopt of part and realizes.