PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/clients/roscpp/include/ros/io.h

https://gitlab.com/F34140r/ros_comm
C Header | 208 lines | 115 code | 21 blank | 72 comment | 0 complexity | ee5ee497817332fd496bb8f7652418fa MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2008, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*****************************************************************************
  35. ** Ifdefs
  36. *****************************************************************************/
  37. #ifndef ROSCPP_IO_H_
  38. #define ROSCPP_IO_H_
  39. /*****************************************************************************
  40. ** Includes
  41. *****************************************************************************/
  42. #include <string>
  43. #include "common.h"
  44. #ifdef WIN32
  45. #include <winsock2.h> // For struct timeval
  46. #include <ws2tcpip.h> // Must be after winsock2.h because MS didn't put proper inclusion guards in their headers.
  47. #include <sys/types.h>
  48. #include <io.h>
  49. #include <fcntl.h>
  50. #include <process.h> // for _getpid
  51. #else
  52. #include <poll.h> // should get cmake to explicitly check for poll.h?
  53. #include <sys/poll.h>
  54. #include <arpa/inet.h>
  55. #include <netdb.h>
  56. #include <unistd.h>
  57. #include <netdb.h> // getnameinfo in network.cpp
  58. #include <netinet/in.h> // sockaddr_in in network.cpp
  59. #include <netinet/tcp.h> // TCP_NODELAY in transport/transport_tcp.cpp
  60. #endif
  61. /*****************************************************************************
  62. ** Cross Platform Macros
  63. *****************************************************************************/
  64. #ifdef WIN32
  65. #define getpid _getpid
  66. #define ROS_INVALID_SOCKET INVALID_SOCKET
  67. #define ROS_SOCKETS_SHUT_RDWR SD_BOTH /* Used by ::shutdown() */
  68. #define ROS_SOCKETS_ASYNCHRONOUS_CONNECT_RETURN WSAEWOULDBLOCK
  69. #ifndef POLLRDNORM
  70. #define POLLRDNORM 0x0100 /* mapped to read fds_set */
  71. #endif
  72. #ifndef POLLRDBAND
  73. #define POLLRDBAND 0x0200 /* mapped to exception fds_set */
  74. #endif
  75. #ifndef POLLIN
  76. #define POLLIN (POLLRDNORM | POLLRDBAND) /* There is data to read. */
  77. #endif
  78. #ifndef POLLPRI
  79. #define POLLPRI 0x0400 /* There is urgent data to read. */
  80. #endif
  81. #ifndef POLLWRNORM
  82. #define POLLWRNORM 0x0010 /* mapped to write fds_set */
  83. #endif
  84. #ifndef POLLOUT
  85. #define POLLOUT (POLLWRNORM) /* Writing now will not block. */
  86. #endif
  87. #ifndef POLLWRBAND
  88. #define POLLWRBAND 0x0020 /* mapped to write fds_set */
  89. #endif
  90. #ifndef POLLERR
  91. #define POLLERR 0x0001 /* Error condition. */
  92. #endif
  93. #ifndef POLLHUP
  94. #define POLLHUP 0x0002 /* Hung up. */
  95. #endif
  96. #ifndef POLLNVAL
  97. #define POLLNVAL 0x0004 /* Invalid polling request. */
  98. #endif
  99. #else
  100. #define ROS_SOCKETS_SHUT_RDWR SHUT_RDWR /* Used by ::shutdown() */
  101. #define ROS_INVALID_SOCKET ((int) -1)
  102. #define ROS_SOCKETS_ASYNCHRONOUS_CONNECT_RETURN EINPROGRESS
  103. #endif
  104. /*****************************************************************************
  105. ** Namespaces
  106. *****************************************************************************/
  107. namespace ros {
  108. /*****************************************************************************
  109. ** Cross Platform Types
  110. *****************************************************************************/
  111. #ifdef WIN32
  112. typedef SOCKET socket_fd_t;
  113. typedef SOCKET signal_fd_t;
  114. /* poll emulation support */
  115. typedef struct socket_pollfd {
  116. socket_fd_t fd; /* file descriptor */
  117. short events; /* requested events */
  118. short revents; /* returned events */
  119. } socket_pollfd;
  120. typedef unsigned long int nfds_t;
  121. #ifdef _MSC_VER
  122. typedef int pid_t; /* return type for windows' _getpid */
  123. #endif
  124. #else
  125. typedef int socket_fd_t;
  126. typedef int signal_fd_t;
  127. typedef struct pollfd socket_pollfd;
  128. #endif
  129. /*****************************************************************************
  130. ** Functions
  131. *****************************************************************************/
  132. ROSCPP_DECL int last_socket_error();
  133. ROSCPP_DECL const char* last_socket_error_string();
  134. ROSCPP_DECL bool last_socket_error_is_would_block();
  135. ROSCPP_DECL int poll_sockets(socket_pollfd *fds, nfds_t nfds, int timeout);
  136. ROSCPP_DECL int set_non_blocking(socket_fd_t &socket);
  137. ROSCPP_DECL int close_socket(socket_fd_t &socket);
  138. ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2]);
  139. /*****************************************************************************
  140. ** Inlines - almost direct api replacements, should stay fast.
  141. *****************************************************************************/
  142. /**
  143. * Closes the signal pair - on windows we're using sockets (because windows
  144. * select() function cant handle pipes). On linux, we're just using the pipes.
  145. * @param signal_pair : the signal pair type.
  146. */
  147. inline void close_signal_pair(signal_fd_t signal_pair[2]) {
  148. #ifdef WIN32 // use a socket pair
  149. ::closesocket(signal_pair[0]);
  150. ::closesocket(signal_pair[1]);
  151. #else // use a socket pair on mingw or pipe pair on linux, either way, close works
  152. ::close(signal_pair[0]);
  153. ::close(signal_pair[1]);
  154. #endif
  155. }
  156. /**
  157. * Write to a signal_fd_t device. On windows we're using sockets (because windows
  158. * select() function cant handle pipes) so we have to use socket functions.
  159. * On linux, we're just using the pipes.
  160. */
  161. #ifdef _MSC_VER
  162. inline int write_signal(const signal_fd_t &signal, const char *buffer, const unsigned int &nbyte) {
  163. return ::send(signal, buffer, nbyte, 0);
  164. // return write(signal, buffer, nbyte);
  165. }
  166. #else
  167. inline ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte) {
  168. return write(signal, buffer, nbyte);
  169. }
  170. #endif
  171. /**
  172. * Read from a signal_fd_t device. On windows we're using sockets (because windows
  173. * select() function cant handle pipes) so we have to use socket functions.
  174. * On linux, we're just using the pipes.
  175. */
  176. #ifdef _MSC_VER
  177. inline int read_signal(const signal_fd_t &signal, char *buffer, const unsigned int &nbyte) {
  178. return ::recv(signal, buffer, nbyte, 0);
  179. // return _read(signal, buffer, nbyte);
  180. }
  181. #else
  182. inline ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte) {
  183. return read(signal, buffer, nbyte);
  184. }
  185. #endif
  186. } // namespace ros
  187. #endif /* ROSCPP_IO_H_ */