4 Replies - 2295 Views - Last Post: 31 December 2010 - 12:28 PM Rate Topic: -----

#1 Sweeneya  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 09

Boost asio big problem

Posted 30 December 2010 - 03:16 PM

I have a problem with boost::async_write function. It doent work I have no idea why but there is now sign of any data send at a client. It works when delete msg.pop_front(); in void Connection::handle_write(const boost::system::error_code &error). But there is an infinitive loop but is send messages I see that at the client....
Can any one please help me ?

#include "Connection.hpp"

Connection::Connection( boost::asio::io_service& io_service):socket(io_service),io(io_service), strand_(io_service)
{
	std::cout<< " Connection created:";
}

void Connection::start()
{
	std::cout<<"Nowe polaczenie\n";
	
		socket.async_read_some(
		boost::asio::buffer(inputBuffer), 
		strand_.wrap(boost::bind(&Connection::handle_read, shared_from_this(), 
		boost::asio::placeholders::error, 
		boost::asio::placeholders::bytes_transferred)) ); 
}

tcp::socket& Connection::getSocket()
{
	return socket;
}

void Connection::handle_write(const boost::system::error_code &error)
{
	if(!error)
	{
		if(!msg.empty())
		{
				std::cout<<"send";
				boost::asio::async_write(socket,
					boost::asio::buffer(msg.front().getData(),msg.front().getLength()),
					strand_.wrap(boost::bind(&Connection::handle_write, shared_from_this(), 
					boost::asio::placeholders::error)));
		msg.pop_front(); //delete this works !?
		}			
	}
	else
	{
		socket.close();
	}	

}

void Connection::handle_read(const boost::system::error_code &error, std::size_t ile)
{
	if (!error) 
	{ 
		std::cout<<"czytam dane\n";

		std::string data = std::string(inputBuffer.data(), ile);
		notify(Types::newMessage, data);

		socket.async_read_some(
			boost::asio::buffer(inputBuffer), 
			boost::bind(&Connection::handle_read, shared_from_this(), 
			boost::asio::placeholders::error, 
			boost::asio::placeholders::bytes_transferred) ); 
	} 
	else
	{
		socket.close();
	}
}

void Connection::send( std::string contens )
{
	Message message;
	char line[Message::max_length + 1];

	strcpy_s( line, contens.c_str() ); 
	message.setLength(strlen(line));
	memcpy(message.getData(), line, message.getLength());

	io.post(boost::bind(&Connection::do_send, shared_from_this(), message));


}

void Connection::do_send( Message message )
{
		msg.push_back(message);

	if(!msg.empty())
	{
		boost::asio::async_write(socket,
			boost::asio::buffer(msg.front().getData(),msg.front().getLength()),
			strand_.wrap(boost::bind(&Connection::handle_write, shared_from_this(), 
			boost::asio::placeholders::error)));
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Boost asio big problem

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,127
  • Joined: 27-December 08

Re: Boost asio big problem

Posted 30 December 2010 - 03:24 PM

Moved to C++ Help. Please reserve the C++ Programmers forum for topical, non-help related discussion.
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Boost asio big problem

Posted 30 December 2010 - 07:45 PM

Well:

1. Your constructor says 'connection created', which isn't true! It is your 'Connection' object that is created in order to establish a connection.

2. I can't see anywhere in your code where you check for a successfully established connection before attempting communication.

I'm not familiar with this boost module, but I'd encourage you to find some good examples from the Boost site.
Was This Post Helpful? 0
  • +
  • -

#4 Sweeneya  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 09

Re: Boost asio big problem

Posted 31 December 2010 - 01:17 AM

It is created because I receive data from client I just cant send them ( they are only sent in infinitive loop when I delete msg.pop_front();
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Boost asio big problem

Posted 31 December 2010 - 12:28 PM

View PostSweeneya, on 31 December 2010 - 07:17 AM, said:

It is created because I receive data from client I just cant send them ( they are only sent in infinitive loop when I delete msg.pop_front();

Are you receiving valid data?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1