Logo
~Sockets~
~Examples~
~Contact~


UdpSocket Class Reference
[Basic sockets]

Socket implementation for UDP. More...

#include <UdpSocket.h>

Inheritance diagram for UdpSocket:
Collaboration diagram for UdpSocket:

List of all members.


Public Member Functions

 UdpSocket (ISocketHandler &h, int ibufsz=16384, bool ipv6=false, int retries=0)
 Constructor.
 ~UdpSocket ()
virtual void OnRawData (const char *buf, size_t len, struct sockaddr *sa, socklen_t sa_len)
 Called when incoming data has been received.
virtual void OnRawData (const char *buf, size_t len, struct sockaddr *sa, socklen_t sa_len, struct timeval *ts)
 Called when incoming data has been received and read timestamp is enabled.
int Bind (port_t &port, int range=1)
 To receive incoming data, call Bind to setup an incoming port.
int Bind (const std::string &intf, port_t &port, int range=1)
 To receive data on a specific interface:port, use this.
int Bind (ipaddr_t a, port_t &port, int range=1)
 To receive data on a specific interface:port, use this.
int Bind (SocketAddress &ad, int range=1)
 To receive data on a specific interface:port, use this.
bool Open (ipaddr_t l, port_t port)
 Define remote host.
bool Open (const std::string &host, port_t port)
 Define remote host.
bool Open (SocketAddress &ad)
 Define remote host.
void SendToBuf (const std::string &, port_t, const char *data, int len, int flags=0)
 Send to specified host.
void SendToBuf (ipaddr_t, port_t, const char *data, int len, int flags=0)
 Send to specified address.
void SendToBuf (SocketAddress &ad, const char *data, int len, int flags=0)
 Send to specified socket address.
void SendTo (const std::string &, port_t, const std::string &, int flags=0)
 Send string to specified host.
void SendTo (ipaddr_t, port_t, const std::string &, int flags=0)
 Send string to specified address.
void SendTo (SocketAddress &ad, const std::string &, int flags=0)
 Send string to specified socket address.
void SendBuf (const char *data, size_t, int flags=0)
 Send to connected address.
void Send (const std::string &, int flags=0)
 Send string to connected address.
void SetBroadcast (bool b=true)
 Set broadcast.
bool IsBroadcast ()
 Check broadcast flag.
void SetMulticastTTL (int ttl=1)
 multicast
int GetMulticastTTL ()
void SetMulticastLoop (bool=true)
bool IsMulticastLoop ()
void SetMulticastDefaultInterface (ipaddr_t a, int if_index=0)
void SetMulticastDefaultInterface (const std::string &intf, int if_index=0)
void AddMulticastMembership (const std::string &group, const std::string &intf="0.0.0.0", int if_index=0)
void DropMulticastMembership (const std::string &group, const std::string &intf="0.0.0.0", int if_index=0)
bool IsBound ()
 Returns true if Bind succeeded.
port_t GetPort ()
 Return Bind port number.
void OnOptions (int, int, int, SOCKET)
 Called when a client socket is created, to set socket options.
int GetLastSizeWritten ()
void SetTimestamp (bool=true)
 Also read timestamp information from incoming message.

Protected Member Functions

 UdpSocket (const UdpSocket &s)
void OnRead ()
 Called when there is something to be read from the file descriptor.

Private Member Functions

UdpSocketoperator= (const UdpSocket &)
void CreateConnection ()
 create before using sendto methods

Private Attributes

char * m_ibuf
 Input buffer.
int m_ibufsz
 Size of input buffer.
bool m_bind_ok
 Bind completed successfully.
port_t m_port
 Bind port number.
int m_last_size_written
int m_retries
bool m_b_read_ts

Detailed Description

Socket implementation for UDP.

Definition at line 44 of file UdpSocket.h.


Constructor & Destructor Documentation

UdpSocket::UdpSocket ( ISocketHandler h,
int  ibufsz = 16384,
bool  ipv6 = false,
int  retries = 0 
)

Constructor.

Parameters:
h ISocketHandler reference
ibufsz Maximum size of receive message (extra bytes will be truncated)
ipv6 'true' if this is an ipv6 socket

Definition at line 58 of file UdpSocket.cpp.

