Command Line Protocol

Application layer protocol is the foundation of socket communications, and the command line protocol (CommandLineProtocol) is built in SuperSocket directly. Command Line Protocol is a widely used protocols, such as Telnet, SMTP, POP3 and FTP protocols, etc. If you do not have a custom protocol, then SuperSocket will use command line protocol by default, which simplifies the development of this kind of protocols.

StringCommandInfo of CommandLineProtocol

CommandLineProtocol's CommandInfo is type of StringCommandInfo:
public class StringCommandInfo
{ 
    string Key {get; set;}

    string Data {get; set;}

    string[] Parameters {get; set;}
  } 

StringCommandInfo has three attributes, Key is the name of the commandinfo is used to associate the processing Command; Data is part of a command parameter; Parameters is a command parameter list.

Because CommandLineProtocol default CommandParser uses a space to split command name and parameters,
So when the client sends:
"LOGIN kerry 123456" + newline
To the server, the server instance will receive a StringCommandInfo cmdInfo, each instance of attribute values ​​as follows:
cmdIndo.Key = "LOGIN";
cmdInfo.Data = "kerry 123456";
cmdInfo.Parameters = kerry", "123456

Custom CommandParser

In some protocol, the separator of commands and parameters, parameters and parameters are not a space, but the other characters instead. In this case you need to create your own CommandParser for CommandLineProtocol.

SuperSocket built-in BasicCommandParser can set the separator of command name and parameters, parameters and parameters. For example, your protocol with ":" between command name and parameters and " " between parameters and parameters, like "LOGIN: kerry, 12345". To resolve this kind of command, you can use ":" and "," as constructor parameters to initialize BasicCommandParser and then assign to CommandLineProtocol instance, like the code below:
public class YourServer : AppServer<YourSession>
{
    public YourServer()
        : base(new CommandLineProtocol(new BasicCommandParser(":", ",")))
    { 

    }
}

Some protocols won't be so simple to work with two separators, in this case we need to completely custom CommandParser. Like such a command "CMD: ECHO AabSfght5656D5Cfa5 ==". The command name "CMD:" are followed by the parameters with a space splitter, but the parameter part are encoded in base64. So you need to implement the interface ICommandParser, like the code below:
/// <summary>
/// CMD:ECHO AabSfght5656D5Cfa5==
/// </summary>
public class CustomCommandParser : ICommandParser
{
    #region ICommandParser Members 

    public StringCommandInfo ParseCommand(string command)
    {
        if(!command.StartsWith("CMD:"))
            return null;

        command = command.Substring(4);
        string[] data = command.Split(' ');
        return new StringCommandInfo(data[0], data[1],
            Encoding.ASCII.GetString(Convert.FromBase64String(data[1])).Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
    }

    #endregion
}

public class YourServer : AppServer<YourSession>
{
    public YourServer()
        : base(new CommandLineProtocol(new CustomCommandParser()))
    { 

    }
}

Last edited Jun 16, 2011 at 6:08 AM by kerryjiang, version 11

Comments

No comments yet.