summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Network/BoostConnectionServer.cpp')
-rw-r--r--Swiften/Network/BoostConnectionServer.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp
index eccffc6..c90b554 100644
--- a/Swiften/Network/BoostConnectionServer.cpp
+++ b/Swiften/Network/BoostConnectionServer.cpp
@@ -9,6 +9,8 @@
#include <boost/bind.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/placeholders.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+#include <boost/optional.hpp>
#include <Swiften/EventLoop/EventLoop.h>
@@ -21,28 +23,36 @@ BoostConnectionServer::BoostConnectionServer(const HostAddress &address, int por
}
void BoostConnectionServer::start() {
+ boost::optional<Error> error = tryStart();
+ if (error) {
+ eventLoop->postEvent(boost::bind(boost::ref(onStopped), *error), shared_from_this());
+ }
+}
+
+boost::optional<BoostConnectionServer::Error> BoostConnectionServer::tryStart() {
try {
assert(!acceptor_);
if (address_.isValid()) {
acceptor_ = new boost::asio::ip::tcp::acceptor(
*ioService_,
- boost::asio::ip::tcp::endpoint(address_.getRawAddress(), port_));
+ boost::asio::ip::tcp::endpoint(address_.getRawAddress(), boost::numeric_cast<unsigned short>(port_)));
}
else {
acceptor_ = new boost::asio::ip::tcp::acceptor(
*ioService_,
- boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port_));
+ boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), boost::numeric_cast<unsigned short>(port_)));
}
acceptNextConnection();
}
catch (const boost::system::system_error& e) {
if (e.code() == boost::asio::error::address_in_use) {
- eventLoop->postEvent(boost::bind(boost::ref(onStopped), Conflict), shared_from_this());
+ return Conflict;
}
else {
- eventLoop->postEvent(boost::bind(boost::ref(onStopped), UnknownError), shared_from_this());
+ return UnknownError;
}
}
+ return boost::optional<Error>();
}