From ff9f5aee4b685b8c705bd39d5768eec75a50c5c2 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 8 Jan 2010 03:48:56 +0000 Subject: UDPPacketSource now opens one socket for each resolved endpoint --- src/packetSource.cpp | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'src/packetSource.cpp') diff --git a/src/packetSource.cpp b/src/packetSource.cpp index c9a0412..7c66d0f 100644 --- a/src/packetSource.cpp +++ b/src/packetSource.cpp @@ -45,18 +45,36 @@ void PacketSource::waitUntilReady() ready_sem_.down(); } -UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) : sock_(io_service_) +UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) { gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType()); } -void UDPPacketSource::onResolve(const PacketSourceResolverIt& it) +UDPPacketSource::~UDPPacketSource() { - PacketSourceEndpoint e = *it; + std::list::iterator it = sockets_.begin(); + for(;it != sockets_.end(); ++it) + delete *it; +} + +void UDPPacketSource::onResolve(PacketSourceResolverIt& it) +{ + while(it != PacketSourceResolverIt()) { + PacketSourceEndpoint e = *it; + cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e; + + proto::socket* sock = new proto::socket(io_service_); + sock->open(e.protocol()); + if(e.protocol() == proto::v6()) { + boost::asio::ip::v6_only option(true); + sock->set_option(option); + } + sock->bind(e); + sockets_.push_back(sock); + + it++; + } - cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e; - sock_.open(e.protocol()); - sock_.bind(e); ready_sem_.up(); } @@ -67,11 +85,11 @@ void UDPPacketSource::onError(const std::runtime_error& e) u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote) { - return static_cast(sock_.receive_from(boost::asio::buffer(buf, len), remote)); + return static_cast(sockets_.front()->receive_from(boost::asio::buffer(buf, len), remote)); } void UDPPacketSource::send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote) { - sock_.send_to(boost::asio::buffer(buf, len), remote); + sockets_.front()->send_to(boost::asio::buffer(buf, len), remote); } -- cgit v1.2.3