ua_transport_connection.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * ua_transport_connection.c
  3. *
  4. * Created on: 10.05.2014
  5. * Author: open62541
  6. */
  7. #include "ua_transport_connection.h"
  8. #include "ua_transport.h"
  9. struct UA_TL_Connection{
  10. UA_Int32 connectionHandle;
  11. UA_UInt32 state;
  12. TL_Buffer localConf;
  13. TL_Buffer remoteConf;
  14. TL_Writer writer;
  15. UA_String localEndpointUrl;
  16. UA_String remoteEndpointUrl;
  17. TL_Closer closeCallback;
  18. void *networkLayerData;
  19. };
  20. UA_Int32 UA_TL_Connection_new(UA_TL_Connection **connection, TL_Buffer localBuffers,TL_Writer writer, TL_Closer closeCallback,UA_Int32 handle, void* networkLayerData)
  21. {
  22. UA_Int32 retval = UA_SUCCESS;
  23. retval |= UA_alloc((void**)connection,sizeof(UA_TL_Connection));
  24. if(retval == UA_SUCCESS)
  25. {
  26. (*connection)->connectionHandle = handle;
  27. (*connection)->localConf = localBuffers;
  28. (*connection)->writer = writer;
  29. (*connection)->closeCallback = closeCallback;
  30. (*connection)->state = CONNECTIONSTATE_CLOSED;
  31. (*connection)->networkLayerData = networkLayerData;
  32. }
  33. return retval;
  34. }
  35. UA_Int32 UA_TL_Connection_delete(UA_TL_Connection *connection)
  36. {
  37. UA_Int32 retval = UA_SUCCESS;
  38. retval |= UA_free((void*)connection);
  39. return retval;
  40. }
  41. UA_Int32 UA_TL_Connection_close(UA_TL_Connection *connection)
  42. {
  43. connection->state = CONNECTIONSTATE_CLOSED;
  44. connection->closeCallback(connection);
  45. return UA_SUCCESS;
  46. }
  47. UA_Boolean UA_TL_Connection_compare(UA_TL_Connection *connection1, UA_TL_Connection *connection2)
  48. {
  49. if(connection1 && connection2)
  50. {
  51. if ((*(UA_TL_Connection**)connection1)->connectionHandle == (*(UA_TL_Connection**)connection2)->connectionHandle)
  52. {
  53. return UA_TRUE;
  54. }
  55. }
  56. return UA_FALSE;
  57. }
  58. UA_Int32 UA_TL_Connection_configByHello(UA_TL_Connection *connection, UA_OPCUATcpHelloMessage *helloMessage)
  59. {
  60. UA_Int32 retval = UA_SUCCESS;
  61. connection->remoteConf.maxChunkCount = helloMessage->maxChunkCount;
  62. connection->remoteConf.maxMessageSize = helloMessage->maxMessageSize;
  63. connection->remoteConf.protocolVersion = helloMessage->protocolVersion;
  64. connection->remoteConf.recvBufferSize = helloMessage->receiveBufferSize;
  65. connection->remoteConf.sendBufferSize = helloMessage->sendBufferSize;
  66. connection->state = CONNECTIONSTATE_ESTABLISHED;
  67. retval |= UA_String_copy(&helloMessage->endpointUrl,&connection->remoteEndpointUrl);
  68. return UA_SUCCESS;
  69. }
  70. UA_Int32 UA_TL_Connection_callWriter(UA_TL_Connection *connection, const UA_ByteString** gather_bufs, UA_Int32 gather_len)
  71. {
  72. return connection->writer(connection->connectionHandle,gather_bufs, gather_len);
  73. }
  74. //setters
  75. UA_Int32 UA_TL_Connection_setWriter(UA_TL_Connection *connection, TL_Writer writer)
  76. {
  77. connection->writer = writer;
  78. return UA_SUCCESS;
  79. }
  80. /*
  81. UA_Int32 UA_TL_Connection_setConnectionHandle(UA_TL_Connection *connection, UA_Int32 connectionHandle)
  82. {
  83. connection->connectionHandle = connectionHandle;
  84. return UA_SUCCESS;
  85. }
  86. */
  87. UA_Int32 UA_TL_Connection_setState(UA_TL_Connection *connection, UA_Int32 connectionState)
  88. {
  89. if(connection)
  90. {
  91. connection->state = connectionState;
  92. return UA_SUCCESS;
  93. }else{
  94. return UA_ERROR;
  95. }
  96. }
  97. //getters
  98. UA_Int32 UA_TL_Connection_getState(UA_TL_Connection *connection, UA_Int32 *connectionState)
  99. {
  100. if(connection)
  101. {
  102. *connectionState = connection->state;
  103. return UA_SUCCESS;
  104. }else{
  105. *connectionState = -1;
  106. return UA_ERROR;
  107. }
  108. }
  109. UA_Int32 UA_TL_Connection_getNetworkLayerData(UA_TL_Connection *connection,void** networkLayerData)
  110. {
  111. if(connection)
  112. {
  113. *networkLayerData = connection->networkLayerData;
  114. return UA_SUCCESS;
  115. }else{
  116. *networkLayerData = UA_NULL;
  117. return UA_ERROR;
  118. }
  119. }
  120. UA_Int32 UA_TL_Connection_getProtocolVersion(UA_TL_Connection *connection, UA_UInt32 *protocolVersion)
  121. {
  122. if(connection)
  123. {
  124. *protocolVersion = connection->localConf.protocolVersion;
  125. return UA_SUCCESS;
  126. }else{
  127. *protocolVersion = 0xFF;
  128. return UA_ERROR;
  129. }
  130. }
  131. UA_Int32 UA_TL_Connection_getLocalConfig(UA_TL_Connection *connection, TL_Buffer *localConfiguration)
  132. {
  133. if(connection)
  134. {
  135. return UA_memcpy(localConfiguration,&connection->localConf, sizeof(TL_Buffer));
  136. }else{
  137. localConfiguration = UA_NULL;
  138. return UA_ERROR;
  139. }
  140. }
  141. UA_Int32 UA_TL_Connection_getHandle(UA_TL_Connection *connection, UA_UInt32 *connectionHandle)
  142. {
  143. if(connection)
  144. {
  145. *connectionHandle = connection->connectionHandle;
  146. return UA_SUCCESS;
  147. }else{
  148. connectionHandle = 0;
  149. return UA_ERROR;
  150. }
  151. }
  152. UA_Int32 UA_TL_Connection_bind(UA_TL_Connection *connection, UA_Int32 handle)
  153. {
  154. if(connection)
  155. {
  156. connection->connectionHandle = handle;
  157. return UA_SUCCESS;
  158. }else{
  159. return UA_ERROR;
  160. }
  161. }