Describes the four message boundaries of the open source. NET Communication Framework NETWORKCOMMS framework

Source: Internet
Author: User

Original URL: Http://www.cnblogs.com/csdev

Networkcomms is a C # language written by TCP/UDP communication framework The author is the British used to charge the current author has open source license is: Apache License v2

Open Source address is: Https://github.com/MarcFletcher/NetworkComms.Net

First, the use of TCP communication when there is a message boundary problem, that is, how to deal with the problem of sticky packets, the NETWORKCOMMS framework itself has a built-in solution to this problem, we use the framework of the direct data communication, we do not need to care about the message boundary problem.

Let's examine how Networkcomms handles the message boundary problem.

Resolving TCP non-protected message boundaries
There are generally 3 solutions for this problem:

(1) Send a fixed-length message

(2) Send the message size to the message piece

(3) Use special tags to differentiate message intervals

The NETWORKCOMMS communication framework uses the second type of message that is sent with the size of the message

Take a look at this process

The client sends a class to the server side

The code is as follows:

  User  user1=new User ();  User1. Userid= "10000";  User1. Name= "Tianya at this time";       Connection. SendObject ("Message type", User1);

Then the NETWORKCOMMS framework starts sending this class

In the ConnectionSendClose.cs file

Determines whether the sent class is a packet type, if it is sent using Sendpacket. If not, convert to packet type and send again

    Public voidSendobject<sendobjecttype> (stringSendingpackettype, Sendobjecttype objecttosend, sendreceiveoptions options, out LongPacketsequencenumber) {
determines whether the sent class is Packet type Packet objecttosendaspacket= Objecttosend asPacket; if(Objecttosendaspacket = =NULL) {
//If not, convert to packet type and send again using(Packet Sendpacket =NewPacket (Sendingpackettype, Objecttosend, Options)) Sendpacket<sendObjectType> (Sendpacket, outpacketsequencenumber); } Else { if(ObjectToSendAsPacket.PacketHeader.PacketType! =Sendingpackettype)Throw NewArgumentException ("Unable to send object of type Packet if the Packetheader.packettype and Sendingpackettype does not match."); Sendpacket<sendObjectType> (Objecttosendaspacket, outpacketsequencenumber); } }

The above code, through this sentence

Packet sendpacket = new Packet (Sendingpackettype, Objecttosend, Options)
Convert the user class to be sent to the packet class

To analyze the packet class

 Public Packet (stringstringobject  payloadobject, sendreceiveoptions options)        {             false);        }

The data class to be sent (this time the user type data) is assigned a value to packet as a parameter.

Packet class, after some kind of internal processing, the user class data, and finally through the conversion of stored in the Packetdata attribute, that is, the package body data.

Serialiseheader (sendreceiveoptions options) in the packet class returns the serialized data of the Baotou (Packetheader)

The Sendpacketspecific method in the connection class sends the header data first, and then the package body data.

Let's take a look at the method of serializing Baotou in packet

   /// <inheritdoc/>         Public byte[] Serialiseheader (sendreceiveoptions options) {if(Options = =NULL)Throw NewArgumentNullException ("Options","provided sendreceiveoptions cannot is null."); //We need to start of by serialising the header//serialize the Baotou into 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 NewSerialisationexception ("unable to send packet as header size is larger than byte.maxvalue. Try reducing the length of provided packettypestr or turning off checkSum validation."); //The first byte now specifies the header size (allows for variable header size)//Baotou converted into binary data, the first byte value, set to the length of the Baotouserialisedheader[0] = (byte) (Serialisedheader.length-1); if(Serialisedheader = =NULL)                Throw NewSerialisationexception ("serialised Header bytes should never be null."); returnSerialisedheader; }

Original URL: Http://www.cnblogs.com/csdev

Describes the four message boundaries of the open source. NET Communication Framework NETWORKCOMMS framework

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.