00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _SOCKETS_UdpSocket_H
00033 #define _SOCKETS_UdpSocket_H
00034
00035 #include "sockets-config.h"
00036 #include "Socket.h"
00037
00038 #ifdef SOCKETS_NAMESPACE
00039 namespace SOCKETS_NAMESPACE {
00040 #endif
00041
00044 class UdpSocket : public Socket
00045 {
00046 public:
00051 UdpSocket(ISocketHandler& h,int ibufsz = 16384,bool ipv6 = false, int retries = 0);
00052 ~UdpSocket();
00053
00059 virtual void OnRawData(const char *buf,size_t len,struct sockaddr *sa,socklen_t sa_len);
00060
00067 virtual void OnRawData(const char *buf,size_t len,struct sockaddr *sa,socklen_t sa_len,struct timeval *ts);
00068
00073 int Bind(port_t& port,int range = 1);
00079 int Bind(const std::string& intf,port_t& port,int range = 1);
00085 int Bind(ipaddr_t a,port_t& port,int range = 1);
00086 #ifdef ENABLE_IPV6
00087 #ifdef IPPROTO_IPV6
00088
00093 int Bind(in6_addr a,port_t& port,int range = 1);
00094 #endif
00095 #endif
00096
00100 int Bind(SocketAddress& ad,int range = 1);
00101
00106 bool Open(ipaddr_t l,port_t port);
00111 bool Open(const std::string& host,port_t port);
00112 #ifdef ENABLE_IPV6
00113 #ifdef IPPROTO_IPV6
00114
00118 bool Open(struct in6_addr& a,port_t port);
00119 #endif
00120 #endif
00121
00124 bool Open(SocketAddress& ad);
00125
00127 void SendToBuf(const std::string& ,port_t,const char *data,int len,int flags = 0);
00129 void SendToBuf(ipaddr_t,port_t,const char *data,int len,int flags = 0);
00130 #ifdef ENABLE_IPV6
00131 #ifdef IPPROTO_IPV6
00132
00133 void SendToBuf(in6_addr,port_t,const char *data,int len,int flags = 0);
00134 #endif
00135 #endif
00136
00137 void SendToBuf(SocketAddress& ad,const char *data,int len,int flags = 0);
00138
00140 void SendTo(const std::string&,port_t,const std::string&,int flags = 0);
00142 void SendTo(ipaddr_t,port_t,const std::string&,int flags = 0);
00143 #ifdef ENABLE_IPV6
00144 #ifdef IPPROTO_IPV6
00145
00146 void SendTo(in6_addr,port_t,const std::string&,int flags = 0);
00147 #endif
00148 #endif
00149
00150 void SendTo(SocketAddress& ad,const std::string&,int flags = 0);
00151
00153 void SendBuf(const char *data,size_t,int flags = 0);
00155 void Send(const std::string& ,int flags = 0);
00156
00158 void SetBroadcast(bool b = true);
00161 bool IsBroadcast();
00162
00164 void SetMulticastTTL(int ttl = 1);
00165 int GetMulticastTTL();
00166 void SetMulticastLoop(bool = true);
00167 bool IsMulticastLoop();
00168 void SetMulticastDefaultInterface(ipaddr_t a, int if_index = 0);
00169 #ifdef ENABLE_IPV6
00170 #ifdef IPPROTO_IPV6
00171 void SetMulticastDefaultInterface(in6_addr a, int if_index = 0);
00172 #endif
00173 #endif
00174 void SetMulticastDefaultInterface(const std::string& intf, int if_index = 0);
00175 void AddMulticastMembership(const std::string& group,const std::string& intf = "0.0.0.0",int if_index = 0);
00176 void DropMulticastMembership(const std::string& group,const std::string& intf = "0.0.0.0",int if_index = 0);
00177 #ifdef ENABLE_IPV6
00178 #ifdef IPPROTO_IPV6
00179
00180 void SetMulticastHops(int = -1);
00182 int GetMulticastHops();
00183 #endif
00184 #endif
00185
00186 bool IsBound();
00188 port_t GetPort();
00189
00190 void OnOptions(int,int,int,SOCKET) {}
00191
00192 int GetLastSizeWritten();
00193
00195 void SetTimestamp(bool = true);
00196
00197 protected:
00198 UdpSocket(const UdpSocket& s) : Socket(s) {}
00199 void OnRead();
00200 #if defined(LINUX) || defined(MACOSX)
00201
00202 int ReadTS(char *ioBuf, int inBufSize, struct sockaddr *from, socklen_t fromlen, struct timeval *ts);
00203 #endif
00204
00205 private:
00206 UdpSocket& operator=(const UdpSocket& ) { return *this; }
00208 void CreateConnection();
00209 char *m_ibuf;
00210 int m_ibufsz;
00211 bool m_bind_ok;
00212 port_t m_port;
00213 int m_last_size_written;
00214 int m_retries;
00215 bool m_b_read_ts;
00216 };
00217
00218
00219 #ifdef SOCKETS_NAMESPACE
00220 }
00221 #endif
00222
00223 #endif // _SOCKETS_UdpSocket_H
00224