- Terminator Receive Filter (SuperSocket. SocketBase. Protocol. Terminator Receive Filter, SuperSocket. SocketBase) - - Terminator Protocol
- CountSpliter Receive Filter (SuperSocket. Facility. Protocol. CountSpliter Receive Filter, SuperSocket. Facility) - - Fixed Number Separator Protocol
- Fixed Size Receive Filter (SuperSocket. Facility. Protocol. Fixed Size Receive Filter, SuperSocket. Facility) - - Fixed Request Size Protocol
- BeginEndMarkReceive Filter (SuperSocket. Facility. Protocol. BeginEndMarkReceive Filter, SuperSocket. Facility) - - With Stop Protocol
- Fixed Header Receive Filter (SuperSocket. Facility. Protocol. Fixed Header Receive Filter, SuperSocket. Facility) -- Header format is fixed and contains content length protocol
Terminator Receive Filter Ender Protocol
The terminator protocol is similar to the command-line protocol in that some protocols use terminators to determine a request. For example, a protocol uses two characters "#" as the terminator, so you can use the class "Terminator Receive FilterFactory":
Terminator Protocol Server:
public class TerminatorProtocolServer : AppServer { public TerminatorProtocolServer() : base(new TerminatorReceiveFilterFactory("##")) { } }
Implement your Receive Filter based on Terminator Receive Filter:
public class YourReceiveFilter : TerminatorReceiveFilter<YourRequestInfo> { //More code }
Implement your Receive Filter Factory to create an instance of the Receive Filter:
public class YourReceiveFilterFactory : IReceiveFilterFactory<YourRequestInfo> { //More code }
CountSpliter Receive Filter Fixed Quantity Separator Protocol
Some protocols define requests in a format like this " Part1 part2 part3 Part4 Part5 part6 Part7 ". Each request has seven parts separated by''. The implementation of this protocol is very simple:
/// <summary> /// Request format: Part1 part2 part3 Part4 Part5 Part6 part7# /// </summary> public class CountSpliterAppServer : AppServer { public CountSpliterAppServer() : base(new CountSpliterReceiveFilterFactory((byte)'#', 8)//8 delimiters, 7 parameters. In addition to using the default filter factory, you can customize the protocol with reference to the previous instance. { } }
Fixed Size Receive Filter Fixed Request Size Protocol
In this protocol, all requests are of the same size. If each request is a string of eight characters, such as "HUANG LI", what you should do is to implement a Receive Filter as follows:
class MyReceiveFilter : FixedSizeReceiveFilter<StringRequestInfo> { public MyReceiveFilter() : base(8) //Input fixed request size { } protected override StringRequestInfo ProcessMatchedRequest(byte[] buffer, int offset, int length, bool toBeCopied) { //TODO: Construct the request instance from the parsed data and return it } }
Then use this Receive Filter in your AppServer class:
public class MyAppServer : AppServer { public MyAppServer() : base(new DefaultReceiveFilterFactory<MyReceiveFilter, StringRequestInfo>()) //Use Default Receive Filter Factory { } }
4. BeginEndMarkReceive Filter with Stop Protocol
There are fixed start and end tags in each request of such protocols. For ex amp le, I have a protocol in which all messages follow this format "& xxxxxxxxxxx#". Therefore, in this case, "&" is the start tag, "#" is the end tag, so your acceptance filter can be defined as as follows:__________
class MyReceiveFilter : BeginEndMarkReceiveFilter<StringRequestInfo> { //Start and end tags can also be two or more bytes private readonly static byte[] BeginMark = new byte[] { (byte)'&' }; private readonly static byte[] EndMark = new byte[] { (byte)'#' }; public MyReceiveFilter() : base(BeginMark, EndMark) //Input start and end markers { } protected override StringRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length) { //TODO: Construct the request instance from the parsed data and return it } }
Then use this Receive Filter in your AppServer class:
public class MyAppServer : AppServer { public MyAppServer() : base(new DefaultReceiveFilterFactory<MyReceiveFilter, StringRequestInfo>()) //Use Default Receive Filter Factory { } }
5. Fixed Header Receive Filter Header Format Fixed and Contains Content Length Protocol
This protocol defines a request as two parts. The first part defines basic information including the length of the second part and so on. We usually call the first part header.
For example, we have a protocol where the header contains six bytes, the first four bytes are used to store the name of the request, and the last two bytes are used to represent the length of the request body:
/// +-------+---+-------------------------------+ /// |request| l | | /// | name | e | request body | /// | (4) | n | | /// | |(2)| | /// +-------+---+-------------------------------+
With SuperSocket, you can easily implement this protocol:
class MyReceiveFilter : FixedHeaderReceiveFilter<BinaryRequestInfo> { public MyReceiveFilter() : base(6) { } protected override int GetBodyLengthFromHeader(byte[] header, int offset, int length) { return (int)header[offset + 4] * 256 + (int)header[offset + 5]; } protected override BinaryRequestInfo ResolveRequestInfo(ArraySegment<byte> header, byte[] bodyBuffer, int offset, int length) { return new BinaryRequestInfo(Encoding.UTF8.GetString(header.Array, header.Offset, 4), bodyBuffer.CloneRange(offset, length)); } }
You need to implement your own reception filter based on the class Fixed Header Receive Filter.
-
- The length of the head is represented by 6 passed into the parent constructor.
- Method "GetBodyLengthFromHeader(...)" should return the length of the request body according to the received head.
- Method ResolveRequestInfo(...): "You should return an instance of your request type based on the request header and body you receive.
Actual use scenarios:
You've seen the templates of the five protocols here, and you know the format processing. Next, let's look at a network example:
Communication protocol format:
Seeing that the above protocol is in tangle with the client to send hexadecimal, how to receive the server, hexadecimal message as follows:
26 01 00 19 4E 4A 30 31 31 01 44 41 31 31 32 00 07 00 00 00 00 00 00 34 23
Hexadecimal or decimal, or other processes, are ultimately converted to byte []. In fact, when processing data, the data sent in the past can be converted to byte [], so the service only needs to parse the byte [] array. By parsing according to the protocol, we can get the desired data. Here's an example of using Fixed Size Receive Filter. The code is as follows:
According to the above communication protocol, start to implement parsing:
The first step is to define an appropriate data structure and protocol
HLDatausing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /**************************************************************** * Author: Before dusk and after dawn * CLR Version: 4.0.30319.42000 * Creation time: 2017-01-23 21:12:30 * 2017 * Description: Protocol Packet * * Revising history: * * *****************************************************************/ namespace SuperSocketDemo { public class HLData { /// <summary> /// Start symbol /// </summary> public char Head { get; set; } /// <summary> /// Protocol Packet Data /// </summary> public byte Ping { get; set; } /// <summary> /// Data length /// </summary> public ushort Lenght { get; set; } /// <summary> /// terminal ID /// </summary> public uint FID { get; set; } /// <summary> /// Target type /// </summary> public byte Type { get; set; } /// <summary> /// Forwarding terminal ID /// </summary> public uint SID { get; set; } /// <summary> /// Sending count /// </summary> public ushort SendCount { get; set; } /// <summary> /// Reserved fields /// </summary> public byte[] Retain { get; set; } /// <summary> /// XOR check /// </summary> public byte Check { get; set; } /// <summary> /// End symbol /// </summary> public char End { get; set; } public override string ToString() { return string.Format("Start symbol:{0},Packet data:{1},Data length:{2},terminal ID:{3},Target type:{4},Forwarding terminal ID:{5},Send Packet Count:{6},Reserved fields:{7},XOR check:{8},End symbol:{9}", Head, Ping, Lenght, FID, Type, SID, SendCount, Retain, Check, End); } } }