server_mainloop.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <signal.h>
  6. #ifdef _WIN32
  7. # include <winsock2.h>
  8. #else
  9. # include <sys/select.h>
  10. #endif
  11. #ifdef UA_NO_AMALGAMATION
  12. # include "ua_types.h"
  13. # include "ua_server.h"
  14. # include "ua_config_standard.h"
  15. # include "ua_network_tcp.h"
  16. # include "ua_log_stdout.h"
  17. #else
  18. # include "open62541.h"
  19. #endif
  20. UA_Boolean running = true;
  21. UA_Logger logger = UA_Log_Stdout;
  22. static void stopHandler(int sign) {
  23. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  24. running = false;
  25. }
  26. /* In this example, we integrate the server into an external "mainloop". This
  27. can be for example the event-loop used in GUI toolkits, such as Qt or GTK. */
  28. int main(int argc, char** argv) {
  29. signal(SIGINT, stopHandler); /* catches ctrl-c */
  30. UA_ServerConfig config = UA_ServerConfig_standard;
  31. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  32. config.networkLayers = &nl;
  33. config.networkLayersSize = 1;
  34. UA_Server *server = UA_Server_new(config);
  35. UA_StatusCode retval = UA_Server_run_startup(server);
  36. if(retval != UA_STATUSCODE_GOOD)
  37. goto cleanup;
  38. /* Should the server networklayer block (with a timeout) until a message
  39. arrives or should it return immediately? */
  40. UA_Boolean waitInternal = false;
  41. while(running) {
  42. /* timeout is the maximum possible delay (in millisec) until the next
  43. _iterate call. Otherwise, the server might miss an internal timeout
  44. or cannot react to messages with the promised responsiveness. */
  45. UA_UInt16 timeout = UA_Server_run_iterate(server, waitInternal);
  46. /* Now we can use the max timeout to do something else. In this case, we
  47. just sleep. (select is used as a platform-independent sleep
  48. function.) */
  49. struct timeval tv;
  50. tv.tv_sec = 0;
  51. tv.tv_usec = timeout * 1000;
  52. select(0, NULL, NULL, NULL, &tv);
  53. }
  54. retval = UA_Server_run_shutdown(server);
  55. cleanup:
  56. UA_Server_delete(server);
  57. nl.deleteMembers(&nl);
  58. return (int)retval;
  59. }