summaryrefslogtreecommitdiffstats
blob: 18a3ca4f79db835535feefffa35b5340bbaa7b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "Swiften/Network/BoostConnectionServer.h"

#include <boost/bind.hpp>

#include "Swiften/EventLoop/MainEventLoop.h"

namespace Swift {

BoostConnectionServer::BoostConnectionServer(int port, boost::asio::io_service* ioService) : acceptor_(*ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) {
}


void BoostConnectionServer::start() {
	acceptNextConnection();
}

void BoostConnectionServer::acceptNextConnection() {
	boost::shared_ptr<BoostConnection> newConnection(new BoostConnection(&acceptor_.io_service()));
	acceptor_.async_accept(newConnection->getSocket(), 
		boost::bind(&BoostConnectionServer::handleAccept, shared_from_this(), newConnection, boost::asio::placeholders::error));
}

void BoostConnectionServer::handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) {
	if (!error) {
		MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), shared_from_this());
		newConnection->listen();
		acceptNextConnection();
	}
}

}