ua_connection.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef UA_CONNECTION_H_
  2. #define UA_CONNECTION_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "ua_types.h"
  7. /**
  8. * Networking
  9. * ----------
  10. * Client-server connection is represented by a `UA_Connection` structure. In
  11. * order to allow for different operating systems and connection types. For
  12. * this, `UA_Connection` stores a pointer to user-defined data and
  13. * function-pointers to interact with the underlying networking implementation.
  14. *
  15. * An example networklayer for TCP communication is contained in the plugins
  16. * folder. The networklayer forwards messages with `UA_Connection` structures to
  17. * the main open62541 library. The library can then return messages vie TCP
  18. * without being aware of the underlying transport technology.
  19. *
  20. * Connection Config
  21. * ^^^^^^^^^^^^^^^^^ */
  22. typedef struct {
  23. UA_UInt32 protocolVersion;
  24. UA_UInt32 sendBufferSize;
  25. UA_UInt32 recvBufferSize;
  26. UA_UInt32 maxMessageSize;
  27. UA_UInt32 maxChunkCount;
  28. } UA_ConnectionConfig;
  29. extern const UA_EXPORT UA_ConnectionConfig UA_ConnectionConfig_standard;
  30. /**
  31. * Connection Structure
  32. * ^^^^^^^^^^^^^^^^^^^^ */
  33. typedef enum {
  34. UA_CONNECTION_OPENING, /* The socket is open, but the HEL/ACK handshake
  35. is not done */
  36. UA_CONNECTION_ESTABLISHED, /* The socket is open and the connection
  37. configured */
  38. UA_CONNECTION_CLOSED, /* The socket has been closed and the connection
  39. will be deleted */
  40. } UA_ConnectionState;
  41. /* Forward declarations */
  42. struct UA_Connection;
  43. typedef struct UA_Connection UA_Connection;
  44. struct UA_SecureChannel;
  45. typedef struct UA_SecureChannel UA_SecureChannel;
  46. struct UA_Connection {
  47. UA_ConnectionState state;
  48. UA_ConnectionConfig localConf;
  49. UA_ConnectionConfig remoteConf;
  50. UA_SecureChannel *channel; /* The securechannel that is attached to
  51. this connection */
  52. UA_Int32 sockfd; /* Most connectivity solutions run on
  53. sockets. Having the socket id here
  54. simplifies the design. */
  55. void *handle; /* A pointer to internal data */
  56. UA_ByteString incompleteMessage; /* A half-received message (TCP is a
  57. streaming protocol) is stored here */
  58. /* Get a buffer for sending */
  59. UA_StatusCode (*getSendBuffer)(UA_Connection *connection, size_t length,
  60. UA_ByteString *buf);
  61. /* Release the send buffer manually */
  62. void (*releaseSendBuffer)(UA_Connection *connection, UA_ByteString *buf);
  63. /* Sends a message over the connection. The message buffer is always freed,
  64. * even if sending fails.
  65. *
  66. * @param connection The connection
  67. * @param buf The message buffer
  68. * @return Returns an error code or UA_STATUSCODE_GOOD. */
  69. UA_StatusCode (*send)(UA_Connection *connection, UA_ByteString *buf);
  70. /* Receive a message from the remote connection
  71. *
  72. * @param connection The connection
  73. * @param response The response string. It is allocated by the connection
  74. * and needs to be freed with connection->releaseBuffer
  75. * @param timeout Timeout of the recv operation in milliseconds
  76. * @return Returns UA_STATUSCODE_BADCOMMUNICATIONERROR if the recv operation
  77. * can be repeated, UA_STATUSCODE_GOOD if it succeeded and
  78. * UA_STATUSCODE_BADCONNECTIONCLOSED if the connection was
  79. * closed. */
  80. UA_StatusCode (*recv)(UA_Connection *connection, UA_ByteString *response,
  81. UA_UInt32 timeout);
  82. /* Release the buffer of a received message */
  83. void (*releaseRecvBuffer)(UA_Connection *connection, UA_ByteString *buf);
  84. /* Close the connection */
  85. void (*close)(UA_Connection *connection);
  86. };
  87. void UA_EXPORT UA_Connection_deleteMembers(UA_Connection *connection);
  88. /**
  89. * EndpointURL Helper
  90. * ^^^^^^^^^^^^^^^^^^ */
  91. /* Split the given endpoint url into hostname and port
  92. * @param endpointUrl The endpoint URL to split up
  93. * @param hostname the target array for hostname. Has to be at least 256 size.
  94. * If an IPv6 address is given, hostname contains e.g.
  95. * '[2001:0db8:85a3::8a2e:0370:7334]'
  96. * @param port set to the port of the url or 0
  97. * @param path pointing to the end of given endpointUrl or to NULL if no
  98. * path given. The starting '/' is NOT included in path
  99. * @return UA_STATUSCODE_BADOUTOFRANGE if url too long,
  100. * UA_STATUSCODE_BADATTRIBUTEIDINVALID if url not starting with
  101. * 'opc.tcp://', UA_STATUSCODE_GOOD on success
  102. */
  103. UA_StatusCode UA_EXPORT
  104. UA_EndpointUrl_split(const char *endpointUrl, char *hostname,
  105. UA_UInt16 * port, const char ** path);
  106. /* Convert given byte string to a positive number. Returns the number of valid
  107. * digits. Stops if a non-digit char is found and returns the number of digits
  108. * up to that point. */
  109. size_t UA_EXPORT
  110. UA_readNumber(UA_Byte *buf, size_t buflen, UA_UInt32 *number);
  111. #ifdef __cplusplus
  112. } // extern "C"
  113. #endif
  114. #endif /* UA_CONNECTION_H_ */