opcuaServerACPLT.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. fflush(stdout);
  39. total_len += gather_buf[i]->length;
  40. }
  41. UA_ByteString msg;
  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. UA_ByteString_printf("new msg: ", &msg);
  49. fflush(stdout);
  50. server.writeData.data = msg.data;
  51. server.writeData.length = msg.length;
  52. server.newDataToWrite = 1;
  53. return UA_SUCCESS;
  54. }
  55. void server_run() {
  56. TL_Connection connection;
  57. connection.connectionState = CONNECTIONSTATE_CLOSED;
  58. connection.writerCallback = (TL_Writer)server_writer;
  59. connection.localConf.maxChunkCount = 1;
  60. connection.localConf.maxMessageSize = BUFFER_SIZE;
  61. connection.localConf.protocolVersion = 0;
  62. connection.localConf.recvBufferSize = BUFFER_SIZE;
  63. connection.localConf.recvBufferSize = BUFFER_SIZE;
  64. UA_ByteString slMessage = { -1, UA_NULL };
  65. int optval = 1;
  66. int sockfd, newsockfd, portno, clilen;
  67. char buffer[BUFFER_SIZE];
  68. struct sockaddr_in serv_addr, cli_addr;
  69. int n;
  70. /* First call to socket() function */
  71. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  72. if (sockfd < 0) {
  73. perror("ERROR opening socket");
  74. exit(1);
  75. }
  76. /* Initialize socket structure */
  77. memset((void *) &serv_addr, 0, sizeof(serv_addr));
  78. portno = PORT;
  79. serv_addr.sin_family = AF_INET;
  80. serv_addr.sin_addr.s_addr = INADDR_ANY;
  81. serv_addr.sin_port = htons(portno);
  82. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)
  83. == -1) {
  84. perror("setsockopt");
  85. exit(1);
  86. }
  87. /* Now bind the host address using bind() call.*/
  88. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  89. perror("ERROR on binding");
  90. exit(1);
  91. }
  92. /* Now start listening for the clients, here process will
  93. * go in sleep mode and will wait for the incoming connection
  94. */
  95. while (listen(sockfd, 5) != -1) {
  96. clilen = sizeof(cli_addr);
  97. /* Accept actual connection from the client */
  98. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
  99. if (newsockfd < 0) {
  100. perror("ERROR on accept");
  101. exit(1);
  102. }
  103. printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connection.connectionState);
  104. /* communication loop */
  105. int i = 0;
  106. do {
  107. /* If connection is established then start communicating */
  108. memset(buffer, 0, BUFFER_SIZE);
  109. n = read(newsockfd, buffer, BUFFER_SIZE);
  110. if (n > 0) {
  111. slMessage.data = (UA_Byte*) buffer;
  112. slMessage.length = n;
  113. UA_ByteString_printx("server_run - received=",&slMessage);
  114. TL_Process(&connection, &slMessage);
  115. } else if (n <= 0) {
  116. perror("ERROR reading from socket1");
  117. exit(1);
  118. }
  119. if (server.newDataToWrite) {
  120. UA_ByteString_printx("Send data:", &server.writeData);
  121. n = write(newsockfd, server.writeData.data, server.writeData.length);
  122. printf("server_run - written %d bytes \n\n", n);
  123. server.newDataToWrite = 0;
  124. UA_ByteString_deleteMembers(&server.writeData);
  125. }
  126. i++;
  127. } while(connection.connectionState != CONNECTIONSTATE_CLOSE);
  128. shutdown(newsockfd,2);
  129. close(newsockfd);
  130. connection.connectionState = CONNECTIONSTATE_CLOSED;
  131. }
  132. shutdown(sockfd,2);
  133. close(sockfd);
  134. }
  135. #endif