Libwesockets.h:lws_write 问题:C++ 字符串到 C 的转换并发送

Libwesockets.h: Issue with lws_write: C++ string to C conversion and send

我正在使用 g++。

代码:

        std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        char *cstr = strdup(str.c_str());
        lwsl_notice("\n%s", cstr);
        return lws_write(wsi, (unsigned char*)cstr, strlen(cstr), LWS_WRITE_TEXT);

这也行不通:

        std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        return lws_write(wsi, (unsigned char*)str.c_str(), strlen(str.c_str()), LWS_WRITE_TEXT);

但这工作正常(运行s 多次没有出现错误):

        char cstr[96] = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        return lws_write(wsi, (unsigned char*)cstr, strlen(cstr), LWS_WRITE_TEXT);

也尝试用 malloc 创建字符串,但这也不起作用:

        std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
        char *cstr = (char *)malloc((str.length() + 1) * sizeof(char));
        strcpy(cstr, str.c_str());
        lwsl_notice("\n%s", cstr);
        return lws_write(wsi, (unsigned char*)cstr, strlen(cstr), LWS_WRITE_TEXT);

我可以运行这个代码几次,但最终我得到这个错误free(): invalid next size (fast) (发送数据后失败)

我也尝试用 LWS_PRE 进行一些实验,但是当我将其添加到字符串时,它会在消息的开头添加几个符号,例如:a":

当我在发送数据后尝试 free(cstr) 时,它立即失败并出现 double free or corruption (out) 错误。

lws版本:1.7.1 os: ubuntu x64

根据文档https://libwebsockets.org/lws-api-doc-master/html/group__sending-data.html#gafd5fdd285a0e25ba7e3e1051deec1001

IMPORTANT NOTICE!

When sending with websocket protocol

LWS_WRITE_TEXT, LWS_WRITE_BINARY, LWS_WRITE_CONTINUATION, LWS_WRITE_PING, LWS_WRITE_PONG,

or sending on http/2,

the send buffer has to have LWS_PRE bytes valid BEFORE the buffer pointer you pass to lws_write().

这意味着您必须为缓冲区分配额外的 LWS_PRE 字节,即

std::string str(LWS_PRE, ' '); //Allocate LWS_PRE bytes
str += "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}"
return lws_write(wsi, (unsigned char*)&str[LWS_PRE], str.size(), LWS_WRITE_TEXT);

使用 malloc

char* createMessage() {
  std::string str = "{\"action\":3,\"data\":{\"account\":\"somehashgoeshear\",\"someint\":0,\"id\":1,\"moreint\":0,\"name\":\"demo\"}}";
  char *cstr = (char *)malloc(LWS_PRE + (str.length() + 1) * sizeof(char));
  cstr += LWS_PRE;
  strcpy(cstr, str.c_str());
  lwsl_notice("\n%s", cstr);
  return cstr;
}
...
char* msg = createMessage();
lws_write(wsi, 
          (unsigned char*)msg, 
          strlen(msg) /* add 1 if receiver expects the null character*/, 
          LWS_WRITE_TEXT);
free(msg - LWS_PRE);

使用 libwebsockets 发送 C++ 字符串的简单方法:

std::string payload = "Some sample string.";

// prepare payload with lws header
std::string buffer(LWS_SEND_BUFFER_PRE_PADDING + payload.size(), ' ');
buffer.insert(LWS_SEND_BUFFER_PRE_PADDING, payload);

// send message
lws_write(wsi, (unsigned char*) &buffer[LWS_SEND_BUFFER_PRE_PADDING], payload.size(), LWS_WRITE_TEXT);