I am creating a server in C # and I have a singleton that handles all the logged users in an array is. This array is an array of classes called usersusation.
This user is a method of session that runs on a separate thread that handles the incoming packet of that user.
OK, consider this code:
class UserSession {public user session (TcpClient client) {var thread = new thread (new parameterized headstart (handle com); Thread.start ((object) client);} public blank handle com TcpClient) {TcpClient Client = (TcpClient) tcpClient; NetworkStream stream = client.GetStream (); While (1 == 1) {byte [buffer = new byte [4096]; Stream Read (buffer, 0, buffer length); Int testShort = Convert.ToInt32 (buffer); Switch (ActionEnum testShort) {case ActionEnum.Something: // Here is my problem! // do some more parsing of the message. Do some (some argue arguments); Argument taken from / / byte array brake, Case ActionName. Another Associate: // Before the Equal Brake; // And it is going on and}}}} What will be the best way to handle al? There are more than 80 different ways in that classroom without recurring, there are different embams. (ActionNenam is an enum with the specific action of the current user).
I serialize that buffer, I have made this code fast for an idea for you.
So: you are basically trying to do this so that a numerical code Turn it into an appeal.
There are some things that you can not get, such as this conversion for you is being done automatically. If you had instead sent the names of the ways, then you can see the reflection of the method You could use it, then open it. Therefore, you have to do some manual work to install mapping.
You have to decide that the best way to map numbers is again the best way.
The only way you are using now is the mapping happening within the dispatch cycle (get number, translate, call). The problem is that the mapping code obscures the transmission code.
During setup:
You can use a combination of command patterns and a hash map in this way:
- One for your command Create a simple interface (or use the closure, because you are using C #)
- Make an example of the interface implementing the interface PR, which you want to map
Add it to Hashp. In the sending loop:
- Received number
- See in the table in the hash table
- If found Open the map object, another failure
For a more flexible approach, see the number as a message
command pattern
liability Key Chain
Comments
Post a Comment