Message processor in networkComms communication framework and networkcomms framework

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.