Message processor in networkComms communication framework and networkcomms framework
Friends who often do Tcp communication know that the client sends data to the server or server to send messages to the client, and the receiving end must have a corresponding processor to process the messages.
There are two concepts that need to be differentiated.
Message Type
The actual data type here refers to the type of the class before serialization
For example
The client sends message A, message B. The two messages may have the same data type, but the message type is different.
The figure above, further extended, may be as follows:
For example, if the data type of message A is received by the ClassA server, the communication framework parses the data of the ClassA type.
When using the networkcomms framework, you must specify the Message Type for each message sent by the sender, and define a processor for the message type for the receiver.
When receiving a message, the receiver selects different processors for Processing Based on the message type.
// In the following code, "ReqMsg1", "ReqMsg2", and "ReqMsg3" are all sent message types.
If a message is returned, you must specify the returned Message Type "ResMsg1", "ResMsg2", "RewMsg3" in the following code"
string resMsg = connection.SendReceiveObject<string>("ReqMsg1", "ResMsg1", 5000, listBox1.Text); string resMsg = connection.SendReceiveObject<string>("ReqMsg2", "ResMsg2", 5000, listBox2.Text); string resMsg = connection.SendReceiveObject<string>("ReqMsg3", "ResMsg3", 5000, listBox3.Text);
In the code above, it is defined to send three messages, all of which are of the string type.
The receiver needs to write three corresponding processors based on the message type.
<1> interconnect the three processors with the Communication Framework
NetworkComms.AppendGlobalIncomingPacketHandler<string>("ReqMsg1", IncomingMsg1Handle); NetworkComms.AppendGlobalIncomingPacketHandler<string>("ReqMsg2", IncomingMsg2Handle); NetworkComms.AppendGlobalIncomingPacketHandler<string>("ReqMsg3", IncomingMsg3Handle);
<2>
Private void IncomingMsg1Handle (PacketHeader header, Connection connection, string msg) {try {string resMsg = ""; // the specific operation connection. sendObject ("ResMsg2", resMsg);} catch (Exception ex ){}}
Private void IncomingMsg2Handle (PacketHeader header, Connection connection, string msg) {try {string resMsg = ""; // the specific operation connection. sendObject ("ResMsg2", resMsg);} catch (Exception ex ){}}
Private void IncomingMsg3Handle (PacketHeader header, Connection connection, string msg) {try {string resMsg = ""; // the specific operation connection. sendObject ("ResMsg3", resMsg);} catch (Exception ex ){}}
Edit www.networkComms.cn