All pastes #297903 Raw Edit

Anonymous

public cpp v1 · immutable
#297903 ·published 2006-12-31 11:46 UTC
rendered paste body
#include <boost/bind.hpp>#include <iostream>#include <boost/algorithm/string.hpp>#include "server.hpp"using asio::ip::tcp;void Server::start(void){	boost::shared_ptr<ClientSocket> new_client(new ClientSocket(io_service));    acceptor_.async_accept(new_client->socket(),    	boost::bind(&Server::handle_accept, this, new_client,        	asio::placeholders::error));			io_service.run();}void Server::stop(void){	acceptor_.close();		std::vector<boost::shared_ptr<ClientSocket> >::iterator i;	for (i=clients.begin(); i != clients.end(); ++i) 	{		(*i)->close();	}}void Server::handle_accept(boost::shared_ptr<ClientSocket> new_client, const asio::error& error){	if (!error)    {		clients.push_back(new_client);		asio::async_read_until(new_client->socket(), buffer, '\n', 			boost::bind(&Server::handle_read, this, new_client, 				asio::placeholders::error, asio::placeholders::bytes_transferred));			boost::shared_ptr<ClientSocket> new_client(new ClientSocket(io_service));	    acceptor_.async_accept(new_client->socket(),	    	boost::bind(&Server::handle_accept, this, new_client,	        	asio::placeholders::error));    }    else    {      new_client.reset();    }}	void Server::handle_read(boost::shared_ptr<ClientSocket> c, const asio::error& error, std::size_t size){	if (!error)   	{    	std::istream is(&buffer);     	std::string line;		std::getline(is, line);				std::vector<std::string> splitVec;		boost::split(splitVec, line, boost::is_any_of(";"));				if (splitVec.at(0).compare("turn") == 0)		{			// check if turn is valid and promote it			std::cout << "turn received " << std::endl;						} else if (splitVec.at(0).compare("chat") == 0)		{					}				this->promoteStringToClients(line);			} else 	{		if (error.code() == asio::error::connection_aborted || 			error.code() == asio::error::network_down ||			error.code() == asio::error::network_reset ||			error.code() == asio::error::network_unreachable ||			error.code() == asio::error::not_connected ) 			{				c.reset();			}	}}void Server::handle_write(boost::shared_ptr<ClientSocket> c, const asio::error& error, std::size_t bytes_transferred){	if (!error)	{		std::vector<boost::shared_ptr<ClientSocket> >::iterator i;		for (i=clients.begin(); i != clients.end(); ++i) 		{			asio::async_read_until((*i)->socket(), buffer, '\n', 				boost::bind(&Server::handle_read, this, *i,					asio::placeholders::error, asio::placeholders::bytes_transferred));		}	}	else 		{			if (error.code() == asio::error::connection_aborted || 				error.code() == asio::error::network_down ||				error.code() == asio::error::network_reset ||				error.code() == asio::error::network_unreachable ||				error.code() == asio::error::not_connected ) 				{					c.reset();				}		}}void Server::promoteStringToClients(std::string str){	std::vector<boost::shared_ptr<ClientSocket> >::iterator i;	for (i=clients.begin(); i != clients.end(); ++i) 	{		this->writeStringToSocket(*i, str);		/*		if (c != *i)		{			this.writeStringToSocket((*i)->socket(), str);		}		*/	}}	void Server::writeStringToSocket(boost::shared_ptr<ClientSocket> c, std::string str){	asio::async_write(c->socket(), asio::buffer(str.c_str(), str.size()),	          boost::bind(&Server::handle_write, this, c,	          asio::placeholders::error, asio::placeholders::bytes_transferred));}int main(int argc, char* argv[]){	try	{		Server s(1337);		s.start();	}  	catch (asio::error& e)  	{    	std::cerr << "Error: " << e << "\n";  	}  	catch (std::exception& e)  	{    	std::cerr << "Exception: " << e.what() << "\n";  	}	return 0;}