Skip to content
This repository has been archived by the owner on Nov 15, 2019. It is now read-only.

Commit

Permalink
improve recieve method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gogs committed May 23, 2016
1 parent c43ba44 commit 9c99378
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
9 changes: 5 additions & 4 deletions client_proxy/client_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,19 @@ class client_proxy : private boost::noncopyable
return init.result.get();
}

void recieve(const std::function<void(char*, size_t)>& callback)
template<typename Buffer>
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;
}
}

Expand Down
15 changes: 5 additions & 10 deletions client_proxy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9c99378

Please sign in to comment.