From 9c9937811dee449a891bedfe9e4426a82878c943 Mon Sep 17 00:00:00 2001 From: Gogs Date: Mon, 23 May 2016 18:05:53 +0800 Subject: [PATCH] improve recieve method. --- client_proxy/client_proxy.hpp | 9 +++++---- client_proxy/main.cpp | 15 +++++---------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/client_proxy/client_proxy.hpp b/client_proxy/client_proxy.hpp index fbccf28..decae9c 100644 --- a/client_proxy/client_proxy.hpp +++ b/client_proxy/client_proxy.hpp @@ -182,18 +182,19 @@ class client_proxy : private boost::noncopyable return init.result.get(); } - void recieve(const std::function& callback) + template + size_t recieve(Buffer& buf) { boost::system::error_code ec; - size_t length = boost::asio::read(socket_, boost::asio::buffer(recv_data_, 12), ec); + size_t length = boost::asio::read(socket_, boost::asio::buffer(buf), ec); if (ec) { //log - callback(nullptr, 0); + return 0; } else { - callback(recv_data_, length); + return length; } } diff --git a/client_proxy/main.cpp b/client_proxy/main.cpp index 3cf093a..12c1351 100644 --- a/client_proxy/main.cpp +++ b/client_proxy/main.cpp @@ -222,19 +222,14 @@ void test_read() std::thread thd([&io_service] {io_service.run(); }); //wait for message from server. + char buf[13] = {}; bool ok = true; while (ok) { - client.recieve([&ok](char* data, size_t len) - { - if (data == nullptr) - { - ok = false; - return; - } - - std::cout << data << std::endl; - }); + size_t length = client.recieve(buf); + + if (length != 0) + std::cout << buf << std::endl; } thd.join();