ESFramework development manual (08) -- reconnection mode and disconnection

Source: Internet
Author: User
Document directory
  • IgnoreNew Mode
  • ReplaceOld Mode

The Rapid engine provided by ESPlus adopts the following rule: After a TCP connection is established between the client and the server, the first message sent is the login message, after the account and password in the logon message are verified by the server, the server extracts the UserID value from the message and binds it to the corresponding TCP connection. In this way, the server will know the user UserID corresponding to each TCP connection. When we ask the server to send a message to a client, the server will know which TCP connection is used to send the message. For the same server, the TCP connection and UserID are one-to-one, and one TCP connection can only correspond to one UserID. Similarly, one UserID can have one TCP connection at most.

 

1. Two logon Modes

In reality, this often happens: for example, if we use QQ, when we use an account to log on to location A, it has not been deprecated yet, and I used this account to log on to B again. What will happen? QQ uses A new connection to replace the old connection, that is, it notifies the client in the region that it has been congested (for example, the prompt "users with the same name have logged on elsewhere "), for subsequent communication, the server will be connected to the client of B.

This mode adopted by QQ is called the ReplaceOld mode in ESFramework. However, some applications may need to keep the connection of location A and ignore the connection of the new location B. In this case, we can adopt another mode: IgnoreNew. ESFramework defines the two modes through RelogonMode enumeration:

Public enum RelogonMode
{
/// <Summary>
/// Ignore the new connection.
/// </Summary>
IgnoreNew = 0,

/// <Summary>
/// Replace the old connection with a new connection.
/// </Summary>
ReplaceOld
}

We can set the RelogonMode attribute of the User Manager IUserManager to control which re-login mode ESFramework adopts. For example:

  rapidServerEngine.UserManager.RelogonMode = RelogonMode.ReplaceOld;

The default logon mode of RapidServerEngine is ReplaceOld.

2. ESFramework's response to the re-login mode IgnoreNew Mode

If we adopt the IgnoreNew mode, when the server receives a message from a user with the same name from another new connection, ESFramework will trigger the NewConnectionIgnored event of IUserManager to notify the server application:

/// <Summary>
/// If the RelogonMode is IgnoreNew, this event is triggered when a user with the same name is received from a new connection.
/// Note that the new connection is closed only after the event is processed. You can notify the client about the event processing function.
/// </Summary>
Event CbGeneric <string, UserAddress> NewConnectionIgnored;

The first parameter string of the event is the ID of the user with the same name, and the second parameter is the client address of the new connection. ESFramework/ESPlus performs the following actions before and after the event is triggered:

  • Notify the client corresponding to the new connection that a user with the same name is online, and the new connection will be closed. LogonResult. HadLoggedOn is returned for the Intialize method of the client Rapid engine.
  • Close a new connection.

We do not need to repeat the above process when using the Rapid engine. Of course, we can still reserve this event to get a notification that the new connection is ignored on the server.

ReplaceOld Mode

If we adopt the ReplaceOld mode, when the server receives a message from a user with the same name from another new connection, ESFramework will trigger the SomeOneBeingPushedOut event of IUserManager to notify the server application:

/// <Summary>
/// This event is triggered when the RelogonMode is ReplaceOld and a user with the same name is received from another new connection.
/// Note that the old connection will be closed and the old address will be replaced only after the event is processed. You can notify the client of the old connection in the event processing function.
/// </Summary>
Event CbGeneric <UserData> SomeOneBeingPushedOut;

 

That is, the client corresponding to the old connection with the same name is squeezed out, and the event parameter contains the relevant information of the old connection.

Similarly, ESFramework/ESPlus performs the following actions before and after the event is triggered:

  • Notify the client corresponding to the old connection that there is a user connection with the same name. The old connection will be closed. The client will trigger the BeingPushedOut event of IBasicOutter.
  • Close the old connection.

We do not need to repeat the above process when using the Rapid engine. We can still schedule this event to get the notifications of old users being squeezed out on the server.

3. Disconnection and reconnection

When the Rapid Client Engine is disconnected from the TCP connection of the server, it attempts to automatically reconnect to the server until the reconnection is successful.

However, in two cases, the disconnection will not be automatically reconnected:

  • The client is congested and dropped. In this case, if the client is automatically reconnected, the new client with the same name will be congested, and the new client will automatically reconnect ......, so as to fall into the "squeeze down the line-Reconnect-squeeze down the line -.......
  • The client's heartbeat times out and goes offline. The heartbeat timeout usually indicates that the current network latency is very high. In this case, even if the automatic reconnection is successful, the heartbeat timeout may be dropped again. Therefore, automatic reconnection is of little significance in this case.

If the application still needs to connect to the server again after disconnection, You can manually call the initialization method of the Rapid Client Engine.

In other cases, please note. After the Client Engine connects to the server for the first time and successfully logs on to the server, it will record the account and password of the currently logged on user. When the connection is lost, it will automatically log on again with the logged account and password. Therefore, if the user's password is changed during the intermediate process, the reconnection will fail. The value of LogonResponse, The RelogonCompleted parameter of IRapidPassiveEngine, is LogonResult. Failed. We can schedule a RelogonCompleted event to get the reconnect failure notification.

For the server, there is no "reconnection". The client re-connects to the server and the client connects to the server for the first time in the same processing mode. Once the client is disconnected, the server clears the Session and related status data of the client.

Some applications can easily achieve this if the server needs to support the reconnection model. When receiving a notification that the user is offline, our server does not clear the user-related service status data, but changes it to unavailable. After the client reconnects, activate the business data corresponding to the user.

 

Read more ESFramework development manual articles.

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Download the free version of ESFramework and demo source code

 

For any questions about ESFramework, please contact us:

Tel: 027-87638960

Q: 372841921

Mail: esframework@oraycn.com

 

 

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.