heartbeat: add basic code.

This commit is contained in:
2025-06-25 17:06:30 +08:00
parent 06bf31e8bd
commit c7b15694b9
5 changed files with 138 additions and 4 deletions

View File

@@ -209,6 +209,11 @@ bool ClientCore::Send(const char* data, qint64 len)
return true;
}
bool ClientCore::IsConnect()
{
return connected_;
}
void ClientCore::SetRemoteID(const QString& id)
{
GlobalData::Ins()->SetRemoteID(id);
@@ -238,7 +243,62 @@ SocketWorker::~SocketWorker()
void SocketWorker::run()
{
// qDebug() << "SocketWorker thread:" << QThread::currentThread();
core_->Instance();
exec();
}
HeatBeat::HeatBeat(ClientCore* core, QObject* parent) : QThread(parent), core_(core)
{
}
HeatBeat::~HeatBeat()
{
Stop();
}
void HeatBeat::Stop()
{
isRun_ = false;
}
void HeatBeat::run()
{
isRun_ = true;
InfoMsg info;
auto frame = core_->GetBuffer(info, FBT_SER_MSG_HEARTBEAT, "");
while (isRun_) {
QThread::sleep(1);
if (!core_->IsConnect()) {
continue;
}
ClientCore::syncInvoke(core_, frame);
}
}
TransBeat::TransBeat(ClientCore* core, QObject* parent) : QThread(parent), core_(core)
{
}
TransBeat::~TransBeat()
{
Stop();
}
void TransBeat::run()
{
isRun_ = true;
InfoMsg info;
while (isRun_) {
QThread::sleep(1);
if (!core_->IsConnect()) {
continue;
}
auto frame = core_->GetBuffer(info, FBT_SER_MSG_JUDGE_OTHER_ALIVE, core_->GetRemoteID());
ClientCore::syncInvoke(core_, frame);
}
}
void TransBeat::Stop()
{
isRun_ = false;
}

View File

@@ -34,6 +34,7 @@ public:
void Disconnect();
bool Send(QSharedPointer<FrameBuffer> frame);
bool Send(const char* data, qint64 len);
bool IsConnect();
template <typename T> bool Send(const T& info, FrameBufferType type, const QString& tid)
{
auto f = GetBuffer<T>(info, type, tid);
@@ -104,6 +105,7 @@ public:
LocalFile localFile_;
};
// Socket Worker Thread
class SocketWorker : public QThread
{
Q_OBJECT
@@ -119,4 +121,44 @@ private:
ClientCore* core_{};
};
// HeatBeat to Server
class HeatBeat : public QThread
{
Q_OBJECT
public:
HeatBeat(ClientCore* core, QObject* parent = nullptr);
~HeatBeat() override;
public:
void Stop();
protected:
void run() override;
private:
bool isRun_{false};
ClientCore* core_{};
};
// judge send client is alive or not when downloading
class TransBeat : public QThread
{
Q_OBJECT
public:
TransBeat(ClientCore* core, QObject* parent = nullptr);
~TransBeat() override;
public:
void Stop();
protected:
void run() override;
private:
bool isRun_{false};
ClientCore* core_{};
};
#endif // CLIENTCORE_H