Skip to content

Commit

Permalink
SkCore: Create WBroadcastClient
Browse files Browse the repository at this point in the history
  • Loading branch information
3unjee committed Sep 8, 2022
1 parent d818cfc commit 534972c
Show file tree
Hide file tree
Showing 19 changed files with 436 additions and 98 deletions.
2 changes: 2 additions & 0 deletions dist/changes/1.9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

- Create WBroadcastServer.

- Create WBroadcastClient.

- Sk: Add the W_FOREACH macro.

- WControllerApplication: Add the 'getLine' function.
Expand Down
1 change: 1 addition & 0 deletions include/SkCore/WBroadcastClient
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../src/SkCore/src/network/WBroadcastClient.h"
1 change: 1 addition & 0 deletions include/SkCore/WBroadcastClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../src/SkCore/src/network/WBroadcastClient.h"
1 change: 1 addition & 0 deletions include/SkCore/private/WBroadcastClient_p
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/SkCore/src/network/WBroadcastClient_p.h"
1 change: 1 addition & 0 deletions include/SkCore/private/WBroadcastClient_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../src/SkCore/src/network/WBroadcastClient_p.h"
26 changes: 13 additions & 13 deletions src/SkCore/src/io/WCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)
{
QEvent::Type type = event->type();

if (type == static_cast<QEvent::Type> (WCacheThread::EventGet))
if (type == static_cast<QEvent::Type> (EventGet))
{
WCacheThreadEventFile * eventFile = static_cast<WCacheThreadEventFile *> (event);

Expand All @@ -475,7 +475,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventAdd))
else if (type == static_cast<QEvent::Type> (EventAdd))
{
WCacheThreadEventFile * eventFile = static_cast<WCacheThreadEventFile *> (event);

Expand All @@ -495,15 +495,15 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventWrite))
else if (type == static_cast<QEvent::Type> (EventWrite))
{
WCacheThreadEventWrite * eventWrite = static_cast<WCacheThreadEventWrite *> (event);

writeData(eventWrite->url, eventWrite->extension, eventWrite->array);

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventAbort))
else if (type == static_cast<QEvent::Type> (EventAbort))
{
WCacheThreadEvent * eventCache = static_cast<WCacheThreadEvent *> (event);

Expand Down Expand Up @@ -548,7 +548,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventPop))
else if (type == static_cast<QEvent::Type> (EventPop))
{
WCacheThreadEventUrls * eventUrls = static_cast<WCacheThreadEventUrls *> (event);

Expand All @@ -566,7 +566,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventRemove))
else if (type == static_cast<QEvent::Type> (EventRemove))
{
WCacheThreadEventUrls * eventUrls = static_cast<WCacheThreadEventUrls *> (event);

Expand Down Expand Up @@ -601,7 +601,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventRemoveFile))
else if (type == static_cast<QEvent::Type> (EventRemoveFile))
{
WCacheThreadEventUrls * eventUrls = static_cast<WCacheThreadEventUrls *> (event);

Expand All @@ -614,7 +614,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventProxy))
else if (type == static_cast<QEvent::Type> (EventProxy))
{
WCacheThreadEventProxy * eventProxy = static_cast<WCacheThreadEventProxy *> (event);

Expand All @@ -625,13 +625,13 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventClearProxy))
else if (type == static_cast<QEvent::Type> (EventClearProxy))
{
manager->setProxy(QNetworkProxy());

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventSizeMax))
else if (type == static_cast<QEvent::Type> (EventSizeMax))
{
WCacheThreadEvent * eventCache = static_cast<WCacheThreadEvent *> (event);

Expand All @@ -641,15 +641,15 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventMaxJobs))
else if (type == static_cast<QEvent::Type> (EventMaxJobs))
{
WCacheThreadEvent * eventCache = static_cast<WCacheThreadEvent *> (event);

maxJobs = eventCache->value.toInt();

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventClearDatas))
else if (type == static_cast<QEvent::Type> (EventClearDatas))
{
timer->stop();

Expand All @@ -667,7 +667,7 @@ WCacheThread::WCacheThread(WCache * cache, const QString & path, qint64 sizeMax)

return true;
}
else if (type == static_cast<QEvent::Type> (WCacheThread::EventClearAll))
else if (type == static_cast<QEvent::Type> (EventClearAll))
{
timer->stop();

Expand Down
247 changes: 247 additions & 0 deletions src/SkCore/src/network/WBroadcastClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
//=================================================================================================
/*
Copyright (C) 2015-2020 Sky kit authors. <http://omega.gg/Sky>
Author: Benjamin Arnaud. <http://bunjee.me> <[email protected]>
This file is part of SkCore.
- GNU Lesser General Public License Usage:
This file may be used under the terms of the GNU Lesser General Public License version 3 as
published by the Free Software Foundation and appearing in the LICENSE.md file included in the
packaging of this file. Please review the following information to ensure the GNU Lesser
General Public License requirements will be met: https://www.gnu.org/licenses/lgpl.html.
- Private License Usage:
Sky kit licensees holding valid private licenses may use this file in accordance with the
private license agreement provided with the Software or, alternatively, in accordance with the
terms contained in written agreement between you and Sky kit authors. For further information
contact us at [email protected].
*/
//=================================================================================================

#include "WBroadcastClient.h"

#ifndef SK_NO_BROADCASTCLIENT

// Qt includes
#include <QCoreApplication>
#include <QThread>
#include <QEvent>
#include <QTcpSocket>
#include <QHostInfo>

//=================================================================================================
// WBroadcastClientThread
//=================================================================================================

class WBroadcastClientThread : public QThread
{
Q_OBJECT

public: // Enums
enum EventType
{
EventConnect = QEvent::User,
EventDisconnect
};

public:
WBroadcastClientThread();

protected: // QThread reimplementation
/* virtual */ void run();

protected: // Events
/* virtual */ bool event(QEvent * event);

private: // Functions
void clearSocket();

private slots:
void onConnected ();
void onDisconnected();

void onRead();

private:
QTcpSocket * socket;
};

//=================================================================================================
// WBroadcastClientConnect
//=================================================================================================

class WBroadcastClientConnect : public QEvent
{
public:
WBroadcastClientConnect(const QString & address, int port)
: QEvent(static_cast<QEvent::Type> (WBroadcastClientThread::EventConnect))
{
this->address = address;
this->port = port;
}

public: // Variables
QString address;
int port;
};

//=================================================================================================
// WBroadcastClientThread
//=================================================================================================

WBroadcastClientThread::WBroadcastClientThread()
{
socket = NULL;

moveToThread(this);

start(QThread::IdlePriority);
}

//-------------------------------------------------------------------------------------------------
// Protected QThread reimplementation
//-------------------------------------------------------------------------------------------------

/* virtual */ void WBroadcastClientThread::run()
{
exec();
}

//-------------------------------------------------------------------------------------------------
// Events
//-------------------------------------------------------------------------------------------------

/* virtual */ bool WBroadcastClientThread::event(QEvent * event)
{
QEvent::Type type = event->type();

if (type == static_cast<QEvent::Type> (EventConnect))
{
WBroadcastClientConnect * eventConnect = static_cast<WBroadcastClientConnect *> (event);

QString address = eventConnect->address;
int port = eventConnect->port;

qDebug("WBroadcastClientThread: Connecting [%s] on port %d...",
address.toLatin1().constData(), port);

if (socket) clearSocket();

socket = new QTcpSocket(this);

socket->connectToHost(QHostAddress(address), port);

connect(socket, SIGNAL(connected ()), this, SLOT(onConnected ()));
connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));

connect(socket, SIGNAL(readyRead()), this, SLOT(onRead()));

return true;
}
else if (type == static_cast<QEvent::Type> (EventDisconnect))
{
qDebug("WBroadcastClientThread: Disconnecting...");

return true;
}
else return QThread::event(event);
}

