Custom protocol

Protocol analysis is the most complex part in a socket communication program. If your application layer protocol design or implementation is poor, the issues of package sticking and package splitting would be difficult to avoid. There is a built-in command line protocol (CommandLineProtocol) in SuperSocket. If you use other format protocol, you must implement your own custom protocol(CustomProtocol).

Implement a custom protocol
You need to create a protocol class which implements a custom protocol interface ICustomProtocol, and a command reader class which implements the interface ICommandReader interface.
public interface ICustomProtocol<TCommandInfo>
    where TCommandInfo : ICommandInfo
    ICommandReader<TCommandInfo> CreateCommandReader(IAppServer appServer);

To achieve a CustomProtocol class, you need a ICommandInfo class as a generic type parameter which is the basic data structure of each command in socket requests. To implement the method CreateCommandReader, you should fill the code creating CommandReader instance by appServer instance.

About CommandReader:
public interface ICommandReader<TCommandInfo>
        where TCommandInfo : ICommandInfo
    IAppServer AppServer { get; }
    TCommandInfo FindCommandInfo(IAppSession session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left);
    int LeftBufferSize { get; }
    ICommandReader<TCommandInfo> NextCommandReader { get; }

Above code is the interface ICommandReader you need to implement for command reader.

IAppServer AppServer {get;}:
: Returns the current CommandReader running on the AppServer;

int LeftBufferSize {get;}:
: Returns the current CommandReader the size of the data cache;

ICommandReader <TCommandInfo> NextCommandReader {get;}:
: Specify when the next round of data received by using CommandReader instance, without using the current CommandReader instance;

TCommandInfo FindCommandInfo (IAppSession session, byte [] readBuffer, int offset, int length, bool isReusableBuffer, out int left);
: FindCommandInfo is CommandReader to achieve the core method, the parameters are explained as follows:

# IAppSession session - the current AppSession

# byte [] readBuffer - the receive buffer

# int offset - the displacement of the data received in the receive buffer

# int length - The length of the data received

# bool isReusableBuffer - indicates that the receive buffer will be used in the next round receiving, and if so, your code can not be directly used it for CommandReader readerBuffer the buffer cache

# out int left - the length of the data which has not been parsed yet by the reader

# Return value TCommandInfo - return the found protocol command data, if not found, returns null

The usage of the CustomProtocol

Using a custom protocol is very simple. In your AppServer, pass your custom protocol instance into the base constructor method:
public class CustomProtocolServer : AppServer<CustomProtocolSession, BinaryCommandInfo>
    public CustomProtocolServer() : base(new MyCustomProtocol())

Last edited Jun 30, 2011 at 6:07 AM by kerryjiang, version 19


No comments yet.