opcuaServer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ============================================================================
  3. Name : opcuaServer.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description :
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "opcua_binaryEncDec.h"
  13. #include "opcua_builtInDatatypes.h"
  14. #include "opcua_transportLayer.h"
  15. #include "opcua_types.h"
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. void server_init();
  19. void server_run();
  20. int main(void)
  21. {
  22. server_init();
  23. server_run();
  24. return EXIT_SUCCESS;
  25. }
  26. void server_init()
  27. {
  28. puts("starting demo Server");
  29. //call listen
  30. }
  31. void server_run()
  32. {
  33. int server_state = 0;
  34. int recv_data = 0;
  35. int send_data = 1;
  36. int new_client = 2;
  37. int new_request = 3;
  38. char buf[8192];
  39. struct sockaddr_in self;
  40. int sockfd;
  41. int clientfd;
  42. struct sockaddr_in client_addr;
  43. int addrlen=sizeof(client_addr);
  44. //---Create streaming socket---
  45. if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  46. {
  47. puts("socket error");
  48. }
  49. bzero(&self, sizeof(self));
  50. self.sin_family = AF_INET;
  51. self.sin_port = htons(4840);
  52. self.sin_addr.s_addr = htonl(INADDR_ANY);
  53. if( bind(sockfd,(struct sockaddr *)&self , sizeof(self)) < 0)
  54. {
  55. //Fehler bei bind()
  56. }
  57. //---Make it a "listening socket"---
  58. if ( listen(sockfd, 1) != 0 )
  59. {
  60. puts("listen error");
  61. }
  62. clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  63. server_state = 0;
  64. while(1)
  65. {
  66. //call recv (nonblocking)
  67. //call TL_getPacketType
  68. //if newData
  69. //
  70. UA_connection connection;
  71. AD_RawMessage *rawMessage;
  72. rawMessage->message = buf;
  73. rawMessage->length = 0;
  74. switch(server_state)
  75. {
  76. recv_data :
  77. {
  78. //call receive function
  79. rawMessage->length = recv(sockfd,buf,8192,0);
  80. if(rawMessage->length > 0)
  81. {
  82. server_state = new_client;
  83. }
  84. break;
  85. }
  86. send_data :
  87. {
  88. //call send function
  89. break;
  90. }
  91. new_client :
  92. {
  93. if(connection.transportLayer.connectionState != connectionState_ESTABLISHED)
  94. {
  95. TL_open(connection,rawMessage);
  96. }
  97. // else
  98. // {
  99. // SL_open(connection,rawMessage);
  100. //
  101. // }
  102. }
  103. new_request :
  104. {
  105. break;
  106. }
  107. }
  108. //if newClient
  109. //TL_processHELMessage(&connection,);
  110. //--------
  111. //call listen
  112. }
  113. }