Closed BarbBoyaji closed 4 years ago
Could you please, send an minimal example with main.cpp
?
Hello, yes I would be happy to. I get the build errors with this file.
#include <iostream>
#include <chrono>
#include <string>
#include <fstream>
#include <vector>
#include <bredis/Connection.hpp>
#include <bredis/Extract.hpp>
#include <bredis/MarkerHelpers.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
int main(){
using Buffer = boost::asio::streambuf;
using Iterator = typename bredis::to_iterator<Buffer>::iterator_t;
boost::asio::io_context ioserv;
// unix socket
using socket_t = boost::asio::local::stream_protocol::socket;
using next_layer_t = socket_t;
boost::asio::local::stream_protocol::endpoint end_point("/shared/redis.sock");
socket_t sock(ioserv);
sock.connect(end_point);
std::cout<<"Redis client is connected!"<<std::endl;
//wrap socket in bredis connection
bredis::Connection<next_layer_t> bredis_con(std::move(socket));
//create buffer
Buffer tx_buff, rx_buff;
//read image in
std::ifstream img_file("./clients/data/nasa1.jpg", std::ios::binary);
std::string img_binary = std::string((std::istreambuf_iterator<char>(img_file)), std::istreambuf_iterator<char>());
//XADD
bredis_con.write(bredis::single_command_t{ "XADD", "bredis", "*", "nasa_pic", img_binary });
auto result_markers = bredis_con.read(rx_buff);
auto extract = boost::apply_visitor(bredis::extractor<Iterator>(), result_markers.result);
try{
auto &reply_str = boost::get<bredis::extracts::string_t>(extract);
}catch(boost::bad_get &err){
std::cout<<"ERROR:" << ": "<< err.what()<< std::endl;
auto &reply_str = boost::get<bredis::extracts::error_t>(extract);
std::cout<<reply_str.str<<std::endl;
}
// consume the buffers, after finishing work with the markers
rx_buff.consume(result_markers.consumed);
//XRANGE
bredis_con.write(bredis::single_command_t{ "XRANGE" , "bredis", "-", "+", "COUNT", "10"});
auto result_markers2 = bredis_con.read(tx_buff);
auto extract2 = boost::apply_visitor(bredis::extractor<Iterator>(), result_markers.result);
tx_buff.consume(result_markers2.consumed);
ioserv.run();
}
If I replace the unix socket with a tcp connection, like below, it builds and runs:
using socket_t = boost::asio::ip::tcp::socket;
using next_layer_t = socket_t;
auto ip_address = boost::asio::ip::address::from_string("172.20.0.2");
auto port = boost::lexical_cast<std::uint16_t>(6379);
boost::asio::ip::tcp::endpoint end_point(ip_address, port);
socket_t socket(ioserv, end_point.protocol());
socket.connect(end_point);
socket_t sock(ioserv); bredis::Connection
bredis_con(std::move(socket));
The local variable is named sock
, while you are trying to move it as socket
.
I found that naming variables as socket
could cause a lot of issues because of socket(2)
syscall on *nix
, so it is better to avoid that name.
Thank you, I appreciate the help! I changed the name and it's working as expected.
Hi, I was initially using boost version 1.73. Wrapping the underlying type
boost::asio::ip::tcp::socket
as a bredis connection works. However, doing the same for the unix sockets with typeboost::asio::local::stream_protocol::socket
does not.This is the code I am running at the moment, which fails during building.
And here is the error I get.
I tried downgrading to boost 1.69, but got the following error:
I'm running inside of a Docker container with Debian GNU/Linux 10 (buster).