2016-05-28 18:54:17 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "net/NetAction.h"
|
|
|
|
|
|
|
|
#include "Validator.h"
|
|
|
|
|
|
|
|
namespace Net {
|
2022-04-22 02:12:14 +01:00
|
|
|
class Sink {
|
|
|
|
public:
|
|
|
|
Sink() = default;
|
2022-04-27 22:36:11 +01:00
|
|
|
virtual ~Sink() = default;
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
public:
|
2022-04-27 22:36:11 +01:00
|
|
|
virtual auto init(QNetworkRequest& request) -> Task::State = 0;
|
|
|
|
virtual auto write(QByteArray& data) -> Task::State = 0;
|
|
|
|
virtual auto abort() -> Task::State = 0;
|
|
|
|
virtual auto finalize(QNetworkReply& reply) -> Task::State = 0;
|
|
|
|
|
|
|
|
virtual auto hasLocalData() -> bool = 0;
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
void addValidator(Validator* validator)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-04-22 02:12:14 +01:00
|
|
|
if (validator) {
|
2018-07-15 13:51:05 +01:00
|
|
|
validators.push_back(std::shared_ptr<Validator>(validator));
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-27 22:36:11 +01:00
|
|
|
protected:
|
|
|
|
bool initAllValidators(QNetworkRequest& request)
|
|
|
|
{
|
|
|
|
for (auto& validator : validators) {
|
|
|
|
if (!validator->init(request))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-22 02:12:14 +01:00
|
|
|
bool finalizeAllValidators(QNetworkReply& reply)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-04-22 02:12:14 +01:00
|
|
|
for (auto& validator : validators) {
|
|
|
|
if (!validator->validate(reply))
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool failAllValidators()
|
|
|
|
{
|
|
|
|
bool success = true;
|
2022-04-22 02:12:14 +01:00
|
|
|
for (auto& validator : validators) {
|
2018-07-15 13:51:05 +01:00
|
|
|
success &= validator->abort();
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
2022-04-22 02:12:14 +01:00
|
|
|
bool writeAllValidators(QByteArray& data)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-04-22 02:12:14 +01:00
|
|
|
for (auto& validator : validators) {
|
|
|
|
if (!validator->write(data))
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-27 22:36:11 +01:00
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
std::vector<std::shared_ptr<Validator>> validators;
|
2016-05-28 18:54:17 +01:00
|
|
|
};
|
2022-04-22 02:12:14 +01:00
|
|
|
} // namespace Net
|