Systems that communicate with each other through the network (such as communications between the client and the server) must have " Common Language " This language is the message protocol. Messages that comply with the message protocol can be understood by our system.
We know that the message transmitted over the network is a byte stream, but what we handle in our mainstream object-oriented systems is " Object " , How to convert the byte received from the network " Object " And how " Object " It is converted to a byte stream so that it can be transmitted to other systems through the network. Icontract Interface Definition content:
1 /// <Summary>
2 /// Icontract is used to abstract basic interfaces of communication protocol formats.
3 /// </Summary>
4 Public Interface Icontract
5 {
6 Void Fillmyself ( Byte [] Data, Int Offset ); // Parse a stream as an object
7 Int Getstreamlength ();
8
9 Byte [] Tostream (); // Convert an object to a stream
10 Void Tostream ( Byte [] Buff, Int Offset );
11 }
IcontractThe meaning of each member in the interface is very clear, includingOffsetParameter overloadTostreamMethod (10Can write the converted result stream to the specifiedBuff.
Many of my friends have come to think of it, . Net The most convenient way to convert an object into a byte stream or convert it into an object is to use " Serialization " Yes, if each communication system is based on . Net The platform is created, of course, no problem. In this case, use . Net remoting It may be better (Use Remoting You can ignore the concern about the message protocol because Remoting I have already done everything for you ). However, in more cases, systems that communicate with each other are heterogeneous. For example, the server is . Net Platform, while the client is PDA Upper C ++ WrittenProgramIn this case, . Net Serialization, Remoting We can't do anything about it. We have to do everything manually. You may say that you can use WebService ? Haha, okay, Esframework The main concern is to build based on TCP Or UDP Network System, under what circumstances should we use WebService Or directly at the lower layer TCP/UDP The problem of building the system is still to be considered by the reader :)
If not . Net Of " Serialization " , So what are the common methods to accomplish this? " Byte stream = message object " , That is Icontract. fillmyself And Icontract. tostream How is it implemented? Processing of one byte and one byte. For example, we stipulate in the message protocol that the first four bytes of the received byte stream are Utf8 Encoded string "@" , We use Utf8 Before receiving 4 Characters are parsed as strings. "@" . Next 4 Byte is an integer, so you can use Bitconverter Class to parse these four bytes into an integer, ...... In this way, all the byte streams are parsed to generate a complete " Message object " . The Message Protocol must be defined when you build an application. All message protocols are implemented Icontract Interface. On this basis, the application system can process all messages in a unified manner without understanding the internal details of the Protocol (this is " Polymorphism " So that the system can dynamically load function plug-ins during running.
Esframework Is a framework that does not focus on the specific message protocols in your specific application. Icontract Interface. Esframework The framework can be reused in different applications. If you want Esframework The framework helps us build applications more easily, Esframework We need to extract more commonalities from our application, so it must " Standardization " More things, Icontract Only one of them. NextArticleYou will see Netmessage The introduction of, messages between systems through network interaction can be expressed Netmessage And Netmessage Various processors and hooks in the system Hook Because of this consistency Netmessage Become Esframework One of the core elements Netmessage For more information, see the following article:
EsframeworkIntroduction (2)-Network communication messageNetmessage
Go to: esframework reusable Communication Framework (sequence)