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 #ifndef _SOCKETS_UdpSocket_H
00031 #define _SOCKETS_UdpSocket_H
00032
00033 #include "sockets-config.h"
00034 #include "Socket.h"
00035
00036 #ifdef SOCKETS_NAMESPACE
00037 namespace SOCKETS_NAMESPACE {
00038 #endif
00039
00042 class UdpSocket : public Socket
00043 {
00044 public:
00049 UdpSocket(ISocketHandler& h,int ibufsz = 16384,bool ipv6 = false);
00050 ~UdpSocket();
00051
00057 virtual void OnRawData(const char *buf,size_t len,struct sockaddr *sa,socklen_t sa_len);
00058
00063 int Bind(port_t& port,int range = 1);
00069 int Bind(const std::string& intf,port_t& port,int range = 1);
00075 int Bind(ipaddr_t a,port_t& port,int range = 1);
00076 #ifdef ENABLE_IPV6
00077 #ifdef IPPROTO_IPV6
00078
00083 int Bind(in6_addr a,port_t& port,int range = 1);
00084 #endif
00085 #endif
00086
00090 int Bind(SocketAddress& ad,int range = 1);
00091
00096 bool Open(ipaddr_t l,port_t port);
00101 bool Open(const std::string& host,port_t port);
00102 #ifdef ENABLE_IPV6
00103 #ifdef IPPROTO_IPV6
00104
00108 bool Open(struct in6_addr& a,port_t port);
00109 #endif
00110 #endif
00111
00114 bool Open(SocketAddress& ad);
00115
00117 void SendToBuf(const std::string& ,port_t,const char *data,int len,int flags = 0);
00119 void SendToBuf(ipaddr_t,port_t,const char *data,int len,int flags = 0);
00120 #ifdef ENABLE_IPV6
00121 #ifdef IPPROTO_IPV6
00122
00123 void SendToBuf(in6_addr,port_t,const char *data,int len,int flags = 0);
00124 #endif
00125 #endif
00126
00127 void SendToBuf(SocketAddress& ad,const char *data,int len,int flags = 0);
00128
00130 void SendTo(const std::string&,port_t,const std::string&,int flags = 0);
00132 void SendTo(ipaddr_t,port_t,const std::string&,int flags = 0);
00133 #ifdef ENABLE_IPV6
00134 #ifdef IPPROTO_IPV6
00135
00136 void SendTo(in6_addr,port_t,const std::string&,int flags = 0);
00137 #endif
00138 #endif
00139
00140 void SendTo(SocketAddress& ad,const std::string&,int flags = 0);
00141
00143 void SendBuf(const char *data,size_t,int flags = 0);
00145 void Send(const std::string& ,int flags = 0);
00146
00148 void SetBroadcast(bool b = true);
00151 bool IsBroadcast();
00152
00154 void SetMulticastTTL(int ttl = 1);
00155 int GetMulticastTTL();
00156 void SetMulticastLoop(bool = true);
00157 bool IsMulticastLoop();
00158 void AddMulticastMembership(const std::string& group,const std::string& intf = "0.0.0.0",int if_index = 0);
00159 void DropMulticastMembership(const std::string& group,const std::string& intf = "0.0.0.0",int if_index = 0);
00160 #ifdef ENABLE_IPV6
00161 #ifdef IPPROTO_IPV6
00162
00163 void SetMulticastHops(int = -1);
00165 int GetMulticastHops();
00166 #endif
00167 #endif
00168
00169 bool IsBound();
00171 port_t GetPort();
00172
00173 void OnOptions(int,int,int,SOCKET) {}
00174
00175 int GetLastSizeWritten();
00176
00177 protected:
00178 UdpSocket(const UdpSocket& s) : Socket(s) {}
00179 void OnRead();
00180
00181 private:
00182 UdpSocket& operator=(const UdpSocket& ) { return *this; }
00184 void CreateConnection();
00185 char *m_ibuf;
00186 int m_ibufsz;
00187 bool m_bind_ok;
00188 port_t m_port;
00189 int m_last_size_written;
00190 };
00191
00192
00193 #ifdef SOCKETS_NAMESPACE
00194 }
00195 #endif
00196
00197 #endif // _SOCKETS_UdpSocket_H