opcuaServerACPLT.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include "ua_types.h"
  5. #include "ua_transport.h"
  6. #include "ua_transport_binary.h"
  7. #include "networklayer.h"
  8. #include "ua_stack_channel_manager.h"
  9. #include "ua_transport_connection.h"
  10. #include "ua_transport_connection_manager.h"
  11. #include "ua_stack_session_manager.h"
  12. #ifdef LINUX
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <sys/socketvar.h>
  17. #include <unistd.h>
  18. void server_run();
  19. #endif
  20. #define PORT 16664
  21. #define MAXMSG 512
  22. #define BUFFER_SIZE 8192
  23. int main(void) {
  24. #ifdef LINUX
  25. printf("Starting open62541 demo server on port %d\n", PORT);
  26. server_run();
  27. #endif
  28. return EXIT_SUCCESS;
  29. }
  30. #ifdef LINUX
  31. void tmpTestFunction()
  32. {
  33. }
  34. void server_run() {
  35. //just for debugging
  36. #ifdef DEBUG
  37. tmpTestFunction();
  38. #endif
  39. // UA_TL_Connection connection;// = UA_NULL;
  40. TL_Buffer localBuffers;
  41. UA_Int32 connectionState;
  42. //connection.connectionState = CONNECTIONSTATE_CLOSED;
  43. //connection.writerCallback = (TL_Writer) NL_TCP_writer;
  44. localBuffers.maxChunkCount = 1;
  45. localBuffers.maxMessageSize = BUFFER_SIZE;
  46. localBuffers.protocolVersion = 0;
  47. localBuffers.recvBufferSize = BUFFER_SIZE;
  48. localBuffers.recvBufferSize = BUFFER_SIZE;
  49. /*init secure Channel manager, which handles more than one channel */
  50. UA_String endpointUrl;
  51. UA_String_copycstring("open62541.org",&endpointUrl);
  52. SL_ChannelManager_init(2,3600000, 873, 23, &endpointUrl);
  53. UA_SessionManager_init(2,300000,5);
  54. UA_ByteString slMessage = { -1, UA_NULL };
  55. int optval = 1;
  56. int sockfd, newsockfd, portno, clilen;
  57. char buffer[BUFFER_SIZE];
  58. struct sockaddr_in serv_addr, cli_addr;
  59. int n;
  60. /* First call to socket() function */
  61. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  62. if (sockfd < 0) {
  63. perror("ERROR opening socket");
  64. exit(1);
  65. }
  66. /* Initialize socket structure */
  67. memset((void *) &serv_addr, 0, sizeof(serv_addr));
  68. portno = PORT;
  69. serv_addr.sin_family = AF_INET;
  70. serv_addr.sin_addr.s_addr = INADDR_ANY;
  71. serv_addr.sin_port = htons(portno);
  72. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) == -1) {
  73. perror("setsockopt");
  74. exit(1);
  75. }
  76. /* Now bind the host address using bind() call.*/
  77. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  78. perror("ERROR on binding");
  79. exit(1);
  80. }
  81. /* Now start listening for the clients, here process will
  82. * go in sleep mode and will wait for the incoming connection
  83. */
  84. UA_TL_Connection tmpConnection;
  85. while (listen(sockfd, 5) != -1) {
  86. clilen = sizeof(cli_addr);
  87. /* Accept actual connection from the client */
  88. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
  89. if (newsockfd < 0) {
  90. perror("ERROR on accept");
  91. exit(1);
  92. }
  93. UA_TL_ConnectionManager_getConnectionByHandle(newsockfd, &tmpConnection);
  94. if(tmpConnection == UA_NULL)
  95. {
  96. UA_TL_Connection_new(&tmpConnection, localBuffers, (TL_Writer)NL_TCP_writer,NL_Connection_close,newsockfd,UA_NULL);
  97. }
  98. UA_TL_Connection_getState(tmpConnection, &connectionState);
  99. printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connectionState);
  100. UA_TL_Connection_bind(tmpConnection, newsockfd);
  101. do {
  102. memset(buffer, 0, BUFFER_SIZE);
  103. n = read(newsockfd, buffer, BUFFER_SIZE);
  104. if (n > 0) {
  105. slMessage.data = (UA_Byte*) buffer;
  106. slMessage.length = n;
  107. #ifdef DEBUG
  108. UA_ByteString_printx("server_run - received=",&slMessage);
  109. #endif
  110. TL_Process(tmpConnection, &slMessage);
  111. } else if (n <= 0) {
  112. perror("ERROR reading from socket1");
  113. // exit(1);
  114. }
  115. UA_TL_Connection_getState(tmpConnection, &connectionState);
  116. } while(connectionState != CONNECTIONSTATE_CLOSE);
  117. shutdown(newsockfd,2);
  118. close(newsockfd);
  119. UA_TL_ConnectionManager_getConnectionByHandle(newsockfd, &tmpConnection);
  120. UA_TL_ConnectionManager_removeConnection(tmpConnection);
  121. }
  122. shutdown(sockfd,2);
  123. close(sockfd);
  124. }
  125. #endif