补充遇到问题的代码段,MQTT的部分代码:
static int net_read(MQTTClient *c, unsigned char *buf, int len, int timeout)
{
int bytes = 0;
int rc;
while (bytes < len)
{
#ifdef MQTT_USING_TLS
if (c->tls_session)
{
rc = mbedtls_client_read(c->tls_session, &buf[bytes], (size_t)(len - bytes));
if (rc <= 0)
{
bytes = -1;
break;
}
else
{
bytes += rc;
}
goto _continue;
}
#endif
rc = recv(c->sock, &buf[bytes], (size_t)(len - bytes), MSG_DONTWAIT);
if (rc == -1)
{
if (errno != ENOTCONN && errno != ECONNRESET)
{
bytes = -1;
break;
}
}
else
bytes += rc;
#ifdef MQTT_USING_TLS
_continue:
#endif
if (bytes >= len)
{
break;
}
if (timeout > 0)
{
fd_set readset;
struct timeval interval;
LOG_D("net_read %d:%d, timeout:%d", bytes, len, timeout);
timeout = 0;
interval.tv_sec = 1;
interval.tv_usec = 0;
FD_ZERO(&readset);
FD_SET(c->sock, &readset);
select(c->sock + 1, &readset, RT_NULL, RT_NULL, &interval);
}
else
{
LOG_D("net_read %d:%d, break!", bytes, len);
break;
}
}
return bytes;
}
后面这里的 的timeout=0会不会是问题啊
static int MQTTPacket_readPacket(MQTTClient *c)
{
int rc = PAHO_FAILURE;
MQTTHeader header = {0};
int len = 0;
int rem_len = 0;
/* 1. read the header byte. This has the packet type in it */
if (net_read(c, c->readbuf, 1, 0) != 1)
goto exit;
补充遇到问题的代码段,MQTT的部分代码:
static int net_read(MQTTClient *c, unsigned char *buf, int len, int timeout)
{
int bytes = 0;
int rc;
while (bytes < len)
{
#ifdef MQTT_USING_TLS
if (c->tls_session)
{
rc = mbedtls_client_read(c->tls_session, &buf[bytes], (size_t)(len - bytes));
if (rc <= 0)
{
bytes = -1;
break;
}
else
{
bytes += rc;
}
goto _continue;
}
#endif
rc = recv(c->sock, &buf[bytes], (size_t)(len - bytes), MSG_DONTWAIT);
if (rc == -1)
{
if (errno != ENOTCONN && errno != ECONNRESET)
{
bytes = -1;
break;
}
}
else
bytes += rc;
#ifdef MQTT_USING_TLS
_continue:
#endif
if (bytes >= len)
{
break;
}
if (timeout > 0)
{
fd_set readset;
struct timeval interval;
LOG_D("net_read %d:%d, timeout:%d", bytes, len, timeout);
timeout = 0;
interval.tv_sec = 1;
interval.tv_usec = 0;
FD_ZERO(&readset);
FD_SET(c->sock, &readset);
select(c->sock + 1, &readset, RT_NULL, RT_NULL, &interval);
}
else
{
LOG_D("net_read %d:%d, break!", bytes, len);
break;
}
}
return bytes;
}
后面这里的 的timeout=0会不会是问题啊
static int MQTTPacket_readPacket(MQTTClient *c)
{
int rc = PAHO_FAILURE;
MQTTHeader header = {0};
int len = 0;
int rem_len = 0;
/* 1. read the header byte. This has the packet type in it */
if (net_read(c, c->readbuf, 1, 0) != 1)
goto exit;
举报