5 MQTT 在“ read 0:1, break “后断开重连

发布于 2020-09-02 14:19:03

MQTT 在 访问时一段时间会出现“ read 0:1, break ”会断开连接一次,重连可正常发送一段
试过onenet的样例,也试过和通讯猫的通讯,都一段时间后会出现这个提示,后断开重连。

msh />onenet_upload_cycle
[D/onenet.sample] buffer : {"temperature":8}
msh />[D/onenet.sample] buffer : {"temperature":52}
[D/onenet.sample] buffer : {"temperature":56}
[D/onenet.sample] buffer : {"temperature":56}
[D/onenet.sample] buffer : {"temperature":19}
[D/onenet.sample] buffer : {"temperature":11}
[D/onenet.sample] buffer : {"temperature":51}
**[D/mqtt] net_read 0:1, break!**
[D/onenet.mqtt] Enter mqtt_offline_callback!
[E/onenet.mqtt] onenet publish failed (-1)!
[E/onenet.sample] upload has an error, stop uploading
[D/mqtt] restart!
[D/onenet.mqtt] Enter mqtt_connect_callback!
[D/mqtt] ipv4 address port: 6002
[D/mqtt] HOST = '183.230.40.39'
[I/mqtt] MQTT server connect success.
[D/onenet.mqtt] Enter mqtt_online_callback!
**[D/mqtt] net_read 0:1, break!**
[D/onenet.mqtt] Enter mqtt_offline_callback!
[D/mqtt] restart!
[D/onenet.mqtt] Enter mqtt_connect_callback!
[D/mqtt] ipv4 address port: 6002
[D/mqtt] HOST = '183.230.40.39'
[I/mqtt] MQTT server connect success.
[D/onenet.mqtt] Enter mqtt_online_callback!
 

查看更多

关注者
0
被浏览
189
2 个回答
ashysage
ashysage 2020-09-02

补充遇到问题的代码段,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;
Polarbear
Polarbear 2020-09-14

我才用多个节点的时候会不断地报出这个信息,导致mqtt一直发送,是怎么回事?[D/mqtt] net_read 0:1, break!

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览