Open Lighting Architecture  0.9.1
 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 
42 namespace ola {
43 namespace network {
44 
45 
46 /*
47  * The UDPSocketInterface.
48  * This is done as an Interface so we can mock it out for testing.
49  */
51  public:
53  ~UDPSocketInterface() {}
54  virtual bool Init() = 0;
55  virtual bool Bind(const IPV4SocketAddress &endpoint) = 0;
56 
57  // Deprecated. Do not use in new code.
58  bool Bind(const IPV4Address &ip, unsigned short port) {
59  return Bind(IPV4SocketAddress(ip, port));
60  }
61 
62  virtual bool GetSocketAddress(IPV4SocketAddress *address) const = 0;
63 
64  virtual bool Close() = 0;
65  virtual ola::io::DescriptorHandle ReadDescriptor() const = 0;
66  virtual ola::io::DescriptorHandle WriteDescriptor() const = 0;
67 
68  virtual ssize_t SendTo(const uint8_t *buffer,
69  unsigned int size,
70  const IPV4Address &ip,
71  unsigned short port) const = 0;
72  virtual ssize_t SendTo(const uint8_t *buffer,
73  unsigned int size,
74  const IPV4SocketAddress &dest) const = 0;
75  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
76  const IPV4Address &ip,
77  unsigned short port) const = 0;
78  virtual ssize_t SendTo(ola::io::IOVecInterface *data,
79  const IPV4SocketAddress &dest) const = 0;
80 
81  virtual bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const = 0;
82  virtual bool RecvFrom(uint8_t *buffer,
83  ssize_t *data_read,
84  IPV4Address &source) const = 0; // NOLINT
85  virtual bool RecvFrom(uint8_t *buffer,
86  ssize_t *data_read,
87  IPV4Address &source, // NOLINT
88  uint16_t &port) const = 0; // NOLINT
89 
90  virtual bool EnableBroadcast() = 0;
91  virtual bool SetMulticastInterface(const IPV4Address &iface) = 0;
92  virtual bool JoinMulticast(const IPV4Address &iface,
93  const IPV4Address &group,
94  bool loop = false) = 0;
95  virtual bool LeaveMulticast(const IPV4Address &iface,
96  const IPV4Address &group) = 0;
97  virtual bool SetTos(uint8_t tos) = 0;
98 
99  private:
100  DISALLOW_COPY_AND_ASSIGN(UDPSocketInterface);
101 };
102 
103 
104 /*
105  * A UDPSocket (non connected)
106  */
108  public:
109  UDPSocket()
110  : UDPSocketInterface(),
111  m_handle(ola::io::INVALID_DESCRIPTOR),
112  m_bound_to_port(false) {}
113  ~UDPSocket() { Close(); }
114  bool Init();
115  bool Bind(const IPV4SocketAddress &endpoint);
116 
117  bool GetSocketAddress(IPV4SocketAddress *address) const;
118 
119  bool Close();
120  ola::io::DescriptorHandle ReadDescriptor() const { return m_handle; }
121  ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
122  ssize_t SendTo(const uint8_t *buffer,
123  unsigned int size,
124  const IPV4Address &ip,
125  unsigned short port) const;
126  ssize_t SendTo(const uint8_t *buffer,
127  unsigned int size,
128  const IPV4SocketAddress &dest) const {
129  return SendTo(buffer, size, dest.Host(), dest.Port());
130  }
131  ssize_t SendTo(ola::io::IOVecInterface *data,
132  const IPV4Address &ip,
133  unsigned short port) const;
134  ssize_t SendTo(ola::io::IOVecInterface *data,
135  const IPV4SocketAddress &dest) const {
136  return SendTo(data, dest.Host(), dest.Port());
137  }
138 
139  bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
140  bool RecvFrom(uint8_t *buffer,
141  ssize_t *data_read,
142  IPV4Address &source) const; // NOLINT
143  bool RecvFrom(uint8_t *buffer,
144  ssize_t *data_read,
145  IPV4Address &source, // NOLINT
146  uint16_t &port) const; // NOLINT
147 
148  bool EnableBroadcast();
149  bool SetMulticastInterface(const IPV4Address &iface);
150  bool JoinMulticast(const IPV4Address &iface,
151  const IPV4Address &group,
152  bool loop = false);
153  bool LeaveMulticast(const IPV4Address &iface,
154  const IPV4Address &group);
155 
156  bool SetTos(uint8_t tos);
157 
158  private:
159  ola::io::DescriptorHandle m_handle;
160  bool m_bound_to_port;
161 
162  DISALLOW_COPY_AND_ASSIGN(UDPSocket);
163 };
164 } // namespace network
165 } // namespace ola
166 #endif // INCLUDE_OLA_NETWORK_SOCKET_H_