![]() |
UdpSocket Class ReferenceSocket implementation for UDP.
More...
|
Public Member Functions | |
UdpSocket (ISocketHandler &h, int ibufsz=16384, bool ipv6=false) | |
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. | |
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 | 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 () |
Protected Member Functions | |
UdpSocket (const UdpSocket &s) | |
void | OnRead () |
Called when there is something to be read from the file descriptor. | |
Private Member Functions | |
UdpSocket & | operator= (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 |
Definition at line 42 of file UdpSocket.h.
UdpSocket::UdpSocket | ( | ISocketHandler & | h, | |
int | ibufsz = 16384 , |
|||
bool | ipv6 = false | |||
) |
Constructor.
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 51 of file UdpSocket.cpp.
00051 : Socket(h) 00052 , m_ibuf(new char[ibufsz]) 00053 , m_ibufsz(ibufsz) 00054 , m_bind_ok(false) 00055 , m_port(0) 00056 , m_last_size_written(-1) 00057 { 00058 #ifdef ENABLE_IPV6 00059 #ifdef IPPROTO_IPV6 00060 SetIpv6(ipv6); 00061 #endif 00062 #endif 00063 }
UdpSocket::~UdpSocket | ( | ) |
UdpSocket::UdpSocket | ( | const UdpSocket & | s | ) | [inline, protected] |
void UdpSocket::OnRawData | ( | const char * | buf, | |
size_t | len, | |||
struct sockaddr * | sa, | |||
socklen_t | sa_len | |||
) | [virtual] |
Called when incoming data has been received.
buf | Pointer to data | |
len | Length of data | |
sa | Pointer to sockaddr struct of sender | |
sa_len | Length of sockaddr struct |
Definition at line 694 of file UdpSocket.cpp.
Referenced by OnRead().
int UdpSocket::Bind | ( | port_t & | port, | |
int | range = 1 | |||
) |
To receive incoming data, call Bind to setup an incoming port.
port | Incoming port number | |
range | Port range to try if ports already in use |
Definition at line 73 of file UdpSocket.cpp.
Referenced by Bind().
00074 { 00075 #ifdef ENABLE_IPV6 00076 #ifdef IPPROTO_IPV6 00077 if (IsIpv6()) 00078 { 00079 Ipv6Address ad(port); 00080 return Bind(ad, range); 00081 } 00082 #endif 00083 #endif 00084 Ipv4Address ad(port); 00085 return Bind(ad, range); 00086 }
int UdpSocket::Bind | ( | const std::string & | intf, | |
port_t & | port, | |||
int | range = 1 | |||
) |
To receive data on a specific interface:port, use this.
intf | Interface ip/hostname | |
port | Port number | |
range | Port range |
Definition at line 89 of file UdpSocket.cpp.
References Bind(), Ipv4Address::IsValid(), and Socket::SetCloseAndDelete().
00090 { 00091 #ifdef ENABLE_IPV6 00092 #ifdef IPPROTO_IPV6 00093 if (IsIpv6()) 00094 { 00095 Ipv6Address ad(intf, port); 00096 if (ad.IsValid()) 00097 { 00098 return Bind(ad, range); 00099 } 00100 SetCloseAndDelete(); 00101 return -1; 00102 } 00103 #endif 00104 #endif 00105 Ipv4Address ad(intf, port); 00106 if (ad.IsValid()) 00107 { 00108 return Bind(ad, range); 00109 } 00110 SetCloseAndDelete(); 00111 return -1; 00112 }
To receive data on a specific interface:port, use this.
a | Ip address | |
port | Port number | |
range | Port range |
Definition at line 115 of file UdpSocket.cpp.
References Bind().
00116 { 00117 Ipv4Address ad(a, port); 00118 return Bind(ad, range); 00119 }
int UdpSocket::Bind | ( | SocketAddress & | ad, | |
int | range = 1 | |||
) |
To receive data on a specific interface:port, use this.
ad | Socket address | |
range | Port range |
Definition at line 133 of file UdpSocket.cpp.
References Socket::Attach(), Socket::CreateSocket(), Errno, SocketAddress::GetFamily(), SocketAddress::GetPort(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_bind_ok, m_port, Socket::SetCloseAndDelete(), Socket::SetNonblocking(), SocketAddress::SetPort(), and StrError.
00134 { 00135 if (GetSocket() == INVALID_SOCKET) 00136 { 00137 Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp")); 00138 } 00139 if (GetSocket() != INVALID_SOCKET) 00140 { 00141 SetNonblocking(true); 00142 int n = bind(GetSocket(), ad, ad); 00143 int tries = range; 00144 while (n == -1 && tries--) 00145 { 00146 ad.SetPort(ad.GetPort() + 1); 00147 n = bind(GetSocket(), ad, ad); 00148 } 00149 if (n == -1) 00150 { 00151 Handler().LogError(this, "bind", Errno, StrError(Errno), LOG_LEVEL_FATAL); 00152 SetCloseAndDelete(); 00153 return -1; 00154 } 00155 m_bind_ok = true; 00156 m_port = ad.GetPort(); 00157 return 0; 00158 } 00159 return -1; 00160 }
Define remote host.
l | Address of remote host | |
port | Port of remote host |
Definition at line 164 of file UdpSocket.cpp.
Referenced by Open().
00165 { 00166 Ipv4Address ad(l, port); 00167 return Open(ad); 00168 }
bool UdpSocket::Open | ( | const std::string & | host, | |
port_t | port | |||
) |
Define remote host.
host | Hostname | |
port | Port number |
Definition at line 171 of file UdpSocket.cpp.
References Ipv4Address::IsValid(), and Open().
00172 { 00173 #ifdef ENABLE_IPV6 00174 #ifdef IPPROTO_IPV6 00175 if (IsIpv6()) 00176 { 00177 Ipv6Address ad(host, port); 00178 if (ad.IsValid()) 00179 { 00180 return Open(ad); 00181 } 00182 return false; 00183 } 00184 #endif 00185 #endif 00186 Ipv4Address ad(host, port); 00187 if (ad.IsValid()) 00188 { 00189 return Open(ad); 00190 } 00191 return false; 00192 }
bool UdpSocket::Open | ( | SocketAddress & | ad | ) |
Define remote host.
ad | Socket address |
Definition at line 206 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.
00207 { 00208 if (GetSocket() == INVALID_SOCKET) 00209 { 00210 Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp")); 00211 } 00212 if (GetSocket() != INVALID_SOCKET) 00213 { 00214 SetNonblocking(true); 00215 if (connect(GetSocket(), ad, ad) == -1) 00216 { 00217 Handler().LogError(this, "connect", Errno, StrError(Errno), LOG_LEVEL_FATAL); 00218 SetCloseAndDelete(); 00219 return false; 00220 } 00221 SetConnected(); 00222 return true; 00223 } 00224 return false; 00225 }
void UdpSocket::SendToBuf | ( | const std::string & | , | |
port_t | , | |||
const char * | data, | |||
int | len, | |||
int | flags = 0 | |||
) |
Send to specified host.
Definition at line 262 of file UdpSocket.cpp.
References Ipv4Address::IsValid().
Referenced by SendTo(), and SendToBuf().
00263 { 00264 #ifdef ENABLE_IPV6 00265 #ifdef IPPROTO_IPV6 00266 if (IsIpv6()) 00267 { 00268 Ipv6Address ad(h, p); 00269 if (ad.IsValid()) 00270 { 00271 SendToBuf(ad, data, len, flags); 00272 } 00273 return; 00274 } 00275 #endif 00276 #endif 00277 Ipv4Address ad(h, p); 00278 if (ad.IsValid()) 00279 { 00280 SendToBuf(ad, data, len, flags); 00281 } 00282 }
Send to specified address.
Definition at line 286 of file UdpSocket.cpp.
References SendToBuf().
00287 { 00288 Ipv4Address ad(a, p); 00289 SendToBuf(ad, data, len, flags); 00290 }
void UdpSocket::SendToBuf | ( | SocketAddress & | ad, | |
const char * | data, | |||
int | len, | |||
int | flags = 0 | |||
) |
Send to specified socket address.
Definition at line 304 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.
00305 { 00306 if (GetSocket() == INVALID_SOCKET) 00307 { 00308 Attach(CreateSocket(ad.GetFamily(), SOCK_DGRAM, "udp")); 00309 } 00310 if (GetSocket() != INVALID_SOCKET) 00311 { 00312 SetNonblocking(true); 00313 if ((m_last_size_written = sendto(GetSocket(), data, len, flags, ad, ad)) == -1) 00314 { 00315 Handler().LogError(this, "sendto", Errno, StrError(Errno), LOG_LEVEL_ERROR); 00316 } 00317 } 00318 }
void UdpSocket::SendTo | ( | const std::string & | , | |
port_t | , | |||
const std::string & | , | |||
int | flags = 0 | |||
) |
Send string to specified host.
Definition at line 321 of file UdpSocket.cpp.
References SendToBuf().
00322 { 00323 SendToBuf(a, p, str.c_str(), (int)str.size(), flags); 00324 }
Send string to specified address.
Definition at line 327 of file UdpSocket.cpp.
References SendToBuf().
00328 { 00329 SendToBuf(a, p, str.c_str(), (int)str.size(), flags); 00330 }
void UdpSocket::SendTo | ( | SocketAddress & | ad, | |
const std::string & | , | |||
int | flags = 0 | |||
) |
Send string to specified socket address.
Definition at line 343 of file UdpSocket.cpp.
References SendToBuf().
00344 { 00345 SendToBuf(ad, str.c_str(), (int)str.size(), flags); 00346 }
void UdpSocket::SendBuf | ( | const char * | data, | |
size_t | , | |||
int | flags = 0 | |||
) | [virtual] |
Send to connected address.
Reimplemented from Socket.
Definition at line 350 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().
00351 { 00352 if (!IsConnected()) 00353 { 00354 Handler().LogError(this, "SendBuf", 0, "not connected", LOG_LEVEL_ERROR); 00355 return; 00356 } 00357 if ((m_last_size_written = send(GetSocket(), data, (int)len, flags)) == -1) 00358 { 00359 Handler().LogError(this, "send", Errno, StrError(Errno), LOG_LEVEL_ERROR); 00360 } 00361 }
void UdpSocket::Send | ( | const std::string & | , | |
int | flags = 0 | |||
) | [virtual] |
Send string to connected address.
Reimplemented from Socket.
Definition at line 364 of file UdpSocket.cpp.
References SendBuf().
00365 { 00366 SendBuf(str.c_str(), (int)str.size(), flags); 00367 }
void UdpSocket::SetBroadcast | ( | bool | b = true |
) |
Set broadcast.
Definition at line 433 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), and StrError.
00434 { 00435 int one = 1; 00436 int zero = 0; 00437 00438 if (GetSocket() == INVALID_SOCKET) 00439 { 00440 CreateConnection(); 00441 } 00442 if (b) 00443 { 00444 if (setsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof(one)) == -1) 00445 { 00446 Handler().LogError(this, "SetBroadcast", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00447 } 00448 } 00449 else 00450 { 00451 if (setsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *) &zero, sizeof(zero)) == -1) 00452 { 00453 Handler().LogError(this, "SetBroadcast", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00454 } 00455 } 00456 }
bool UdpSocket::IsBroadcast | ( | ) |
Check broadcast flag.
Definition at line 459 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), and StrError.
00460 { 00461 int is_broadcast = 0; 00462 socklen_t size; 00463 00464 if (GetSocket() == INVALID_SOCKET) 00465 { 00466 CreateConnection(); 00467 } 00468 if (getsockopt(GetSocket(), SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, &size) == -1) 00469 { 00470 Handler().LogError(this, "IsBroadcast", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00471 } 00472 return is_broadcast != 0; 00473 }
void UdpSocket::SetMulticastTTL | ( | int | ttl = 1 |
) |
multicast
Definition at line 476 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.
00477 { 00478 if (GetSocket() == INVALID_SOCKET) 00479 { 00480 CreateConnection(); 00481 } 00482 if (setsockopt(GetSocket(), SOL_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(int)) == -1) 00483 { 00484 Handler().LogError(this, "SetMulticastTTL", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00485 } 00486 }
int UdpSocket::GetMulticastTTL | ( | ) |
Definition at line 489 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.
00490 { 00491 int ttl = 0; 00492 socklen_t size = sizeof(int); 00493 00494 if (GetSocket() == INVALID_SOCKET) 00495 { 00496 CreateConnection(); 00497 } 00498 if (getsockopt(GetSocket(), SOL_IP, IP_MULTICAST_TTL, (char *)&ttl, &size) == -1) 00499 { 00500 Handler().LogError(this, "GetMulticastTTL", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00501 } 00502 return ttl; 00503 }
void UdpSocket::SetMulticastLoop | ( | bool | = true |
) |
Reimplemented from Socket.
Definition at line 506 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.
00507 { 00508 if (GetSocket() == INVALID_SOCKET) 00509 { 00510 CreateConnection(); 00511 } 00512 #ifdef ENABLE_IPV6 00513 #ifdef IPPROTO_IPV6 00514 if (IsIpv6()) 00515 { 00516 int val = x ? 1 : 0; 00517 if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&val, sizeof(int)) == -1) 00518 { 00519 Handler().LogError(this, "SetMulticastLoop", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00520 } 00521 return; 00522 } 00523 #endif 00524 #endif 00525 int val = x ? 1 : 0; 00526 if (setsockopt(GetSocket(), SOL_IP, IP_MULTICAST_LOOP, (char *)&val, sizeof(int)) == -1) 00527 { 00528 Handler().LogError(this, "SetMulticastLoop", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00529 } 00530 }
bool UdpSocket::IsMulticastLoop | ( | ) |
Definition at line 533 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, and StrError.
00534 { 00535 if (GetSocket() == INVALID_SOCKET) 00536 { 00537 CreateConnection(); 00538 } 00539 #ifdef ENABLE_IPV6 00540 #ifdef IPPROTO_IPV6 00541 if (IsIpv6()) 00542 { 00543 int is_loop = 0; 00544 socklen_t size = sizeof(int); 00545 if (getsockopt(GetSocket(), IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&is_loop, &size) == -1) 00546 { 00547 Handler().LogError(this, "IsMulticastLoop", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00548 } 00549 return is_loop ? true : false; 00550 } 00551 #endif 00552 #endif 00553 int is_loop = 0; 00554 socklen_t size = sizeof(int); 00555 if (getsockopt(GetSocket(), SOL_IP, IP_MULTICAST_LOOP, (char *)&is_loop, &size) == -1) 00556 { 00557 Handler().LogError(this, "IsMulticastLoop", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00558 } 00559 return is_loop ? true : false; 00560 }
void UdpSocket::AddMulticastMembership | ( | const std::string & | group, | |
const std::string & | intf = "0.0.0.0" , |
|||
int | if_index = 0 | |||
) |
Definition at line 563 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, StrError, and Utility::u2ip().
00564 { 00565 if (GetSocket() == INVALID_SOCKET) 00566 { 00567 CreateConnection(); 00568 } 00569 #ifdef ENABLE_IPV6 00570 #ifdef IPPROTO_IPV6 00571 if (IsIpv6()) 00572 { 00573 struct ipv6_mreq x; 00574 struct in6_addr addr; 00575 if (Utility::u2ip( group, addr )) 00576 { 00577 x.ipv6mr_multiaddr = addr; 00578 x.ipv6mr_interface = if_index; 00579 if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&x, sizeof(struct ipv6_mreq)) == -1) 00580 { 00581 Handler().LogError(this, "AddMulticastMembership", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00582 } 00583 } 00584 return; 00585 } 00586 #endif 00587 #endif 00588 struct ip_mreq x; // ip_mreqn 00589 ipaddr_t addr; 00590 if (Utility::u2ip( group, addr )) 00591 { 00592 memcpy(&x.imr_multiaddr.s_addr, &addr, sizeof(addr)); 00593 Utility::u2ip( local_if, addr); 00594 memcpy(&x.imr_interface.s_addr, &addr, sizeof(addr)); 00595 // x.imr_ifindex = if_index; 00596 if (setsockopt(GetSocket(), SOL_IP, IP_ADD_MEMBERSHIP, (char *)&x, sizeof(struct ip_mreq)) == -1) 00597 { 00598 Handler().LogError(this, "AddMulticastMembership", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00599 } 00600 } 00601 }
void UdpSocket::DropMulticastMembership | ( | const std::string & | group, | |
const std::string & | intf = "0.0.0.0" , |
|||
int | if_index = 0 | |||
) |
Definition at line 604 of file UdpSocket.cpp.
References CreateConnection(), Errno, Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, LOG_LEVEL_WARNING, ISocketHandler::LogError(), SOL_IP, StrError, and Utility::u2ip().
00605 { 00606 if (GetSocket() == INVALID_SOCKET) 00607 { 00608 CreateConnection(); 00609 } 00610 #ifdef ENABLE_IPV6 00611 #ifdef IPPROTO_IPV6 00612 if (IsIpv6()) 00613 { 00614 struct ipv6_mreq x; 00615 struct in6_addr addr; 00616 if (Utility::u2ip( group, addr )) 00617 { 00618 x.ipv6mr_multiaddr = addr; 00619 x.ipv6mr_interface = if_index; 00620 if (setsockopt(GetSocket(), IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, (char *)&x, sizeof(struct ipv6_mreq)) == -1) 00621 { 00622 Handler().LogError(this, "DropMulticastMembership", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00623 } 00624 } 00625 return; 00626 } 00627 #endif 00628 #endif 00629 struct ip_mreq x; // ip_mreqn 00630 ipaddr_t addr; 00631 if (Utility::u2ip( group, addr )) 00632 { 00633 memcpy(&x.imr_multiaddr.s_addr, &addr, sizeof(addr)); 00634 Utility::u2ip( local_if, addr); 00635 memcpy(&x.imr_interface.s_addr, &addr, sizeof(addr)); 00636 // x.imr_ifindex = if_index; 00637 if (setsockopt(GetSocket(), SOL_IP, IP_DROP_MEMBERSHIP, (char *)&x, sizeof(struct ip_mreq)) == -1) 00638 { 00639 Handler().LogError(this, "DropMulticastMembership", Errno, StrError(Errno), LOG_LEVEL_WARNING); 00640 } 00641 } 00642 }
bool UdpSocket::IsBound | ( | ) |
Returns true if Bind succeeded.
Definition at line 688 of file UdpSocket.cpp.
References m_bind_ok.
00689 { 00690 return m_bind_ok; 00691 }
port_t UdpSocket::GetPort | ( | ) | [virtual] |
Return Bind port number.
Reimplemented from Socket.
Definition at line 699 of file UdpSocket.cpp.
References m_port.
00700 { 00701 return m_port; 00702 }
void UdpSocket::OnOptions | ( | int | family, | |
int | type, | |||
int | protocol, | |||
SOCKET | s | |||
) | [inline, virtual] |
Called when a client socket is created, to set socket options.
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 173 of file UdpSocket.h.
int UdpSocket::GetLastSizeWritten | ( | ) |
Definition at line 705 of file UdpSocket.cpp.
References m_last_size_written.
00706 { 00707 return m_last_size_written; 00708 }
void UdpSocket::OnRead | ( | ) | [protected, virtual] |
Called when there is something to be read from the file descriptor.
Reimplemented from Socket.
Definition at line 370 of file UdpSocket.cpp.
References Errno, Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, ISocketHandler::LogError(), m_ibuf, m_ibufsz, OnRawData(), and StrError.
00371 { 00372 #ifdef ENABLE_IPV6 00373 #ifdef IPPROTO_IPV6 00374 if (IsIpv6()) 00375 { 00376 struct sockaddr_in6 sa; 00377 socklen_t sa_len = sizeof(sa); 00378 int n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len); 00379 int q = 10; // receive max 10 at one cycle 00380 while (n > 0) 00381 { 00382 if (sa_len != sizeof(sa)) 00383 { 00384 Handler().LogError(this, "recvfrom", 0, "unexpected address struct size", LOG_LEVEL_WARNING); 00385 } 00386 this -> OnRawData(m_ibuf, n, (struct sockaddr *)&sa, sa_len); 00387 if (!q--) 00388 break; 00389 // 00390 n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len); 00391 } 00392 if (n == -1) 00393 { 00394 #ifdef _WIN32 00395 if (Errno != WSAEWOULDBLOCK) 00396 #else 00397 if (Errno != EWOULDBLOCK) 00398 #endif 00399 Handler().LogError(this, "recvfrom", Errno, StrError(Errno), LOG_LEVEL_ERROR); 00400 } 00401 return; 00402 } 00403 #endif 00404 #endif 00405 struct sockaddr_in sa; 00406 socklen_t sa_len = sizeof(sa); 00407 int n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len); 00408 int q = 10; 00409 while (n > 0) 00410 { 00411 if (sa_len != sizeof(sa)) 00412 { 00413 Handler().LogError(this, "recvfrom", 0, "unexpected address struct size", LOG_LEVEL_WARNING); 00414 } 00415 this -> OnRawData(m_ibuf, n, (struct sockaddr *)&sa, sa_len); 00416 if (!q--) 00417 break; 00418 // 00419 n = recvfrom(GetSocket(), m_ibuf, m_ibufsz, 0, (struct sockaddr *)&sa, &sa_len); 00420 } 00421 if (n == -1) 00422 { 00423 #ifdef _WIN32 00424 if (Errno != WSAEWOULDBLOCK) 00425 #else 00426 if (Errno != EWOULDBLOCK) 00427 #endif 00428 Handler().LogError(this, "recvfrom", Errno, StrError(Errno), LOG_LEVEL_ERROR); 00429 } 00430 }
void UdpSocket::CreateConnection | ( | ) | [private] |
create before using sendto methods
Definition at line 228 of file UdpSocket.cpp.
References Socket::Attach(), Socket::CreateSocket(), Socket::GetSocket(), INVALID_SOCKET, and Socket::SetNonblocking().
Referenced by AddMulticastMembership(), DropMulticastMembership(), GetMulticastTTL(), IsBroadcast(), IsMulticastLoop(), SetBroadcast(), SetMulticastLoop(), and SetMulticastTTL().
00229 { 00230 #ifdef ENABLE_IPV6 00231 #ifdef IPPROTO_IPV6 00232 if (IsIpv6()) 00233 { 00234 if (GetSocket() == INVALID_SOCKET) 00235 { 00236 SOCKET s = CreateSocket(AF_INET6, SOCK_DGRAM, "udp"); 00237 if (s == INVALID_SOCKET) 00238 { 00239 return; 00240 } 00241 SetNonblocking(true, s); 00242 Attach(s); 00243 } 00244 return; 00245 } 00246 #endif 00247 #endif 00248 if (GetSocket() == INVALID_SOCKET) 00249 { 00250 SOCKET s = CreateSocket(AF_INET, SOCK_DGRAM, "udp"); 00251 if (s == INVALID_SOCKET) 00252 { 00253 return; 00254 } 00255 SetNonblocking(true, s); 00256 Attach(s); 00257 } 00258 }
char* UdpSocket::m_ibuf [private] |
int UdpSocket::m_ibufsz [private] |
bool UdpSocket::m_bind_ok [private] |
port_t UdpSocket::m_port [private] |
int UdpSocket::m_last_size_written [private] |
Definition at line 189 of file UdpSocket.h.
Referenced by GetLastSizeWritten(), SendBuf(), and SendToBuf().