ua_connection.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  22. * @defgroup communication Communication
  23. *
  24. * @{
  25. */
  26. typedef enum UA_ConnectionState {
  27. UA_CONNECTION_OPENING, ///< The socket is open, but the HEL/ACK handshake is not done
  28. UA_CONNECTION_ESTABLISHED, ///< The socket is open and the connection configured
  29. UA_CONNECTION_CLOSED, ///< The socket has been closed and the connection will be deleted
  30. } UA_ConnectionState;
  31. typedef struct UA_ConnectionConfig {
  32. UA_UInt32 protocolVersion;
  33. UA_UInt32 sendBufferSize;
  34. UA_UInt32 recvBufferSize;
  35. UA_UInt32 maxMessageSize;
  36. UA_UInt32 maxChunkCount;
  37. } UA_ConnectionConfig;
  38. extern const UA_EXPORT UA_ConnectionConfig UA_ConnectionConfig_standard;
  39. /* Forward declaration */
  40. struct UA_SecureChannel;
  41. typedef struct UA_SecureChannel UA_SecureChannel;
  42. struct UA_Connection;
  43. typedef struct UA_Connection UA_Connection;
  44. /**
  45. * The connection to a single client (or server). The connection is defined independent of the
  46. * underlying network layer implementation. This allows a plugging-in custom implementations (e.g.
  47. * an embedded TCP stack)
  48. */
  49. struct UA_Connection {
  50. UA_ConnectionState state;
  51. UA_ConnectionConfig localConf;
  52. UA_ConnectionConfig remoteConf;
  53. UA_SecureChannel *channel; ///> The securechannel that is attached to this connection (or null)
  54. UA_Int32 sockfd; ///> Most connectivity solutions run on sockets. Having the socket id here simplifies the design.
  55. void *handle; ///> A pointer to the networklayer
  56. UA_ByteString incompleteMessage; ///> Half-received messages (tcp is a streaming protocol) get stored here
  57. UA_StatusCode (*getBuffer)(UA_Connection *connection, UA_ByteString *buf); ///> Get a buffer of the maximum remote recv size
  58. void (*releaseBuffer)(UA_Connection *connection, UA_ByteString *buf); ///> Release the buffer manually
  59. /**
  60. * Sends a message over the connection.
  61. * @param connection The connection
  62. * @param buf The message buffer is potentially reused (or freed) internally if sending succeeds.
  63. * @param buflen Since the buffer is potentially reused, we provide a separate content length.
  64. * @return Returns an error code or UA_STATUSCODE_GOOD.
  65. */
  66. UA_StatusCode (*write)(UA_Connection *connection, UA_ByteString *buf, size_t buflen);
  67. /**
  68. * Receive a message from the remote connection
  69. * @param connection The connection
  70. * @param response The response string. It is allocated by the connection and needs to be freed with connection->releaseBuffer
  71. * @param timeout Timeout of the recv operation in milliseconds
  72. * @return Returns UA_STATUSCODE_BADCOMMUNICATIONERROR if the recv operation can be repeated, UA_STATUSCODE_GOOD if it succeeded and
  73. * UA_STATUSCODE_BADCONNECTIONCLOSED if the connection was closed.
  74. */
  75. UA_StatusCode (*recv)(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout);
  76. void (*close)(UA_Connection *connection);
  77. };
  78. void UA_EXPORT UA_Connection_init(UA_Connection *connection);
  79. void UA_EXPORT UA_Connection_deleteMembers(UA_Connection *connection);
  80. void UA_EXPORT UA_Connection_detachSecureChannel(UA_Connection *connection);
  81. void UA_EXPORT UA_Connection_attachSecureChannel(UA_Connection *connection, UA_SecureChannel *channel);
  82. /** Returns a string of complete message (the length entry is decoded for that).
  83. If the received message is incomplete, it is retained in the connection. */
  84. UA_ByteString UA_EXPORT UA_Connection_completeMessages(UA_Connection *connection, UA_ByteString received);
  85. /** @} */
  86. #ifdef __cplusplus
  87. } // extern "C"
  88. #endif
  89. #endif /* UA_CONNECTION_H_ */