Scalable SockBase design and implementation (3)

zhaozj2021-02-16  85

Scalable SockBase design and implementation (3)

table of Contents

Summary

Problems brought by embedded messages

Design implementation of custom message command classes

Custom message command class

Summary

In the previous article, we are done directly through the embedded string through the SockBase. For example, "login", "logout", this question is that if you accident, write a wrong one Words, in the compile period, cannot be checked.

Problems brought by embedded messages

Since this is a sockets-based network programming, we lose any message commands through Sockets. In the SockBase implementation, we send a message directly through the send (STRING) function. When the SockBase's Send function, The code we wrote is like this:

Private void getfileHandler (String cmdtext)

{

/ / Check if the file exists

IF ((New FileManager ()). CheckfileExist (cmdtxt)))

{

Send ("OK");

}

Else

{

Send ("failure");

}

}

In this, we can see the news we send is to write strings such as "OK", "failure" directly in the code. Of course, our Send function can only send a string .. In this program, It is no problem.

If, in the later version, we modified the message command body, change the "OK" to "OKEY", "Failure" is replaced with "fail", then we have to pass the search function of the vs.net ID or I have to replace the original characters in the current character. If this program code is not much, it is more than 100, it is very simple. I will get it. But if this program is bigger? Do you have a thousand lines of code? How many places have been used in many places?

At this time, the modification program has become a nightmare. It is very careful in the process of modification. It is slightly inadvertent. The program will appear. This situation is because the message is uncertain, and we In the code, the direct embedded message (in the form of a string), and the logic code is directly mixed, affecting the program's modification, scalability.

Design implementation of custom message command classes

Based on the above analysis, we do not use the direct embedded message command in the form of a string. Reveate it, we use our custom message command class.

Our message command has two parts, part of the message body, and the other is the corresponding parameters corresponding to this message. So, we define the message command class CommandBuilder as follows:

Public Class CommandBuilder

{

PRIVATE STRING M_COMMAND;

PRIVATE STRING M_COMMANDTEXT;

PRIVATE STRING M_ALLCMDTEXT;

// Command prefix, ie command

Public String Command

{

Get {return m_command;}

Set {m_command = value;}

}

// Command content

Public String CommandText

{

Get {return m_commandtext;}

Set {m_commandtext = value;

}

}

Initialize it through the constructor:

// External message sends

Public CommandBuilder (String CommandPrefix, String CommandText)

{

m_command = commandprefix;

m_commandtext = commandText;

}

// Constructor is a complete command

Public CommandBuilder (String AllcommandText)

{

m_allcmdtext = allcommandtext;

}

Since we are sent through SockBase, including messages and parameters, we have to connect the message body and parameters. In order to facilitate the resolution of the message body and parameters, we have to add a separator in the message body and parameters. Based on the same reason. We don't use the direct embedded string, use a member variable inside the CommandBuilder to save the divider .private string m_splitechars = ";";

In this way, if we want to modify, just modify the value of this member, and we can also store the separator in the configuration file. Read directly from the configuration file when you use. This is more flexible. But here, We use the above way.

Ok, the basic data member of the message command class is complete.

Now provide a function that directly outputs the finals that can be sent directly through the SOCKBASE's send function:

Public String Sendermsg

{

get

{

IF (m_allcmdtext! = null && m_allcmdtext! = String.empty)

Return M_AllcmdText;

Else

{

RETURN M_COMMAND M_SPLITECHARS M_COMMANDTEXT;

}

}

}

Now the message command class is basically completed, and now wrap the command. We can write directly into a static member of a class, all the reference to the message directly uses the static members in this class. Similarly, we can also Write into a configuration file. Here, for simple, we use the static members of the class directly. Defining the commandList is as follows:

Public class commandlist

{

// Command string

Public static string getfile = "getfile";

Public static string ok = "ok";

Public static string failure = "failure";

}

Ok, you can start using CommandBuilder and CommandList instead of the inline string.

Custom message command class

Here, we don't have to modify SockBase, just modify the Client_ListentHread class in the previous article. The modified code is as follows:

Private void getfileHandler (String cmdtext)

{

CommandBuilder Cmdbuilder;

/ / Check if the file exists

IF ((New FileManager ()). CheckfileExist (cmdtxt)))

{

Cmdbuilder = New CommandBuilder (CommandList.Getfile, CommandList.ok);

}

Else

{

Cmdbuilder = New CommandBuilder (CommandList.Getfile);

}

Send (cmdbuilder.sendermsg);

}

to sum up

Packaging the string (static) variables such as strings in the program are a good habit, especially in more program development. This is conducive to the multiplexing of the code, and the maintenance of the code modification.

转载请注明原文地址:https://www.9cbs.com/read-12098.html

New Post(0)