sockets.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * File: ardupilot_SITL_ROV.c
  3. * Date: 03 Aug 2019
  4. * Description: integration with ardupilot SITL simulation.
  5. * Author: M.S.Hefny (HefnySco)
  6. * Modifications:
  7. */
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <webots/supervisor.h>
  13. #include "sockets.h"
  14. #include "sensors.h"
  15. bool socket_init() {
  16. #ifdef _WIN32 /* initialize the socket API */
  17. WSADATA info;
  18. if (WSAStartup(MAKEWORD(1, 1), &info) != 0) {
  19. fprintf(stderr, "Cannot initialize Winsock.\n");
  20. return false;
  21. }
  22. #endif
  23. return true;
  24. }
  25. bool socket_set_non_blocking(int fd) {
  26. if (fd < 0)
  27. return false;
  28. #ifdef _WIN32
  29. unsigned long mode = 1;
  30. return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false;
  31. #else
  32. int flags = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
  33. return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
  34. #endif
  35. }
  36. int socket_accept(int server_fd) {
  37. int cfd;
  38. struct sockaddr_in client;
  39. struct hostent *client_info;
  40. #ifndef _WIN32
  41. socklen_t asize;
  42. #else
  43. int asize;
  44. #endif
  45. asize = sizeof(struct sockaddr_in);
  46. cfd = accept(server_fd, (struct sockaddr *)&client, &asize);
  47. if (cfd == -1) {
  48. #ifdef _WIN32
  49. int e = WSAGetLastError();
  50. if (e == WSAEWOULDBLOCK)
  51. return 0;
  52. fprintf(stderr, "Accept error: %d.\n", e);
  53. #else
  54. if (errno == EWOULDBLOCK)
  55. return 0;
  56. fprintf(stderr, "Accept error: %d.\n", errno);
  57. #endif
  58. return -1;
  59. }
  60. client_info = gethostbyname((char *)inet_ntoa(client.sin_addr));
  61. printf("Accepted connection from: %s.\n", client_info->h_name);
  62. return cfd;
  63. }
  64. bool socket_close(int fd) {
  65. #ifdef _WIN32
  66. return (closesocket(fd) == 0) ? true : false;
  67. #else
  68. return (close(fd) == 0) ? true : false;
  69. #endif
  70. }
  71. bool socket_cleanup() {
  72. #ifdef _WIN32
  73. return (WSACleanup() == 0) ? true : false;
  74. #else
  75. return true;
  76. #endif
  77. }
  78. /*
  79. Creates a socket and bind it to port.
  80. */
  81. int create_socket_server(int port) {
  82. int sfd, rc;
  83. struct sockaddr_in address;
  84. if (!socket_init())
  85. {
  86. fprintf (stderr, "socket_init failed");
  87. return -1;
  88. }
  89. sfd = socket(AF_INET, SOCK_STREAM, 0);
  90. if (sfd == -1) {
  91. fprintf(stderr, "Cannot create socket.\n");
  92. return -1;
  93. }
  94. int one = 1;
  95. setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  96. memset(&address, 0, sizeof(struct sockaddr_in));
  97. address.sin_family = AF_INET;
  98. address.sin_port = htons((unsigned short)port);
  99. address.sin_addr.s_addr = INADDR_ANY;
  100. rc = bind(sfd, (struct sockaddr *)&address, sizeof(struct sockaddr));
  101. if (rc == -1) {
  102. fprintf(stderr, "Cannot bind port %d.\n", port);
  103. socket_close(sfd);
  104. return -1;
  105. }
  106. if (listen(sfd, 1) == -1) {
  107. fprintf(stderr, "Cannot listen for connections.\n");
  108. socket_close(sfd);
  109. return -1;
  110. }
  111. printf ("socket initialized at port %d.\n", port);
  112. return sfd;
  113. }