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* socket_error_string(int err);
149 ROSCPP_DECL const char* last_socket_error_string();
150 ROSCPP_DECL bool last_socket_error_is_would_block();
151 ROSCPP_DECL pollfd_vector_ptr poll_sockets(int epfd, socket_pollfd *fds, nfds_t nfds, int timeout);
152 ROSCPP_DECL int is_async_connected(socket_fd_t &socket, int &err);
153 ROSCPP_DECL int set_non_blocking(socket_fd_t &socket);
154 ROSCPP_DECL int close_socket(socket_fd_t &socket);
155 ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2]);
156 
157 ROSCPP_DECL int create_socket_watcher();
158 ROSCPP_DECL void close_socket_watcher(int fd);
159 ROSCPP_DECL void add_socket_to_watcher(int epfd, int fd);
160 ROSCPP_DECL void del_socket_from_watcher(int epfd, int fd);
161 ROSCPP_DECL void set_events_on_socket(int epfd, int fd, int events);
162 
163 /*****************************************************************************
164 ** Inlines - almost direct api replacements, should stay fast.
165 *****************************************************************************/
166 
172 inline void close_signal_pair(signal_fd_t signal_pair[2]) {
173 #ifdef WIN32 // use a socket pair
174  ::closesocket(signal_pair[0]);
175  ::closesocket(signal_pair[1]);
176 #else // use a socket pair on mingw or pipe pair on linux, either way, close works
177  ::close(signal_pair[0]);
178  ::close(signal_pair[1]);
179 #endif
180 }
181 
187 #ifdef _MSC_VER
188  inline int write_signal(const signal_fd_t &signal, const char *buffer, const unsigned int &nbyte) {
189  return ::send(signal, buffer, nbyte, 0);
190 // return write(signal, buffer, nbyte);
191  }
192 #else
193  inline ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte) {
194  return write(signal, buffer, nbyte);
195  }
196 #endif
197 
198 
204 #ifdef _MSC_VER
205  inline int read_signal(const signal_fd_t &signal, char *buffer, const unsigned int &nbyte) {
206  return ::recv(signal, buffer, nbyte, 0);
207 // return _read(signal, buffer, nbyte);
208  }
209 #else
210  inline ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte) {
211  return read(signal, buffer, nbyte);
212  }
213 #endif
214 
215 } // namespace ros
216 
217 #endif /* ROSCPP_IO_H_ */
218 
ros::socket_error_string
const ROSCPP_DECL char * socket_error_string(int err)
Definition: io.cpp:78
boost::shared_ptr
ros::create_signal_pair
ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2])
Definition: io.cpp:548
ros
ros::is_async_connected
ROSCPP_DECL int is_async_connected(socket_fd_t &socket, int &err)
Definition: io.cpp:384
ros::poll_sockets
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:196
ros::read_signal
ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte)
Definition: io.h:212
ros::close_signal_pair
void close_signal_pair(signal_fd_t signal_pair[2])
Definition: io.h:174
ros::write_signal
ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte)
Definition: io.h:195
ros::close_socket_watcher
ROSCPP_DECL void close_socket_watcher(int fd)
Definition: io.cpp:126
ros::del_socket_from_watcher
ROSCPP_DECL void del_socket_from_watcher(int epfd, int fd)
Definition: io.cpp:149
ros::last_socket_error_is_would_block
ROSCPP_DECL bool last_socket_error_is_would_block()
Definition: io.cpp:98
ros::add_socket_to_watcher
ROSCPP_DECL void add_socket_to_watcher(int epfd, int fd)
Definition: io.cpp:131
ros::socket_fd_t
int socket_fd_t
Definition: io.h:138
ros::last_socket_error
ROSCPP_DECL int last_socket_error()
Definition: io.cpp:70
ros::close_socket
ROSCPP_DECL int close_socket(socket_fd_t &socket)
Close the socket.
Definition: io.cpp:524
io.h
ros::signal_fd_t
int signal_fd_t
Definition: io.h:139
ros::pollfd_vector_ptr
boost::shared_ptr< std::vector< socket_pollfd > > pollfd_vector_ptr
Definition: io.h:143
ros::socket_pollfd
struct pollfd socket_pollfd
Definition: io.h:140
ros::set_events_on_socket
ROSCPP_DECL void set_events_on_socket(int epfd, int fd, int events)
Definition: io.cpp:161
ros::set_non_blocking
ROSCPP_DECL int set_non_blocking(socket_fd_t &socket)
Definition: io.cpp:503
ros::last_socket_error_string
const ROSCPP_DECL char * last_socket_error_string()
Definition: io.cpp:90
ros::create_socket_watcher
ROSCPP_DECL int create_socket_watcher()
Definition: io.cpp:114


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:35