Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 Socket interfaces
18  * Copyright (C) 2005 Simon Newton
19  *
20  * - UDPSocket, allows sending and receiving UDP datagrams
21  * - TCPSocket, this represents a TCP connection to a remote endpoint
22  *
23  * AcceptingSocket is the interface that defines sockets which can spawn new
24  * ConnectedDescriptors. TCPAcceptingSocket is the only subclass and provides
25  * the accept() functionality.
26  */
27 
28 #ifndef INCLUDE_OLA_NETWORK_SOCKET_H_
29 #define INCLUDE_OLA_NETWORK_SOCKET_H_
30 
31 #include <stdint.h>
32 
33 #include <ola/Callback.h>
34 #include <ola/base/Macro.h>
35 #include <ola/io/Descriptor.h>
36 #include <ola/io/IOQueue.h>
39 #include <string>
40 
41 namespace ola {
42 namespace network {
43 
51  public:
53  ~UDPSocketInterface() {}
54 
59  virtual bool Init() = 0;
60 
66  virtual bool Bind(const IPV4SocketAddress &endpoint) = 0;
67 
73  virtual bool GetSocketAddress(IPV4SocketAddress *address) const = 0;
74 
79  virtual bool Close() = 0;
80 
81  virtual ola::io::DescriptorHandle ReadDescriptor() const = 0;
82  virtual ola::io::DescriptorHandle WriteDescriptor() const = 0;
83 
94  virtual ssize_t SendTo(const uint8_t *buffer,
95  unsigned int size,
96  const IPV4Address &ip,
97  unsigned short port) const = 0;
98 
106  virtual ssize_t SendTo(const uint8_t *buffer,
107  unsigned int size,
108  const IPV4SocketAddress &dest) const = 0;
109 
123  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
124  const IPV4Address &ip,
125  unsigned short port) const = 0;
126 
137  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
138  const IPV4SocketAddress &dest) const = 0;
139 
147  virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const = 0;
148 
159  virtual bool RecvFrom(uint8_t *buffer,
160  ssize_t *data_read,
161  IPV4Address &source) const = 0; // NOLINT
162 
174  virtual bool RecvFrom(uint8_t *buffer,
175  ssize_t *data_read,
176  IPV4Address &source, // NOLINT
177  uint16_t &port) const = 0; // NOLINT
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
270  bool RecvFrom(uint8_t *buffer,
271  ssize_t *data_read,
272  IPV4Address &source, // NOLINT
273  uint16_t &port) const; // NOLINT
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 
293  DISALLOW_COPY_AND_ASSIGN(UDPSocket);
294 };
295 } // namespace network
296 } // namespace ola
297 #endif // INCLUDE_OLA_NETWORK_SOCKET_H_