UdpSocket.h
1 /*
2  * Copyright (C) 2006-2013 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Net_UdpSocket_h
30 #define Pt_Net_UdpSocket_h
31 
32 #include <Pt/Net/Api.h>
33 #include <Pt/Net/Endpoint.h>
34 #include <Pt/System/IODevice.h>
35 #include <Pt/Types.h>
36 #include <cstddef>
37 
38 namespace Pt {
39 
40 namespace Net {
41 
44 class PT_NET_API UdpSocketOptions
45 {
46  public:
50 
54 
58 
61  UdpSocketOptions& operator=(const UdpSocketOptions& opts);
62 
65  bool isBroadcast() const
66  { return (_flags & Broadcast) != 0; }
67 
70  void setBroadcast()
71  { _flags |= Broadcast; }
72 
75  int hopLimit() const
76  { return _hoplimit; }
77 
80  void setHopLimit(int n)
81  { _hoplimit = n; }
82 
83  private:
85  enum Flags
86  {
87  Broadcast = 1
88  };
89 
90  Pt::uint32_t _flags;
91  int _hoplimit;
92  varint_t _r0;
93  varint_t _r1;
94  varint_t _r2;
95 };
96 
97 
100 class PT_NET_API UdpSocket : public System::IODevice
101 {
102  public:
105  UdpSocket();
106 
109  explicit UdpSocket(System::EventLoop& loop);
110 
113  ~UdpSocket();
114 
119  void bind(const Endpoint& ep);
120 
125  void bind(const Endpoint& ep, const UdpSocketOptions& o);
126 
127  //void bindMulticast(const Endpoint& e);
128 
138  bool beginBind(const Endpoint& ep);
139 
144  bool beginBind(const Endpoint& ep, const UdpSocketOptions& o);
145 
146  //bool beginBindMulticast(const Endpoint& iface, const UdpSocketOptions& o);
147 
152  void endBind();
153 
160  { return _bound; }
161 
164  bool isBound() const;
165 
170  void connect(const Endpoint& ep);
171 
176  void connect(const Endpoint& ep, const UdpSocketOptions& o);
177 
182  void setTarget(const Endpoint& ep);
183 
188  void setTarget(const Endpoint& ep, const UdpSocketOptions& o);
189 
194  bool beginConnect(const Endpoint& ep);
195 
200  bool beginConnect(const Endpoint& ep, const UdpSocketOptions& o);
201 
206  void endConnect();
207 
214  { return _connected; }
215 
218  bool isConnected() const;
219 
222  void joinMulticastGroup(const std::string& ipaddr);
223 
224  //void dropMulticastGroup(const std::string& ipaddr);
225 
228  void localEndpoint(Endpoint& ep) const;
229 
232  const Endpoint& remoteEndpoint() const;
233 
234  protected:
235  // inherit doc
236  virtual void onClose();
237 
238  // inherit doc
239  void onSetTimeout(std::size_t timeout);
240 
241  // inherit doc
242  virtual bool onRun();
243 
244  // inherit doc
245  virtual std::size_t onBeginRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
246 
247  // inherit doc
248  virtual std::size_t onEndRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
249 
250  // inherit doc
251  virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof);
252 
253  // inherit doc
254  virtual std::size_t onBeginWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
255 
256  // inherit doc
257  virtual std::size_t onEndWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
258 
259  // inherit doc
260  virtual std::size_t onWrite(const char* buffer, std::size_t count);
261 
262  // inherit doc
263  virtual void onCancel();
264 
265  private:
267  class UdpSocketImpl* _impl;
268 
270  Signal<UdpSocket&> _connected;
271 
273  Signal<UdpSocket&> _bound;
274 
276  bool _connecting;
277 
279  bool _binding;
280 };
281 
282 } // namespace Net
283 
284 } // namespace Pt
285 
286 #endif
Signal< UdpSocket & > & connected()
Notifies that the socket was connected.
Definition: UdpSocket.h:213
void setHopLimit(int n)
Sets the hop limit.
Definition: UdpSocket.h:80
void setBroadcast()
enables UDP broadcast.
Definition: UdpSocket.h:70
Signal< UdpSocket & > & bound()
Notifies that the socket was bound.
Definition: UdpSocket.h:159
UDP server and client socket.
Definition: UdpSocket.h:100
bool isBroadcast() const
Returns true if UDP broadcast is enabled.
Definition: UdpSocket.h:65
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Endpoint for I/O operations.
Definition: IODevice.h:55
Represents a Network Host.
Definition: Endpoint.h:46
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
int hopLimit() const
Returns the hop limit.
Definition: UdpSocket.h:75
UDP socket options.
Definition: UdpSocket.h:44