33 const ibrcommon::vaddress UDPDatagramService::BROADCAST_ADDR(
"ff02::1", UDPDatagramService::BROADCAST_PORT, AF_INET6);
36 : _msock(NULL), _iface(iface), _bind_port(port)
66 _msock->
join(BROADCAST_ADDR, _iface);
68 if (e.
error() != EADDRINUSE) {
83 std::list<ibrcommon::vaddress> addrs = _iface.
getAddresses();
86 std::stringstream ss; ss << _bind_port;
88 for (std::list<ibrcommon::vaddress>::iterator iter = addrs.begin(); iter != addrs.end(); ++iter) {
104 throw DatagramException(
"bind failed");
130 UDPDatagramService::decode(identifier, destination);
133 send(type, flags, seqno, destination, buf, length);
144 send(type, flags, seqno, BROADCAST_ADDR, buf, length);
150 std::vector<char> tmp(length + 2);
156 tmp[1] =
static_cast<char>((0xf0 & (flags << 4)) | (0x0f & seqno));
159 ::memcpy(&tmp[2], buf, length);
161 IBRCOMMON_LOGGER_DEBUG_TAG(
"UDPDatagramService", 20) <<
"send() type: " << std::hex << (int)type <<
"; flags: " << std::hex << (
int)flags << "; seqno: " << std::dec << seqno << "; address: " << destination.toString() <<
IBRCOMMON_LOGGER_ENDL;
164 ibrcommon::
socketset sockset = _vsocket.getAll();
165 for (ibrcommon::
socketset::iterator iter = sockset.begin(); iter != sockset.end(); ++iter) {
166 if ((*iter) == _msock)
continue;
169 sock.
sendto(&tmp[0], length + 2, 0, destination);
172 }
catch (
const std::bad_cast&) { }
194 _vsocket.select(&readfds, NULL, NULL, NULL);
196 for (ibrcommon::socketset::iterator iter = readfds.begin(); iter != readfds.end(); ++iter) {
200 std::vector<char> tmp(length + 2);
202 size_t ret = sock.
recvfrom(&tmp[0], length + 2, 0, peeraddr);
208 flags = 0x0f & (tmp[1] >> 4);
209 seqno = 0x0f & tmp[1];
212 address = UDPDatagramService::encode(peeraddr);
215 ::memcpy(buf, &tmp[2], ret - 2);
220 }
catch (
const std::bad_cast&) {
248 std::list<ibrcommon::vaddress> addrs = _iface.
getAddresses();
250 for (std::list<ibrcommon::vaddress>::iterator iter = addrs.begin(); iter != addrs.end(); ++iter) {
258 return UDPDatagramService::encode(addr, _bind_port);
295 const std::string UDPDatagramService::encode(
const ibrcommon::vaddress &address,
const int port)
297 std::stringstream ss;
298 ss <<
"ip=" << address.
address() <<
";port=";
300 if (port == 0) ss << address.
service();
307 void UDPDatagramService::decode(
const std::string &identifier,
ibrcommon::vaddress &address)
314 std::vector<string>::const_iterator param_iter = parameters.begin();
316 while (param_iter != parameters.end())
320 if (p[0].compare(
"ip") == 0)
325 if (p[0].compare(
"port") == 0)