52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
#include "sock_client.h"
|
|
|
|
client::client(boost::asio::io_context & io_context, const std::string & unix_file, std::function<void(const std::vector<uint8_t>&)> func_cb)
|
|
: socket_(io_context)
|
|
, callback_func(func_cb)
|
|
{
|
|
socket_.async_connect(seq_packet::seqpacket_protocol::endpoint(unix_file),
|
|
std::bind(&client::handle_connect, this,
|
|
std::placeholders::_1));
|
|
}
|
|
|
|
client::~client()
|
|
{
|
|
do_close();
|
|
}
|
|
|
|
void client::write(const std::vector<uint8_t> & data)
|
|
{
|
|
boost::asio::write(socket_, boost::asio::buffer(data));
|
|
}
|
|
|
|
void client::do_close()
|
|
{
|
|
socket_.close();
|
|
}
|
|
|
|
void client::start()
|
|
{
|
|
seq_packet::seqpacket_protocol::socket::message_flags in_flags { MSG_WAITALL };
|
|
socket_.async_receive(boost::asio::buffer(data_), in_flags,
|
|
std::bind(&client::handle_read, this,
|
|
std::placeholders::_1,
|
|
std::placeholders::_2));
|
|
}
|
|
|
|
void client::handle_connect(const boost::system::error_code & error)
|
|
{
|
|
if (!error)
|
|
start();
|
|
}
|
|
|
|
void client::handle_read(const boost::system::error_code & error, size_t bytes_transferred)
|
|
{
|
|
if (!error && callback_func && bytes_transferred)
|
|
{
|
|
callback_func(std::vector<uint8_t>(std::begin(data_), std::begin(data_) + bytes_transferred));
|
|
start();
|
|
}
|
|
//else
|
|
// do_close();
|
|
}
|