C ++ / Gloox: how to check when a connection is down?

I am trying to write my own jabber bot in C ++ / gloox. Everything is going well, but when the Internet connection does not work, the bot thinks it is still connected, and when the connection is up again - of course, the bot does not respond to any message.

Each time the bot is successfully connected, gloox 'recv () returns a ConnNoError, even if the interface is disconnected and the cable is disconnected.

I tried to use the lock and non-blocking connection of gloox and recv (), and everything was without any result. Periodically checking the availability of the xmpp server in different threads does not seem like a good idea, so how to check if the bot is connected right now or not?

If this cannot be done only with gloox, please give me a good method, but let it be available on unix.

+3
source share
2 answers

I have the same question, and I found the reason recv always throws ConnNoError. Here is what I found. When the connection is established, recv calls funciton named dataAvailable In ConnectionTCPBase.cpp, which return

( ( select( m_socket + 1, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 ) && FD_ISSET( m_socket, &fds ) != 0 )

search google, , , FD_ISSET (m_socket, & fds) , readble, ... FD_ISSET (m_socket, & fds) 0, . dataAvailable , , , ConnNoError recv.

if( !dataAvailable( timeout ) )
{
  m_recvMutex.unlock();
  return ConnNoError;
}

, , , .

, , SIGPIPE, , , .

- , heartbeat.

gloox, heartBeat(), m_pClient gloox:: Client

void    CXmpp::heartBeat()
{
    m_pClient->xmppPing(m_pClient->jid(), this);
    if (++heart) > 3) {
        m_pClient->disconnect();
    }
}

xmppPing eventhandler, ping , handleEvent handleEvent

void CEventHandler::handleEvent(const Event& event)  
{  
    std::string sEvent;  
    switch (event.eventType())  
    {  
        case Event::PingPing:   
            sEvent = "PingPing";  
            break;  
        case Event::PingPong:   
            sEvent = "PingPong";  
            //recieve from server, decrease the count of heart
            --heart;  
            break;  
        case Event::PingError:  
            sEvent = "PingError";  
            break;  
        default:  
            break;  
    }  
    return;  
}  

, , 3 !

+3

Source: https://habr.com/ru/post/1761019/


All Articles