赞
踩
本文记录一个问题的理解过程,试验条件:
server端基于open62541,
// server.c #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; }
Client代码是基于asyncua,
当删除subscription时,在client端会收到一个warning,
在open62541里对这个错误进行搜索,即UA_STATUSCODE_BADNOSUBSCRIPTION,找到相关代码,如下,
void UA_Session_detachSubscription(UA_Server *server, UA_Session *session, UA_Subscription *sub, UA_Boolean releasePublishResponses) { /* Detach from the session */ sub->session = NULL; TAILQ_REMOVE(&session->subscriptions, sub, sessionListEntry); /* Reduce the count */ UA_assert(session->subscriptionsSize > 0); session->subscriptionsSize--; /* Reduce the number of outstanding retransmissions */ session->totalRetransmissionQueueSize -= sub->retransmissionQueueSize; /* Send remaining publish responses if the last subscription was removed */ if(!releasePublishResponses || !TAILQ_EMPTY(&session->subscriptions)) return; UA_PublishResponseEntry *pre; while((pre = UA_Session_dequeuePublishReq(session))) { UA_PublishResponse *response = &pre->response; response->responseHeader.serviceResult = UA_STATUSCODE_BADNOSUBSCRIPTION; response->responseHeader.timestamp = UA_DateTime_now(); sendResponse(server, session, session->header.channel, pre->requestId, (UA_Response*)response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]); UA_PublishResponse_clear(response); UA_free(pre); } }
具体原理如下,当client删除subscription时,会发送删除请求,server收到请求后会调用UA_Session_detachSubscription(),代码里会判断这个subscription是否是最后一个subscription,如果是那么就会把剩余的response发送出去,并把这个response的状态置为UA_STATUSCODE_BADNOSUBSCRIPTION
可以看出这个是正常的操作,本文代码里只有一个subscription,所以删除时就会把剩余的response都发出去;但是如果有多个subscription,此时只删除一个subscription就不会收到这个warning。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。