opcuaServerACPLT.c 3.4 KB

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