Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
Socket.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * Socket.h
17 * The UDP Socket interfaces
18 * Copyright (C) 2005 Simon Newton
19 *
20 * UDPSocket, allows sending and receiving UDP datagrams
21 */
22
23#ifndef INCLUDE_OLA_NETWORK_SOCKET_H_
24#define INCLUDE_OLA_NETWORK_SOCKET_H_
25
26#include <stdint.h>
27
28// On MinGW, SocketAddress.h pulls in WinSock2.h, which needs to be after
29// WinSock2.h, hence this order
31
32#include <ola/Callback.h>
33#include <ola/base/Macro.h>
34#include <ola/io/Descriptor.h>
35#include <ola/io/IOQueue.h>
37#include <string>
38
39namespace ola {
40namespace network {
41
49 public:
52
57 virtual bool Init() = 0;
58
64 virtual bool Bind(const IPV4SocketAddress &endpoint) = 0;
65
71 virtual bool GetSocketAddress(IPV4SocketAddress *address) const = 0;
72
77 virtual bool Close() = 0;
78
79 virtual ola::io::DescriptorHandle ReadDescriptor() const = 0;
80 virtual ola::io::DescriptorHandle WriteDescriptor() const = 0;
81
92 virtual ssize_t SendTo(const uint8_t *buffer,
93 unsigned int size,
94 const IPV4Address &ip,
95 unsigned short port) const = 0;
96
104 virtual ssize_t SendTo(const uint8_t *buffer,
105 unsigned int size,
106 const IPV4SocketAddress &dest) const = 0;
107
121 virtual ssize_t SendTo(ola::io::IOVecInterface *data,
122 const IPV4Address &ip,
123 unsigned short port) const = 0;
124
135 virtual ssize_t SendTo(ola::io::IOVecInterface *data,
136 const IPV4SocketAddress &dest) const = 0;
137
145 virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const = 0;
146
157 virtual bool RecvFrom(
158 uint8_t *buffer,
159 ssize_t *data_read,
160 IPV4Address &source) const = 0; // NOLINT(runtime/references)
161
173 virtual bool RecvFrom(
174 uint8_t *buffer,
175 ssize_t *data_read,
176 IPV4Address &source, // NOLINT(runtime/references)
177 uint16_t &port) const = 0; // NOLINT(runtime/references)
178
187 virtual bool RecvFrom(uint8_t *buffer,
188 ssize_t *data_read,
189 IPV4SocketAddress *source) = 0;
190
195 virtual bool EnableBroadcast() = 0;
196
201 virtual bool SetMulticastInterface(const IPV4Address &iface) = 0;
202
210 virtual bool JoinMulticast(const IPV4Address &iface,
211 const IPV4Address &group,
212 bool multicast_loop = false) = 0;
213
220 virtual bool LeaveMulticast(const IPV4Address &iface,
221 const IPV4Address &group) = 0;
222
228 virtual bool SetTos(uint8_t tos) = 0;
229
230 private:
231 UDPSocketInterface(const UDPSocketInterface &) = delete;
232 const UDPSocketInterface &operator=(const UDPSocketInterface &) = delete;
233};
234
235
236/*
237 * A UDPSocket (non connected)
238 */
240 public:
241 UDPSocket()
243 m_handle(ola::io::INVALID_DESCRIPTOR),
244 m_bound_to_port(false) {}
245 ~UDPSocket() { Close(); }
246 bool Init();
247 bool Bind(const IPV4SocketAddress &endpoint);
248
249 bool GetSocketAddress(IPV4SocketAddress *address) const;
250
251 bool Close();
252 ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
253 ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
254 ssize_t SendTo(const uint8_t *buffer,
255 unsigned int size,
256 const IPV4Address &ip,
257 unsigned short port) const;
258 ssize_t SendTo(const uint8_t *buffer,
259 unsigned int size,
260 const IPV4SocketAddress &dest) const;
261 ssize_t SendTo(ola::io::IOVecInterface *data,
262 const IPV4Address &ip,
263 unsigned short port) const;
264 ssize_t SendTo(ola::io::IOVecInterface *data,
265 const IPV4SocketAddress &dest) const;
266
267 bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
268 bool RecvFrom(uint8_t *buffer,
269 ssize_t *data_read,
270 IPV4Address &source) const; // NOLINT(runtime/references)
271 bool RecvFrom(uint8_t *buffer,
272 ssize_t *data_read,
273 IPV4Address &source, // NOLINT(runtime/references)
274 uint16_t &port) const; // NOLINT(runtime/references)
275
276 bool RecvFrom(uint8_t *buffer,
277 ssize_t *data_read,
278 IPV4SocketAddress *source);
279
280 bool EnableBroadcast();
281 bool SetMulticastInterface(const IPV4Address &iface);
282 bool JoinMulticast(const IPV4Address &iface,
283 const IPV4Address &group,
284 bool multicast_loop = false);
285 bool LeaveMulticast(const IPV4Address &iface,
286 const IPV4Address &group);
287
288 bool SetTos(uint8_t tos);
289
290 private:
291 ola::io::DescriptorHandle m_handle;
292 bool m_bound_to_port;
293
294 UDPSocket(const UDPSocket &) = delete;
295 const UDPSocket &operator=(const UDPSocket &) = delete;
296};
297} // namespace network
298} // namespace ola
299#endif // INCLUDE_OLA_NETWORK_SOCKET_H_
Represents an IPv4 Address.
Helper macros.
Represents Socket Addresses.
A file descriptor that supports both read & write.
Definition Descriptor.h:201
Definition IOVecInterface.h:53
Represents a IPv4 Address.
Definition IPV4Address.h:55
An IPv4 SocketAddress.
Definition SocketAddress.h:78
Definition Socket.h:239
bool Bind(const IPV4SocketAddress &endpoint)
Bind this socket to an external address:port.
Definition Socket.cpp:107
ola::io::DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition Socket.h:252
bool JoinMulticast(const IPV4Address &iface, const IPV4Address &group, bool multicast_loop=false)
Join a multicast group.
Definition Socket.cpp:387
bool Init()
Initialize the socket.
Definition Socket.cpp:84
ola::io::DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition Socket.h:253
bool SetMulticastInterface(const IPV4Address &iface)
Set the outgoing interface to be used for multicast transmission.
Definition Socket.cpp:367
bool GetSocketAddress(IPV4SocketAddress *address) const
Return the local address this socket is bound to.
Definition Socket.cpp:158
bool LeaveMulticast(const IPV4Address &iface, const IPV4Address &group)
Leave a multicast group.
Definition Socket.cpp:427
ssize_t SendTo(const uint8_t *buffer, unsigned int size, const IPV4Address &ip, unsigned short port) const
Send data on this UDPSocket.
Definition Socket.cpp:193
bool EnableBroadcast()
Enable broadcasting for this socket.
Definition Socket.cpp:345
bool Close()
Close the socket.
Definition Socket.cpp:171
bool SetTos(uint8_t tos)
Set the tos field for a socket.
Definition Socket.cpp:450
bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const
Receive data.
Definition Socket.cpp:282
The interface for UDPSockets.
Definition Socket.h:48
virtual ola::io::DescriptorHandle ReadDescriptor() const =0
Returns the read descriptor for this socket.
virtual bool SetTos(uint8_t tos)=0
Set the tos field for a socket.
virtual bool Bind(const IPV4SocketAddress &endpoint)=0
Bind this socket to an external address:port.
virtual bool LeaveMulticast(const IPV4Address &iface, const IPV4Address &group)=0
Leave a multicast group.
virtual bool Init()=0
Initialize the socket.
virtual ola::io::DescriptorHandle WriteDescriptor() const =0
Returns the write descriptor for this socket.
virtual ssize_t SendTo(const uint8_t *buffer, unsigned int size, const IPV4Address &ip, unsigned short port) const =0
Send data on this UDPSocket.
virtual ssize_t SendTo(ola::io::IOVecInterface *data, const IPV4SocketAddress &dest) const =0
Send data from an IOVecInterface.
virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read, IPV4Address &source, uint16_t &port) const =0
Receive data and record the src address & port.
virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const =0
Receive data.
virtual bool EnableBroadcast()=0
Enable broadcasting for this socket.
virtual bool SetMulticastInterface(const IPV4Address &iface)=0
Set the outgoing interface to be used for multicast transmission.
virtual bool Close()=0
Close the socket.
virtual bool GetSocketAddress(IPV4SocketAddress *address) const =0
Return the local address this socket is bound to.
virtual ssize_t SendTo(const uint8_t *buffer, unsigned int size, const IPV4SocketAddress &dest) const =0
Send data on this UDPSocket.
virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read, IPV4SocketAddress *source)=0
Receive a datagram on the UDP Socket.
virtual ssize_t SendTo(ola::io::IOVecInterface *data, const IPV4Address &ip, unsigned short port) const =0
Send data from an IOVecInterface.
virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read, IPV4Address &source) const =0
Receive data.
virtual bool JoinMulticast(const IPV4Address &iface, const IPV4Address &group, bool multicast_loop=false)=0
Join a multicast group.
The namespace containing all OLA symbols.
Definition Credentials.cpp:44