From 72df3216a50c871b8738781f6d47eb3ad3ffea0e Mon Sep 17 00:00:00 2001 From: taynpg Date: Sat, 14 Jun 2025 23:44:13 +0800 Subject: [PATCH] send: client send part. --- ClientCore/ClientCore.cpp | 48 +++++++++++++++++++++++++++++++++++++++ ClientCore/ClientCore.h | 17 ++++++++++---- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/ClientCore/ClientCore.cpp b/ClientCore/ClientCore.cpp index 89db477..42b1442 100644 --- a/ClientCore/ClientCore.cpp +++ b/ClientCore/ClientCore.cpp @@ -28,3 +28,51 @@ bool ClientCore::Connect(const QString& ip, quint16 port) void ClientCore::Disconnect() { } + +void ClientCore::onReadyRead() +{ + QByteArray data = socket_->readAll(); + recvBuffer_.append(data); + while (true) { + auto frame = Protocol::ParseBuffer(recvBuffer_); + if (frame == nullptr) { + break; + } + UseFrame(frame); + } +} + +void ClientCore::onDisconnected() +{ + qCritical() << QString("client %1 disconnected...").arg(remoteID_); +} + +void ClientCore::UseFrame(QSharedPointer frame) +{ +} + +bool ClientCore::Send(QSharedPointer frame) +{ + if (frame == nullptr) { + return false; + } + auto data = Protocol::PackBuffer(frame); + if (data.size() == 0) { + return false; + } + return Send(data.constData(), data.size()); +} + +bool ClientCore::Send(const char* data, qint64 len) +{ + if (socket_->state() != QAbstractSocket::ConnectedState) { + qCritical() << QString("client %1 not connected...").arg(remoteID_); + return false; + } + qint64 bytesWritten = socket_->write(data, len); + if (bytesWritten == -1 || !socket_->waitForBytesWritten(5000)) { + qCritical() << QString("Send data to server failed. %1").arg(socket_->errorString()); + return false; + } + return true; +} diff --git a/ClientCore/ClientCore.h b/ClientCore/ClientCore.h index aa1378e..e6401bd 100644 --- a/ClientCore/ClientCore.h +++ b/ClientCore/ClientCore.h @@ -21,15 +21,22 @@ public: bool Connect(const QString& ip, quint16 port); void Disconnect(); +private: + void onReadyRead(); + void onDisconnected(); + +private: + void UseFrame(QSharedPointer frame); + bool Send(QSharedPointer frame); + bool Send(const char* data, qint64 len); + public: - QTcpSocket* socket_; QMutex conMutex_; - // QSharedPointer cf; + QString remoteID_; + QTcpSocket* socket_; + QByteArray recvBuffer_; std::function pathCall_; - - QString remoteID_; - QByteArray recvBuffer_; }; #endif // CLIENTCORE_H \ No newline at end of file