-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFifoSession.cpp
More file actions
115 lines (99 loc) · 2.65 KB
/
FifoSession.cpp
File metadata and controls
115 lines (99 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (C) 2021 Ilya Entin
*/
#include "FifoSession.h"
#include <filesystem>
#include <sys/stat.h>
#include <boost/stacktrace.hpp>
#include "Fifo.h"
#include "Server.h"
#include "ServerOptions.h"
namespace fifo {
constexpr auto TYPE{ "fifo" };
FifoSession::FifoSession(ServerWeakPtr server,
std::string_view encodedPeerPubKeyAes,
std::string_view signatureWithPubKey) :
RunnableT(ServerOptions::_maxFifoSessions),
Session(server, encodedPeerPubKeyAes, signatureWithPubKey) {}
FifoSession::~FifoSession() {
try {
std::filesystem::remove(_fifoName);
}
catch (const std::exception& e) {
Warn << e.what() << '\n';
}
}
bool FifoSession::start() {
_fifoName = Options::_fifoDirectoryName + '/';
_fifoName += ioutility::toCharsBoost(_clientId);
if (mkfifo(_fifoName.data(), 0666) == -1 && errno != EEXIST) {
LogError << strerror(errno) << '-' << _fifoName << '\n';
return false;
}
return true;
}
void FifoSession::run() {
CountRunning countRunning;
if (!std::filesystem::exists(_fifoName) || _stopped)
return;
while (!_stopped) {
if (!receiveRequest())
break;
}
}
void FifoSession::stop() {
_stopped = true;
Fifo::onExit(_fifoName);
}
bool FifoSession::receiveRequest() {
if (!std::filesystem::exists(_fifoName))
return false;
switch (_status) {
case STATUS::MAX_OBJECTS_OF_TYPE:
case STATUS::MAX_TOTAL_OBJECTS:
_status = STATUS::NONE;
break;
default:
break;
}
try {
_request.clear();
HEADER header;
std::array<std::reference_wrapper<std::string>, 1> array{std::ref(_request)};
if (!Fifo::readMessage(_fifoName, true, header, array))
return false;
if (processTask())
return sendResponse();
}
catch (const std::exception& e) {
Warn << boost::stacktrace::stacktrace() << '\n';
Warn << e.what() << '\n';
}
return false;
}
bool FifoSession::sendResponse() {
if (!std::filesystem::exists(_fifoName))
return false;
auto [header, payload] = buildReply(_status);
if (payload.empty())
return false;
return Fifo::sendMessage(false, _fifoName, header, payload);
}
void FifoSession::sendStatusToClient() {
auto lambda = [] (const HEADER& header,
std::string_view idStr,
std::string_view pubKey) {
Fifo::sendMessage(false, Options::_acceptorName, header, idStr, pubKey);
};
Session::sendStatusToClient(lambda, _status);
}
void FifoSession::displayCapacityCheck(std::atomic<unsigned>& totalNumberObjects) {
Session::displayCapacityCheck(TYPE,
totalNumberObjects,
getNumberObjects(),
getNumberRunningByType(),
_maxNumberRunningByType,
_status);
sendStatusToClient();
}
} // end of namespace fifo