Learn open62541 --- [34] Keep connected

Client and Server communicate through a secure channel (see official document Part 2), called Secure Channel
Insert picture description here
in English. In the default configuration of the Client, the timeout period of the secure channel is 10 minutes. After 10 minutes, it will disconnect from the Server. , As follows,
Insert picture description here
but in most cases we want to be connected all the time, and whether to disconnect is determined by the Client. The intuitive method is to increase the timeout period, but it is not a permanent cure for the symptoms. After checking the source code, I found that there is a better method, which is briefly described below.


Keep connected method

The principle of the method is that the client adds a subscription to the server, and then every 1 second, the client sends a publish request to the server, and the server returns a keep alive response. Detailed operation can refer to this article

The client code is as follows, here for the convenience of testing, the timeout period of the secure channel is changed to 10 seconds,


#include <stdlib.h>
#include "open62541.h"


UA_Boolean running = true;


static void stopHandler(int sign) {
    
    
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
    running = false;
}


int main(void) 
{
    
    
    signal(SIGINT, stopHandler);
    signal(SIGTERM, stopHandler);
    
    UA_Client *client = UA_Client_new();
    UA_ClientConfig *cc = UA_Client_getConfig(client);
    UA_ClientConfig_setDefault(cc);
  
    cc->secureChannelLifeTime = 10000; // 安全通道超时时间改为10s
    
    
    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
    if(retval != UA_STATUSCODE_GOOD) {
    
    
        UA_Client_delete(client);
        return (int)retval;
    }
    
    // ----------------------------- 添加订阅 --------------------------------
    UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
    /* Force the server to send keep alive responses every second to trigger
     * the client to send new publish requests. Requests from the client
     * will make the server to change to the new SecurityToken after renewal.
     */
    request.requestedPublishingInterval = 1000; // publish间隔设置为1s
    request.requestedMaxKeepAliveCount = 1;
    UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
                                                                            NULL, NULL, NULL);

    UA_CreateSubscriptionResponse_clear(&response);
    // -----------------------------------------------------------------------
    
    
    while (running)
    {
    
    
        UA_Client_run_iterate(client, 0);
    }
    
    
    /* Clean up */
    UA_Client_delete(client); /* Disconnects the client internally */
    
    return EXIT_SUCCESS;
}

The server code is as follows,

#include "open62541.h"

#include <signal.h>
#include <stdlib.h>

UA_Boolean running = true;

static void stopHandler(int sign) {
    
    
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
    running = false;
}

int main(void) 
{
    
    
    signal(SIGINT, stopHandler);
    signal(SIGTERM, stopHandler);

    UA_Server *server = UA_Server_new();
    UA_ServerConfig_setDefault(UA_Server_getConfig(server));
    UA_StatusCode retval = UA_Server_run(server, &running);
    
    UA_Server_delete(server);
    
    return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}

I won’t talk about how to organize the project and compile the link. You can refer to the previous series of articles. After the final compilation is ok, run the Server first, and then run the Client.

The print on the client side is as follows,
Insert picture description here
you can see that the client side will re-create the secure channel every about 7.5 seconds, and the server side will always remain connected .

Why is it 7.5 seconds? Because UA_Client_run_iterate() will recreate the secure channel and update the SecurityToken at 0.75*secureChannelLifeTime, this is also specified in the official document (Official Document Part 4).
Insert picture description here


to sum up

This article mainly describes how the Client automatically recreates the secure channel before the timeout time of the secure channel arrives, and maintains the connection with the Server. The solution was also found in the test cases in the source code.

If there is something wrong with the writing, I hope to leave a message to correct it, thank you for reading.

Guess you like

Origin blog.csdn.net/whahu1989/article/details/107991510