Scalable SockBase design and implementation (2)

zhaozj2021-02-16  90

table of Contents

Summary

Using HashTable to establish a message mapping table

Message mapping design and implementation

Message map class in SockBase

Summary

In the previous article << Scalable SockBase Design and Implementation (1) >> Our message mapping table is built by simple HashTable table. This is relatively simple, and it is not convenient to expand. And HashTable Some of the features are unnecessary. So here, we use the custom message mapping class (set class CommandLerList and message command / processing function class commandhandler.) To establish a message mapping table.

Using HashTable to establish a message mapping table

Our message mapping table, requires a message command to correspond to one / or more processing functions. At the same time, for the storage of the entire message mapping table, it is necessary to easily add / delete the entries. And can be more convenient The processing function corresponding to the message corresponding to the message is used in the form.

Hashtable is a key / value set of .NET Framework. When a project is joined, the HashTable is called the GetHashCode method of the key value. Since all classes are inherited from System.Objec, the method is called. The hash code of the class can be determined and stored in this code. From the perspective of performance, because the key value search is limited to the key value having the same hash code, HashTable can quickly retrieve any element from the collection, thereby reducing the number of key values ​​that must be checked to find the match. However, because each object-key value inserted into the collection must generate the corresponding hash code, the cost of the project insert is a bit higher. Therefore, HashTable is mainly used in conjunction with a large number of relatively static data in response to any key value.

Since all of our messages are strings, the comparison of the object is made as long as the string matches, it is not necessary to get the HashCode by HashTable. At the same time, because the HashTable is only the key / value pair, directly use cannot bring more Big scalability. So we choose to give up directly using HashTable. Use our custom class.

Message mapping design and implementation

From the above instructions, we have to customize the message mapping class. First define each message in each message mapping table and the class thereof.

Since the message and the actual processing function are not in the same class, we should use the .NET provided by the .NET provided. Because the messages sent / received by Sockets are strings, The parameters of the processing function are also received, so the parameters of the function corresponding to the delegate are String types. And for some special circumstances, such as receiving a message can trigger multiple processing functions. We define it For events. Definitions are as follows:

Public Delegate Void CommandeventHandler (Object Sender, CommandHandlerargs E);

Public Class CommandHandlerarggs: System.EventArgs

{

PRIVATE STRING M_CMDTEXT;

Public String CommandText

{

Get {return m_cmdtext;}

Set {m_cmdtext = value;

}

Public CommandHandlerargs (String cmdText)

{

m_cmdtext = cmdtext;

}

}

In this CommandHandlerargs, m_cmdtext is the parameter of the message.

You can now define our CommandHandler class.

Public Class CommandHandler

{

PRIVATE STRING M_CMDPREFIX;

Private Event CommandeventHandler M_Execmd;

Public String CommandPrefix

{

Get {return m_cmdprefix;}

Set {m_cmdprefix = value;

}

Public Event Commandeventhandler CommandEvent {

Add {m_execmd = value;

REMOVE {m_execmd- = value;

}

Public CommandHandler (String cmdprefix)

{

m_cmdprefix = cmdprefix;

}

Public void Execommand (String cmdtext)

{

CommandHandlerargs E = New CommandHandlerargs (cmdtext);

IF (m_execmd! = NULL)

m_execmd (this, e);

}

}

In CommandHandler, we define an event to set the message processing function. At the same time, in order to match the message received and the message in the message mapping table, an m_cmdprefix is ​​added to set the message command. And one is one Directly run function execummand, pass the message to the message processing function for the Socket by the parameter cmdtext.

Now there is only the implementation of the message mapping table. Since it is a table, where the store is stored. For the convenience, we inherit directly from CollectionBase. The code is as follows:

Public Class CommandHandlerList: CollectionBase

{

Public CommandHandler Add (CommandHandler Cmdhandler)

{

List.add (cmdhandler);

Return cmdhandler;

}

Public Void Removeat (int index)

{

List.Removeat (Index);

}

Public Void Remove (CommandHandler Cmdhandler)

{

List.remove (cmdhandler);

}

Public int LENGTH

{

Get {return this.count;}

}

Public void clear ()

{

List.clear ();

}

}

In fact, this COMMANDHANDLERLIST class is very simple, which is directly used to store each CommandHandler.

Ok, until now, the structure of the basic message mapping table is complete. Now it is created and runs our message mapping table.

Message mapping table Used in SockBase

First, change the original use of the lux using our custom to CommandLostlerList using our customizes.

// The command processing set to be processed

Protected commandhandlerlist m_commandhandlerlist;

Modify the message distribution function CMDHandler query the code called the message, the modified code is as follows:

Foreach (CommandHandler cmd in m_commandhandlerlist)

{

IF (cmd.commandprefix == cmdlist [0])

{

Cmd.exeCommand (cmdtext);

Break;

}

}

Finally, the part of the initialization message mapping table. The LoadCommandLIST function in class client_listenthread, the modified code is as follows:

Public void loadingcommandhandlerlist ()

{

m_commandhandlerlist.add (New CommandHandler ("getfile") .commandevent = new commandeventhandler (getFileHandler);

M_commandhandlerList.Add (New CommandHandler ("Fileok")). CommandEvent = New CommandeventHandler (FileokHandler);

}

At this time, our message mapping table that puts the original HashTable to our custom class. Summary

Since Hashtalbe is not very suitable for our custom message mapping table, we have replaced our own CommandHandler and CommandHandlerList.

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

New Post(0)