diff options
author | dknn <yoann.blein@free.fr> | 2012-07-11 16:06:53 (GMT) |
---|---|---|
committer | dknn <yoann.blein@free.fr> | 2012-09-22 09:01:48 (GMT) |
commit | ce9a3d75677f08af0a226bb3d2bf02c28c938c33 (patch) | |
tree | f69350fcfe12205de616ed01941ee9d239b3d4a4 | |
parent | 6d458439d9536be1736ba1a98c51040b64946284 (diff) | |
download | swift-contrib-ce9a3d75677f08af0a226bb3d2bf02c28c938c33.zip swift-contrib-ce9a3d75677f08af0a226bb3d2bf02c28c938c33.tar.bz2 |
Some UDP networking utilities
-rw-r--r-- | Swiften/Network/BoostUDPSocket.cpp | 20 | ||||
-rw-r--r-- | Swiften/Network/BoostUDPSocket.h | 4 | ||||
-rw-r--r-- | Swiften/Network/HostAddressPort.cpp | 5 | ||||
-rw-r--r-- | Swiften/Network/HostAddressPort.h | 1 | ||||
-rw-r--r-- | Swiften/Network/UDPSocket.h | 2 |
5 files changed, 27 insertions, 5 deletions
diff --git a/Swiften/Network/BoostUDPSocket.cpp b/Swiften/Network/BoostUDPSocket.cpp index dff941f..cedaad4 100644 --- a/Swiften/Network/BoostUDPSocket.cpp +++ b/Swiften/Network/BoostUDPSocket.cpp @@ -76,11 +76,13 @@ void BoostUDPSocket::connect(const HostAddressPort &address) void BoostUDPSocket::connectToFirstIncoming() { - boost::shared_ptr<SafeByteArray> buf = boost::make_shared<SafeByteArray>(1); + readBuffer_ = boost::make_shared<SafeByteArray>(1); socket_.async_receive_from( - boost::asio::buffer(*buf), + boost::asio::buffer(*readBuffer_), remoteEndpoint_, - boost::bind(&BoostUDPSocket::handleFirstRead, shared_from_this(), boost::asio::placeholders::error)); + boost::bind(&BoostUDPSocket::handleFirstRead, shared_from_this(), + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); } void BoostUDPSocket::close() { @@ -89,6 +91,16 @@ void BoostUDPSocket::close() { socket_.close(); } +HostAddressPort BoostUDPSocket::getLocalAddress() const +{ + return HostAddressPort(socket_.local_endpoint()); +} + +HostAddressPort BoostUDPSocket::getRemoteAddress() const +{ + return HostAddressPort(socket_.remote_endpoint()); +} + void BoostUDPSocket::send(const SafeByteArray& data) { boost::lock_guard<boost::mutex> lock(sendMutex_); @@ -129,7 +141,7 @@ void BoostUDPSocket::handleSocketRead(const boost::system::error_code& error, si // } } -void BoostUDPSocket::handleFirstRead(const boost::system::error_code &error) +void BoostUDPSocket::handleFirstRead(const boost::system::error_code &error, std::size_t /*bytes_transferred*/) { if (!error) { socket_.connect(remoteEndpoint_); // TODO: handle errors diff --git a/Swiften/Network/BoostUDPSocket.h b/Swiften/Network/BoostUDPSocket.h index 5cc8b1d..c7de5b7 100644 --- a/Swiften/Network/BoostUDPSocket.h +++ b/Swiften/Network/BoostUDPSocket.h @@ -42,6 +42,8 @@ namespace Swift { virtual void connectToFirstIncoming(); virtual void send(const SafeByteArray& data); virtual void close(); + virtual HostAddressPort getLocalAddress() const; + virtual HostAddressPort getRemoteAddress() const; boost::asio::ip::udp::socket& getSocket() { return socket_; @@ -51,7 +53,7 @@ namespace Swift { BoostUDPSocket(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop); void handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred); - void handleFirstRead(const boost::system::error_code& error); + void handleFirstRead(const boost::system::error_code& error, std::size_t /*bytes_transferred*/); void handleDataWritten(const boost::system::error_code& error); void doRead(); void doSend(const SafeByteArray& data); diff --git a/Swiften/Network/HostAddressPort.cpp b/Swiften/Network/HostAddressPort.cpp index 0540e24..104cd2f 100644 --- a/Swiften/Network/HostAddressPort.cpp +++ b/Swiften/Network/HostAddressPort.cpp @@ -18,6 +18,11 @@ HostAddressPort::HostAddressPort(const boost::asio::ip::tcp::endpoint& endpoint) port_ = endpoint.port(); } +HostAddressPort::HostAddressPort(const boost::asio::ip::udp::endpoint& endpoint) { + address_ = HostAddress(endpoint.address()); + port_ = endpoint.port(); +} + std::string HostAddressPort::toString() const { return getAddress().toString() + ":" + boost::lexical_cast<std::string>(getPort()); } diff --git a/Swiften/Network/HostAddressPort.h b/Swiften/Network/HostAddressPort.h index bf97bff..68b757e 100644 --- a/Swiften/Network/HostAddressPort.h +++ b/Swiften/Network/HostAddressPort.h @@ -17,6 +17,7 @@ namespace Swift { public: HostAddressPort(const HostAddress& address = HostAddress(), int port = -1); HostAddressPort(const boost::asio::ip::tcp::endpoint& endpoint); + HostAddressPort(const boost::asio::ip::udp::endpoint& endpoint); const HostAddress& getAddress() const { return address_; diff --git a/Swiften/Network/UDPSocket.h b/Swiften/Network/UDPSocket.h index ff324fd..4ba9962 100644 --- a/Swiften/Network/UDPSocket.h +++ b/Swiften/Network/UDPSocket.h @@ -25,6 +25,8 @@ namespace Swift { virtual void connectToFirstIncoming() = 0; virtual void send(const SafeByteArray& data) = 0; virtual void close() = 0; + virtual HostAddressPort getLocalAddress() const = 0; + virtual HostAddressPort getRemoteAddress() const = 0; public: boost::signal<void (boost::shared_ptr<SafeByteArray>)> onDataRead; |