00058                                                                           : Socket(h)
00059 , m_ibuf(new char[ibufsz])
00060 , m_ibufsz(ibufsz)
00061 , m_bind_ok(false)
00062 , m_port(0)
00063 , m_last_size_written(-1)
00064 , m_retries(retries)
00065 , m_b_read_ts(false)
00066 {
00067 #ifdef ENABLE_IPV6
00068 #ifdef IPPROTO_IPV6
00069         SetIpv6(ipv6);
00070 #endif
00071 #endif
00072 }

UdpSocket::~UdpSocket (  ) 

Definition at line 75 of file UdpSocket.cpp.

References Socket::Close(), and m_ibuf.

00076 {
00077         Close();
00078         delete[] m_ibuf;
00079 }

UdpSocket::UdpSocket ( const UdpSocket s  )  [inline, protected]

Definition at line 198 of file UdpSocket.h.

00198 : Socket(s) {}


Member Function Documentation

void UdpSocket::OnRawData ( const char *  buf,
size_t  len,
struct sockaddr *  sa,
socklen_t  sa_len 
) [virtual]

Called when incoming data has been received.

Parameters:
buf Pointer to data
len Length of data
sa Pointer to sockaddr struct of sender
sa_len Length of sockaddr struct

Definition at line 840 of file UdpSocket.cpp.

Referenced by OnRead().

00841 {
00842 }

void UdpSocket::OnRawData ( const char *  buf,
size_t  len,
struct sockaddr *  sa,
socklen_t  sa_len,
struct timeval *  ts 
) [virtual]

Called when incoming data has been received and read timestamp is enabled.

Parameters:
buf Pointer to data
len Length of data
sa Pointer to sockaddr struct of sender
sa_len Length of sockaddr struct
ts Timestamp from message

Definition at line 845 of file UdpSocket.cpp.

00846 {
00847 }

int UdpSocket::Bind ( port_t port,
int  range = 1 
)

To receive incoming data, call Bind to setup an incoming port.

Parameters:
port Incoming port number
range Port range to try if ports already in use
Returns:
0 if bind succeeded

Definition at line 82 of file UdpSocket.cpp.

References m_bind_ok, and m_port.

Referenced by Bind().

00083 {
00084 #ifdef ENABLE_IPV6
00085 #ifdef IPPROTO_IPV6
00086         if (IsIpv6())
00087         {
00088                 Ipv6Address ad(port);
00089                 int n = Bind(ad, range);
00090                 if (m_bind_ok)
00091                         port = m_port;
00092                 return n;
00093         }
00094 #endif
00095 #endif
00096         Ipv4Address ad(port);
00097         int n = Bind(ad, range);
00098         if (m_bind_ok)
00099                 port = m_port;
00100         return n;
00101 }

int UdpSocket::Bind ( const std::string &  intf,
port_t port,
int  range = 1 
)

To receive data on a specific interface:port, use this.

Parameters:
intf Interface ip/hostname
port Port number
range Port range
Returns:
0 if bind succeeded

Definition at line 104 of file UdpSocket.cpp.

References Bind(), Ipv4Address::IsValid(), m_bind_ok, m_port, and Socket::SetCloseAndDelete().

00105 {
00106 #ifdef ENABLE_IPV6
00107 #ifdef IPPROTO_IPV6
00108         if (IsIpv6())
00109         {
00110                 Ipv6Address ad(intf, port);
00111                 if (ad.IsValid())
00112                 {
00113                         int n = Bind(ad, range);
00114                         if (m_bind_ok)
00115                                 port = m_port;
00116                         return n;
00117                 }
00118                 SetCloseAndDelete();
00119                 return -1;
00120         }
00121 #endif
00122 #endif
00123         Ipv4Address ad(intf, port);
00124         if (ad.IsValid())
00125         {
00126                 int n = Bind(ad, range);
00127                 if (m_bind_ok)
00128                         port = m_port;
00129                 return n;
00130         }
00131         SetCloseAndDelete();
00132         return -1;
00133 }

int UdpSocket::Bind ( ipaddr_t  a,
port_t port,
int  range = 1 
)

To receive data on a specific interface:port, use this.

Parameters:
a Ip address
port Port number
range Port range
Returns:
0 if bind succeeded

Definition at line 136 of file UdpSocket.cpp.

References Bind(), m_bind_ok, and m_port.

00137 {
00138         Ipv4Address ad(a, port);
00139         int n = Bind(ad, range);
00140         if (m_bind_ok)
00141                 port = m_port;
00142         return n;
00143 }

