3 ** \author grymse@alhem.net
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifndef _SOCKETS_UdpSocket_H
31 #define _SOCKETS_UdpSocket_H
33 #include "sockets-config.h"
36 #ifdef SOCKETS_NAMESPACE
37 namespace SOCKETS_NAMESPACE
{
40 /** Socket implementation for UDP.
42 class UdpSocket
: public Socket
46 \param h ISocketHandler reference
47 \param ibufsz Maximum size of receive message (extra bytes will be truncated)
48 \param ipv6 'true' if this is an ipv6 socket */
49 UdpSocket(ISocketHandler
& h
,int ibufsz
= 16384,bool ipv6
= false);
52 /** Called when incoming data has been received.
53 \param buf Pointer to data
54 \param len Length of data
55 \param sa Pointer to sockaddr struct of sender
56 \param sa_len Length of sockaddr struct */
57 virtual void OnRawData(const char *buf
,size_t len
,struct sockaddr
*sa
,socklen_t sa_len
);
59 /** To receive incoming data, call Bind to setup an incoming port.
60 \param port Incoming port number
61 \param range Port range to try if ports already in use
62 \return 0 if bind succeeded */
63 int Bind(port_t
& port
,int range
= 1);
64 /** To receive data on a specific interface:port, use this.
65 \param intf Interface ip/hostname
66 \param port Port number
67 \param range Port range
68 \return 0 if bind succeeded */
69 int Bind(const std::string
& intf
,port_t
& port
,int range
= 1);
70 /** To receive data on a specific interface:port, use this.
72 \param port Port number
73 \param range Port range
74 \return 0 if bind succeeded */
75 int Bind(ipaddr_t a
,port_t
& port
,int range
= 1);
78 /** To receive data on a specific interface:port, use this.
80 \param port Port number
81 \param range Port range
82 \return 0 if bind succeeded */
83 int Bind(in6_addr a
,port_t
& port
,int range
= 1);
86 /** To receive data on a specific interface:port, use this.
87 \param ad Socket address
88 \param range Port range
89 \return 0 if bind succeeded */
90 int Bind(SocketAddress
& ad
,int range
= 1);
92 /** Define remote host.
93 \param l Address of remote host
94 \param port Port of remote host
95 \return true if successful */
96 bool Open(ipaddr_t l
,port_t port
);
97 /** Define remote host.
99 \param port Port number
100 \return true if successful */
101 bool Open(const std::string
& host
,port_t port
);
104 /** Define remote host.
105 \param a Address of remote host, ipv6
106 \param port Port of remote host
107 \return true if successful */
108 bool Open(struct in6_addr
& a
,port_t port
);
111 /** Define remote host.
112 \param ad Socket address
113 \return true if successful */
114 bool Open(SocketAddress
& ad
);
116 /** Send to specified host */
117 void SendToBuf(const std::string
& ,port_t
,const char *data
,int len
,int flags
= 0);
118 /** Send to specified address */
119 void SendToBuf(ipaddr_t
,port_t
,const char *data
,int len
,int flags
= 0);
122 /** Send to specified ipv6 address */
123 void SendToBuf(in6_addr
,port_t
,const char *data
,int len
,int flags
= 0);
126 /** Send to specified socket address */
127 void SendToBuf(SocketAddress
& ad
,const char *data
,int len
,int flags
= 0);
129 /** Send string to specified host */
130 void SendTo(const std::string
&,port_t
,const std::string
&,int flags
= 0);
131 /** Send string to specified address */
132 void SendTo(ipaddr_t
,port_t
,const std::string
&,int flags
= 0);
135 /** Send string to specified ipv6 address */
136 void SendTo(in6_addr
,port_t
,const std::string
&,int flags
= 0);
139 /** Send string to specified socket address */
140 void SendTo(SocketAddress
& ad
,const std::string
&,int flags
= 0);
142 /** Send to connected address */
143 void SendBuf(const char *data
,size_t,int flags
= 0);
144 /** Send string to connected address. */
145 void Send(const std::string
& ,int flags
= 0);
148 void SetBroadcast(bool b
= true);
149 /** Check broadcast flag.
150 \return true broadcast is enabled. */
154 void SetMulticastTTL(int ttl
= 1);
155 int GetMulticastTTL();
156 void SetMulticastLoop(bool = true);
157 bool IsMulticastLoop();
158 void AddMulticastMembership(const std::string
& group
,const std::string
& intf
= "0.0.0.0",int if_index
= 0);
159 void DropMulticastMembership(const std::string
& group
,const std::string
& intf
= "0.0.0.0",int if_index
= 0);
162 /** multicast, ipv6 only */
163 void SetMulticastHops(int = -1);
164 /** multicast, ipv6 only */
165 int GetMulticastHops();
168 /** Returns true if Bind succeeded. */
170 /** Return Bind port number */
173 void OnOptions(int,int,int,SOCKET
) {}
175 int GetLastSizeWritten();
178 UdpSocket(const UdpSocket
& s
) : Socket(s
) {}
182 UdpSocket
& operator=(const UdpSocket
& ) { return *this; }
183 /** create before using sendto methods */
184 void CreateConnection();
185 char *m_ibuf
; ///< Input buffer
186 int m_ibufsz
; ///< Size of input buffer
187 bool m_bind_ok
; ///< Bind completed successfully
188 port_t m_port
; ///< Bind port number
189 int m_last_size_written
;
193 #ifdef SOCKETS_NAMESPACE
197 #endif // _SOCKETS_UdpSocket_H