Files
frelay/ClientCore/ClientCore.h

71 lines
1.8 KiB
C
Raw Normal View History

2025-06-14 23:25:16 +08:00
#ifndef CLIENTCORE_H
#define CLIENTCORE_H
2025-06-15 14:31:54 +08:00
#include <InfoClient.h>
#include <InfoDirFile.h>
2025-06-15 23:13:30 +08:00
#include <InfoMsg.h>
#include <InfoPack.hpp>
#include <LocalFile.h>
2025-06-14 23:25:16 +08:00
#include <Protocol.h>
#include <QDataStream>
#include <QHostAddress>
#include <QMutex>
#include <QMutexLocker>
#include <QTcpSocket>
#include <QThread>
2025-06-16 20:06:49 +08:00
#include <array>
2025-06-14 23:25:16 +08:00
class ClientCore : public QObject
{
Q_OBJECT
public:
ClientCore(QObject* parent = nullptr);
~ClientCore();
public:
bool Connect(const QString& ip, quint16 port);
void Disconnect();
2025-06-15 14:31:54 +08:00
bool Send(QSharedPointer<FrameBuffer> frame);
bool Send(const char* data, qint64 len);
2025-06-15 23:13:30 +08:00
template <typename T> bool Send(const T& info, FrameBufferType type, const QString& tid)
{
auto f = QSharedPointer<FrameBuffer>::create();
f->tid = tid;
f->data = infoPack<T>(info);
f->type = type;
return Send(f);
}
2025-06-14 23:25:16 +08:00
2025-06-14 23:44:13 +08:00
private:
void onReadyRead();
void onDisconnected();
2025-06-14 23:25:16 +08:00
2025-06-14 23:44:13 +08:00
private:
void UseFrame(QSharedPointer<FrameBuffer> frame);
2025-06-15 14:31:54 +08:00
public:
void SetClientsCall(const std::function<void(const InfoClientVec& clients)>& call);
void SetPathCall(const std::function<void(const QString& path)>& call);
void SetFileCall(const std::function<void(const DirFileInfoVec& files)>& call);
2025-06-16 20:06:49 +08:00
void SetFrameCall(FrameBufferType type, const std::function<void(FrameBuffer*)>& call);
2025-06-15 14:31:54 +08:00
void SetRemoteID(const QString& id);
QString GetRemoteID();
2025-06-14 23:25:16 +08:00
2025-06-14 23:44:13 +08:00
public:
QMutex conMutex_;
QString ownID_;
2025-06-14 23:25:16 +08:00
QString remoteID_;
2025-06-14 23:44:13 +08:00
QTcpSocket* socket_;
2025-06-14 23:25:16 +08:00
QByteArray recvBuffer_;
2025-06-14 23:44:13 +08:00
2025-06-15 23:13:30 +08:00
LocalFile localFile_;
2025-06-14 23:44:13 +08:00
std::function<void(const QString& path)> pathCall_;
std::function<void(const InfoClientVec& clients)> clientsCall_;
std::function<void(const DirFileInfoVec& files)> fileCall_;
2025-06-16 20:06:49 +08:00
std::array<std::function<void(FrameBuffer*)>, 256> frameCall_;
2025-06-14 23:25:16 +08:00
};
#endif // CLIENTCORE_H