awaitable<void> echo(tcp::socket s)
{
 auto token = co_await this_coro::token();

 try
 {
   char data1[1024];
   char data2[1024];

   char* p1 = data1;
   char* p2 = data2;

   // Perform initial read into first buffer.
   size_t n = co_await s.async_read_some(boost::asio::buffer(p1, 1024), token);

   for (;;)
   {
     // Swap received data to other buffer and initiate write operation.
     std::swap(p1, p2);
     auto write_result = boost::asio::async_write(s, boost::asio::buffer(p2, n), token);

     // Perform next read while write operation is in progress.
     n = co_await s.async_read_some(boost::asio::buffer(p1, 1024), token);

     // Wait for write operation to complete before proceeding.
     co_await write_result;
   }
 }
 catch (std::exception& e)
 {
   std::printf("echo Exception: %s\n", e.what());
 }
}
Example #2
0
 void start()
 {
     fprintf(stderr, "session::handle_read()\n");
     socket_.async_read_some(boost::asio::buffer(data_, max_length),
             boost::bind(&session::handle_read, this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred));
 }
void read_handler(const boost::system::error_code &ec,
                  std::size_t bytes_transferred)
{
    if (!ec)
    {
        std::cout.write(bytes.data(), bytes_transferred);
        tcp_socket.async_read_some(buffer(bytes), read_handler);
    }
}
void connect_handler(const boost::system::error_code &ec)
{
    if (!ec)
    {
        std::string r =
            "GET / HTTP/1.1\r\nHost: theboostcpplibraries.com\r\n\r\n";
        write(tcp_socket, buffer(r));
        tcp_socket.async_read_some(buffer(bytes), read_handler);
    }
}
Example #5
0
 void handle_write(const boost::system::error_code &error)
 {
     fprintf(stderr, "session::handle_write()\n");
     if (!error)
     {
         socket_.async_read_some(boost::asio::buffer(data_, max_length),
                 boost::bind(&session::handle_read, this,
                     boost::asio::placeholders::error,
                     boost::asio::placeholders::bytes_transferred));
     }
     else
     {
         delete this;
     }
 }
Example #6
0
	void f() {
		auto p(shared_from_this());
		using namespace std;
		cout << "f**k" << endl;
		//_socket.open(tcp::v4());
		_socket.async_read_some(asio::buffer(reply, 100),
			[p, this](std::error_code ec, std::size_t len) {
				using namespace std;
				cout << ec << " " << len << endl;
				std::cout.write(reply, len);
		});
		/*asio::async_read(_socket, asio::buffer(reply, 10240), [this, p](std::error_code ec, std::size_t len) {
			using namespace std;
			cout << ec << " " << len << endl;
		});*/

		/*int len = asio::read(_socket, asio::buffer(reply, 10));
    	std::cout.write(reply, len);*/
	}
Example #7
0
 void handle_write() {
         socket_.async_read_some(boost::asio::buffer(data_, max_length),
                                 bind(&session::handle_read, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
 }