The message frame processor communication networkComms

Tcp communications often do friends know, the client sends a message to the server sends data to the client or the server, the receiving end should have a corresponding processor processes the message.

 

There are two concepts we need to be distinguished  

  Message Type  

  Actual data type refers to the type herein before class is not serialized

for example

 A message sent from a client B the two messages may have the same data type, but the message type is not the same

Above that figure further extended look, it may be as shown below:

 

 

A message such as the figure above data class type ClassA server received communication frame is responsible for parsing the data type ClassA  

When using networkcomms frame, each transmitted in a message sender, the message will need to specify the type, a processor must be defined for the type of message at the receiving side.

 

When the receiving end receives the message, select different processors for processing according to the message type.

// The following code message "ReqMsg1", "ReqMsg2", "ReqMsg3" type are transmitted

If there is a return message, as the following code is also required to specify the type of return message "ResMsg1", "ResMsg2", "RewMsg3"

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);
the above code, sends three messages defined, three string type messages are

The receiving end needs then the message type, writing the corresponding processor 3

<1> the processor 3 to interface with the communications 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 = "";
//具体操作
connection.SendObject("ResMsg2", resMsg);
}
catch (Exception ex)
{

}
}


private void IncomingMsg2Handle(PacketHeader header, Connection connection, string msg)
{
try
{
string resMsg = "";
//具体操作
connection.SendObject("ResMsg2", resMsg);
}
catch (Exception ex)
{

}
}


private void IncomingMsg3Handle(PacketHeader header, Connection connection, string msg)
{
try
{
string resMsg = "";
//具体操作
connection.SendObject("ResMsg3", resMsg);
}
catch (Exception ex)
{

}
}

 

www.networkComms.cn editor


---------------------
Author: networkcomms
Source: CSDN
Original: https: //blog.csdn.net/networkcomms/article/details/44218217
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/Jeely/p/10972218.html