io.h
Go to the documentation of this file.
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 /*****************************************************************************
41 ** Includes
42 *****************************************************************************/
43 
44 #include <string>
45 #include "common.h"
46 
47 #ifdef WIN32
48  #include <winsock2.h> // For struct timeval
49  #include <ws2tcpip.h> // Must be after winsock2.h because MS didn't put proper inclusion guards in their headers.
50  #include <sys/types.h>
51  #include <io.h>
52  #include <fcntl.h>
53  #include <process.h> // for _getpid
54 #else
55  #include <poll.h> // should get cmake to explicitly check for poll.h?
56  #include <sys/poll.h>
57  #include <arpa/inet.h>
58  #include <netdb.h>
59  #include <unistd.h>
60  #include <netdb.h> // getnameinfo in network.cpp
61  #include <netinet/in.h> // sockaddr_in in network.cpp
62  #include <netinet/tcp.h> // TCP_NODELAY in transport/transport_tcp.cpp
63 #endif
64 
65 /*****************************************************************************
66 ** Cross Platform Macros
67 *****************************************************************************/
68 
69 #ifdef WIN32
70  #define getpid _getpid
71  #define ROS_INVALID_SOCKET INVALID_SOCKET
72  #define ROS_SOCKETS_SHUT_RDWR SD_BOTH /* Used by ::shutdown() */
73  #define ROS_SOCKETS_ASYNCHRONOUS_CONNECT_RETURN WSAEWOULDBLOCK
74  #ifndef POLLRDNORM
75  #define POLLRDNORM 0x0100 /* mapped to read fds_set */
76  #endif
77  #ifndef POLLRDBAND
78  #define POLLRDBAND 0x0200 /* mapped to exception fds_set */
79  #endif
80  #ifndef POLLIN
81  #define POLLIN (POLLRDNORM | POLLRDBAND) /* There is data to read. */
82  #endif
83  #ifndef POLLPRI
84  #define POLLPRI 0x0400 /* There is urgent data to read. */
85  #endif
86 
87  #ifndef POLLWRNORM
88  #define POLLWRNORM 0x0010 /* mapped to write fds_set */
89  #endif
90  #ifndef POLLOUT
91  #define POLLOUT (POLLWRNORM) /* Writing now will not block. */
92  #endif
93  #ifndef POLLWRBAND
94  #define POLLWRBAND 0x0020 /* mapped to write fds_set */
95  #endif
96  #ifndef POLLERR
97  #define POLLERR 0x0001 /* Error condition. */
98  #endif
99  #ifndef POLLHUP
100  #define POLLHUP 0x0002 /* Hung up. */
101  #endif
102  #ifndef POLLNVAL
103  #define POLLNVAL 0x0004 /* Invalid polling request. */
104  #endif
105 #else
106  #define ROS_SOCKETS_SHUT_RDWR SHUT_RDWR /* Used by ::shutdown() */
107  #define ROS_INVALID_SOCKET ((int) -1)
108  #define ROS_SOCKETS_ASYNCHRONOUS_CONNECT_RETURN EINPROGRESS
109 #endif
110 
111 /*****************************************************************************
112 ** Namespaces
113 *****************************************************************************/
114 
115 namespace ros {
116 
117 /*****************************************************************************
118 ** Cross Platform Types
119 *****************************************************************************/
120 
121 #ifdef WIN32
122  typedef SOCKET socket_fd_t;
123  typedef SOCKET signal_fd_t;
124  /* poll emulation support */
125  typedef struct socket_pollfd {
126  socket_fd_t fd; /* file descriptor */
127  short events; /* requested events */
128  short revents; /* returned events */
129  } socket_pollfd;
130 
131  typedef unsigned long int nfds_t;
132  #ifdef _MSC_VER
133  typedef int pid_t; /* return type for windows' _getpid */
134  #endif
135 #else
136  typedef int socket_fd_t;
137  typedef int signal_fd_t;
138  typedef struct pollfd socket_pollfd;
139 #endif
140 
142 
143 /*****************************************************************************
144 ** Functions
145 *****************************************************************************/
146 
147 ROSCPP_DECL int last_socket_error();
148 ROSCPP_DECL const char* last_socket_error_string();
149 ROSCPP_DECL bool last_socket_error_is_would_block();
150 ROSCPP_DECL pollfd_vector_ptr poll_sockets(int epfd, socket_pollfd *fds, nfds_t nfds, int timeout);
151 ROSCPP_DECL int set_non_blocking(socket_fd_t &socket);
152 ROSCPP_DECL int close_socket(socket_fd_t &socket);
153 ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2]);
154 
155 ROSCPP_DECL int create_socket_watcher();
156 ROSCPP_DECL void close_socket_watcher(int fd);
157 ROSCPP_DECL void add_socket_to_watcher(int epfd, int fd);
158 ROSCPP_DECL void del_socket_from_watcher(int epfd, int fd);
159 ROSCPP_DECL void set_events_on_socket(int epfd, int fd, int events);
160 
161 /*****************************************************************************
162 ** Inlines - almost direct api replacements, should stay fast.
163 *****************************************************************************/
164 
170 inline void close_signal_pair(signal_fd_t signal_pair[2]) {
171 #ifdef WIN32 // use a socket pair
172  ::closesocket(signal_pair[0]);
173  ::closesocket(signal_pair[1]);
174 #else // use a socket pair on mingw or pipe pair on linux, either way, close works
175  ::close(signal_pair[0]);
176  ::close(signal_pair[1]);
177 #endif
178 }
179 
185 #ifdef _MSC_VER
186  inline int write_signal(const signal_fd_t &signal, const char *buffer, const unsigned int &nbyte) {
187  return ::send(signal, buffer, nbyte, 0);
188 // return write(signal, buffer, nbyte);
189  }
190 #else
191  inline ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte) {
192  return write(signal, buffer, nbyte);
193  }
194 #endif
195 
196 
202 #ifdef _MSC_VER
203  inline int read_signal(const signal_fd_t &signal, char *buffer, const unsigned int &nbyte) {
204  return ::recv(signal, buffer, nbyte, 0);
205 // return _read(signal, buffer, nbyte);
206  }
207 #else
208  inline ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte) {
209  return read(signal, buffer, nbyte);
210  }
211 #endif
212 
213 } // namespace ros
214 
215 #endif /* ROSCPP_IO_H_ */
216 
ROSCPP_DECL void del_socket_from_watcher(int epfd, int fd)
Definition: io.cpp:137
ROSCPP_DECL int create_socket_watcher()
Definition: io.cpp:102
ROSCPP_DECL int set_non_blocking(socket_fd_t &socket)
Definition: io.cpp:371
int signal_fd_t
Definition: io.h:137
int socket_fd_t
Definition: io.h:136
ROSCPP_DECL void close_socket_watcher(int fd)
Definition: io.cpp:114
ROSCPP_DECL const char * last_socket_error_string()
Definition: io.cpp:74
ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte)
Definition: io.h:191
ROSCPP_DECL bool last_socket_error_is_would_block()
Definition: io.cpp:86
ROSCPP_DECL pollfd_vector_ptr poll_sockets(int epfd, socket_pollfd *fds, nfds_t nfds, int timeout)
A cross platform polling function for sockets.
Definition: io.cpp:184
ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte)
Definition: io.h:208
struct pollfd socket_pollfd
Definition: io.h:138
ROSCPP_DECL void set_events_on_socket(int epfd, int fd, int events)
Definition: io.cpp:149
void close_signal_pair(signal_fd_t signal_pair[2])
Definition: io.h:170
ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2])
Definition: io.cpp:416
boost::shared_ptr< std::vector< socket_pollfd > > pollfd_vector_ptr
Definition: io.h:141
ROSCPP_DECL int last_socket_error()
Definition: io.cpp:67
ROSCPP_DECL int close_socket(socket_fd_t &socket)
Close the socket.
Definition: io.cpp:392
ROSCPP_DECL void add_socket_to_watcher(int epfd, int fd)
Definition: io.cpp:119


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:26