ua_connection.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2014 the contributors as stated in the AUTHORS file
  3. *
  4. * This file is part of open62541. open62541 is free software: you can
  5. * redistribute it and/or modify it under the terms of the GNU Lesser General
  6. * Public License, version 3 (as published by the Free Software Foundation) with
  7. * a static linking exception as stated in the LICENSE file provided with
  8. * open62541.
  9. *
  10. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. * details.
  14. */
  15. #ifndef UA_CONNECTION_H_
  16. #define UA_CONNECTION_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include "ua_types.h"
  21. #include "ua_job.h"
  22. typedef enum UA_ConnectionState {
  23. UA_CONNECTION_OPENING, ///< The socket is open, but the HEL/ACK handshake is not done
  24. UA_CONNECTION_ESTABLISHED, ///< The socket is open and the connection configured
  25. UA_CONNECTION_CLOSED, ///< The socket has been closed and the connection will be deleted
  26. } UA_ConnectionState;
  27. typedef struct UA_ConnectionConfig {
  28. UA_UInt32 protocolVersion;
  29. UA_UInt32 sendBufferSize;
  30. UA_UInt32 recvBufferSize;
  31. UA_UInt32 maxMessageSize;
  32. UA_UInt32 maxChunkCount;
  33. } UA_ConnectionConfig;
  34. extern const UA_EXPORT UA_ConnectionConfig UA_ConnectionConfig_standard;
  35. /* Forward declaration */
  36. struct UA_SecureChannel;
  37. typedef struct UA_SecureChannel UA_SecureChannel;
  38. /**
  39. * The connection to a single client (or server). The connection is defined independent of the
  40. * underlying network layer implementation. This allows a plugging-in custom implementations (e.g.
  41. * an embedded TCP stack)
  42. */
  43. struct UA_Connection {
  44. UA_ConnectionState state;
  45. UA_ConnectionConfig localConf;
  46. UA_ConnectionConfig remoteConf;
  47. UA_SecureChannel *channel; ///< The securechannel that is attached to this connection (or null)
  48. UA_Int32 sockfd; ///< Most connectivity solutions run on sockets. Having the socket id here
  49. /// simplifies the design.
  50. void *handle; ///< A pointer to the networklayer
  51. UA_ByteString incompleteMessage; ///< A half-received message (TCP is a streaming protocol) is stored here
  52. /** Get a buffer for sending */
  53. UA_StatusCode (*getSendBuffer)(UA_Connection *connection, size_t length, UA_ByteString *buf);
  54. /** Release the send buffer manually */
  55. void (*releaseSendBuffer)(UA_Connection *connection, UA_ByteString *buf);
  56. /**
  57. * Sends a message over the connection.
  58. * @param connection The connection
  59. * @param buf The message buffer is always released (freed) internally
  60. * @return Returns an error code or UA_STATUSCODE_GOOD.
  61. */
  62. UA_StatusCode (*send)(UA_Connection *connection, UA_ByteString *buf);
  63. /**
  64. * Receive a message from the remote connection
  65. * @param connection The connection
  66. * @param response The response string. It is allocated by the connection and needs to be freed
  67. with connection->releaseBuffer
  68. * @param timeout Timeout of the recv operation in milliseconds
  69. * @return Returns UA_STATUSCODE_BADCOMMUNICATIONERROR if the recv operation can be repeated,
  70. * UA_STATUSCODE_GOOD if it succeeded and UA_STATUSCODE_BADCONNECTIONCLOSED if the
  71. * connection was closed.
  72. */
  73. UA_StatusCode (*recv)(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout);
  74. /** Release the buffer of a received message */
  75. void (*releaseRecvBuffer)(UA_Connection *connection, UA_ByteString *buf);
  76. /** Close the connection */
  77. void (*close)(UA_Connection *connection);
  78. };
  79. void UA_EXPORT UA_Connection_init(UA_Connection *connection);
  80. void UA_EXPORT UA_Connection_deleteMembers(UA_Connection *connection);
  81. void UA_EXPORT UA_Connection_detachSecureChannel(UA_Connection *connection);
  82. void UA_EXPORT UA_Connection_attachSecureChannel(UA_Connection *connection, UA_SecureChannel *channel);
  83. /**
  84. * The network layer may receive chopped up messages since TCP is a streaming
  85. * protocol. Furthermore, the networklayer may operate on ringbuffers or
  86. * statically assigned memory.
  87. *
  88. * If an entire message is received, it is forwarded directly. But the memory
  89. * needs to be freed with the networklayer-specific mechanism. If a half message
  90. * is received, we copy it into a local buffer. Then, the stack-specific free
  91. * needs to be used.
  92. *
  93. * @param connection The connection
  94. * @param message The received message. The content may be overwritten when a
  95. * previsouly received buffer is completed.
  96. * @param realloced The Boolean value is set to true if the outgoing message has
  97. * been reallocated from the network layer.
  98. * @return Returns UA_STATUSCODE_GOOD or an error code. When an error occurs, the ingoing message
  99. * and the current buffer in the connection are freed.
  100. */
  101. UA_StatusCode UA_EXPORT
  102. UA_Connection_completeMessages(UA_Connection *connection, UA_ByteString * UA_RESTRICT message,
  103. UA_Boolean * UA_RESTRICT realloced);
  104. #ifdef __cplusplus
  105. } // extern "C"
  106. #endif
  107. #endif /* UA_CONNECTION_H_ */