root/trunk/dep/include/sockets/UdpSocket.h @ 2

Revision 2, 6.9 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/** \file UdpSocket.h
2 **     \date  2004-02-13
3 **     \author grymse@alhem.net
4**/
5/*
6Copyright (C) 2004-2007  Anders Hedstrom
7
8This library is made available under the terms of the GNU GPL.
9
10If you would like to use this library in a closed-source application,
11a separate license agreement is available. For information about
12the closed-source license agreement for the C++ sockets library,
13please visit http://www.alhem.net/Sockets/license.html and/or
14email license@alhem.net.
15
16This program is free software; you can redistribute it and/or
17modify it under the terms of the GNU General Public License
18as published by the Free Software Foundation; either version 2
19of the License, or (at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
28Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29*/
30#ifndef _SOCKETS_UdpSocket_H
31#define _SOCKETS_UdpSocket_H
32
33#include "sockets-config.h"
34#include "Socket.h"
35
36#ifdef SOCKETS_NAMESPACE
37namespace SOCKETS_NAMESPACE {
38#endif
39
40/** Socket implementation for UDP.
41        \ingroup basic */
42class UdpSocket : public Socket
43{
44public:
45        /** Constructor.
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);
50        ~UdpSocket();
51
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);
58
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);
66
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.
79                \param a Ip address
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);
84#ifdef ENABLE_IPV6
85#ifdef IPPROTO_IPV6
86        /** To receive data on a specific interface:port, use this.
87                \param a Ipv6 address
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);
92#endif
93#endif
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);
99
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.
106                \param host Hostname
107                \param port Port number
108                \return true if successful */
109        bool Open(const std::string& host,port_t port);
110#ifdef ENABLE_IPV6
111#ifdef IPPROTO_IPV6
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);
117#endif
118#endif
119        /** Define remote host.
120                \param ad Socket address
121                \return true if successful */
122        bool Open(SocketAddress& ad);
123
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);
128#ifdef ENABLE_IPV6
129#ifdef IPPROTO_IPV6
130        /** Send to specified ipv6 address */
131        void SendToBuf(in6_addr,port_t,const char *data,int len,int flags = 0);
132#endif
133#endif
134        /** Send to specified socket address */
135        void SendToBuf(SocketAddress& ad,const char *data,int len,int flags = 0);
136
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);
141#ifdef ENABLE_IPV6
142#ifdef IPPROTO_IPV6
143        /** Send string to specified ipv6 address */
144        void SendTo(in6_addr,port_t,const std::string&,int flags = 0);
145#endif
146#endif
147        /** Send string to specified socket address */
148        void SendTo(SocketAddress& ad,const std::string&,int flags = 0);
149
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);
154
155        /** Set broadcast */
156        void SetBroadcast(bool b = true);
157        /** Check broadcast flag.
158                \return true broadcast is enabled. */
159        bool IsBroadcast();
160
161        /** multicast */
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);
168#ifdef ENABLE_IPV6
169#ifdef IPPROTO_IPV6
170        /** multicast, ipv6 only */
171        void SetMulticastHops(int = -1);
172        /** multicast, ipv6 only */
173        int GetMulticastHops();
174#endif
175#endif
176        /** Returns true if Bind succeeded. */
177        bool IsBound();
178        /** Return Bind port number */
179        port_t GetPort();
180
181        void OnOptions(int,int,int,SOCKET) {}
182
183        int GetLastSizeWritten();
184
185        /** Also read timestamp information from incoming message */
186        void SetTimestamp(bool = true);
187
188protected:
189        UdpSocket(const UdpSocket& s) : Socket(s) {}
190        void OnRead();
191#if defined(LINUX) || defined(MACOSX)
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);
194#endif
195
196private:
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;
205        int m_retries;
206        bool m_b_read_ts;
207};
208
209
210#ifdef SOCKETS_NAMESPACE
211}
212#endif
213
214#endif // _SOCKETS_UdpSocket_H
215
Note: See TracBrowser for help on using the browser.