virtual void send(raw_data& data) {
		bool write_in_progress = !buffer_write_queue_.empty();
		buffer_write_queue_.push_back(data);
		if (!write_in_progress) {
			socket_.async_write_some(
					boost::asio::buffer(buffer_write_queue_.front().bytes, buffer_write_queue_.front().length),
					boost::bind(&session::handle_write, shared_from_this(),
							boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
		}
	}
	void handle_write(const boost::system::error_code& error, std::size_t bytes_transferred) {
		if (!error) {
			buffer_write_queue_.pop_front();
			if (!buffer_write_queue_.empty()) {
				socket_.async_write_some(
						boost::asio::buffer(buffer_write_queue_.front().bytes, buffer_write_queue_.front().length),
						boost::bind(&session::handle_write, shared_from_this(),
								boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
			}
		}
		else {
			std::cout << "error in writing data" << std::endl;
			this->onError();
		}
	}
void AsyncLoopSocket(boost::function<TaskState()> doWork, boost::asio::ip::tcp::socket& sock, bool isRead)
{
    if (sock.get_io_service().stopped())
    {
        return;
    }

    TaskState st = doWork();

    if (st == TASK_WORKING)
    {
        if (isRead)
        {
            sock.async_read_some(boost::asio::null_buffers(), bind(AsyncLoopSocket, doWork, boost::ref(sock),
                isRead));
        }
        else
        {
            sock.async_write_some(boost::asio::null_buffers(), bind(AsyncLoopSocket, doWork, boost::ref(sock),
                isRead));
        }

        return;
    }

    // Work is over. stop any outstanding events.
    // NOTE: this isn't 100% reliable, and there may be events that linger in the queue.
    // The next time we reset() and run() the io_service, these events will be processed,
    // and io_service::stopped() will return false, because we've just done reset() and run().
    // The state management (SSHSession::State) is our first step in controlling this, and we
    // may need to implement our own cancel mechanism
    //
    // this io_service feature poses an additional problem - by spreading the implementation
    // responsibility across multiple classes, we create scenarios where io_service's queue
    // may contain outstanding events referring to objects that were, in the meantime, destroyed.
    // this is why we're favouring, for now, the use of AsyncLoopTimer, where we can use an
    // io_service for each timer, and destroy it right after being used. this makes sure we
    // process no "zombie" outstanding events.
    sock.get_io_service().stop();
}
Beispiel #4
0
	auto send(Func handler, ARGS&...args)
	{
		return s_opt.async_write_some(boost::asio::buffer(args...), handler);
	}