Esframework 4.0 (06)-regular message sender

Source: Internet
Author: User

InEsframework 4.0 (04)-driving force: communication engine (II)At the end of this article, we have assembled the communication engine and the entire message skeleton process. As soon as the communication engine receives the message, the framework will run according to the prescribed process. Here, I naturally think of a question: how can I send messages? If the message is not sent, there will be no subsequent skeleton process driving the entire message processing.

 

I. problems encountered when sending messages

When learning engine interfaces in esframework 4.0 (03)-driving force: communication engine (I), We know that various engines provide message sending methods, the sendmessagetoclient method and postmessagetoclient method of iserverengine of the server engine, and the sendmessagetoserver method of ipassiveengine of the Client Engine. We can call them directly to send constructed messages, but two problems may occur:

(1) because our message encryption, compression, and other links are located in messagepipe's messagetransformer component, if you directly call the communication engine to send a message, the message will not participate in these transformations.

(2) Likewise, these directly sent messages will not be monitored by innermessagespy and gatewaymessagespy.

That is to say, when a communication engine is used to send messages directly, messagepipe is bypassed, which destroys the skeleton Process specified by esframework, which may cause some serious consequences. For example, in a project, we mounted an innermessagespy, which was originally expected to intercept all the "plaintext" messages sent out, but the results were a bit out of the net; originally, we expected all the messages to be sent after being encrypted by messagetransformer. As a result, some messages were sent to the network in plain text.

What should I do? Use the regular message sender iregularsender, which means that the message must pass through messagepipe before it is sent to the network by the communication engine.

 

Ii. Regular message sender

We can use the following enhanced skeleton FlowchartThe role of iregularsender in the skeleton process is more clearly displayed:

Iregularsender switches between nakedispatcher and messagepipe, and transmits the message to messagepipe, just as nakedispatcher passes the message returned by the message processor to messagepipe. This ensures that all messages are sent completely the same and comply with the procedures stipulated by esframework.

You may say that if some types of messages are not very important and I don't want to use messagetransformer to encrypt them to save CPU time, what should I do? It's actually easy. First, messgepipe and messagetransformerYes, but you can configure certain message types in messagetransformer. As long as messagetransformer encounters these types of messages, it will not perform any changes and directly return the original message. Esframework. Core.BasemessagetransformerAbstract classes are built into this function for you. You only need to inherit messagetransformer from it. In this way, the problem is solved and the skeleton process is not damaged.

 

Iii. Regular message sender on the server

Esframework. server.IregularsenderAnd its implementation class esframework. server.RegularsenderTo ensure that the messages sent from the service end to the Client follow the esframework skeleton process. Let's take a look.Esframework. server.RegularsenderClass diagram:

 

(1)RegularsenderThere are two public overload methods used to send or deliver messages to N users. The USERID parameter specifies the target object for sending messages. We know that when a message is sent through iserverengine, the parameter it receives is the user address useraddress, soRegularsender must use the User Manager iusermanager to obtain the corresponding useraddress Based on the userid.

(2)RegularsenderDepending on messagepipe, it is easy to understand. It is precisely to let the message pass throughMessagepipeThis ensures the skeleton process.

(3)RegularsenderWe also provide a protected virtual method sendmessagetoforeigner, which implements nothing by default:

Protected   Virtual   Void Sendmessagetoforeigner ( IMessage MSG, String Userid)
{
}

What is the use of this method? It is used in esplatform. When many application servers are used as clusters, an online user who needs to receive the message may be located on another server. In esplatform, we only need to override this method to send messages across application servers. For details, we will discuss esplatform in detail.

Use the followingCodeTo instantiate a server's regular message sender instance:

Iserverengine Serverengine =   ...... ;
Imessagepipe Messagepipe =   ...... ;
Iusermanager Usermanager =   ...... ;
Regularsender Regularsender =   New   Regularsender ();
Regularsender. messagepipe = Messagepipe;
Regularsender. usermanager = Usermanager;
Regularsender. serverengine = Serverengine;
// Send message
IMessage Message =   Null ;
Regularsender. sendmessage (message, " Aa01 " );

For more information about how to construct server engine instances, User Manager instances, and Message Queue instances, see the first several articles in esframework 4.0 advanced series.Article.

 

4. Regular message sender on the client

Esframework. passive.IregularsenderAnd its implementation class esframework. passive.RegularsenderTo ensure that all messages sent from the client to the server comply with the esframework skeleton process. Let's take a look at esframework. passive.RegularsenderClass diagram:

ClientRegularsenderRelatively simpler, messages are always submitted to the server, and even P2P messages are forwarded through the server (about sending P2P messages through P2P channels, we will use messagetransceiver, which will be introduced later ).

Finally, we construct a client's regular message sender instance:

Ipassiveengine Passiveengine = ......;
Imessagepipe Messagepipe = ......;
Regularsender Regularsender =   New   Regularsender ();
Regularsender. messagepipe = Messagepipe;
Regularsender. passiveengine = Passiveengine;
// Send messages to the server
IMessage Message =   Null ;
Regularsender. sendmessage (message, datemediority. Common );

 

Now, the introduction of the regular message sender is complete.UsuallyTo send messages on the server.RegularsenderBut it is rarely used directly during client development,Instead, iserveragent and IMessagetransceiver,Iserveragent is a message synchronization call solution provided by esframework.ImessagetransceiverThe user is blocked from using the P2P channel to send P2P messages directly or through the server. In the next article, we will introduce them one by one. Thank you for your attention. 

 

Esframework 4.0 Overview

What are the advantages of esframework 4.0?

Esframework 4.0 upgrade description (continuous update)

All articles in esframework 4.0 Quick Start

All articles in esframework 4.0 advanced edition Series 

 

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.