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
38namespace Pt {
39
40namespace Net {
41
46class PT_NET_API UdpSocketOptions
47{
48 public:
52
56
60
64
67 bool isBroadcast() const
68 { return (_flags & Broadcast) != 0; }
69
73 { _flags |= Broadcast; }
74
77 int hopLimit() const
78 { return _hoplimit; }
79
82 void setHopLimit(int n)
83 { _hoplimit = n; }
84
85 private:
87 enum Flags
88 {
89 Broadcast = 1
90 };
91
92 Pt::uint32_t _flags;
93 int _hoplimit;
94 varint_t _r0;
95 varint_t _r1;
96 varint_t _r2;
97};
98
99
192class PT_NET_API UdpSocket : public System::IODevice
193{
194 public:
198
202
206
212 void bind(const Endpoint& ep);
213
219 void bind(const Endpoint& ep, const UdpSocketOptions& o);
220
221 //void bindMulticast(const Endpoint& e);
222
231 bool beginBind(const Endpoint& ep);
232
241 bool beginBind(const Endpoint& ep, const UdpSocketOptions& o);
242
243 //bool beginBindMulticast(const Endpoint& iface, const UdpSocketOptions& o);
244
250 void endBind();
251
255 { return _bound; }
256
259 bool isBound() const;
260
265 void connect(const Endpoint& ep);
266
271 void connect(const Endpoint& ep, const UdpSocketOptions& o);
272
277 void setTarget(const Endpoint& ep);
278
283 void setTarget(const Endpoint& ep, const UdpSocketOptions& o);
284
292 bool beginConnect(const Endpoint& ep);
293
301 bool beginConnect(const Endpoint& ep, const UdpSocketOptions& o);
302
308
312 { return _connected; }
313
316 bool isConnected() const;
317
320 void joinMulticastGroup(const std::string& ipaddr);
321
322 //void dropMulticastGroup(const std::string& ipaddr);
323
326 void localEndpoint(Endpoint& ep) const;
327
330 const Endpoint& remoteEndpoint() const;
331
332 protected:
333 // inherit doc
334 virtual void onClose();
335
336 // inherit doc
337 void onSetTimeout(std::size_t timeout);
338
339 // inherit doc
340 virtual bool onRun();
341
342 // inherit doc
343 virtual std::size_t onBeginRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
344
345 // inherit doc
346 virtual std::size_t onEndRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
347
348 // inherit doc
349 virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof);
350
351 // inherit doc
352 virtual std::size_t onBeginWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
353
354 // inherit doc
355 virtual std::size_t onEndWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
356
357 // inherit doc
358 virtual std::size_t onWrite(const char* buffer, std::size_t count);
359
360 // inherit doc
361 virtual void onCancel();
362
363 private:
365 class UdpSocketImpl* _impl;
366
368 Signal<UdpSocket&> _connected;
369
371 Signal<UdpSocket&> _bound;
372
374 bool _connecting;
375
377 bool _binding;
378};
379
380} // namespace Net
381
382} // namespace Pt
383
384#endif
Host and service address.
Definition Endpoint.h:77
UDP socket bind and send options.
Definition UdpSocket.h:47
int hopLimit() const
Returns the hop limit.
Definition UdpSocket.h:77
bool isBroadcast() const
Returns true if UDP broadcast is enabled.
Definition UdpSocket.h:67
~UdpSocketOptions()
Destroys the options.
UdpSocketOptions()
Creates default options.
UdpSocketOptions(const UdpSocketOptions &opts)
Copies the options.
void setHopLimit(int n)
Sets the hop limit.
Definition UdpSocket.h:82
UdpSocketOptions & operator=(const UdpSocketOptions &opts)
Assigns the options.
void setBroadcast()
Enables UDP broadcast.
Definition UdpSocket.h:72
~UdpSocket()
Destroys the socket.
bool isBound() const
Returns true if the socket is bound.
void endConnect()
Ends connecting to an endpoint.
UdpSocket(System::EventLoop &loop)
Creates a socket and attaches it to loop.
bool beginConnect(const Endpoint &ep, const UdpSocketOptions &o)
Begins connecting to ep with o.
const Endpoint & remoteEndpoint() const
Returns the remote endpoint.
virtual void onCancel()
Blocks until operation has cancelled.
Signal< UdpSocket & > & connected()
Returns the signal that the socket was connected.
Definition UdpSocket.h:311
UdpSocket()
Creates a closed socket.
bool beginBind(const Endpoint &ep)
Begins binding to ep.
void joinMulticastGroup(const std::string &ipaddr)
Joins multicast group ipaddr.
virtual bool onRun()
Check if ready and run.
bool isConnected() const
Returns true if the socket is connected.
bool beginConnect(const Endpoint &ep)
Begins connecting to ep.
void connect(const Endpoint &ep, const UdpSocketOptions &o)
Connects to ep with o.
void bind(const Endpoint &ep, const UdpSocketOptions &o)
Binds to ep with o.
void bind(const Endpoint &ep)
Binds to local endpoint ep.
void setTarget(const Endpoint &ep)
Sets the send target to ep.
Signal< UdpSocket & > & bound()
Returns the signal that the socket was bound.
Definition UdpSocket.h:254
void setTarget(const Endpoint &ep, const UdpSocketOptions &o)
Sets the send target to ep with o.
void localEndpoint(Endpoint &ep) const
Writes the local endpoint into ep.
void connect(const Endpoint &ep)
Connects to ep.
bool beginBind(const Endpoint &ep, const UdpSocketOptions &o)
Begins binding to ep with o.
void endBind()
Ends binding to a local endpoint.
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
Endpoint for I/O operations.
Definition IODevice.h:96
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:240
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
TCP and UDP network sockets.
Definition Client.h:45
Core module.
Definition Allocator.h:33