ua_server_async.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2019 (c) Fraunhofer IOSB (Author: Klaus Schick)
  6. * based on
  7. * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  8. * Copyright 2014, 2017 (c) Florian Palm
  9. * Copyright 2015 (c) Sten Grüner
  10. * Copyright 2015 (c) Oleksiy Vasylyev
  11. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  12. */
  13. #ifndef UA_SERVER_ASYNC_H_
  14. #define UA_SERVER_ASYNC_H_
  15. #include <open62541/server.h>
  16. #include "open62541_queue.h"
  17. #include "ua_util_internal.h"
  18. _UA_BEGIN_DECLS
  19. #if UA_MULTITHREADING >= 100
  20. struct UA_AsyncResponse;
  21. typedef struct UA_AsyncResponse UA_AsyncResponse;
  22. /* A single operation (of a larger request) */
  23. typedef struct UA_AsyncOperation {
  24. TAILQ_ENTRY(UA_AsyncOperation) pointers;
  25. UA_CallMethodRequest request;
  26. UA_CallMethodResult response;
  27. size_t index; /* Index of the operation in the array of ops in
  28. * request/response */
  29. UA_AsyncResponse *parent; /* Always non-NULL. The parent is only removed
  30. * when its operations are removed */
  31. } UA_AsyncOperation;
  32. struct UA_AsyncResponse {
  33. TAILQ_ENTRY(UA_AsyncResponse) pointers; /* Insert new at the end */
  34. UA_UInt32 requestId;
  35. UA_NodeId sessionId;
  36. UA_UInt32 requestHandle;
  37. UA_DateTime timeout;
  38. UA_AsyncOperationType operationType;
  39. union {
  40. UA_CallResponse callResponse;
  41. UA_ReadResponse readResponse;
  42. UA_WriteResponse writeResponse;
  43. } response;
  44. UA_UInt32 opCountdown; /* Counter for outstanding operations. The AR can
  45. * only be deleted when all have returned. */
  46. };
  47. typedef TAILQ_HEAD(UA_AsyncOperationQueue, UA_AsyncOperation) UA_AsyncOperationQueue;
  48. typedef struct {
  49. /* Requests / Responses */
  50. TAILQ_HEAD(, UA_AsyncResponse) asyncResponses;
  51. size_t asyncResponsesCount;
  52. /* Operations for the workers. The queues are all FIFO: Put in at the tail,
  53. * take out at the head.*/
  54. UA_LOCK_TYPE(queueLock)
  55. UA_AsyncOperationQueue newQueue; /* New operations for the workers */
  56. UA_AsyncOperationQueue dispatchedQueue; /* Operations taken by a worker. When a result is
  57. * returned, we search for the op here to see if it
  58. * is still "alive" (not timed out). */
  59. UA_AsyncOperationQueue resultQueue; /* Results to be integrated */
  60. size_t opsCount; /* How many operations are transient (in one of the three queues)? */
  61. UA_UInt64 checkTimeoutCallbackId; /* Registered repeated callbacks */
  62. } UA_AsyncManager;
  63. void UA_AsyncManager_init(UA_AsyncManager *am, UA_Server *server);
  64. void UA_AsyncManager_clear(UA_AsyncManager *am, UA_Server *server);
  65. UA_StatusCode
  66. UA_AsyncManager_createAsyncResponse(UA_AsyncManager *am, UA_Server *server,
  67. const UA_NodeId *sessionId,
  68. const UA_UInt32 requestId,
  69. const UA_UInt32 requestHandle,
  70. const UA_AsyncOperationType operationType,
  71. UA_AsyncResponse **outAr);
  72. /* Only remove the AsyncResponse when the operation count is zero */
  73. void
  74. UA_AsyncManager_removeAsyncResponse(UA_AsyncManager *am, UA_AsyncResponse *ar);
  75. UA_StatusCode
  76. UA_AsyncManager_createAsyncOp(UA_AsyncManager *am, UA_Server *server,
  77. UA_AsyncResponse *ar, size_t opIndex,
  78. const UA_CallMethodRequest *opRequest);
  79. typedef void (*UA_AsyncServiceOperation)(UA_Server *server, UA_Session *session,
  80. UA_UInt32 requestId, UA_UInt32 requestHandle,
  81. size_t opIndex, const void *requestOperation,
  82. void *responseOperation, UA_AsyncResponse **ar);
  83. /* Creates an AsyncResponse in-situ when an async operation is encountered. If
  84. * that is the case, the sync responses are moved to the AsyncResponse. */
  85. UA_StatusCode
  86. UA_Server_processServiceOperationsAsync(UA_Server *server, UA_Session *session,
  87. UA_UInt32 requestId, UA_UInt32 requestHandle,
  88. UA_AsyncServiceOperation operationCallback,
  89. const size_t *requestOperations,
  90. const UA_DataType *requestOperationsType,
  91. size_t *responseOperations,
  92. const UA_DataType *responseOperationsType,
  93. UA_AsyncResponse **ar)
  94. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  95. #endif /* UA_MULTITHREADING >= 100 */
  96. _UA_END_DECLS
  97. #endif /* UA_SERVER_ASYNC_H_ */