diff options
author | Christian Pointner <equinox@anytun.org> | 2008-11-24 20:52:38 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2008-11-24 20:52:38 +0000 |
commit | 84f11351fe5ab9fb2e8c7b7caecb4c7b5a26b595 (patch) | |
tree | cbbecc42f3f51b73dac3ac4c7fe9e99f8019d89e | |
parent | ports int -> string (diff) |
PacketSource changed to use endpoint instead of port and address
-rw-r--r-- | src/packetSource.cpp | 36 | ||||
-rw-r--r-- | src/packetSource.h | 14 |
2 files changed, 16 insertions, 34 deletions
diff --git a/src/packetSource.cpp b/src/packetSource.cpp index b144e8c..ee8951e 100644 --- a/src/packetSource.cpp +++ b/src/packetSource.cpp @@ -30,56 +30,36 @@ */ #include <boost/asio.hpp> -#include <sstream> #include "datatypes.h" #include "packetSource.h" #include "buffer.h" -UDPPacketSource::UDPPacketSource(u_int16_t port) : sock_(io_service_) +UDPPacketSource::UDPPacketSource(std::string port) : sock_(io_service_) { - std::stringstream ps; - ps << port; - boost::asio::ip::udp::resolver resolver(io_service_); - boost::asio::ip::udp::resolver::query query(ps.str()); + boost::asio::ip::udp::resolver::query query(port); boost::asio::ip::udp::endpoint e = *resolver.resolve(query); sock_.open(e.protocol()); sock_.bind(e); } -UDPPacketSource::UDPPacketSource(std::string localaddr, u_int16_t port) : sock_(io_service_) +UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port) : sock_(io_service_) { - std::stringstream ps; - ps << port; - boost::asio::ip::udp::resolver resolver(io_service_); - boost::asio::ip::udp::resolver::query query(localaddr, ps.str()); + boost::asio::ip::udp::resolver::query query(localaddr, port); boost::asio::ip::udp::endpoint e = *resolver.resolve(query); sock_.open(e.protocol()); sock_.bind(e); } -u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, std::string& addr, u_int16_t &port) +u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote) { - boost::asio::ip::udp::endpoint e; - u_int32_t rtn = sock_.receive_from(boost::asio::buffer(buf, len), e); - - addr = e.address().to_string(); - port = e.port(); - - return rtn; + return sock_.receive_from(boost::asio::buffer(buf, len), remote); } -void UDPPacketSource::send(u_int8_t* buf, u_int32_t len, std::string addr, u_int16_t port) +void UDPPacketSource::send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote) { - std::stringstream ps; - ps << port; - - boost::asio::ip::udp::resolver resolver(io_service_); - boost::asio::ip::udp::resolver::query query(addr, ps.str()); - boost::asio::ip::udp::endpoint e = *resolver.resolve(query); - - sock_.send_to(boost::asio::buffer(buf, len), e); + sock_.send_to(boost::asio::buffer(buf, len), remote); } diff --git a/src/packetSource.h b/src/packetSource.h index 2b08cf8..d8914b9 100644 --- a/src/packetSource.h +++ b/src/packetSource.h @@ -34,23 +34,25 @@ #include "datatypes.h" #include "buffer.h" +typedef boost::asio::ip::udp::endpoint PacketSourceEndpoint; + class PacketSource { public: virtual ~PacketSource() {} - virtual u_int32_t recv(u_int8_t* buf, u_int32_t len, std::string& addr, u_int16_t &port) = 0; - virtual void send(u_int8_t* buf, u_int32_t len, std::string addr, u_int16_t port) = 0; + virtual u_int32_t recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote) = 0; + virtual void send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote) = 0; }; class UDPPacketSource : public PacketSource { public: - UDPPacketSource(u_int16_t port); - UDPPacketSource(std::string localaddr, u_int16_t port); + UDPPacketSource(std::string port); + UDPPacketSource(std::string localaddr, std::string port); - u_int32_t recv(u_int8_t* buf, u_int32_t len, std::string& addr, u_int16_t &port); - void send(u_int8_t* buf, u_int32_t len, std::string addr, u_int16_t port); + u_int32_t recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote); + void send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote); private: boost::asio::io_service io_service_; |