尝试改成asio通信
This commit is contained in:
@@ -13,20 +13,20 @@
|
||||
#include <QMutexLocker>
|
||||
#include <QQueue>
|
||||
#include <QReadWriteLock>
|
||||
#include <QTcpSocket>
|
||||
#include <QThread>
|
||||
#include <array>
|
||||
#include <asio.hpp>
|
||||
#include <queue>
|
||||
|
||||
class ClientCore : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ClientCore(QObject* parent = nullptr);
|
||||
ClientCore(QObject* parent, asio::io_context& ioContext);
|
||||
~ClientCore();
|
||||
|
||||
public slots:
|
||||
bool SendFrame(QSharedPointer<FrameBuffer> frame);
|
||||
void Disconnect();
|
||||
|
||||
public slots:
|
||||
@@ -35,13 +35,12 @@ public slots:
|
||||
public:
|
||||
void Instance();
|
||||
bool Connect(const QString& ip, quint16 port);
|
||||
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)
|
||||
const asio::any_io_executor& get_executor();
|
||||
template <typename T> asio::awaitable<bool> Send(const T& info, FrameBufferType type, const QString& tid)
|
||||
{
|
||||
auto f = GetBuffer<T>(info, type, tid);
|
||||
return Send(f);
|
||||
co_return co_await Send(f);
|
||||
}
|
||||
template <typename T> QSharedPointer<FrameBuffer> GetBuffer(const T& info, FrameBufferType type, const QString& tid)
|
||||
{
|
||||
@@ -54,11 +53,21 @@ public:
|
||||
static bool syncInvoke(ClientCore* context, QSharedPointer<FrameBuffer> frame)
|
||||
{
|
||||
bool result = false;
|
||||
bool success = QMetaObject::invokeMethod(context, "SendFrame", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, result),
|
||||
Q_ARG(QSharedPointer<FrameBuffer>, frame));
|
||||
if (!success) {
|
||||
return false;
|
||||
try {
|
||||
std::future<bool> fut = asio::co_spawn(
|
||||
context->get_executor(), [context, frame]() -> asio::awaitable<bool> { co_return co_await context->Send(frame); },
|
||||
asio::use_future);
|
||||
result = fut.get();
|
||||
} catch (const std::exception& ex) {
|
||||
// std::cerr << e.what() << '\n';
|
||||
}
|
||||
|
||||
// bool success = QMetaObject::invokeMethod(context, "SendFrame", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool,
|
||||
// result),
|
||||
// Q_ARG(QSharedPointer<FrameBuffer>, frame));
|
||||
// if (!success) {
|
||||
// return false;
|
||||
// }
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -92,10 +101,10 @@ signals:
|
||||
private:
|
||||
void onReadyRead();
|
||||
void onDisconnected();
|
||||
void handleAsk(QSharedPointer<FrameBuffer> frame);
|
||||
asio::awaitable<void> handleAsk(QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
private:
|
||||
void UseFrame(QSharedPointer<FrameBuffer> frame);
|
||||
asio::awaitable<void> UseFrame(QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
public:
|
||||
void SetRemoteID(const QString& id);
|
||||
@@ -116,11 +125,23 @@ public:
|
||||
QString remoteID_;
|
||||
|
||||
QMutex sockMut_;
|
||||
QTcpSocket* socket_{};
|
||||
QByteArray recvBuffer_;
|
||||
|
||||
bool connected_{false};
|
||||
LocalFile localFile_;
|
||||
|
||||
// asio变更
|
||||
private:
|
||||
asio::io_context& ioContext_;
|
||||
asio::ip::tcp::socket mSocket_;
|
||||
std::array<char, CHUNK_BUF_SIZE> mBuffer_;
|
||||
std::mutex frameMutex_;
|
||||
std::queue<QSharedPointer<FrameBuffer>> frameQueue_;
|
||||
|
||||
private:
|
||||
asio::awaitable<bool> Send(QSharedPointer<FrameBuffer> frame);
|
||||
asio::awaitable<bool> Send(const char* data, size_t size);
|
||||
asio::awaitable<void> recvData();
|
||||
};
|
||||
|
||||
// Socket Worker Thread
|
||||
|
||||
Reference in New Issue
Block a user