From 623e15d35dffbda9f30d0d4aa7e42439723df31e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 22 Mar 2009 23:57:35 +0000 Subject: added gResolver to SyncServer some cleanup --- src/syncServer.cpp | 55 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) (limited to 'src/syncServer.cpp') diff --git a/src/syncServer.cpp b/src/syncServer.cpp index c53f809..bd5120c 100644 --- a/src/syncServer.cpp +++ b/src/syncServer.cpp @@ -30,41 +30,60 @@ */ #include "syncServer.h" +#include "resolver.h" +#include "log.h" //using asio::ip::tcp; -SyncServer::SyncServer(boost::asio::io_service& io_service, SyncTcpConnection::proto::endpoint tcp_endpoint ) - : acceptor_(io_service, tcp_endpoint) +SyncServer::SyncServer(std::string localaddr, std::string port, ConnectCallback onConnect) + : acceptor_(io_service_), onConnect_(onConnect) { + gResolver.resolveTcp(localaddr, port, boost::bind(&SyncServer::onResolve, this, _1), boost::bind(&SyncServer::onResolvError, this, _1)); +} + +void SyncServer::onResolve(const SyncTcpConnection::proto::endpoint& e) +{ + acceptor_.open(e.protocol()); + acceptor_.set_option(boost::asio::socket_base::reuse_address(true)); + acceptor_.bind(e); + acceptor_.listen(); start_accept(); + ready_sem_.up(); + cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e; } -void SyncServer::start_accept() +void SyncServer::onResolvError(const std::runtime_error& e) { - Lock lock(mutex_); - SyncTcpConnection::pointer new_connection = - SyncTcpConnection::create(acceptor_.io_service()); - conns_.push_back(new_connection); - - acceptor_.async_accept(new_connection->socket(), - boost::bind(&SyncServer::handle_accept, this, new_connection, - boost::asio::placeholders::error)); + cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what(); + // TODO: stop daemon?? +} + +void SyncServer::run() +{ + ready_sem_.down(); + io_service_.run(); } void SyncServer::send(std::string message) { Lock lock(mutex_); - for(std::list::iterator it = conns_.begin() ;it != conns_.end(); ++it) { + for(std::list::iterator it = conns_.begin() ;it != conns_.end(); ++it) (*it)->Send(message); - } } -void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, - const boost::system::error_code& error) +void SyncServer::start_accept() +{ + Lock lock(mutex_); + SyncTcpConnection::pointer new_connection = SyncTcpConnection::create(acceptor_.io_service()); + conns_.push_back(new_connection); + acceptor_.async_accept(new_connection->socket(), + boost::bind(&SyncServer::handle_accept, this, new_connection, boost::asio::placeholders::error)); +} + +void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error) { - if (!error) - { - new_connection->onConnect=onConnect; + if (!error) { + new_connection->onConnect = onConnect_; new_connection->start(); start_accept(); } -- cgit v1.2.3