//-------------------------------------------------------------------------------------------------
// Private slots
//-------------------------------------------------------------------------------------------------

void WBroadcastClientThread::onConnected()
{

}

void WBroadcastClientThread::onDisconnected()
{
qDebug("WBroadcastClientThread: Disconnected.");

if (socket == NULL) return;

disconnect(socket, 0, this, 0);

socket->deleteLater();

socket = NULL;
}

void WBroadcastClientThread::onRead()
{

}

//-------------------------------------------------------------------------------------------------
// Private functions
//-------------------------------------------------------------------------------------------------

void WBroadcastClientThread::clearSocket()
{
// NOTE: We need to disconnect to avoid receiving a disconnect signal upon deletion.
disconnect(socket, 0, this, 0);

delete socket;

socket = NULL;
}

//=================================================================================================
// WBroadcastClientPrivate
//=================================================================================================

#include "WBroadcastClient_p.h"

WBroadcastClientPrivate::WBroadcastClientPrivate(WBroadcastClient * p) : WPrivate(p) {}

/* virtual */ WBroadcastClientPrivate::~WBroadcastClientPrivate()
{
thread->quit();
thread->wait();

delete thread;
}

//-------------------------------------------------------------------------------------------------

void WBroadcastClientPrivate::init()
{
thread = new WBroadcastClientThread();
}

//=================================================================================================
// WBroadcastClient
//=================================================================================================

/* explicit */ WBroadcastClient::WBroadcastClient(QObject * parent)
: QObject(parent), WPrivatable(new WBroadcastClientPrivate(this))
{
Q_D(WBroadcastClient); d->init();
}

//-------------------------------------------------------------------------------------------------
// Interface
//-------------------------------------------------------------------------------------------------

/* Q_INVOKABLE */ void WBroadcastClient::connectHost(const QString & address, int port)
{
Q_D(WBroadcastClient);

QCoreApplication::postEvent(d->thread, new WBroadcastClientConnect(address, port));
}

/* Q_INVOKABLE */ void WBroadcastClient::disconnectHost()
{
Q_D(WBroadcastClient);

QCoreApplication::postEvent(d->thread, new QEvent(static_cast<QEvent::Type>
(WBroadcastClientThread::EventDisconnect)));
}

#endif // SK_NO_BROADCASTCLIENT

#include "WBroadcastClient.moc"
Loading

0 comments on commit 534972c

Please sign in to comment.