The application layer protocol is the basis for socket communication, while supersocket directly has the commandlineprotocol ). The command line protocol is a widely used protocol. For example, common telnet, SMTP, POP3, and FTP protocols are all based on the command line protocol. If you do not have a custom protocol, the default supersocket protocol is the command line protocol, which greatly simplifies the development based on this protocol.
Stringcommandinfo of the command line protocol
Commandinfo of the command line protocol is of the stringcommandinfo type:
Public ClassStringcommandinfo
{
StringKey {Get; set ;}
StringData {Get; set ;}
String[] Parameters {Get; set ;}
}
Stringcommandinfo has three attributes: key is the command name of the command line, which is a string used to associate the command; data is the parameter part of a command; parameters is the parameter list of a command.
The default commandlineprotocol commandparser uses spaces to differentiate command names and parameters,
So when the client sends
"Login Kerry 123456" + line feed
When arriving at the server side, the server will receive a stringcommandinfo instance, whose attribute values are as follows:
Cmdindo. Key = "login ";
Raise info. Data = "kerry123456 ";
Raise info. Parameters = {"Kerry", "123456 "}
Custom commandparser
In some protocols, the delimiter between commands and parameters is not a space, but other characters. In this case, you need to reset commandlineprotocol commandparser.
The basiccommandparser built in supersocket can directly set the command name and parameter, the delimiter between the parameter and the parameter. For example, your protocol command names and parameters are separated by ":", and parameters are separated by ",", for example, "login: Kerry, 12345 ". To implement such Command Parsing, you only need to use ":" and "," as the parameter to instantiate basiccommandparser and then assign it to the commandlineprotocol instance,CodeAs follows:
Public ClassYourserver: appserver <yoursession>
{
PublicYourserver ()
:Base(NewCommandlineprotocol (NewBasiccommandparser (":",",")))
{
}
}
Some protocols cannot be simply parsed using two delimiters, so we need to completely customize commandparser. If there is such a command "cmd: Echo aabsfght56d5cfa5 =", the command name is "cmd:", and the parameters are separated by spaces, however, the parameter is a string that is encoded by base64 after multiple parameters are connected by space. In this way, you need to implement the icommandparser class by yourself. The Code is as follows:
/// <Summary>
// Cmd: Echo aabsfght5656d5cfa5 =
/// </Summary>
Public ClassCustomcommandparser: icommandparser
{
# RegionIcommandparser members
PublicStringcommandinfo parsecommand (StringCommand)
{
If(! Command. startswith ("Cmd :"))
Return Null;
Command = command. substring (4 );
String[] DATA = command. Split ('');
Return NewStringcommandinfo (data [0], data [1],
Encoding. ASCII. getstring (convert. frombase64string (data [1]). Split (New String[] {""}, Stringsplitoptions. removeemptyentries ));
}
# Endregion
}
Public ClassYourserver: appserver <yoursession>
{
PublicYourserver ()
:Base(NewCommandlineprotocol (NewCustomcommandparser ()))
{
}
}