if (msg_hdr->len > *resp_len) { err = -1; *resp_len = 0;
时间: 2024-02-15 12:46:31 浏览: 25
这段代码看起来像是在处理网络消息的函数中的错误处理部分。根据代码中的注释,如果接收到的消息长度大于预期的响应长度,那么会返回一个错误并将响应长度设置为0。
具体而言,这里的`msg_hdr`是一个指向消息头部的指针,`len`表示消息的长度,`resp_len`是一个指向预期响应长度的指针。如果接收到的消息长度超过了预期的响应长度,那么就会返回一个错误码,并将`resp_len`设置为0,表示没有响应数据。
相关问题
bool serial_port::serial_port_write_read_at(const std::string &at_command, const std::string &resp_prefix, AT_RESPONSE *at_response) { char *read_line; bool ret_value = false; pthread_mutex_lock(&read_write_mutex); if (!serial_port_write_at_cmd(at_command.c_str())) { goto END; } do { read_line = serial_port_read_one_line(); if (read_line) { //LOG_F(INFO, "read one line from serial: %s", read_line); if (!strncmp(read_line, "AT", strlen("AT")) || !strncmp(read_line, "at", strlen("at"))) { //Do nothing } else if (!strncmp(read_line, "OK", strlen("OK"))) { //LOG_F(INFO, "find final response OK"); ret_value = true; if (at_response != nullptr) { at_response->state = RESP_OK; } break; } else if (!strncmp(read_line, "ERROR", strlen("ERROR")) || !strncmp(read_line, "+CME ERROR", strlen("+CME ERROR"))) { //LOG_F(INFO, "find final response ERROR"); ret_value = false; if (at_response != nullptr) { at_response->state = RESP_ERROR; } break; } else { if (at_response != nullptr) { //LOG_F(INFO, "put response string to AT_RESPONSE"); at_response->response_data[at_response->response_lines] = strdup(read_line); at_response->response_lines += 1; if (at_response->response_lines >= MAX_RESPONSE_LENGTH) at_response->response_lines = 0; } } } } while (true); END: pthread_mutex_unlock(&read_write_mutex); return ret_value; } 没有break反复循环的情况下,开头都是从serial_port_read_one_line这里开始吗?也就是do后面开始执行
是的,如果没有`break`语句来跳出循环,那么每次循环开始时都会执行`serial_port_read_one_line`函数来读取一行数据,并根据读取的内容进行相应的处理。所以,在没有`break`的情况下,循环会一直执行,并且每次循环开始都会重新调用`serial_port_read_one_line`函数。在这段代码中,循环体的开头就是从`serial_port_read_one_line`这里开始执行。
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> <soapenv:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ESB_CS_QRY_USED_RESUSED_001 xmlns="http://esb-sc. yw.zj.chinamobile.com"> <reqXml xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <![CDATA[ <?xml version="1.0" encoding="UTF-8"?><RESP_PARAM><PUB_INFO><RETURN_RESULT>0</RETURN_RESULT><RETURN_DESC>操作成功</RETURN_DESC><OSB_SERIAL_NO>gateway-app-zt^16846071330000000001</OSB_SERIAL_NO></PUB_INFO><BUSI_INFO><GPRS_INFO><GPRS_IN>0</GPRS_IN><GPRS_OUT>0</GPRS_OUT><GPRS_OTHER>0</GPRS_OTHER><GPRS_DURA>0</GPRS_DURA><GPRS_FEE>0</GPRS_FEE></GPRS_INFO><MMS_INFO><MMS_AMOUNT>0</MMS_AMOUNT></MMS_INFO><SMS_INFO><TOTAL_MSG_NUM>15</TOTAL_MSG_NUM><OUT_MSG_NUM>0</OUT_MSG_NUM><INNER_MSG_NUM>0</INNER_MSG_NUM><MEN_MSG_NUM>15</MEN_MSG_NUM></SMS_INFO></BUSI_INFO></RESP_PARAM>]]> </reqXml> </ESB_CS_QRY_USED_RESUSED_001> </soapenv:Body> </soapenv:Envelope>
好的,以下是将该返回内容转换为 JSON 的方法:
1. 点击 Postman 中响应 Body 旁边的 `Code` 按钮,在弹出的对话框中选择 `Node.js - request` 代码片段。
2. 将代码片段中的 `body` 变量赋值为返回内容,即替换下面代码中的 `XMLRESPONSE`:
```
var body = `<xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<soapenv:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ESB_CS_QRY_USED_RESUSED_001 xmlns="http://esb-sc. yw.zj.chinamobile.com">
<reqXml xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?><RESP_PARAM><PUB_INFO><RETURN_RESULT>0</RETURN_RESULT><RETURN_DESC>操作成功</RETURN_DESC><OSB_SERIAL_NO>gateway-app-zt^16846071330000000001</OSB_SERIAL_NO></PUB_INFO><BUSI_INFO><GPRS_INFO><GPRS_IN>0</GPRS_IN><GPRS_OUT>0</GPRS_OUT><GPRS_OTHER>0</GPRS_OTHER><GPRS_DURA>0</GPRS_DURA><GPRS_FEE>0</GPRS_FEE></GPRS_INFO><MMS_INFO><MMS_AMOUNT>0</MMS_AMOUNT></MMS_INFO><SMS_INFO><TOTAL_MSG_NUM>15</TOTAL_MSG_NUM><OUT_MSG_NUM>0</OUT_MSG_NUM><INNER_MSG_NUM>0</INNER_MSG_NUM><MEN_MSG_NUM>15</MEN_MSG_NUM></SMS_INFO></BUSI_INFO></RESP_PARAM>
]]>
</reqXml>
</ESB_CS_QRY_USED_RESUSED_001>
</soapenv:Body>
</soapenv:Envelope>`;
```
3. 在代码片段中添加以下代码来将 XML 转换为 JSON:
```
var parseString = require('xml2js').parseString;
parseString(body, function (err, result) {
console.log(JSON.stringify(result));
});
```
4. 点击 `Send` 按钮,运行代码片段并在控制台中查看转换后的 JSON 结果。
以上就是将该返回内容转换为 JSON 的方法。希望对您有所帮助!