Hi Community,
I've a problem with named-pipe communication. Please help me investigate this. I'm totally clueless now.
I have a Connector class. I want to read some data from a socket.
This is it:
class Connector {
public:
Connector(boost::asio::io_service& io_service)
: _io_service(io_service), _socket(io_service) {
_endpoint.address(boost::asio::ip::address::from_string("127.0.0.1"));
_endpoint.port(4242);
_socket.async_connect(_endpoint,
boost::bind(&Connector::handle_accept, this, boost::asio::placeholders::error));
}
void close() {
_io_service.post(boost::bind(&Connector::do_close, this));
_io_service.stop();
}
private:
void handle_accept(const boost::system::error_code& error) {
if (error) {
throw ConnectorError(_T("Cannot estabilish connection."));
}
boost::asio::async_read(_socket, boost::asio::buffer(_readbuffer, 1024),
boost::bind(&Connector::handle_read, this, boost::asio::placeholders::error));
}
void handle_read(const boost::system::error_code& error) {
if (!error) {
if (_readbuffer.empty()) {
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
} else {
std::string string_buffer;
std::copy(_readbuffer.begin(), _readbuffer.end(),
std::back_insert_iterator<std::string>(string_buffer));
std::cout << string_buffer << std::endl;
_readbuffer.clear();
}
boost::asio::async_read(_socket, boost::asio::buffer(_readbuffer, 1024),
boost::bind(&Connector::handle_read, this,
boost::asio::placeholders::error));
}
}
void do_close() {
_socket.close();
}
boost::asio::io_service& _io_service;
boost::asio::ip::tcp::socket _socket;
boost::asio::ip::tcp::endpoint _endpoint;
std::vector<char> _readbuffer;
};
This is how I use it:
boost::asio::io_service io_service;
Connector connector(io_service);
boost::thread async_io_thread(boost::bind(&boost::asio::io_service::run, &io_service));