I am learning boost asio and have mistake. I have written simple client ( I can send data from it but when I read data I cant even compile it) I used protocol buffer to serialize data . So file #include "test.pb.h" is probuffer class
My code client :
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include "test.pb.h"
using boost::asio::ip::tcp;
int main(int argc, char** argv)
{
try
{
// connect to the server:
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
std::string const server_address = "localhost";
std::string const server_port = "10000";
tcp::resolver::query query(server_address, server_port);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
//while ( true)
{
Person p = Person();
p.set_id(22);
p.set_name("Jack vorobey");
// std::cout << p.id();
// std::cout << p.name();
std::string data; // this will hold serialized data
bool ok = p.SerializeToString(&data);
assert(ok);
// std::cout << data.size() << std::endl;
boost::asio::write(socket, boost::asio::buffer(data))
boost::asio::read(socket, boost::asio::buffer(data));;
// break; // Connection closed cleanly by peer.
// std::cout << data.size() << std::endl; // shows a reduction in amount of
remaining data
// boost::asio::read(socket, boost::asio::buffer(data) /*,
}
boost::asio::transfer_exactly(65536) */);
}
catch (std::exception& e)
{
//std::cerr << e.what(luuu) << std::endl;
}
std::cout << "\nClosing";
std::string dummy;
}
The code of my mistake I dont understand :
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const boost::asio::const_buffer' (or there is no acceptable conversion)
1> c:\local\boost_1_55_0\boost\asio\buffer.hpp(136): could be 'boost::asio::mutable_buffer &boost::asio::mutable_buffer::operator =(const boost::asio::mutable_buffer &)'
1> while trying to match the argument list '(boost::asio::mutable_buffer, const boost::asio::const_buffer)'
This is because template<typename Elem, typename Traits, typename Allocator> const_buffers_1 buffer(const std::basic_string<Elem, Traits, Allocator> &) returns an instance of const_buffers_1 (which is a model of ConstBufferSequence concept). Certainly, you cannot read data into a constant buffer.
Do not read data into a std::string, because it's not intended for that (note that its c_str() and data() member functions return const char*). Instead, allocate another buffer or use asio::streambuf.
You can use a streambuf, or specify the (preallocated!) size:
#include <boost/asio.hpp>
#include <string>
using boost::asio::ip::tcp;
int main()
{
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::socket socket(io_service);
boost::asio::connect(socket, resolver.resolve(tcp::resolver::query("localhost", "10000")));
std::string request("request");
boost::asio::write(socket, boost::asio::buffer(request));
#if 0
std::string response;
response.resize(32);
boost::asio::read(socket, boost::asio::buffer(&response[0], response.size()));
#else
boost::asio::streambuf response;
boost::asio::read(socket, response);
#endif
}
Related
I'm trying to develop a simple chat program using Boost. I came accross strange situation. I use netcat to listen at specific port while I run the program that's sending a simple text. Connection is established but the text is messed up. Actually instead of whole line I sometimes get one random characters or two. Im putting the code down below:
#include "lib/client.h"
#include <boost/asio.hpp>
#include <boost/asio/io_service.hpp>
#include <iostream>
#include <thread>
#include <string>
int main(int argc, char* argv[]){
if(argc != 3){
std::cout << "Wrong use. After specifying executable, add host and port\n";
return 0;
}
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
auto endpoint = resolver.resolve({argv[1],argv[2]});
Client c(io_service, endpoint);
std::thread t([&io_service](){ io_service.run();});
std::string text = "Welcome host!";
c.add_msg_to_deque(text);
t.join();
c.close();
return 0;
}
And here are client methods:
#include "../lib/client.h"
#include <boost/asio.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/array.hpp>
#include <deque>
#include <iostream>
#include <string>
void Client::connect(boost::asio::ip::tcp::resolver::iterator endpoint){
boost::asio::async_connect(socket, endpoint,
[this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator)
{
if (!ec)
{
}
});
}
void Client::close()
{
ios.post([this]() { socket.close(); });
}
void Client::add_msg_to_deque(const std::string& msg){
ios.post([this,msg](){
write_msg_deque.push_back(msg);
send_msg();
});
}
void Client::send_msg(){
boost::array<char,128> buf;
std::string temp_string = write_msg_deque.front();
std::copy(temp_string.begin(),temp_string.end(),buf.begin());
boost::asio::async_write(socket, boost::asio::buffer(buf,temp_string.size()),[this](boost::system::error_code ec, std::size_t){
if(!ec){
write_msg_deque.pop_front();
if(!write_msg_deque.empty())
send_msg();
}
else{
socket.close();
}
});
}
You are using async_write with local data it is bad idea. async_write returns immediately. After calling async_write your method send_msg terminates, so local data (buf array) is destroyed before your message is sent. You can use a synchronous version of IO functions to send data or keep buf as member of your class to provide data exists until data is sent successfully.
Given a file that contains one integer per line, I am trying to use this code to read the file and store the numbers in a vector.
Strangely, after finishing reading the file, my program is printing the string "stoi". Is this just a behavior of std::stoi? I couldn't find anything about this in documentation.
I am using g++ 6.2.1.
Here is the relevant code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
void usage() {
std::cout << "Usage: ./binary_tree [FILE]\n";
}
int main(int argc, char* argv[]) {
try{
if (argc <= 1) {
usage();
return 1;
}
std::ifstream inputFile;
inputFile.open(argv[1], std::ios::in);
if (!inputFile.is_open()) throw std::runtime_error("Failed to open file");
std::string line;
std::vector<int> nums;
while(!inputFile.eof()) {
getline(inputFile, line);
int num = std::stoi(line);
nums.push_back(num);
}
// Clean up
inputFile.close();
return 0;
}
catch(const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
I am trying to implement lock free queue of user defined data type using boost library, but I am getting wrong result.
Please help me out where I am doing wrong.
#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
#include <string.h>
#include <time.h>
class Queue
{
private:
unsigned char *m_data;
int m_len;
public:
Queue(unsigned char *data,int len);
Queue(const Queue &obj);
~Queue();
Queue & operator =(const Queue &obj);
unsigned char *getdata()
{
return m_data;
}
int getint()
{
return m_len;
}
};
Queue::Queue(unsigned char* data, int len)
{
m_len=len;
m_data=new unsigned char[m_len];
memcpy(m_data,data,m_len);
}
Queue::Queue(const Queue& obj)
{
m_len= obj.m_len;
m_data=new unsigned char[m_len];
memcpy(m_data,(unsigned char *)obj.m_data,m_len);
}
Queue::~Queue()
{
delete[] m_data;
m_len=0;
}
Queue & Queue::operator =(const Queue &obj)
{
if(this != &obj)
{
m_len=obj.m_len;
m_data=new unsigned char[m_len];
memcpy(m_data,(unsigned char *)obj.m_data,m_len);
}
return *this;
}
boost::lockfree::spsc_queue<Queue*> q(10);
void produce()
{
int i=0;
unsigned char* data=(unsigned char *)malloc(10);
memset(data,1,9);
Queue obj(data,10);
Queue *pqueue=&obj;
printf("%d\n",pqueue->getint());
q.push(pqueue);
}
void consume()
{
Queue *obj;
q.pop(&obj);
printf("%d\n",obj->getint());
}
int main(int argc, char** argv) {
// std::thread t1{produce};
// std::thread t2{consume};
//
// t1.join();
// t2.join();
produce();
consume();
return 0;
}
As per boost::lockfree::queue requirements I created following in class.
Copy Constructor
Assignment Operator
Destructor
Please let me know if anything other requires.
Thanks.
You're using malloc in C++.
You die.
You have 2 lives left.
Seriously, don't do that. Especially since using it with delete[] is clear cut Undefined Behaviour.
Sadly you lose another life here:
Queue obj(data,10);
Queue *pqueue=&obj;
q.push(pqueue);
You store a pointer to a local. More Undefined Behaviour
You have 1 life left.
Last life at
q.pop(&obj);
You pop using an iterator. It will be treated as an output iterator.
You get a return that indicates the number of elements popped, and items
will be written to &obj[0], &obj[1], &obj[2], etc.
Guess what? Undefined Behaviour.
See also: Boost spsc queue segfault
You died.
You're already dead. But you forsake your afterlife with
printf("%d\n",obj->getint());
Since pop might not have popped anything (the queue may have been empty), this in itself is Undefined Behaviour.
The funny part is, you talk about all these constructor requirements but you store pointers in the lockfree queue...?! Just write it:
typedef std::vector<unsigned char> Data;
class Queue {
private:
Data m_data;
public:
Queue(Data data) : m_data(std::move(data)) {}
Queue() : m_data() {}
unsigned char const *getdata() const { return m_data.data(); }
size_t getint() const { return m_data.size(); }
};
boost::lockfree::spsc_queue<Queue> q(10);
Live On Coliru
Notes:
you need to make the consumer check the return code of pop. The push might not have happened, and lock free queues don't block.
you don't need that contraption. Just pass vectors all the way:
C++ Code
Live On Coliru
#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
#include <vector>
typedef std::vector<unsigned char> Queue;
boost::lockfree::spsc_queue<Queue> q(10);
void produce() {
Queue obj(10, 1);
std::cout << __FUNCTION__ << " - " << obj.size() << "\n";
q.push(std::move(obj));
}
void consume() {
Queue obj;
while (!q.pop(obj)) { }
std::cout << __FUNCTION__ << " - " << obj.size() << "\n";
}
int main() {
std::thread t1 {produce};
std::thread t2 {consume};
t1.join();
t2.join();
}
Recently, I've been working on a little project alongside my c++ game-dev engine : it's a programming language, written in C++, in one header, named kickC. Here is what I have done so far : (See question below)
#ifndef KICK_C_INCLUDED_H
#define KICK_C_INCLUDED_H
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <exception>
#include <functional>
#include <unordered_map>
#include <vector>
#define LOG(x) std::cout << x << std::endl;
namespace strutil
{
inline unsigned CountWords(const std::string& value){
std::string temp = value;
std::replace_if(temp.begin(), temp.end(), std::ptr_fun<int, int>(std::isspace), ' ');
temp.erase(0, temp.find_first_not_of(" "));
if(temp.empty())
return 0;
return std::count(temp.begin(), std::unique(temp.begin(), temp.end()), ' ') + !std::isspace(*value.rbegin());
}
}
class KickCException : std::exception
{
public:
explicit KickCException(const char* msg, bool fatal = false)
: msg_(msg){}
explicit KickCException(const std::string& msg)
: msg_(msg){}
virtual ~KickCException() throw(){}
virtual const char* what() const throw(){
return std::string("[error :] [")
.append(msg_)
.append("]")
.c_str();
}
protected:
std::string msg_;
};
class KickCFileException : KickCException
{
public:
explicit KickCFileException(const char* msg)
: KickCException(msg){}
explicit KickCFileException(const std::string& msg)
: KickCException(msg){}
virtual ~KickCFileException() throw(){}
const char* what() const throw() override{
return std::string("[file error :] [")
.append(msg_)
.append("]")
.c_str();
}
};
class KickCEmptyStringException : KickCException
{
public:
explicit KickCEmptyStringException(const char* msg)
: KickCException(msg){}
explicit KickCEmptyStringException(const std::string& msg)
: KickCException(msg){}
virtual ~KickCEmptyStringException() throw(){}
const char* what() const throw() override{
return std::string("[empty string error :] [")
.append(msg_)
.append("]")
.c_str();
}
};
class KickCAPIBehaviourImplementation
{
public:
KickCAPIBehaviourImplementation(){}
~KickCAPIBehaviourImplementation(){}
void AddDefined(const std::string& str, std::function<void(void)> func){
m_values[str] = func;
}
void ParseAndApplyLine(const std::string& line){
std::istringstream iss(line);
for(unsigned i = 0; i < strutil::CountWords(line); ++i){
static std::string word = "";
iss >> word;
for(auto it_map = m_values.begin(); it_map != m_values.end(); ++it_map){
if(it_map->first == word)
{
(it_map->second)(/*HERE ! GIVE SOME ARGUMENTS ! */);
}
}
}
}
private:
std::unordered_map<std::string, std::function<void(void)>> ///so far, args is void... m_values;
};
#endif //KICK_C_INCLUDED_H
///src
int main(int argc, const char** args){
std::ifstream file("script.kick");
KickCAPIBehaviourImplementation kickCApiBehaviour;
try{
if(!file.is_open())
throw KickCFileException("unvalid fileName taken at input");
kickCApiBehaviour.AddDefined("print", [&](void){std::cout << "print found !" << std::endl;});
while(!file.eof()){
std::string line;
std::getline(file, line);
kickCApiBehaviour.ParseAndApplyLine(line);
}
}catch(KickCException& e){
LOG(e.what());
}
file.close();
std::cin.get();
}
So here is the Question : I would like to pass std::function (see class KickCAPIBehaviourImplementation ) a variable argument of types : I need to use variatic templates, of course, but the question how can I implement it so i end up calling my functions like this ?
kickCApiBehaviour.AddDefined("print", [&](int arg1, char * arg2, int arg3){std::cout << arg1 << arg2 << arg3 << std::endl;});
Move the parser into the std::function.
Where you add the function, include a signature:
// helper type:
template<class T>struct tag{using type=T;};
kickCApiBehaviour.AddDefined(
"print", // the name
tag<void(int,char*,int)>{}, // the signature
[&](int arg1, char * arg2, int arg3){
std::cout << arg1 << arg2 << arg3 << std::endl;
} // the operation
);
store a std::function< error_code(ParserState*) >. Inside AddDefined, store a lambda that includes a call to the code that parses arguments and calls the passed in lambda:
template<class R, class...Args, class F>
void AddDefined(std::string name, tag<R(Args...)>, F f) {
std::function< error_code(ParserState*) > r =
[f](ParserState* self)->error_code {
// here, parse each of Args... out of `self`
// then call `f`. Store its return value,
// back in `self`. If there is a parse error (argument mismatch, etc),
// return an error code, otherwise return no_error
};
m_values[name] = r;
};
then m_values contains the operation "take a parser state, and parse the arguments, and call the function in question on them".
What the behavior of a deadline_timer whose expiration is 0 milliseconds?
In my code, I have:
boost::asio::io_service ios;
...
boost::asio::deadline_timer ptimer(ios);
ptimer.expires_from_now(boost::posix_time::milliseconds(duration)); // Duration might be 0 sometimes
boost::system::error_code ec;
ptimer.async_wait(boost::bind(&SomeTimeOutHandler, this, ec));
I found that if duration == 0, the handler SomeTimeOutHandler never gets called.
I want it gets called.
However, if I change to duration == 1, the handler does get called.
So what the exact behavior it should be when the deadline_timer's expiration is 0?
Edit:
But the following HelloWorld test program is working (suggested by #Roger):
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
void SomeTimeOutHandler(const boost::system::error_code& ec)
{
if (ec)
{
std::cout << "SomeTimeOutHandler error_code" << std::endl;
}
else
{
std::cout << "I'm in good shape" << std::endl;
}
}
void Test(int duration)
{
boost::asio::io_service ios;
boost::asio::deadline_timer ptimer(ios);
ptimer.expires_from_now(boost::posix_time::milliseconds(duration));
boost::system::error_code ec;
ptimer.async_wait(boost::bind(&SomeTimeOutHandler, ec));
ios.run();
// boost::this_thread::sleep(boost::posix_time::milliseconds(duration * 2 + 1000));
}
int main(int argc, char* argv[])
{
Test(10); // Test(0);
return 0;
}