ua_transport_connection.c 4.9 KB

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