Beetle. SL object Encapsulation Protocol

Source: Internet
Author: User

Beetle. SL provides object-based socket TCP transmission functions other than WCF for Silverlight, although an object is provided to describe the Protocol, if you do not understand the seal of the protocol, it seems that it is more difficult to communicate with other platforms over the network. Although beetle. SL describes the protocol as an object, it is highly flexible. Taking the common header 4-byte message length as an example, a component can describe existing protocols through objects and perform object information interaction through components. This section describes how to use the analyzer with the first 4 characters to describe the length for protocol analysis and acquisition. Headsizepackage is just an abstract class. If you need to use it for analysis, the protocol must inherit it and implement two methods.

    • Protected abstract iMessage readmessagebytype (bufferreader reader, out object typetag );
    • Protected abstract void writemessagetype (iMessage MSG, bufferwriter writer );

These two methods are respectively how to write the Message Type tag when writing the message, how to obtain the tag when reading the message, and how to create related information objects according to the tag. the following is a simple description of writing and reading with the class type name as a tag.

Public class headsizepackage: beetle. headsizeofpackage {protected override beetle. iMessage readmessagebytype (beetle. bufferreader reader, out object typetag) {typetag = reader. readstring (); Switch (string) typetag) {Case "user": return new user (); default: return NULL ;}} protected override void writemessagetype (beetle. iMessage MSG, beetle. bufferwriter writer) {If (MSG is user) {writer. write (MSG. getType (). name);} else {writer. write ("null ");}}}

The above OBJECT tag processing is very simple. If the object is a user, the type name will be written. If there is no matching, a null character will be written, if it is marked as a user during reading, a user object is created and returned, so that a simple protocol analyzer is defined; however, this does not show how headsizepackage writes an object to a data stream or reads information from a stream. The following describes this in detail using a simple object.

Public class user: beetle. iMessage {public string name; Public String email; Public void load (beetle. bufferreader reader) {name = reader. readstring (); email = reader. readstring ();} public void save (beetle. bufferwriter writer) {writer. write (name); writer. write (email );}}

This is a simple user information. There are two other member names and email. What is the format when the channel writes data through headsizepackage. First, we instance an object and SET related values.

 
User user = new user (); User. Name = "Henry"; user. Email = "henryfan@msn.com ";

The byte [] hexadecimal information obtained after headsizepackage processing is as follows:

 
Bytes

Next, we will describe the composition structure of the message through a detailed illustration:

The above is only one way to handle headsizepackage. There are many ways to develop the protocol. In some cases, the existing Protocol already exists, and it cannot be modified to adapt to the existing one. Therefore, headsizepackage provides sufficient flexibility for performance adaptation. The following is an implementation that uses int as the message tag.

 
Public class headsizepackage: beetle. headsizeofpackage {protected override beetle. iMessage readmessagebytype (beetle. bufferreader reader, out object typetag) {typetag = reader. readint32 (); Switch (INT) typetag) {Case 1: return new user (); default: return NULL ;}} protected override void writemessagetype (beetle. iMessage MSG, beetle. bufferwriter writer) {If (MSG is user) {writer. write (1);} else {writer. write (-1 );}}}

The Protocol content generated by the same message is as follows:

2500000000000000500000068656e7279000000068656e727966616e406d736e2e636f6d

Describe the structure through a detailed illustration:

There are many actual situations. Here we only briefly describe the two cases. The bufferwriter and bufferreader methods provided by components are sufficient to meet the actual needs. For details about the methods of the above two objects, see beetle. SL bufferreader, bufferwriter

In the new version of beetle. SL integrated into the variable length of numerical write, can better save bandwidth resources detailed look: http://beetlesl.codeplex.com/

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.