Open Lighting Architecture  0.9.6
 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(
160  uint8_t *buffer,
161  ssize_t *data_read,
162  IPV4Address &source) const = 0; // NOLINT(runtime/references)
163 
175  virtual bool RecvFrom(
176  uint8_t *buffer,
177  ssize_t *data_read,
178  IPV4Address &source, // NOLINT(runtime/references)
179  uint16_t &port) const = 0; // NOLINT(runtime/references)
180 
189  virtual bool RecvFrom(uint8_t *buffer,
190  ssize_t *data_read,
191  IPV4SocketAddress *source) = 0;
192 
197  virtual bool EnableBroadcast() = 0;
198 
203  virtual bool SetMulticastInterface(const IPV4Address &iface) = 0;
204 
212  virtual bool JoinMulticast(const IPV4Address &iface,
213  const IPV4Address &group,
214  bool multicast_loop = false) = 0;
215 
222  virtual bool LeaveMulticast(const IPV4Address &iface,
223  const IPV4Address &group) = 0;
224 
230  virtual bool SetTos(uint8_t tos) = 0;
231 
232  private:
233  DISALLOW_COPY_AND_ASSIGN(UDPSocketInterface);
234 };
235 
236 
237 /*
238  * A UDPSocket (non connected)
239  */
241  public:
242  UDPSocket()
243  : UDPSocketInterface(),
244  m_handle(ola::io::INVALID_DESCRIPTOR),
245  m_bound_to_port(false) {}
246  ~UDPSocket() { Close(); }
247  bool Init();
248  bool Bind(const IPV4SocketAddress &endpoint);
249 
250  bool GetSocketAddress(IPV4SocketAddress *address) const;
251 
252  bool Close();
253  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
254  ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
255  ssize_t SendTo(const uint8_t *buffer,
256  unsigned int size,
257  const IPV4Address &ip,
258  unsigned short port) const;
259  ssize_t SendTo(const uint8_t *buffer,
260  unsigned int size,
261  const IPV4SocketAddress &dest) const;
262  ssize_t SendTo(ola::io::IOVecInterface *data,
263  const IPV4Address &ip,
264  unsigned short port) const;
265  ssize_t SendTo(ola::io::IOVecInterface *data,
266  const IPV4SocketAddress &dest) const;
267 
268  bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
269  bool RecvFrom(uint8_t *buffer,
270  ssize_t *data_read,
271  IPV4Address &source) const; // NOLINT(runtime/references)
272  bool RecvFrom(uint8_t *buffer,
273  ssize_t *data_read,
274  IPV4Address &source, // NOLINT(runtime/references)
275  uint16_t &port) const; // NOLINT(runtime/references)
276 
277  bool RecvFrom(uint8_t *buffer,
278  ssize_t *data_read,
279  IPV4SocketAddress *source);
280 
281  bool EnableBroadcast();
282  bool SetMulticastInterface(const IPV4Address &iface);
283  bool JoinMulticast(const IPV4Address &iface,
284  const IPV4Address &group,
285  bool multicast_loop = false);
286  bool LeaveMulticast(const IPV4Address &iface,
287  const IPV4Address &group);
288 
289  bool SetTos(uint8_t tos);
290 
291  private:
292  ola::io::DescriptorHandle m_handle;
293  bool m_bound_to_port;
294 
295  DISALLOW_COPY_AND_ASSIGN(UDPSocket);
296 };
297 } // namespace network
298 } // namespace ola
299 #endif // INCLUDE_OLA_NETWORK_SOCKET_H_