Analysis of communication protocols: Analysis of byte-throttling protocol

Source: Internet
Author: User
Because of the common characteristics of the Word throttling protocol, parsing is very simple, mainly involving two design modes: Template method, Command mode.
Basic Steps for parsing:
1 to judge the beginning of a complete frame, the beginning byte of the frame is judged according to the protocol stipulation
2 Get a complete frame. There are two main kinds, one is to judge a complete frame according to the beginning byte end byte of the frame, one is to judge by the start frame and the frame length
3 Get a complete frame, according to the command Word to create different commands, according to different command word processing.
Through the template method to parse the frame, because the different command specific composition is different, so according to the command Word to create various commands, using the command mode, the specific processing of each resolution of the frame.
The following is a concrete example of parsing, you can use it in practice simply by modifying it
[CSharp] View plaincopy
public class Dataprocess
{
Protected list<byte> fragment = new list<byte> ()//Save last processed, remaining bytes


<summary>
Ways to call outside
</summary>
<param name= "Newreceiveddata" > newly received byte data </param>
public void ProcessData (byte[] newreceiveddata)
{
Try
{
Fragment. AddRange (Newreceiveddata)//merge the newly received data with the last processed data
var data = new list<byte> ();
Data. AddRange (fragment);
List<byte[]> frames = parseframes (data);//Parse frame
if (frames. Count > 0)
{
Try
{
for (int i = 0; I < frames. Count; i++)
{
Log (String. Format ("process data: {0}", Frames[i]));
Processframe (Frames[i]);//Processing frame
}


}
catch (Exception ex)
{


Throw ex;
}
}
Savefragment (data)//Save the remaining fragments after this process
}
catch (Exception ex)
{
Log ("Error processing device data.") ");
}
}


<summary>
Looping parsing frames
</summary>
<param name= "Data" ></param>
<returns></returns>
Protected list<byte[]> parseframes (list<byte> data)
{
list<byte[]> Frames = new list<byte[]> ();
int framestartindex = getframestartindex (data);//Judge the beginning of a frame
while (framestartindex >= 0)
{
int frameendindex = Getframeendindex (data, framestartindex);//judge the end of frame
if (Frameendindex < 0)//frame incomplete
{
return Frames;
}
byte[] Oneframebyte = getoneframe (data, Framestartindex, Frameendindex);
Frames.add (Oneframebyte);
Data. RemoveRange (0, Framestartindex);//You can have this sentence to avoid incomplete frames, which will affect the subsequent parsing.
Data. RemoveRange (Framestartindex, frameendindex-framestartindex);//Remove processed data
Framestartindex = Getframestartindex (data);
}
return Frames;
}


<summary>
Need to rewrite according to the actual situation
</summary>
<param name= "Frame" ></param>
protected void Processframe (byte[] frame)
{
int commandtypeindex = 2;//Third byte specify command type
int commandbyte = Frame[commandtypeindex];
Switch (commandbyte)//Depending on the command, the generated command is handled separately, using the command mode
{
Case 1:
//
Break
Case 2:
Break
Case 3:
Break
}


}


<summary>
After the parsing frame is processed, the remaining data
</summary>
<param name= "Frag" ></param>
protected void Savefragment (list<byte> frag)
{
int maxfragmentlength = 1000; Maximum length of an unhandled frame
Legacy data fragment is too long, there is a problem. Does not prevent memory pressure from being too large and needs to be emptied fragment
if (Frag. Count > Maxfragmentlength)
{
Frag = new list<byte> ();
}
Fragment. Clear ();
Fragment. AddRange (Frag);
if (Frag. Count > 0)
{
Log (String. Format ("remaining data fragment: {0}", Frag));
}


}

Spirit Field Www.uy0.net
<summary>
Need to rewrite according to the actual situation
</summary>
<param name= "Data" ></param>
<returns></returns>
private static int Getframestartindex (list<byte> data)
{
byte FrameStartByte1 = 0x08;
byte FrameStartByte2 = 0x04;
x8,0x04
for (int i = 0; i < data. Count-1; i++)
{
if (data[i] = = FrameStartByte1 && data[i + 1] = = FrameStartByte2)//Frame header is 0x08,0x04 two bytes start
{
return i;
}
}

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.