diff options
| author | Bent Bisballe Nyeng <deva@aasimon.org> | 2022-04-29 17:44:19 +0200 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2022-05-26 18:41:21 +0200 | 
| commit | 5e351b7783e5ad135b6918b5f5d92d78723a4a04 (patch) | |
| tree | 6af5e4242d424c40425c1d9a80f886c4b3c820d7 /src | |
| parent | 006f0286fd5fbd9655391b3c0ad10a7d000fd907 (diff) | |
Add client socket support for 'html-cast' functionality to thin client.
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.cc | 77 | ||||
| -rw-r--r-- | src/client.h | 38 | ||||
| -rw-r--r-- | src/viewer.cc | 5 | ||||
| -rw-r--r-- | src/viewer.h | 2 | 
4 files changed, 122 insertions, 0 deletions
| diff --git a/src/client.cc b/src/client.cc new file mode 100644 index 0000000..bcc7958 --- /dev/null +++ b/src/client.cc @@ -0,0 +1,77 @@ +#include "client.h" + +#include <QTcpSocket> +#include <QTimer> + +Client::Client(QObject *parent) +	: QObject(parent) +{ +	socket = new QTcpSocket(this); +	connect(socket, &QTcpSocket::errorOccurred, +	        this, &Client::errorOccurred); +	connect(socket, &QTcpSocket::connected, +	        this, &Client::connected); +	connect(socket, &QTcpSocket::disconnected, +	        this, &Client::disconnected); + +	timer = new QTimer(this); +	timer->setSingleShot(true); +	connect(timer, &QTimer::timeout, this, &Client::timeout); +} + +bool Client::connectToHost(const QString& host) +{ +	this->host = host; +	retryConnect(); +	return true; +} + +bool Client::writeData(const QByteArray& data) +{ +	if(socket->state() != QAbstractSocket::ConnectedState) +	{ +		return false; +	} + +	socket->write(data); +	return socket->waitForBytesWritten(100); // wait at most 100ms +} + +void Client::errorOccurred(QAbstractSocket::SocketError socketError) +{ +	if(socketError == QAbstractSocket::NetworkError || +	   socketError == QAbstractSocket::ConnectionRefusedError) +	{ +		if(retries > 0) +		{ +			timer->setInterval(interval * 1000); +			retries--; +		} +		else +		{ +			timer->setInterval(interval_long * 1000); +		} +		timer->start(); +	} +} + +void Client::connected() +{ +	emit isConnected(); +} + +void Client::disconnected() +{ +	emit isDisconnected(); +	retryConnect(); +} + +void Client::timeout() +{ +	retryConnect(); +} + +void Client::retryConnect() +{ +	socket->connectToHost(host, 1024); +} diff --git a/src/client.h b/src/client.h new file mode 100644 index 0000000..d590a59 --- /dev/null +++ b/src/client.h @@ -0,0 +1,38 @@ +// -*- c++ -*- +#pragma once + +#include <QtCore> +#include <QtNetwork> + +class QTcpSocket; +class QTimer; + +class Client +	: public QObject +{ +	Q_OBJECT +public: +	Client(QObject *parent = 0); + +public slots: +	bool connectToHost(const QString& host); +	bool writeData(const QByteArray& data); +	void errorOccurred(QAbstractSocket::SocketError socketError); +	void connected(); +	void disconnected(); +	void timeout(); + +signals: +	void isConnected(); +	void isDisconnected(); + +private: +	void retryConnect(); + +	QTcpSocket *socket; +	QString host; +	QTimer *timer; +	int retries{10}; +	int interval{5}; +	int interval_long{120}; +}; diff --git a/src/viewer.cc b/src/viewer.cc index 9ed0dba..ac9796c 100644 --- a/src/viewer.cc +++ b/src/viewer.cc @@ -32,6 +32,7 @@  #include <QHBoxLayout>  #include "recipe.h" +#include "client.h"  Viewer::Viewer()  { @@ -39,6 +40,8 @@ Viewer::Viewer()  	textEdit = new QTextEdit();  	layout()->addWidget(textEdit);  	textEdit->setReadOnly(true); +	//client.connectToHost("127.0.0.1"); +	client.connectToHost("192.168.0.46");  }  void Viewer::show(const Recipe& recipe) @@ -93,4 +96,6 @@ void Viewer::show(const Recipe& recipe)  		"<p>" + instructions + "</p>"  		;  	textEdit->setHtml(html); + +	client.writeData(html.toUtf8());  } diff --git a/src/viewer.h b/src/viewer.h index f65a53d..8788d45 100644 --- a/src/viewer.h +++ b/src/viewer.h @@ -29,6 +29,7 @@  #include <QWidget>  #include "recipe.h" +#include "client.h"  class QTextEdit; @@ -43,4 +44,5 @@ public:  private:  	QTextEdit* textEdit{nullptr}; +	Client client;  }; | 
