Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
datasender.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8
9#include "datasender.h"
10#include <QAbstractSocket>
11#include <quasarapp.h>
12#include <QThread>
13
14namespace QH {
15
16DataSender::DataSender(QThread *thread):
17 Async(thread ) {
18
19}
20
21bool DataSender::sendData(const QByteArray &array, void *target, bool await) const {
22 return asyncLauncher(std::bind(&DataSender::sendPackagePrivate, this, array, target), await);
23}
24
25bool QH::DataSender::sendPackagePrivate(QByteArray array, void *target) const {
26 auto ptr = static_cast<QAbstractSocket*>(target);
27
28 if (!(ptr && ptr->isValid() && ptr->isWritable())) {
29 QuasarAppUtils::Params::log("Send raw data error. Socket is invalid", QuasarAppUtils::Error);
30 return false;
31 }
32
33 int wrote = 0;
34
35 while (wrote < array.length() && ptr->isWritable()) {
36 wrote += ptr->write(array.mid(wrote, array.length()));
37 }
38
39 if (!ptr->flush()) {
40 QuasarAppUtils::Params::log("Send raw data error. data not flushed", QuasarAppUtils::Error);
41
42 return false;
43 }
44
45 if (array.size() != wrote) {
46 QuasarAppUtils::Params::log("not writed data to socket", QuasarAppUtils::Error);
47 return false;
48 }
49
50 return true;
51}
52
53}
The Async class This is bundle of async templates and async wrappers.
Definition async.h:26
bool asyncLauncher(const Job &job, bool await=false, bool freaze=true) const
asyncLauncher This method invoke a job on the thread (using the asyncHandler method) of this object.
Definition async.cpp:80
DataSender(QThread *thread)
bool sendData(const QByteArray &array, void *target, bool await=false) const
sendPackagePrivate This slot move send package to a main thread.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13