ua_connection.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2014-2016 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. /* Forward declarations */
  22. struct UA_Connection;
  23. typedef struct UA_Connection UA_Connection;
  24. struct UA_SecureChannel;
  25. typedef struct UA_SecureChannel UA_SecureChannel;
  26. /**
  27. * Networking
  28. * ----------
  29. * Client-server connection is represented by a `UA_Connection` structure. In
  30. * order to allow for different operating systems and connection types. For
  31. * this, `UA_Connection` stores a pointer to user-defined data and
  32. * function-pointers to interact with the underlying networking implementation.
  33. *
  34. * An example networklayer for TCP communication is contained in the plugins
  35. * folder. The networklayer forwards messages with `UA_Connection` structures to
  36. * the main open62541 library. The library can then return messages vie TCP
  37. * without being aware of the underlying transport technology.
  38. *
  39. * Connection Config
  40. * =================
  41. **/
  42. typedef struct UA_ConnectionConfig {
  43. UA_UInt32 protocolVersion;
  44. UA_UInt32 sendBufferSize;
  45. UA_UInt32 recvBufferSize;
  46. UA_UInt32 maxMessageSize;
  47. UA_UInt32 maxChunkCount;
  48. } UA_ConnectionConfig;
  49. extern const UA_EXPORT UA_ConnectionConfig UA_ConnectionConfig_standard;
  50. /**
  51. * Connection Structure
  52. * ====================
  53. */
  54. typedef enum UA_ConnectionState {
  55. UA_CONNECTION_OPENING, /* The socket is open, but the HEL/ACK handshake
  56. is not done */
  57. UA_CONNECTION_ESTABLISHED, /* The socket is open and the connection
  58. configured */
  59. UA_CONNECTION_CLOSED, /* The socket has been closed and the connection
  60. will be deleted */
  61. } UA_ConnectionState;
  62. struct UA_Connection {
  63. UA_ConnectionState state;
  64. UA_ConnectionConfig localConf;
  65. UA_ConnectionConfig remoteConf;
  66. UA_SecureChannel *channel; /* The securechannel that is attached to
  67. this connection */
  68. UA_Int32 sockfd; /* Most connectivity solutions run on
  69. sockets. Having the socket id here
  70. simplifies the design. */
  71. void *handle; /* A pointer to the networklayer */
  72. UA_ByteString incompleteMessage; /* A half-received message (TCP is a
  73. streaming protocol) is stored here */
  74. /* Get a buffer for sending */
  75. UA_StatusCode (*getSendBuffer)(UA_Connection *connection, size_t length,
  76. UA_ByteString *buf);
  77. /* Release the send buffer manually */
  78. void (*releaseSendBuffer)(UA_Connection *connection, UA_ByteString *buf);
  79. /* Sends a message over the connection. The message buffer is always freed,
  80. * even if sending fails.
  81. *
  82. * @param connection The connection
  83. * @param buf The message buffer
  84. * @return Returns an error code or UA_STATUSCODE_GOOD. */
  85. UA_StatusCode (*send)(UA_Connection *connection, UA_ByteString *buf);
  86. /* Receive a message from the remote connection
  87. *
  88. * @param connection The connection
  89. * @param response The response string. It is allocated by the connection
  90. * and needs to be freed with connection->releaseBuffer
  91. * @param timeout Timeout of the recv operation in milliseconds
  92. * @return Returns UA_STATUSCODE_BADCOMMUNICATIONERROR if the recv operation
  93. * can be repeated, UA_STATUSCODE_GOOD if it succeeded and
  94. * UA_STATUSCODE_BADCONNECTIONCLOSED if the connection was
  95. * closed. */
  96. UA_StatusCode (*recv)(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout);
  97. /* Release the buffer of a received message */
  98. void (*releaseRecvBuffer)(UA_Connection *connection, UA_ByteString *buf);
  99. /* Close the connection */
  100. void (*close)(UA_Connection *connection);
  101. };
  102. void UA_EXPORT UA_Connection_init(UA_Connection *connection);
  103. void UA_EXPORT UA_Connection_deleteMembers(UA_Connection *connection);
  104. #ifdef __cplusplus
  105. } // extern "C"
  106. #endif
  107. #endif /* UA_CONNECTION_H_ */