#ifndef CLIENTCORE_H #define CLIENTCORE_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class ClientCore : public QObject { Q_OBJECT public: ClientCore(QObject* parent = nullptr); ~ClientCore(); public: bool Connect(const QString& ip, quint16 port); void Disconnect(); bool Send(QSharedPointer frame); bool Send(const char* data, qint64 len); template bool Send(const T& info, FrameBufferType type, const QString& tid) { auto f = GetBuffer(info, type, tid); return Send(f); } template QSharedPointer GetBuffer(const T& info, FrameBufferType type, const QString& tid) { auto f = QSharedPointer::create(); f->tid = tid; f->data = infoPack(info); f->type = type; return f; } template static bool asyncInvoke(QObject* context, Callable&& func) { auto promise = QSharedPointer>::create(); QFuture future = promise->future(); QMetaObject::invokeMethod( context, [func = std::forward(func), promise]() mutable { try { promise->addResult(func()); } catch (...) { promise->addResult(false); } promise->finish(); }, Qt::QueuedConnection); future.waitForFinished(); return future.result(); } signals: void sigDisconnect(); private: void onReadyRead(); void onDisconnected(); private: void UseFrame(QSharedPointer frame); public: void SetClientsCall(const std::function& call); void SetPathCall(const std::function& call); void SetFileCall(const std::function& call); void SetFrameCall(FrameBufferType type, const std::function)>& call); void SetRemoteID(const QString& id); QString GetRemoteID(); QString GetOwnID(); public: QMutex conMutex_; QString ownID_; QString remoteID_; QMutex sockMut_; QTcpSocket* socket_; QByteArray recvBuffer_; LocalFile localFile_; std::function pathCall_; std::function clientsCall_; std::function fileCall_; std::array)>, 256> frameCall_; }; class SocketWorker : public QThread { Q_OBJECT public: SocketWorker(ClientCore* core, QObject* parent = nullptr); ~SocketWorker(); public: void SetConnectInfo(const QString& ip, qint16 port); protected: void run() override; signals: void conSuccess(); void connecting(); void conFailed(); void disconnected(); private: QString ip_; qint16 port_; ClientCore* core_; }; #endif // CLIENTCORE_H