This section describes the four message boundaries of the open-source. net Communication Framework NetworkComms framework. netnetworkcomms

Source: Internet
Author: User

This section describes the four message boundaries of the open-source. net Communication Framework NetworkComms framework. netnetworkcomms

Http://www.cnblogs.com/csdev

Networkcomms is a C # TCP/UDP communication framework written by the British. It was previously charged by the British. Currently, the author has open-source License: Apache License v2.

Open Source Address: https://github.com/MarcFletcher/NetworkComms.Net

First, there is a message boundary problem during TCP communication, that is, how to handle the packet sticking problem. The networkcomms framework itself has a built-in solution to this problem, when using the framework, we can directly communicate with each other without worrying about message boundary issues.

Next, let's analyze how networkcomms handles message boundary issues.

Solution to TCP unprotected message Boundary
There are generally three solutions to this problem:

(1) Send a fixed-length message

(2) send the message size together with the message size.

(3) use special tags to differentiate message intervals

The NetworkComms communication framework uses the second one:The size of the message is sent together with the size of the message.

 

Let's take a look at this process.

The client sends a class to the server.

The Code is as follows:

User user1 = new User (); user1.UserID = "10000"; user1.Name = ""; connection. SendObject ("Message Type", user1 );

Then the networkcomms framework starts to send this class

In the ConnectionSendClose. cs File

Determines whether the class to be sent is of the Packet type. If SendPacket is used for sending. If not, convert to Packet type before sending

Public void SendObject <sendObjectType> (string sendingPacketType, sendObjectType objectToSend, SendReceiveOptions options, out long packetSequenceNumber ){
// Determine whether the sent class is Packet type. Packet objectToSendAsPacket = objectToSend as Packet; if (objectToSendAsPacket = null ){
// If not, convert it to Packet type and then send using (Packet sendPacket = new Packet (sendingPacketType, objectToSend, options) SendPacket <sendObjectType> (sendPacket, out packetSequenceNumber );} else {if (objectToSendAsPacket. packetHeader. packetType! = SendingPacketType) throw new ArgumentException ("Unable to send object of type Packet if the PacketHeader. packetType and sendingPacketType do not match. "); SendPacket <sendObjectType> (objectToSendAsPacket, out packetSequenceNumber );}}

In the above Code

Packet sendPacket = new Packet (sendingPacketType, objectToSend, options)
Convert the User class to Packet.

To analyze the Packet class

 public Packet(string sendingPacketTypeStr, string requestReturnPacketTypeStr, object payloadObject, SendReceiveOptions options)        {            Constructor(sendingPacketTypeStr, requestReturnPacketTypeStr, payloadObject, options, false);        }

 

The data class to be sent (User data this time), which is assigned to Packet as a parameter.

Packet class, which is internally processed by some classes. User-class data is converted and stored in the PacketData attribute, that is, PacketData.

The SerialiseHeader (SendReceiveOptions options) in the Packet class returns the serialized data of the Packet header.

The SendPacketSpecific method in the Connection class sends packet header data and packet body data.

Let's take a look at the methods for serializing headers in Packet.

/// <Inheritdoc/> public byte [] SerialiseHeader (SendReceiveOptions options) {if (options = null) throw new ArgumentNullException ("options", "Provided SendReceiveOptions cannot be null. "); // We need to start of by serialising the header // serialize the header to a binary array byte [] serialisedHeader; using (StreamTools. streamSendWrapper sendWrapper = options. dataSerializer. serialiseDataObject (_ packetHeader, options. dataProcessors, null) serialisedHeader = sendWrapper. threadSafeStream. toArray (1); if (serialisedHeader. length-1> byte. maxValue) throw new SerialisationException ("Unable to send packet as header size is larger than Byte. maxValue. try to raise the length of provided packetTypeStr or turning off checkSum validation. "); // The first byte now specifies the header size (allows for variable header size) // The value of the first byte of The binary data converted from the header, set the length of the header to serialisedHeader [0] = (byte) (serialisedHeader. length-1); if (serialisedHeader = null) throw new SerialisationException ("Serialised header bytes shoshould never be null. "); return serialisedHeader ;}

 

Http://www.cnblogs.com/csdev

 

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.