Lely core libraries  2.2.5
sock.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_IO_SOCK_HPP_
23 #define LELY_IO_SOCK_HPP_
24 
25 #ifndef __cplusplus
26 #error "include <lely/io/sock.h> for the C interface"
27 #endif
28 
29 #include <lely/io/io.hpp>
30 #include <lely/io/sock.h>
31 
32 #include <utility>
33 
34 namespace lely {
35 
37 class IOSock : public IOHandle {
38  public:
39  IOSock(int domain, int type) : IOHandle(io_open_socket(domain, type)) {
40  if (!operator bool()) throw_or_abort(bad_init());
41  }
42 
43  IOSock(const IOSock& sock) noexcept : IOHandle(sock) {}
44 
45  IOSock(IOSock&& sock) noexcept : IOHandle(::std::forward<IOSock>(sock)) {}
46 
47  IOSock&
48  operator=(const IOSock& sock) noexcept {
49  IOHandle::operator=(sock);
50  return *this;
51  }
52 
53  IOSock&
54  operator=(IOSock&& sock) noexcept {
55  IOHandle::operator=(::std::forward<IOSock>(sock));
56  return *this;
57  }
58 
59  static int
60  open(int domain, int type, IOSock sock[2]) noexcept {
61  io_handle_t handle_vector[2];
62  if (io_open_socketpair(domain, type, handle_vector) == -1) return -1;
63  sock[0] = IOSock(handle_vector[0]);
64  sock[1] = IOSock(handle_vector[1]);
65  return 0;
66  }
67 
68  ssize_t
69  recv(void* buf, size_t nbytes, io_addr_t* addr = 0, int flags = 0) noexcept {
70  return io_recv(*this, buf, nbytes, addr, flags);
71  }
72 
73  ssize_t
74  send(const void* buf, size_t nbytes, const io_addr_t* addr = 0,
75  int flags = 0) noexcept {
76  return io_send(*this, buf, nbytes, addr, flags);
77  }
78 
79  IOSock
80  accept(io_addr_t* addr = 0) noexcept {
81  return IOSock(io_accept(*this, addr));
82  }
83 
84  int
85  connect(const io_addr_t& addr) noexcept {
86  return io_connect(*this, &addr);
87  }
88 
89  int
90  get_domain() const noexcept {
91  return io_sock_get_domain(*this);
92  }
93  int
94  get_type() const noexcept {
95  return io_sock_get_type(*this);
96  }
97 
98  int
99  bind(const io_addr_t& addr) noexcept {
100  return io_sock_bind(*this, &addr);
101  }
102 
103  int
104  listen(int backlog = 0) noexcept {
105  return io_sock_listen(*this, backlog);
106  }
107 
108  int
109  shutdown(int how) noexcept {
110  return io_sock_shutdown(*this, how);
111  }
112 
113  int
114  getSockname(io_addr_t& addr) const noexcept {
115  return io_sock_get_sockname(*this, &addr);
116  }
117 
118  int
119  getPeername(io_addr_t& addr) const noexcept {
120  return io_sock_get_peername(*this, &addr);
121  }
122 
123  static int
124  getMaxConn() noexcept {
125  return io_sock_get_maxconn();
126  }
127 
128  int
129  getAcceptConn() const noexcept {
130  return io_sock_get_acceptconn(*this);
131  }
132 
133  int
134  getBroadcast() const noexcept {
135  return io_sock_get_broadcast(*this);
136  }
137 
138  int
139  setBroadcast(bool broadcast) noexcept {
140  return io_sock_set_broadcast(*this, broadcast);
141  }
142 
143  int
144  getDebug() const noexcept {
145  return io_sock_get_debug(*this);
146  }
147 
148  int
149  setDebug(bool debug) noexcept {
150  return io_sock_set_debug(*this, debug);
151  }
152 
153  int
154  getDontRoute() const noexcept {
155  return io_sock_get_dontroute(*this);
156  }
157 
158  int
159  setDontRoute(bool dontroute) noexcept {
160  return io_sock_set_dontroute(*this, dontroute);
161  }
162 
163  int
164  getError(int& error) noexcept {
165  return io_sock_get_error(*this, &error);
166  }
167 
168  int
169  getKeepAlive() const noexcept {
170  return io_sock_get_keepalive(*this);
171  }
172 
173  int
174  setKeepAlive(bool keepalive, int time = 0, int interval = 0) noexcept {
175  return io_sock_set_keepalive(*this, keepalive, time, interval);
176  }
177 
178  int
179  getLinger() const noexcept {
180  return io_sock_get_linger(*this);
181  }
182 
183  int
184  setLinger(int time = 0) noexcept {
185  return io_sock_set_linger(*this, time);
186  }
187 
188  int
189  getOOBInline() const noexcept {
190  return io_sock_get_oobinline(*this);
191  }
192 
193  int
194  setOOBInline(bool oobinline) noexcept {
195  return io_sock_set_oobinline(*this, oobinline);
196  }
197 
198  int
199  getRcvBuf() const noexcept {
200  return io_sock_get_rcvbuf(*this);
201  }
202 
203  int
204  setRcvBuf(int size) noexcept {
205  return io_sock_set_rcvbuf(*this, size);
206  }
207 
208  int
209  setRcvTimeo(int timeout) noexcept {
210  return io_sock_set_rcvtimeo(*this, timeout);
211  }
212 
213  int
214  getReuseAddr() const noexcept {
215  return io_sock_get_reuseaddr(*this);
216  }
217 
218  int
219  setReuseAddr(bool reuseaddr) noexcept {
220  return io_sock_set_reuseaddr(*this, reuseaddr);
221  }
222 
223  int
224  getSndBuf() const noexcept {
225  return io_sock_get_sndbuf(*this);
226  }
227 
228  int
229  setSndBuf(int size) noexcept {
230  return io_sock_set_sndbuf(*this, size);
231  }
232 
233  int
234  setSndTimeo(int timeout) noexcept {
235  return io_sock_set_sndtimeo(*this, timeout);
236  }
237 
238  int
239  getTCPNoDelay() const noexcept {
240  return io_sock_get_tcp_nodelay(*this);
241  }
242 
243  int
244  setTCPNoDelay(bool nodelay) noexcept {
245  return io_sock_set_tcp_nodelay(*this, nodelay);
246  }
247 
248  ssize_t
249  getNRead() const noexcept {
250  return io_sock_get_nread(*this);
251  }
252 
253  int
254  getMcastLoop() const noexcept {
255  return io_sock_get_mcast_loop(*this);
256  }
257 
258  int
259  setMcastLoop(bool loop) noexcept {
260  return io_sock_set_mcast_loop(*this, loop);
261  }
262 
263  int
264  getMcastTTL() const noexcept {
265  return io_sock_get_mcast_ttl(*this);
266  }
267 
268  int
269  setMcastTTL(int ttl) noexcept {
270  return io_sock_set_mcast_ttl(*this, ttl);
271  }
272 
273  int
274  mcastJoinGroup(unsigned int index, const io_addr_t& group) noexcept {
275  return io_sock_mcast_join_group(*this, index, &group);
276  }
277 
278  int
279  mcastBlockSource(unsigned int index, const io_addr_t& group,
280  const io_addr_t& source) noexcept {
281  return io_sock_mcast_block_source(*this, index, &group, &source);
282  }
283 
284  int
285  mcastUnblockSource(unsigned int index, const io_addr_t& group,
286  const io_addr_t& source) noexcept {
287  return io_sock_mcast_unblock_source(*this, index, &group, &source);
288  }
289 
290  int
291  mcastLeaveGroup(unsigned int index, const io_addr_t& group) noexcept {
292  return io_sock_mcast_leave_group(*this, index, &group);
293  }
294 
295  int
296  mcastJoinSourceGroup(unsigned int index, const io_addr_t& group,
297  const io_addr_t& source) noexcept {
298  return io_sock_mcast_join_source_group(*this, index, &group, &source);
299  }
300 
301  int
302  mcastLeaveSourceGroup(unsigned int index, const io_addr_t& group,
303  const io_addr_t& source) noexcept {
304  return io_sock_mcast_leave_source_group(*this, index, &group, &source);
305  }
306 
307  protected:
308  IOSock(io_handle_t handle) noexcept : IOHandle(handle) {}
309 };
310 
311 } // namespace lely
312 
313 #endif // !LELY_IO_SOCK_HPP_
int io_sock_mcast_unblock_source(io_handle_t handle, unsigned int index, const io_addr_t *group, const io_addr_t *source)
Unblocks data from a given source to a given multicast group.
Definition: sock.c:1258
ssize_t io_send(io_handle_t handle, const void *buf, size_t nbytes, const io_addr_t *addr, int flags)
Performs a send operation on a network socket.
Definition: sock.c:319
int io_sock_get_keepalive(io_handle_t handle)
Checks if the TCP keep-alive option is enabled for a socket.
Definition: sock.c:688
int io_sock_get_mcast_loop(io_handle_t handle)
Checks if the loopback of outgoing multicast datagrams is enabled for a socket.
Definition: sock.c:1046
int io_sock_listen(io_handle_t handle, int backlog)
Marks a connection-mode socket (IO_SOCK_STREAM) as accepting connections.
Definition: sock.c:419
int io_open_socketpair(int domain, int type, io_handle_t handle_vector[2])
Opens a pair of connected sockets.
Definition: sock.c:181
int io_sock_set_linger(io_handle_t handle, int time)
Sets the time (in seconds) io_close() will wait for unsent messages to be sent.
Definition: sock.c:773
int io_sock_get_oobinline(io_handle_t handle)
Checks if out-of-band data is received in the normal data stream of a socket.
Definition: sock.c:793
int io_sock_mcast_join_source_group(io_handle_t handle, unsigned int index, const io_addr_t *group, const io_addr_t *source)
Joins a source-specific multicast group.
Definition: sock.c:1320
int io_sock_set_broadcast(io_handle_t handle, int broadcast)
Enables a socket to send broadcast messages if broadcast is non-zero, and disables this option otherw...
Definition: sock.c:574
int io_sock_set_sndtimeo(io_handle_t handle, int timeout)
Sets the timeout (in milliseconds) of a send operation on a socket.
Definition: sock.c:955
int io_sock_get_linger(io_handle_t handle)
Obtains the linger time (in seconds) of a socket.
Definition: sock.c:756
This header file is part of the I/O library; it contains the C++ interface of the I/O device handle...
int io_connect(io_handle_t handle, const io_addr_t *addr)
Connects a socket to a network address.
Definition: sock.c:354
int io_sock_get_maxconn(void)
Returns the maximum queue length for pending connections.
Definition: sock.c:526
int io_sock_shutdown(io_handle_t handle, int how)
Causes all or part of a full-duplex connection on a socket to be shut down.
Definition: sock.c:430
int io_sock_get_sockname(io_handle_t handle, io_addr_t *addr)
Obtains the locally-bound name of a socket and stores the resulting address in *addr.
Definition: sock.c:466
An opaque network address type.
Definition: addr.h:30
int io_sock_set_rcvtimeo(io_handle_t handle, int timeout)
Sets the timeout (in milliseconds) of a receive operation on a socket.
Definition: sock.c:864
int io_sock_set_mcast_loop(io_handle_t handle, int loop)
Enables the loopback of outgoing multicast datagrams for a socket if loop is non-zero, and disables this option otherwise (enabled by default).
Definition: sock.c:1086
int io_sock_get_error(io_handle_t handle, int *perror)
Obtains and clears the current error number of a socket, and stores the value in *perror.
Definition: sock.c:673
int io_sock_set_sndbuf(io_handle_t handle, int size)
Sets the size (in bytes) of the send buffer of a socket.
Definition: sock.c:941
int io_sock_get_tcp_nodelay(io_handle_t handle)
Checks if Nagle&#39;s algorithm for send coalescing is enabled for a socket.
Definition: sock.c:975
int io_sock_get_acceptconn(io_handle_t handle)
Checks if a socket is currently listening for incoming connections.
Definition: sock.c:532
int io_sock_set_keepalive(io_handle_t handle, int keepalive, int time, int interval)
Enables or disables the TCP keep-alive option for a socket (disabled by default). ...
Definition: sock.c:709
int io_sock_mcast_block_source(io_handle_t handle, unsigned int index, const io_addr_t *group, const io_addr_t *source)
Blocks data from a given source to a given multicast group.
Definition: sock.c:1226
int io_sock_mcast_leave_group(io_handle_t handle, unsigned int index, const io_addr_t *group)
Leaves an any-source multicast group.
Definition: sock.c:1290
io_handle_t io_accept(io_handle_t handle, io_addr_t *addr)
Accepts an incoming connection on a listening socket.
Definition: sock.c:337
ssize_t io_recv(io_handle_t handle, void *buf, size_t nbytes, io_addr_t *addr, int flags)
Performs a receive operation on a network socket.
Definition: sock.c:301
int io_sock_get_peername(io_handle_t handle, io_addr_t *addr)
Obtains the peer address of a socket and stores the result in *addr.
Definition: sock.c:496
ssize_t io_sock_get_nread(io_handle_t handle)
Obtains the amount of data (in bytes) in the input buffer of a socket.
Definition: sock.c:1016
int io_sock_get_broadcast(io_handle_t handle)
Checks if a socket is allowed to send broadcast messages.
Definition: sock.c:553
int io_sock_set_reuseaddr(io_handle_t handle, int reuseaddr)
Enables a socket to be bound to an address that is already in use if reuseaddr is non-zero...
Definition: sock.c:905
An I/O device handle.
Definition: io.hpp:35
int io_sock_mcast_join_group(io_handle_t handle, unsigned int index, const io_addr_t *group)
Joins an any-source multicast group.
Definition: sock.c:1196
The type of objects thrown as exceptions to report a system error with an associated error code...
Definition: exception.hpp:54
This header file is part of the I/O library; it contains the network socket declarations.
int io_sock_set_dontroute(io_handle_t handle, int dontroute)
Bypasses normal routing for a socket if dontroute is non-zero, and disables this option otherwise (di...
Definition: sock.c:654
int io_sock_get_mcast_ttl(io_handle_t handle)
Obtains the TTL (time to live) value for IP multicast traffic on a socket.
Definition: sock.c:1121
int io_sock_get_domain(io_handle_t handle)
Obtains the domain of a socket (the first parameter in a call to io_open_socket() or io_open_socketpa...
Definition: sock.c:373
The type of objects thrown as exceptions to report a failure to initialize an instantiation of a C ty...
Definition: c_type.hpp:38
int io_sock_bind(io_handle_t handle, const io_addr_t *addr)
Binds a local network address to a socket.
Definition: sock.c:405
int io_sock_get_debug(io_handle_t handle)
Checks if debugging is enabled for a socket.
Definition: sock.c:593
int io_sock_mcast_leave_source_group(io_handle_t handle, unsigned int index, const io_addr_t *group, const io_addr_t *source)
Leaves a source-specific multicast group.
Definition: sock.c:1352
int io_sock_get_reuseaddr(io_handle_t handle)
Checks if a socket is allowed to be bound to an address that is already in use.
Definition: sock.c:884
int io_sock_set_debug(io_handle_t handle, int debug)
Enables (platform dependent) debugging output for a socket if debug is non-zero, and disables this op...
Definition: sock.c:614
int io_sock_set_mcast_ttl(io_handle_t handle, int ttl)
Sets the TTL (time to live) value for IP multicast traffic on a socket (the default is 1)...
Definition: sock.c:1161
int io_sock_set_oobinline(io_handle_t handle, int oobinline)
Requests that out-of-band data is placed into the normal data stream of socket if oobinline is non-ze...
Definition: sock.c:814
int io_sock_get_type(io_handle_t handle)
Obtains the type of a network socket (the second parameter in a call to io_open_socket() or io_open_s...
Definition: sock.c:389
A sock I/O device handle.
Definition: sock.hpp:37
A network socket.
Definition: sock.c:36
Definition: buf.hpp:32
int io_sock_get_sndbuf(io_handle_t handle)
Obtains the size (in bytes) of the send buffer of a socket.
Definition: sock.c:924
io_handle_t io_open_socket(int domain, int type)
Opens a network socket.
Definition: sock.c:74
int io_sock_get_rcvbuf(io_handle_t handle)
Obtains the size (in bytes) of the receive buffer of a socket.
Definition: sock.c:833
int io_sock_set_rcvbuf(io_handle_t handle, int size)
Sets the size (in bytes) of the receive buffer of a socket.
Definition: sock.c:850
int io_sock_set_tcp_nodelay(io_handle_t handle, int nodelay)
Disables Nagle&#39;s algorithm for send coalescing if nodelay is non-zero, and enables it otherwise...
Definition: sock.c:996
An I/O device handle.
Definition: handle.h:41
#define throw_or_abort(e)
If exceptions are disabled, aborts the process instead of throwing an exception.
Definition: exception.hpp:38
int io_sock_get_dontroute(io_handle_t handle)
Checks if routing is disabled for a socket.
Definition: sock.c:633