int UdpSocket::Bind ( SocketAddress ad,
int  range = 1 
)

To receive data on a specific interface:port, use this.

Parameters:
ad Socket address
range Port range
Returns:
0 if bind succeeded

Definition at line 160 of file UdpSocket.cpp.

References Socket::Attach(), Socket::CreateSocket(), Errno, SocketAddress::GetFamily(), SocketAddress::GetPort(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, Utility::l2string(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_bind_ok, m_port, Socket::SetCloseAndDelete(), Socket::SetNonblocking(), SocketAddress::SetPort(), and StrError.

00161 {
00162         if (GetSocket() == INVALID_SOCKET)
00163         {
00164                 Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp"));
00165         }
00166         if (GetSocket() != INVALID_SOCKET)
00167         {
00168                 SetNonblocking(true);
00169                 int n = bind(GetSocket(), ad, ad);
00170                 int tries = range;
00171                 while (n == -1 && tries--)
00172                 {
00173                         ad.SetPort(ad.GetPort() + 1);
00174                         n = bind(GetSocket(), ad, ad);
00175                 }
00176                 if (n == -1)
00177                 {
00178                         Handler().LogError(this, "bind", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00179                         SetCloseAndDelete();
00180 #ifdef ENABLE_EXCEPTIONS
00181                         throw Exception("bind() failed for UdpSocket, port:range: " + Utility::l2string(ad.GetPort()) + ":" + Utility::l2string(range));
00182 #endif
00183                         return -1;
00184                 }
00185                 m_bind_ok = true;
00186                 m_port = ad.GetPort();
00187                 return 0;
00188         }
00189         return -1;
00190 }

bool UdpSocket::Open ( ipaddr_t  l,
port_t  port 
)

Define remote host.

if you wish to use Send, first Open a connection

Parameters:
l Address of remote host
port Port of remote host
Returns:
true if successful

Definition at line 194 of file UdpSocket.cpp.

Referenced by Open().

00195 {
00196         Ipv4Address ad(l, port);
00197         return Open(ad);
00198 }

bool UdpSocket::Open ( const std::string &  host,
port_t  port 
)

Define remote host.

Parameters:
host Hostname
port Port number
Returns:
true if successful

Definition at line 201 of file UdpSocket.cpp.

References Ipv4Address::IsValid(), and Open().

00202 {
00203 #ifdef ENABLE_IPV6
00204 #ifdef IPPROTO_IPV6
00205         if (IsIpv6())
00206         {
00207                 Ipv6Address ad(host, port);
00208                 if (ad.IsValid())
00209                 {
00210                         return Open(ad);
00211                 }
00212                 return false;
00213         }
00214 #endif
00215 #endif
00216         Ipv4Address ad(host, port);
00217         if (ad.IsValid())
00218         {
00219                 return Open(ad);
00220         }
00221         return false;
00222 }

bool UdpSocket::Open ( SocketAddress ad  ) 

Define remote host.

Parameters:
ad Socket address
Returns:
true if successful

Definition at line 236 of file UdpSocket.cpp.

References Socket::Attach(), Socket::CreateSocket(), Errno, SocketAddress::GetFamily(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_FATAL, ISocketHandler::LogError(), Socket::SetCloseAndDelete(), Socket::SetConnected(), Socket::SetNonblocking(), and StrError.

00237 {
00238         if (GetSocket() == INVALID_SOCKET)
00239         {
00240                 Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp"));
00241         }
00242         if (GetSocket() != INVALID_SOCKET)
00243         {
00244                 SetNonblocking(true);
00245                 if (connect(GetSocket(), ad, ad) == -1)
00246                 {
00247                         Handler().LogError(this, "connect", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00248                         SetCloseAndDelete();
00249                         return false;
00250                 }
00251                 SetConnected();
00252                 return true;
00253         }
00254         return false;
00255 }

void UdpSocket::SendToBuf ( const std::string &  h,
port_t  p,
const char *  data,
int  len,
int  flags = 0 
)

Send to specified host.

send to specified address

Definition at line 292 of file UdpSocket.cpp.

References Ipv4Address::IsValid().

Referenced by SendTo(), and SendToBuf().

00293 {
00294 #ifdef ENABLE_IPV6
00295 #ifdef IPPROTO_IPV6
00296         if (IsIpv6())
00297         {
00298                 Ipv6Address ad(h, p);
00299                 if (ad.IsValid())
00300                 {
00301                         SendToBuf(ad, data, len, flags);
00302                 }
00303                 return;
00304         }
00305 #endif
00306 #endif
00307         Ipv4Address ad(h, p);
00308         if (ad.IsValid())
00309         {
00310                 SendToBuf(ad, data, len, flags);
00311         }
00312 }

void UdpSocket::SendToBuf ( ipaddr_t  a,
port_t  p,
const char *  data,
int  len,
int  flags = 0 
)

Send to specified address.

send to specified address

Definition at line 316 of file UdpSocket.cpp.

References SendToBuf().

00317 {
00318         Ipv4Address ad(a, p);
00319         SendToBuf(ad, data, len, flags);
00320 }

void UdpSocket::SendToBuf ( SocketAddress ad,
const char *  data,
int  len,
int  flags = 0 
)

Send to specified socket address.

Definition at line 334 of file UdpSocket.cpp.

References Socket::Attach(), Socket::CreateSocket(), Errno, SocketAddress::GetFamily(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_ERROR, ISocketHandler::LogError(), m_last_size_written, Socket::SetNonblocking(), and StrError.

00335 {
00336         if (GetSocket() == INVALID_SOCKET)
00337         {
00338                 Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp"));
00339         }
00340         if (GetSocket() != INVALID_SOCKET)
00341         {
00342                 SetNonblocking(true);
00343                 if ((m_last_size_written = sendto(GetSocket(), data, len, flags, ad, ad)) == -1)
00344                 {
00345                         Handler().LogError(this, "sendto", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00346                 }
00347         }
00348 }

void UdpSocket::SendTo ( const std::string &  a,
port_t  p,
const std::string &  str,
int  flags = 0 
)

Send string to specified host.

Definition at line 351 of file UdpSocket.cpp.

References SendToBuf().

00352 {
00353         SendToBuf(a, p, str.c_str(), (int)str.size(), flags);
00354 }

void UdpSocket::SendTo ( ipaddr_t  a,
port_t  p,
const std::string &  str,
int  flags = 0 
)

Send string to specified address.

Definition at line 357 of file UdpSocket.cpp.

References SendToBuf().

00358 {
00359         SendToBuf(a, p, str.c_str(), (int)str.size(), flags);
00360 }

void UdpSocket::SendTo ( SocketAddress ad,
const std::string &  str,
int  flags = 0 
)

Send string to specified socket address.

Definition at line 373 of file UdpSocket.cpp.

References SendToBuf().

00374 {
00375         SendToBuf(ad, str.c_str(), (int)str.size(), flags);
00376 }

void UdpSocket::SendBuf ( const char *  data,
size_t  len,
int  flags = 0 
) [virtual]

Send to connected address.

send to connected address

Reimplemented from Socket.

Definition at line 380 of file UdpSocket.cpp.

References Errno, Socket::GetSocket(), Socket::Handler(), Socket::IsConnected(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), m_last_size_written, and StrError.

Referenced by Send().

00381 {
00382         if (!IsConnected())
00383         {
00384                 Handler().LogError(this, "SendBuf", 0, "not connected", LOG_LEVEL_ERROR);
00385                 return;
00386         }
00387         if ((m_last_size_written = send(GetSocket(), data, (int)len, flags)) == -1)
00388         {
00389                 Handler().LogError(this, "send", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00390         }
00391 }

void UdpSocket::Send ( const std::string &  str,
int  flags = 0 
) [virtual]

Send string to connected address.

Reimplemented from Socket.

Definition at line 394 of file UdpSocket.cpp.

References SendBuf().

00395 {
00396         SendBuf(str.c_str(), (int)str.size(), flags);
00397 }

void UdpSocket::SetBroadcast ( bool  b = true  ) 

Set broadcast.

Definition at line 579 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), and StrError.

00580 {
00581         int one = 1;
00582         int zero = 0;
00583 
00584         if (GetSocket() == INVALID_SOCKET)
00585         {
00586                 CreateConnection();
00587         }
00588         if (b)
00589         {
00590                 if (setsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof(one)) == -1)
00591                 {
00592                         Handler().LogError(this, "SetBroadcast", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00593                 }
00594         }
00595         else
00596         {
00597                 if (setsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *) &zero, sizeof(zero)) == -1)
00598                 {
00599                         Handler().LogError(this, "SetBroadcast", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00600                 }
00601         }
00602 }

bool UdpSocket::IsBroadcast (  ) 

Check broadcast flag.

Returns:
true broadcast is enabled.

Definition at line 605 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), and StrError.

00606 {
00607         int is_broadcast = 0;
00608         socklen_t size = sizeof(int);
00609 
00610         if (GetSocket() == INVALID_SOCKET)
00611         {
00612                 CreateConnection();
00613         }
00614         if (getsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, &size) == -1)
00615         {
00616                 Handler().LogError(this, "IsBroadcast", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00617         }
00618         return is_broadcast != 0;
00619 }

void UdpSocket::SetMulticastTTL ( int  ttl = 1  ) 

multicast

Definition at line 622 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.

00623 {
00624         if (GetSocket() == INVALID_SOCKET)
00625         {
00626                 CreateConnection();
00627         }
00628         if (setsockopt(GetSocket(), SOL_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(int)) == -1)
00629         {
00630                 Handler().LogError(this, "SetMulticastTTL", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00631         }
00632 }

int UdpSocket::GetMulticastTTL (  ) 

Definition at line 635 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.

00636 {
00637         int ttl = 0;
00638         socklen_t size = sizeof(int);
00639 
00640         if (GetSocket() == INVALID_SOCKET)
00641         {
00642                 CreateConnection();
00643         }
00644         if (getsockopt(GetSocket(), SOL_IP, IP_MULTICAST_TTL, (char *)&ttl, &size) == -1)
00645         {
00646                 Handler().LogError(this, "GetMulticastTTL", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00647         }
00648         return ttl;
00649 }

void UdpSocket::SetMulticastLoop ( bool  x = true  ) 

Reimplemented from Socket.

Definition at line 652 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.

00653 {
00654         if (GetSocket() == INVALID_SOCKET)
00655         {
00656                 CreateConnection();
00657         }
00658 #ifdef ENABLE_IPV6
00659 #ifdef IPPROTO_IPV6
00660         if (IsIpv6())
00661         {
00662                 int val = x ? 1 : 0;
00663                 if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&val, sizeof(int)) == -1)
00664                 {
00665                         Handler().LogError(this, "SetMulticastLoop(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00666                 }
00667                 return;
00668         }
00669 #endif
00670 #endif
00671         int val = x ? 1 : 0;
00672         if (setsockopt(GetSocket(), SOL_IP, IP_MULTICAST_LOOP, (char *)&val, sizeof(int)) == -1)
00673         {
00674                 Handler().LogError(this, "SetMulticastLoop(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00675         }
00676 }

bool UdpSocket::IsMulticastLoop (  ) 

Definition at line 679 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.

00680 {
00681         if (GetSocket() == INVALID_SOCKET)
00682         {
00683                 CreateConnection();
00684         }
00685 #ifdef ENABLE_IPV6
00686 #ifdef IPPROTO_IPV6
00687         if (IsIpv6())
00688         {
00689                 int is_loop = 0;
00690                 socklen_t size = sizeof(int);
00691                 if (getsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&is_loop, &size) == -1)
00692                 {
00693                         Handler().LogError(this, "IsMulticastLoop(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00694                 }
00695                 return is_loop ? true : false;
00696         }
00697 #endif
00698 #endif
00699         int is_loop = 0;
00700         socklen_t size = sizeof(int);
00701         if (getsockopt(GetSocket(), SOL_IP, IP_MULTICAST_LOOP, (char *)&is_loop, &size) == -1)
00702         {
00703                 Handler().LogError(this, "IsMulticastLoop(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00704         }
00705         return is_loop ? true : false;
00706 }

void UdpSocket::SetMulticastDefaultInterface ( ipaddr_t  a,
int  if_index = 0 
)

Definition at line 868 of file UdpSocket.cpp.

References Errno, Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and StrError.

Referenced by SetMulticastDefaultInterface().

00869 {
00870         struct in_addr x;
00871         memcpy(&x.s_addr, &a, sizeof(x.s_addr));
00872         if (setsockopt(GetSocket(), IPPROTO_IP, IP_MULTICAST_IF, (char *)&x, sizeof(x)) == -1)
00873         {
00874                 Handler().LogError(this, "SetMulticastDefaultInterface(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00875         }
00876 }

void UdpSocket::SetMulticastDefaultInterface ( const std::string &  intf,
int  if_index = 0 
)

Definition at line 892 of file UdpSocket.cpp.

References CreateConnection(), Socket::GetSocket(), INVALID_SOCKET, SetMulticastDefaultInterface(), and Utility::u2ip().

00893 {
00894         if (GetSocket() == INVALID_SOCKET)
00895         {
00896                 CreateConnection();
00897         }
00898 #ifdef ENABLE_IPV6
00899 #ifdef IPPROTO_IPV6
00900         if (IsIpv6())
00901         {
00902                 struct in6_addr a;
00903                 if (Utility::u2ip( intf, a ))
00904                 {
00905                         SetMulticastDefaultInterface( a, if_index );
00906                 }
00907                 return;
00908         }
00909 #endif
00910 #endif
00911         ipaddr_t a;
00912         if (Utility::u2ip( intf, a ))
00913         {
00914                 SetMulticastDefaultInterface( a, if_index );
00915         }
00916 }

void UdpSocket::AddMulticastMembership ( const std::string &  group,
const std::string &  intf = "0.0.0.0",
int  if_index = 0 
)

Definition at line 709 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, StrError, and Utility::u2ip().

00710 {
00711         if (GetSocket() == INVALID_SOCKET)
00712         {
00713                 CreateConnection();
00714         }
00715 #ifdef ENABLE_IPV6
00716 #ifdef IPPROTO_IPV6
00717         if (IsIpv6())
00718         {
00719                 struct ipv6_mreq x;
00720                 struct in6_addr addr;
00721                 if (Utility::u2ip( group, addr ))
00722                 {
00723                         x.ipv6mr_multiaddr = addr;
00724                         x.ipv6mr_interface = if_index;
00725                         if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&x, sizeof(struct ipv6_mreq)) == -1)
00726                         {
00727                                 Handler().LogError(this, "AddMulticastMembership(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00728                         }
00729                 }
00730                 return;
00731         }
00732 #endif
00733 #endif
00734         struct ip_mreq x; // ip_mreqn
00735         ipaddr_t addr;
00736         if (Utility::u2ip( group, addr ))
00737         {
00738                 memcpy(&x.imr_multiaddr.s_addr, &addr, sizeof(addr));
00739                 Utility::u2ip( local_if, addr);
00740                 memcpy(&x.imr_interface.s_addr, &addr, sizeof(x.imr_interface.s_addr));
00741 //              x.imr_ifindex = if_index;
00742                 if (setsockopt(GetSocket(), SOL_IP, IP_ADD_MEMBERSHIP, (char *)&x, sizeof(struct ip_mreq)) == -1)
00743                 {
00744                         Handler().LogError(this, "AddMulticastMembership(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00745                 }
00746         }
00747 }

void UdpSocket::DropMulticastMembership ( const std::string &  group,
const std::string &  intf = "0.0.0.0",
int  if_index = 0 
)

Definition at line 750 of file UdpSocket.cpp.

References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, StrError, and Utility::u2ip().

00751 {
00752         if (GetSocket() == INVALID_SOCKET)
00753         {
00754                 CreateConnection();
00755         }
00756 #ifdef ENABLE_IPV6
00757 #ifdef IPPROTO_IPV6
00758         if (IsIpv6())
00759         {
00760                 struct ipv6_mreq x;
00761                 struct in6_addr addr;
00762                 if (Utility::u2ip( group, addr ))
00763                 {
00764                         x.ipv6mr_multiaddr = addr;
00765                         x.ipv6mr_interface = if_index;
00766                         if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, (char *)&x, sizeof(struct ipv6_mreq)) == -1)
00767                         {
00768                                 Handler().LogError(this, "DropMulticastMembership(ipv6)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00769                         }
00770                 }
00771                 return;
00772         }
00773 #endif
00774 #endif
00775         struct ip_mreq x; // ip_mreqn
00776         ipaddr_t addr;
00777         if (Utility::u2ip( group, addr ))
00778         {
00779                 memcpy(&x.imr_multiaddr.s_addr, &addr, sizeof(addr));
00780                 Utility::u2ip( local_if, addr);
00781                 memcpy(&x.imr_interface.s_addr, &addr, sizeof(x.imr_interface.s_addr));
00782 //              x.imr_ifindex = if_index;
00783                 if (setsockopt(GetSocket(), SOL_IP, IP_DROP_MEMBERSHIP, (char *)&x, sizeof(struct ip_mreq)) == -1)
00784                 {
00785                         Handler().LogError(this, "DropMulticastMembership(ipv4)", Errno, StrError(Errno), LOG_LEVEL_WARNING);
00786                 }
00787         }
00788 }

bool UdpSocket::IsBound (  ) 

Returns true if Bind succeeded.

Definition at line 834 of file UdpSocket.cpp.

References m_bind_ok.

00835 {
00836         return m_bind_ok;
00837 }

port_t UdpSocket::GetPort (  )  [virtual]

Return Bind port number.

Reimplemented from Socket.

Definition at line 850 of file UdpSocket.cpp.

References m_port.

00851 {
00852         return m_port;
00853 }

void UdpSocket::OnOptions ( int  family,
int  type,
int  protocol,
SOCKET  s 
) [inline, virtual]

Called when a client socket is created, to set socket options.

Parameters:
family AF_INET, AF_INET6, etc
type SOCK_STREAM, SOCK_DGRAM, etc
protocol Protocol number (tcp, udp, sctp, etc)
s Socket file descriptor

Implements Socket.

Definition at line 190 of file UdpSocket.h.

00190 {}

int UdpSocket::GetLastSizeWritten (  ) 

Definition at line 856 of file UdpSocket.cpp.

References m_last_size_written.

00857 {
00858         return m_last_size_written;
00859 }

void UdpSocket::SetTimestamp ( bool  x = true  ) 

Also read timestamp information from incoming message.

Definition at line 862 of file UdpSocket.cpp.

References m_b_read_ts.

00863 {
00864         m_b_read_ts = x;
00865 }

void UdpSocket::OnRead (  )  [protected, virtual]

Called when there is something to be read from the file descriptor.

Reimplemented from Socket.

Definition at line 466 of file UdpSocket.cpp.

References Errno, Socket::GetSocket(), Utility::GetTime(), Socket::Handler(), LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, ISocketHandler::LogError(), m_b_read_ts, m_ibuf, m_ibufsz, m_retries, OnRawData(), and StrError.

00467 {
00468 #ifdef ENABLE_IPV6
00469 #ifdef IPPROTO_IPV6
00470         if (IsIpv6())
00471         {
00472                 struct sockaddr_in6 sa;
00473                 socklen_t sa_len = sizeof(sa);
00474                 if (m_b_read_ts)
00475                 {
00476                         struct timeval ts;
00477                         Utility::GetTime(&ts);
00478 #if !defined(LINUX) && !defined(MACOSX)
00479                         int n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len);
00480 #else
00481                         int n = ReadTS(m_ibuf, m_ibufsz, (struct sockaddr *)&sa, sa_len, &ts);
00482 #endif
00483                         if (n > 0)
00484                         {
00485                                 this -> OnRawData(m_ibuf, n, (struct sockaddr *)&sa, sa_len, &ts);
00486                         }
00487                         else
00488                         if (n == -1)
00489                         {
00490 #ifdef _WIN32
00491                                 if (Errno != WSAEWOULDBLOCK)
00492 #else
00493                                 if (Errno != EWOULDBLOCK)
00494 #endif
00495                                         Handler().LogError(this, "recvfrom", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00496                         }
00497                         return;
00498                 }
00499                 int n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len);
00500                 int q = m_retries; // receive max 10 at one cycle
00501                 while (n > 0)
00502                 {
00503                         if (sa_len != sizeof(sa))
00504                         {
00505                                 Handler().LogError(this, "recvfrom", 0, "unexpected address struct size", LOG_LEVEL_WARNING);
00506                         }
00507                         this -> OnRawData(m_ibuf, n, (struct sockaddr *)&sa, sa_len);
00508                         if (!q--)
00509                                 break;
00510                         //
00511                         n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len);
00512                 }
00513                 if (n == -1)
00514                 {
00515 #ifdef _WIN32
00516                         if (Errno != WSAEWOULDBLOCK)
00517 #else
00518                         if (Errno != EWOULDBLOCK)
00519 #endif
00520                                 Handler().LogError(this, "recvfrom", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00521                 }
00522                 return;
00523         }
00524 #endif
00525 #endif
00526         struct sockaddr_in sa;
00527         socklen_t sa_len = sizeof(sa);
00528         if (m_b_read_ts)
00529         {
00530                 struct timeval ts;
00531                 Utility::GetTime(&ts);
00532 #if !defined(LINUX) && !defined(MACOSX)
00533                 int n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len);
00534 #else
00535                 int n = ReadTS(m_ibuf, m_ibufsz, (struct sockaddr *)&sa, sa_len, &ts);
00536 #endif
00537                 if (n > 0)
00538                 {
00539                         this -> OnRawData(m_ibuf, n, (struct sockaddr *)&sa, sa_len, &ts);
00540                 }
00541                 else
00542                 if (n == -1)
00543                 {
00544 #ifdef _WIN32
00545                         if (Errno != WSAEWOULDBLOCK)
00546 #else
00547                         if (Errno != EWOULDBLOCK)
00548 #endif
00549                                 Handler().LogError(this, "recvfrom", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00550                 }
00551                 return;
00552         }
00553         int n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len);
00554         int q = m_retries;
00555         while (n > 0)
00556         {
00557                 if (sa_len != sizeof(sa))
00558                 {
00559                         Handler().LogError(this, "recvfrom", 0, "unexpected address struct size", LOG_LEVEL_WARNING);
00560                 }
00561                 this -> OnRawData(m_ibuf, n, (struct sockaddr *)&sa, sa_len);
00562                 if (!q--)
00563                         break;
00564                 //
00565                 n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len);
00566         }
00567         if (n == -1)
00568         {
00569 #ifdef _WIN32
00570                 if (Errno != WSAEWOULDBLOCK)
00571 #else
00572                 if (Errno != EWOULDBLOCK)
00573 #endif
00574                         Handler().LogError(this, "recvfrom", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00575         }
00576 }

UdpSocket& UdpSocket::operator= ( const UdpSocket  )  [inline, private]

Definition at line 206 of file UdpSocket.h.

00206 { return *this; }

void UdpSocket::CreateConnection (  )  [private]

create before using sendto methods

Definition at line 258 of file UdpSocket.cpp.

References Socket::Attach(), Socket::CreateSocket(), Socket::GetSocket(), INVALID_SOCKET, and Socket::SetNonblocking().

Referenced by AddMulticastMembership(), DropMulticastMembership(), GetMulticastTTL(), IsBroadcast(), IsMulticastLoop(), SetBroadcast(), SetMulticastDefaultInterface(), SetMulticastLoop(), and SetMulticastTTL().

00259 {
00260 #ifdef ENABLE_IPV6
00261 #ifdef IPPROTO_IPV6
00262         if (IsIpv6())
00263         {
00264                 if (GetSocket() == INVALID_SOCKET)
00265                 {
00266                         SOCKET s = CreateSocket(AF_INET6, SOCK_DGRAM, "udp");
00267                         if (s == INVALID_SOCKET)
00268                         {
00269                                 return;
00270                         }
00271                         SetNonblocking(true, s);
00272                         Attach(s);
00273                 }
00274                 return;
00275         }
00276 #endif
00277 #endif
00278         if (GetSocket() == INVALID_SOCKET)
00279         {
00280                 SOCKET s = CreateSocket(AF_INET, SOCK_DGRAM, "udp");
00281                 if (s == INVALID_SOCKET)
00282                 {
00283                         return;
00284                 }
00285                 SetNonblocking(true, s);
00286                 Attach(s);
00287         }
00288 }


Member Data Documentation

char* UdpSocket::m_ibuf [private]

Input buffer.

Definition at line 209 of file UdpSocket.h.

Referenced by OnRead(), and ~UdpSocket().

int UdpSocket::m_ibufsz [private]

Size of input buffer.

Definition at line 210 of file UdpSocket.h.

Referenced by OnRead().

bool UdpSocket::m_bind_ok [private]

Bind completed successfully.

Definition at line 211 of file UdpSocket.h.

Referenced by Bind(), and IsBound().

Bind port number.

Definition at line 212 of file UdpSocket.h.

Referenced by Bind(), and GetPort().

Definition at line 213 of file UdpSocket.h.

Referenced by GetLastSizeWritten(), SendBuf(), and SendToBuf().

int UdpSocket::m_retries [private]

Definition at line 214 of file UdpSocket.h.

Referenced by OnRead().

bool UdpSocket::m_b_read_ts [private]

Definition at line 215 of file UdpSocket.h.

Referenced by OnRead(), and SetTimestamp().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4