示例#1
0
void handler::async_read() {
	auto self(shared_from_this());
	m_socket.async_read_some(boost::asio::buffer(m_buffer),
	                         std::bind(&handler::handle_read, self,
	                                   std::placeholders::_1,
	                                   std::placeholders::_2));
}
示例#2
0
	void start()
	{
		socket_.async_read_some(boost::asio::buffer(data_),
			boost::bind(&session::handle_read, shared_from_this(),
				boost::asio::placeholders::error,
				boost::asio::placeholders::bytes_transferred));
	}
		void	startRead()
		{
			checkNotReading(true);
			 
			reading = true;
			readBuff.ensure_write_space(MAX_READ_BYTES_ONCE);
			socket.async_read_some(boost::asio::buffer(readBuff.wt_ptr(), readBuff.writeable_bytes()),
				boost::bind(&TcpConnection::handleRead, shared_from_this(),
				boost::asio::placeholders::error,
				boost::asio::placeholders::bytes_transferred));
		}
示例#4
0
void run_socket_io(boost::asio::ip::tcp::socket& socket)
{
   std::array<char, 1024> read_buffer;
   std::array<char, 1024> write_buffer;

   socket.async_read_some(boost::asio::buffer(write_buffer, 1024),
       [&write_buffer](const boost::system::error_code& ec, std::size_t length)
       {
          if (!ec)
             std::cout.write(write_buffer.data(), length);
       });

   while (std::cin.getline(read_buffer.data(), 1024))
   {
      socket.send(
          boost::asio::buffer(read_buffer, std::strlen(read_buffer.data())));
   }
}
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();
}
	inline void connection::handle_read(const boost::system::error_code& e,
					    std::size_t bytes_transferred) {
	    if (!e) {
		boost::tribool result;
		boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
		  *request_, buffer_.data(), buffer_.data() + bytes_transferred);

		if (result) {
		    try {
			request_handler_.handle_request(*request_, reply_);
		    } catch (webserver::reply rep) {
			reply_ = rep;
		    }
		    std::cerr << reply_.content << std::endl;
		    boost::asio::async_write(socket_, reply_.to_buffers(),
		     strand_.wrap(
			  boost::bind(&connection::handle_write, shared_from_this(),
				      boost::asio::placeholders::error)));
		} else if (!result) {
		    reply_ = reply::stock_reply(reply::bad_request);
		    boost::asio::async_write(socket_, reply_.to_buffers(),
		     strand_.wrap(
			  boost::bind(&connection::handle_write, shared_from_this(),
				      boost::asio::placeholders::error)));
		} else {
		    socket_.async_read_some(boost::asio::buffer(buffer_),
			    strand_.wrap(
				 boost::bind(&connection::handle_read, shared_from_this(),
					     boost::asio::placeholders::error,
					     boost::asio::placeholders::bytes_transferred)));
		}
	    }

	    // If an error occurs then no new asynchronous operations are started. This
	    // means that all shared_ptr references to the connection object will
	    // disappear and the object will be destroyed automatically after this
	    // handler returns. The connection class's destructor closes the socket.
	}
	void async_read() {
		socket_.async_read_some(boost::asio::buffer(buffer_read_.bytes, MAX_BUFFER_SIZE),
				boost::bind( &session::handle_read, shared_from_this(),
						boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
	}
示例#8
0
	size_t recv(Func handler,
		ARGS&...args)
	{
		return s_opt.async_read_some(boost::asio::buffer(args...), handler);
	}
示例#9
-1
	inline void connection::start() {
	    socket_.async_read_some(boost::asio::buffer(buffer_),
		    strand_.wrap(
			 boost::bind(&connection::handle_read, shared_from_this(),
				     boost::asio::placeholders::error,
				     boost::asio::placeholders::bytes_transferred)));
	}
示例#10
-1
/**
* Вызывается всякий раз, когда данные получены.
*/
static void handleRead(
    ba::ip::tcp::socket&  readFrom,
    ba::ip::tcp::socket&  writeTo,
    char*   readBuffer,
    size_t  bytes,
    const boost::system::error_code&  e
) {
#ifdef _DEBUG
    const std::string  data( readBuffer, readBuffer + bytes );
    std::cout << data << std::endl;
#endif

    // отправляем полученные данные "другой стороне"
    writeTo.send( ba::buffer( readBuffer, bytes ) );

    // читаем ещё данные с "этой стороны"
    readFrom.async_read_some(
        ba::buffer( readBuffer, 1024 ),
        boost::bind(
            handleRead,
            boost::ref( readFrom ),
            boost::ref( writeTo),
            readBuffer,
            ba::placeholders::bytes_transferred,
            ba::placeholders::error
    ) );
}