From 3d3c8c93b891858d878610a277a1ed3bdeddb2e3 Mon Sep 17 00:00:00 2001 From: johan Date: Sat, 21 Dec 2013 17:20:02 +0100 Subject: [PATCH] fixed some XBMC checker reconnection Former-commit-id: ec9c327965597b4b552d939cfb664ca95e2f1786 --- deploy/hyperion.tar.gz.REMOVED.git-id | 2 +- include/xbmcvideochecker/XBMCVideoChecker.h | 3 ++ libsrc/xbmcvideochecker/XBMCVideoChecker.cpp | 41 ++++++++++++++++---- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/deploy/hyperion.tar.gz.REMOVED.git-id b/deploy/hyperion.tar.gz.REMOVED.git-id index d99fb231..fad44076 100644 --- a/deploy/hyperion.tar.gz.REMOVED.git-id +++ b/deploy/hyperion.tar.gz.REMOVED.git-id @@ -1 +1 @@ -a975349d4d5b010597f15b5efa6363f43434627f \ No newline at end of file +27dcc318ae9a9226676fb33626500e57703d7b6d \ No newline at end of file diff --git a/include/xbmcvideochecker/XBMCVideoChecker.h b/include/xbmcvideochecker/XBMCVideoChecker.h index 97e1bf1a..08fc4236 100644 --- a/include/xbmcvideochecker/XBMCVideoChecker.h +++ b/include/xbmcvideochecker/XBMCVideoChecker.h @@ -65,6 +65,9 @@ private slots: /// Called when disconnected from XBMC void disconnected(); + /// reconnect to XBMC + void reconnect(); + /// Called when a connection error was encountered void connectionError(QAbstractSocket::SocketError error); diff --git a/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp b/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp index af755f52..0c396693 100644 --- a/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp +++ b/libsrc/xbmcvideochecker/XBMCVideoChecker.cpp @@ -44,7 +44,7 @@ XBMCVideoChecker::XBMCVideoChecker(const std::string & address, uint16_t port, b void XBMCVideoChecker::start() { - disconnected(); + reconnect(); } void XBMCVideoChecker::receiveReply() @@ -157,17 +157,42 @@ void XBMCVideoChecker::connected() void XBMCVideoChecker::disconnected() { std::cout << "XBMC Disconnected" << std::endl; - - // try to connect - _socket.connectToHost(_address, _port); + reconnect(); } -void XBMCVideoChecker::connectionError(QAbstractSocket::SocketError) +void XBMCVideoChecker::reconnect() { - std::cout << "XBMC Connection error" << std::endl; + if (_socket.state() == QTcpSocket::ConnectedState) + { + return; + } - // try to connect again in 1 second - QTimer::singleShot(1000, this, SLOT(disconnected())); + // try to connect + switch (_socket.state()) + { + case QTcpSocket::ConnectingState: + // Somehow when XBMC restarts we get stuck in connecting state + // If we get here we tried to connect already for 5 seconds. abort and try again in 1 second. + _socket.reset(); + _socket.waitForDisconnected(50); + QTimer::singleShot(1000, this, SLOT(reconnect())); + break; + case QTcpSocket::UnconnectedState: + _socket.connectToHost(_address, _port); + QTimer::singleShot(10000, this, SLOT(reconnect())); + break; + default: + QTimer::singleShot(10000, this, SLOT(reconnect())); + break; + } +} + +void XBMCVideoChecker::connectionError(QAbstractSocket::SocketError error) +{ + std::cout << "XBMC Connection error (" << error << ")" << std::endl; + + // close the socket + _socket.close(); } void XBMCVideoChecker::setGrabbingMode(GrabbingMode newGrabbingMode)