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, int retries
= 0);
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 /** Called when incoming data has been received and read timestamp is enabled.
60 \param buf Pointer to data
61 \param len Length of data
62 \param sa Pointer to sockaddr struct of sender
63 \param sa_len Length of sockaddr struct
64 \param ts Timestamp from message */
65 virtual void OnRawData(const char *buf
,size_t len
,struct sockaddr
*sa
,socklen_t sa_len
,struct timeval
*ts
);
67 /** To receive incoming data, call Bind to setup an incoming port.
68 \param port Incoming port number
69 \param range Port range to try if ports already in use
70 \return 0 if bind succeeded */
71 int Bind(port_t
& port
,int range
= 1);
72 /** To receive data on a specific interface:port, use this.
73 \param intf Interface ip/hostname
74 \param port Port number
75 \param range Port range
76 \return 0 if bind succeeded */
77 int Bind(const std::string
& intf
,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(ipaddr_t a
,port_t
& port
,int range
= 1);
86 /** To receive data on a specific interface:port, use this.
88 \param port Port number
89 \param range Port range
90 \return 0 if bind succeeded */
91 int Bind(in6_addr a
,port_t
& port
,int range
= 1);
94 /** To receive data on a specific interface:port, use this.
95 \param ad Socket address
96 \param range Port range
97 \return 0 if bind succeeded */
98 int Bind(SocketAddress
& ad
,int range
= 1);
100 /** Define remote host.
101 \param l Address of remote host
102 \param port Port of remote host
103 \return true if successful */
104 bool Open(ipaddr_t l
,port_t port
);
105 /** Define remote host.
107 \param port Port number
108 \return true if successful */
109 bool Open(const std::string
& host
,port_t port
);
112 /** Define remote host.
113 \param a Address of remote host, ipv6
114 \param port Port of remote host
115 \return true if successful */
116 bool Open(struct in6_addr
& a
,port_t port
);
119 /** Define remote host.
120 \param ad Socket address
121 \return true if successful */
122 bool Open(SocketAddress
& ad
);
124 /** Send to specified host */
125 void SendToBuf(const std::string
& ,port_t
,const char *data
,int len
,int flags
= 0);
126 /** Send to specified address */
127 void SendToBuf(ipaddr_t
,port_t
,const char *data
,int len
,int flags
= 0);
130 /** Send to specified ipv6 address */
131 void SendToBuf(in6_addr
,port_t
,const char *data
,int len
,int flags
= 0);
134 /** Send to specified socket address */
135 void SendToBuf(SocketAddress
& ad
,const char *data
,int len
,int flags
= 0);
137 /** Send string to specified host */
138 void SendTo(const std::string
&,port_t
,const std::string
&,int flags
= 0);
139 /** Send string to specified address */
140 void SendTo(ipaddr_t
,port_t
,const std::string
&,int flags
= 0);
143 /** Send string to specified ipv6 address */
144 void SendTo(in6_addr
,port_t
,const std::string
&,int flags
= 0);
147 /** Send string to specified socket address */
148 void SendTo(SocketAddress
& ad
,const std::string
&,int flags
= 0);
150 /** Send to connected address */
151 void SendBuf(const char *data
,size_t,int flags
= 0);
152 /** Send string to connected address. */
153 void Send(const std::string
& ,int flags
= 0);
156 void SetBroadcast(bool b
= true);
157 /** Check broadcast flag.
158 \return true broadcast is enabled. */
162 void SetMulticastTTL(int ttl
= 1);
163 int GetMulticastTTL();
164 void SetMulticastLoop(bool = true);
165 bool IsMulticastLoop();
166 void AddMulticastMembership(const std::string
& group
,const std::string
& intf
= "0.0.0.0",int if_index
= 0);
167 void DropMulticastMembership(const std::string
& group
,const std::string
& intf
= "0.0.0.0",int if_index
= 0);
170 /** multicast, ipv6 only */
171 void SetMulticastHops(int = -1);
172 /** multicast, ipv6 only */
173 int GetMulticastHops();
176 /** Returns true if Bind succeeded. */
178 /** Return Bind port number */
181 void OnOptions(int,int,int,SOCKET
) {}
183 int GetLastSizeWritten();
185 /** Also read timestamp information from incoming message */
186 void SetTimestamp(bool = true);
189 UdpSocket(const UdpSocket
& s
) : Socket(s
) {}
192 /** This method emulates socket recvfrom, but uses messages so we can get the timestamp */
193 int ReadTS(char *ioBuf
, int inBufSize
, struct sockaddr
*from
, socklen_t fromlen
, struct timeval
*ts
);
197 UdpSocket
& operator=(const UdpSocket
& ) { return *this; }
198 /** create before using sendto methods */
199 void CreateConnection();
200 char *m_ibuf
; ///< Input buffer
201 int m_ibufsz
; ///< Size of input buffer
202 bool m_bind_ok
; ///< Bind completed successfully
203 port_t m_port
; ///< Bind port number
204 int m_last_size_written
;
210 #ifdef SOCKETS_NAMESPACE
214 #endif // _SOCKETS_UdpSocket_H