Open Lighting Architecture  Latest Git
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 
39 namespace ola {
40 namespace network {
41 
49  public:
51  ~UDPSocketInterface() {}
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  DISALLOW_COPY_AND_ASSIGN(UDPSocketInterface);
232 };
233 
234 
235 /*
236  * A UDPSocket (non connected)
237  */
239  public:
240  UDPSocket()
241  : UDPSocketInterface(),
242  m_handle(ola::io::INVALID_DESCRIPTOR),
243  m_bound_to_port(false) {}
244  ~UDPSocket() { Close(); }
245  bool Init();
246  bool Bind(const IPV4SocketAddress &endpoint);
247 
248  bool GetSocketAddress(IPV4SocketAddress *address) const;
249 
250  bool Close();
251  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
252  ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
253  ssize_t SendTo(const uint8_t *buffer,
254  unsigned int size,
255  const IPV4Address &ip,
256  unsigned short port) const;
257  ssize_t SendTo(const uint8_t *buffer,
258  unsigned int size,
259  const IPV4SocketAddress &dest) const;
260  ssize_t SendTo(ola::io::IOVecInterface *data,
261  const IPV4Address &ip,
262  unsigned short port) const;
263  ssize_t SendTo(ola::io::IOVecInterface *data,
264  const IPV4SocketAddress &dest) const;
265 
266  bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
267  bool RecvFrom(uint8_t *buffer,
268  ssize_t *data_read,
269  IPV4Address &source) const; // NOLINT(runtime/references)
270  bool RecvFrom(uint8_t *buffer,
271  ssize_t *data_read,
272  IPV4Address &source, // NOLINT(runtime/references)
273  uint16_t &port) const; // NOLINT(runtime/references)
274 
275  bool RecvFrom(uint8_t *buffer,
276  ssize_t *data_read,
277  IPV4SocketAddress *source);
278 
279  bool EnableBroadcast();
280  bool SetMulticastInterface(const IPV4Address &iface);
281  bool JoinMulticast(const IPV4Address &iface,
282  const IPV4Address &group,
283  bool multicast_loop = false);
284  bool LeaveMulticast(const IPV4Address &iface,
285  const IPV4Address &group);
286 
287  bool SetTos(uint8_t tos);
288 
289  private:
290  ola::io::DescriptorHandle m_handle;
291  bool m_bound_to_port;
292 
294 };
295 } // namespace network
296 } // namespace ola
297 #endif // INCLUDE_OLA_NETWORK_SOCKET_H_
virtual bool LeaveMulticast(const IPV4Address &iface, const IPV4Address &group)=0
Leave a multicast group.
Represents Socket Addresses.
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 IPV4Address &ip, unsigned short port) const =0
Send data on this UDPSocket.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: Socket.h:238
The interface for UDPSockets.
Definition: Socket.h:48
virtual bool SetMulticastInterface(const IPV4Address &iface)=0
Set the outgoing interface to be used for multicast transmission.
virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const =0
Receive data.
ola::io::DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition: Socket.h:252
ola::io::DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition: Socket.h:251
virtual bool Init()=0
Initialize the socket.
virtual bool JoinMulticast(const IPV4Address &iface, const IPV4Address &group, bool multicast_loop=false)=0
Join a multicast group.
Represents a IPv4 Address.
Definition: IPV4Address.h:55
A file descriptor that supports both read & write.
Definition: Descriptor.h:200
virtual ola::io::DescriptorHandle WriteDescriptor() const =0
Returns the write descriptor for this socket.
virtual bool Bind(const IPV4SocketAddress &endpoint)=0
Bind this socket to an external address:port.
virtual ola::io::DescriptorHandle ReadDescriptor() const =0
Returns the read descriptor for this socket.
virtual bool Close()=0
Close the socket.
Definition: IOVecInterface.h:53
Represents an IPv4 Address.
Helper macros.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
virtual bool SetTos(uint8_t tos)=0
Set the tos field for a socket.
An IPv4 SocketAddress.
Definition: SocketAddress.h:78
virtual bool EnableBroadcast()=0
Enable broadcasting for this socket.