diff options
Diffstat (limited to 'src/qookie-cast-client.cc')
-rw-r--r-- | src/qookie-cast-client.cc | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/qookie-cast-client.cc b/src/qookie-cast-client.cc new file mode 100644 index 0000000..93e04c9 --- /dev/null +++ b/src/qookie-cast-client.cc @@ -0,0 +1,70 @@ +#include "qookie-cast-client.h" + +#include <cstdint> + +#include <QApplication> + +Server::Server(WebView& webview, QObject *parent) + : QObject(parent) + , webview(webview) +{ + server = new QTcpServer(this); + connect(server, SIGNAL(newConnection()), SLOT(newConnection())); + server->listen(QHostAddress::Any, 1024); +} + +void Server::newConnection() +{ + size = -1; + html = ""; + + while (server->hasPendingConnections()) + { + QTcpSocket *socket = server->nextPendingConnection(); + connect(socket, SIGNAL(readyRead()), SLOT(readyRead())); + connect(socket, SIGNAL(disconnected()), SLOT(disconnected())); + } +} + +void Server::disconnected() +{ + QTcpSocket *socket = static_cast<QTcpSocket*>(sender()); + socket->deleteLater(); +} + +void Server::readyRead() +{ + QTcpSocket *socket = static_cast<QTcpSocket*>(sender()); + + if(size < 0) + { + std::uint32_t sz; + socket->read((char*)&sz, sizeof(std::uint32_t)); + size = sz; + } + + while (socket->bytesAvailable() > 0) + { + auto data = socket->readAll(); + html.append(QString::fromUtf8(data)); + size -= data.size(); + } + + if(size <= 0) + { + webview.setHtml(html); + size = -1; + html = ""; + } +} + +int main(int argc, char *argv[]) +{ + QApplication qapp(argc, argv); + WebView webview; + webview.show();//Maximized(); + + Server server(webview); + + return qapp.exec(); +} |