opcuaServerACPLT.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifdef LINUX
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <sys/socketvar.h>
  12. #include <unistd.h>
  13. void server_init();
  14. void server_run();
  15. #endif
  16. #define PORT 16664
  17. #define MAXMSG 512
  18. #define BUFFER_SIZE 8192
  19. int main(void) {
  20. #ifdef LINUX
  21. server_init();
  22. server_run();
  23. #endif
  24. return EXIT_SUCCESS;
  25. }
  26. #ifdef LINUX
  27. void server_init() {
  28. printf("Starting open62541 demo server on port %d\n", PORT);
  29. }
  30. typedef struct T_Server {
  31. int newDataToWrite;
  32. UA_ByteString writeData;
  33. } Server;
  34. Server server;
  35. UA_Int32 server_writer(TL_connection* connection, UA_ByteString** gather_buf, UA_UInt32 gather_len) {
  36. UA_UInt32 total_len = 0;
  37. for(UA_UInt32 i=0;i<gather_len;i++) {
  38. total_len += gather_buf[i]->length;
  39. }
  40. UA_ByteString *msg;
  41. UA_alloc((void **)&msg, sizeof(UA_ByteString));
  42. UA_ByteString_newMembers(msg, total_len);
  43. UA_UInt32 pos = 0;
  44. for(UA_UInt32 i=0;i<gather_len;i++) {
  45. memcpy(msg->data+pos, gather_buf[i]->data, gather_buf[i]->length);
  46. pos += gather_buf[i]->length;
  47. }
  48. server.writeData = *msg;
  49. server.newDataToWrite = 1;
  50. UA_free(msg);
  51. return UA_SUCCESS;
  52. }
  53. void server_run() {
  54. TL_connection connection;
  55. connection.connectionState = connectionState_CLOSE;
  56. connection.writerCallback = (TL_writer)server_writer;
  57. connection.localConf.maxChunkCount = 1;
  58. connection.localConf.maxMessageSize = BUFFER_SIZE;
  59. connection.localConf.protocolVersion = 0;
  60. connection.localConf.recvBufferSize = BUFFER_SIZE;
  61. connection.localConf.recvBufferSize = BUFFER_SIZE;
  62. UA_ByteString slMessage = { -1, UA_NULL };
  63. int optval = 1;
  64. int sockfd, newsockfd, portno, clilen;
  65. char buffer[BUFFER_SIZE];
  66. struct sockaddr_in serv_addr, cli_addr;
  67. int n;
  68. /* First call to socket() function */
  69. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  70. if (sockfd < 0) {
  71. perror("ERROR opening socket");
  72. exit(1);
  73. }
  74. /* Initialize socket structure */
  75. memset((void *) &serv_addr, 0, sizeof(serv_addr));
  76. portno = PORT;
  77. serv_addr.sin_family = AF_INET;
  78. serv_addr.sin_addr.s_addr = INADDR_ANY;
  79. serv_addr.sin_port = htons(portno);
  80. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)
  81. == -1) {
  82. perror("setsockopt");
  83. exit(1);
  84. }
  85. /* Now bind the host address using bind() call.*/
  86. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  87. perror("ERROR on binding");
  88. exit(1);
  89. }
  90. /* Now start listening for the clients, here process will
  91. * go in sleep mode and will wait for the incoming connection
  92. */
  93. while (listen(sockfd, 5) != -1) {
  94. clilen = sizeof(cli_addr);
  95. /* Accept actual connection from the client */
  96. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
  97. if (newsockfd < 0) {
  98. perror("ERROR on accept");
  99. exit(1);
  100. }
  101. printf("connection accepted\n");
  102. /* communication loop */
  103. while (connection.connectionState != connectionState_CLOSE) {
  104. /* If connection is established then start communicating */
  105. memset(buffer, 0, BUFFER_SIZE);
  106. n = read(newsockfd, buffer, BUFFER_SIZE);
  107. if (n > 0) {
  108. slMessage.data = (UA_Byte*) buffer;
  109. slMessage.length = n;
  110. UA_ByteString_printx("server_run - received=",&slMessage);
  111. TL_process(&connection, &slMessage);
  112. } else if (n < 0) {
  113. perror("ERROR reading from socket1");
  114. exit(1);
  115. }
  116. if (server.newDataToWrite) {
  117. UA_ByteString_printx("Send data:", &server.writeData);
  118. n = write(newsockfd, server.writeData.data,
  119. server.writeData.length);
  120. printf("written %d bytes \n", n);
  121. server.newDataToWrite = 0;
  122. UA_ByteString_deleteMembers(&server.writeData);
  123. server.writeData.data = UA_NULL;
  124. server.writeData.length = 0;
  125. }
  126. }
  127. close(newsockfd);
  128. connection.connectionState = connectionState_CLOSED;
  129. }
  130. }
  131. #endif