ua_connection.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, UA_Int32 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. /** Returns a job that contains either a message-bytestring managed by the network layer or a
  84. message-bytestring that was newly allocated (or a nothing-job). Half-received messages are
  85. attached to the connection. The next completion tries to create a complete message with the next
  86. buffer the connection receives. */
  87. UA_Job UA_EXPORT UA_Connection_completeMessages(UA_Connection *connection, UA_ByteString received);
  88. #ifdef __cplusplus
  89. } // extern "C"
  90. #endif
  91. #endif /* UA_CONNECTION_H_ */