The ESFramework 4.0 Quick Start series describes how to use the Rapid engine-RapidServerEngine and RapidPassiveEngine. In fact, you can think of these two engines as two shells, and the network engine of ESFramework is packaged internally, ESFramework supports many network engines (Client/Server, binary/text, TCP/UDP ), rapidServerEngine and RapidPassiveEngine use server and client Engines Based on TCP and binary protocols. The purpose of these two shells is to get started with ESFramework application development very quickly without understanding the internal mechanism of ESFramework. However, there are also some restrictions, that is, the Rapid engine only uses the most basic features of ESFramework, and there are many important features that cannot be reflected through the Rapid engine. Of course, it can be fully reflected through complicated encapsulation, but in that case, the Rapid engine is not so easy to get started, and Rapid is not "Rapid.
Even so, the Rapid engine is sufficient for some simple applications. Here, I will add some information that needs to be learned to better use the Rapid engine.
1. Symmetry
The components in the ESPlus. Application namespace are used to help us quickly develop ESFramework applications and provide support for both the server and client. For example, the support for the custom information mentioned above is what ESPlus. Application. CustomizeInfo does. It contains two sub-namespaces: ESPlus. Application. CustomizeInfo.ServerAnd ESPlus. Application. CustomizeInfo.PassiveFor the server and client respectively.
You may have noticed that ESPlus. Application. xxxxxx. Server and ESPlus. Application. xxxxxx. Passive usually appear in pairs to solve the xxxxxx problem and support the Server and client. In addition, this pair relationship does not produce incorrect matching. For example, the server calls ESPlus. application. basic. the Broadcast method of IBasicController under Server must be ESPlus. application. basic. the HandleBroadcastFromServer method of the IBasicBusinessHandler interface under Passive is used for processing. application. customizeInfo. the HandleBroadcastFromServer method of the ICustomizeInfoBusinessHandler interface in the Passive space (used to process intra-group broadcasts.
2. The Rapid engine only uses two namespaces under ESPlus. Application.
The Rapid engine mainly uses components in the ESPlus. Application. Basic and ESPlus. Application. CustomizeInfo namespaces. If you want to use ESPlus. components in other namespaces under Application (such as ESPlus. application. fileTransfering namespace is used for file transfer), so you cannot use the Rapid engine shell. You must use the real network engines listed in ESFramework 4.0 overview.
3. Simple friend management and group management in the Rapid Engine
The Initialize method of IRapidServerEngine has a slightly complicated overload, that is, the two parameters IFriendsManager and IGroupManager are accepted. I will explain the functions of these two parameters a little.
(1) IFriendsManager is for ESPlus. application. basic. when a user is online or offline, the user's friends must be notified (through ESPlus. application. basic. passive. IBasicBusinessHandler Interface), the server knows from where the user has friends, that is, the IFriendsManager interface:
Public interface IFriendsManager
{
List <string> GetFriendList (string ownerID );}
If the parameter is null when the Initialize method of RapidServerEngine is called, RapidServerEngine automatically uses DefaultFriendsManager. This assumes that all online users are friends, any online or offline user will be notified to all other online users.
(2) The IGroupManager interface is for ESPlus. application. customizeInfo. for components in the Server namespace, when the client or Server needs to broadcast messages in a group, the Server needs to obtain members of the group according to the groupID, which is obtained through the IGroupManager interface:
Public interface IGroupManager
{
/// <Summary>
/// Obtain the list of all members of a group.
/// </Summary>
List <string> GetMemberList (string groupID );
}
If the parameter is null when the server-side Rapid engine is initialized, RapidServerEngine automatically uses EmptyGroupManager. The GetMemberList method always returns an empty list. If you need to use the group Broadcast Function, You can implement this simple interface and inject it to the server's Rapid engine.
(3) Some friends asked how to add friends, create a group, or join a group? These are all determined by your application, and ESFramework has no restrictions. For example, ESFramework uses the IGroupManager interface only for built-inGroup broadcast messageTherefore, ESFramework defines IGroupManager very easily. As long as your application implements the IGroupManager interface, you can directly use the built-in broadcast function of the framework.
Well, now let's assume that your project needs the function of adding QQ to a group. What should I do? In fact, you can design it as follows: Define a custom information type (such as 1300) for a request to join a group. The information content includes the number to join the group; define the type of the reply information (for example, 1301) to be added to the group. The information contains the result of successful or failed addition. When you want to join a group, the client sends a message of 1300 type to the server. After the server receives and processes the message (for example, you can save the group relationship to the DB ), A message of Type 1301 is returned to the client. After receiving a message of Type 1301, the client can notify the user on the UI. For specific examples, see this article. In this way, the project requirements for joining the group are met. Similarly, you can use similar methods to create groups, add friends, and delete friends, as long as you use the custom information function provided by ESPlus. If you are not familiar with the use of custom information, you can refer to ESFramework 4.0 Quick Start-how to use custom messages ?.
For a more comprehensive description of friends and groups in ESFramework, see ESFramework 4.0 advanced (11)-friends and groups.
4. Length of UserID
In ESFramework 4.0 (01), we introduced the default message header implementation provided by ESPlus, and the Rapid engine uses the binary-based message header StreamMessageHeader provided by ESPlus, the default length of this message header is 36 bytes, and the maximum length of the allowed UserID is 11 bytes. However, what if the length of UserID required in your system exceeds 11 bytes? We can call the SetMaxLengthOfUserID static method of StreamMessageHeader to set the maximum length of UserID allowed by ESFramework:
/// <Summary>
/// Set the maximum length of UserID (including GroupID) (up to 255 ). Note that the client and server must be configured in a unified manner.
/// </Summary>
Public static void SetMaxLengthOfUserID (byte maxLen)
Note: You must call the SetMaxLengthOfUserID method before executing the Initialize method of the Rapid engine. In addition, the client and the server must adopt the same settings. Otherwise, communication exceptions between the server and the client will occur. If your client uses Silverlight, this is also true when ESFramework. SL is used.
(1) The server and desktop client must call the SetMaxLengthOfUserID method of ESPlus. Core. StreamMessageHeader to set the parameters.
(2) For Silverlight clients, call the SetMaxLengthOfUserID method of ESFramework. SL. StreamMessageHeader to set the parameters.
In ESFramework, group IDs (the groupID mentioned above) also use the same rules as userids.
It should be noted that, when the project requirements can be met, the maximum length of UserID should be shorter as much as possible, so that the message header can be shorter, this avoids wasting unnecessary bandwidth. This is especially critical for high-performance, high-concurrency applications.
5. Maximum message length
The maximum message length set by default in the Rapid engine is 1 MB (1024*1024), and this length also contains the length of the above message header. If the length of a single message to be sent by your application exceeds 1 MB, the message is considered malicious by ESFramework. ESFramework discards the message and closes the connection.
We recommend that you minimize the number of messages sent to meet the same project requirements. This not only saves bandwidth, but also improves concurrency performance. If the length of information required by an application exceeds 1 MB, you can use the internal Core Engine interface exposed by the Rapid engine to set the maximum message length allowed:
(1) server: The TcpServerEngine kernel engine exposed by RapidServerEngine. You can set the value of its MaxMessageSize attribute.
(2) client: the TcpPassiveEngine kernel engine exposed by RapidPassiveEngine. You can also set the value of its MaxMessageSize attribute.
ESFramework 4.0 Overview