sending an ostringstream supported?
Closed this issue · 2 comments
Sec42 commented
Hi,
sorry if this issue is just a lack of my C++ understanding. But I could not find a way to send an ostringstream with the C++ interface.
std::ostringstream line;
line << "Hello World" << std::endl;
I tried different variants of passing the string that would not compile:
nng_sock.send(line.str());
I tried making a buffer view for it:
const std::string tmp = line.str();
const char* cstr = tmp.c_str();
nng_sock.send(nng::buffer(cstr,strlen(cstr)));
but that seems to try & free the pointer passed.
What I could get to work was calling the underlying nng_send
const std::string tmp = line.str();
const char* cstr = tmp.c_str();
int r = nng_send(nng_sock.get(),(void*)cstr,strlen(cstr),0);
if( r != 0 ) {
throw nng::exception(r,"nng_send");
}
Is there a better way to do it?
Preferably without copying the data in memory.
cwzx commented
nng::buffer
is not a view. Try this:
std::string tmp = line.str();
sock.send(nng::view(tmp.data(), tmp.size()));
Sec42 commented
That seems to work fine. Thanks very much!