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 
141 /*****************************************************************************
142 ** Functions
143 *****************************************************************************/
144 
145 ROSCPP_DECL int last_socket_error();
146 ROSCPP_DECL const char* last_socket_error_string();
147 ROSCPP_DECL bool last_socket_error_is_would_block();
148 ROSCPP_DECL int poll_sockets(socket_pollfd *fds, nfds_t nfds, int timeout);
149 ROSCPP_DECL int set_non_blocking(socket_fd_t &socket);
150 ROSCPP_DECL int close_socket(socket_fd_t &socket);
151 ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2]);
152 
153 /*****************************************************************************
154 ** Inlines - almost direct api replacements, should stay fast.
155 *****************************************************************************/
156 
162 inline void close_signal_pair(signal_fd_t signal_pair[2]) {
163 #ifdef WIN32 // use a socket pair
164  ::closesocket(signal_pair[0]);
165  ::closesocket(signal_pair[1]);
166 #else // use a socket pair on mingw or pipe pair on linux, either way, close works
167  ::close(signal_pair[0]);
168  ::close(signal_pair[1]);
169 #endif
170 }
171 
177 #ifdef _MSC_VER
178  inline int write_signal(const signal_fd_t &signal, const char *buffer, const unsigned int &nbyte) {
179  return ::send(signal, buffer, nbyte, 0);
180 // return write(signal, buffer, nbyte);
181  }
182 #else
183  inline ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte) {
184  return write(signal, buffer, nbyte);
185  }
186 #endif
187 
188 
194 #ifdef _MSC_VER
195  inline int read_signal(const signal_fd_t &signal, char *buffer, const unsigned int &nbyte) {
196  return ::recv(signal, buffer, nbyte, 0);
197 // return _read(signal, buffer, nbyte);
198  }
199 #else
200  inline ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte) {
201  return read(signal, buffer, nbyte);
202  }
203 #endif
204 
205 } // namespace ros
206 
207 #endif /* ROSCPP_IO_H_ */
208 
ROSCPP_DECL int set_non_blocking(socket_fd_t &socket)
Definition: io.cpp:237
int signal_fd_t
Definition: io.h:137
int socket_fd_t
Definition: io.h:136
ROSCPP_DECL const char * last_socket_error_string()
Definition: io.cpp:62
ssize_t write_signal(const signal_fd_t &signal, const void *buffer, const size_t &nbyte)
Definition: io.h:183
ROSCPP_DECL bool last_socket_error_is_would_block()
Definition: io.cpp:74
ROSCPP_DECL int poll_sockets(socket_pollfd *fds, nfds_t nfds, int timeout)
A cross platform polling function for sockets.
Definition: io.cpp:104
ssize_t read_signal(const signal_fd_t &signal, void *buffer, const size_t &nbyte)
Definition: io.h:200
struct pollfd socket_pollfd
Definition: io.h:138
void close_signal_pair(signal_fd_t signal_pair[2])
Definition: io.h:162
ROSCPP_DECL int create_signal_pair(signal_fd_t signal_pair[2])
Definition: io.cpp:282
ROSCPP_DECL int last_socket_error()
Definition: io.cpp:55
ROSCPP_DECL int close_socket(socket_fd_t &socket)
Close the socket.
Definition: io.cpp:258


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Wed Dec 20 2017 03:58:41