-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFifoAcceptor.cpp
More file actions
102 lines (89 loc) · 2.6 KB
/
FifoAcceptor.cpp
File metadata and controls
102 lines (89 loc) · 2.6 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
/*
* Copyright (C) 2021 Ilya Entin
*/
#include "FifoAcceptor.h"
#include <filesystem>
#include <sys/stat.h>
#include <boost/stacktrace.hpp>
#include "Fifo.h"
#include "Options.h"
#include "Server.h"
namespace fifo {
FifoAcceptor::FifoAcceptor(ServerWeakPtr server) :
_acceptorName(Options::_acceptorName),
_server(server) {}
FifoAcceptor::~FifoAcceptor() {
try {
removeFifoFiles();
}
catch (const std::exception& e) {
Warn << e.what() << '\n';
}
}
std::tuple<HEADERTYPE,
std::string,
std::string,
std::string,
std::string>
FifoAcceptor::unblockAcceptor() {
// blocks until the client opens writing end
if (_stopped)
return { HEADERTYPE::ERROR, std::string(), std::string(), std::string(), std::string() };
std::string primarySignatureWithKey;
std::string primaryPubKeyAes;
std::string secondarySignatureWithKey;
std::string secondaryPubKeyAes;
std::array<std::reference_wrapper<std::string>, 2> array{ std::ref(primarySignatureWithKey),
std::ref(primaryPubKeyAes) };
if (!Fifo::readMessage(_acceptorName, true,_header, array))
return { HEADERTYPE::ERROR, std::string(), std::string(), std::string(), std::string() };
return { extractHeaderType(_header), primarySignatureWithKey, primaryPubKeyAes,
secondarySignatureWithKey, secondaryPubKeyAes };
}
void FifoAcceptor::run() {
try {
while (!_stopped) {
auto [type, primarySignatureWithKey, primaryPubKeyAes, secondarySignatureWithKey, secondaryPubKeyAes] =
unblockAcceptor();
if (_stopped)
break;
switch (type) {
case HEADERTYPE::DH_INIT:
if (auto server = _server.lock())
server->createFifoSession(primarySignatureWithKey, primaryPubKeyAes);
break;
default:
break;
}
}
}
catch (const std::exception& e) {
LogError << boost::stacktrace::stacktrace() << '\n';
LogError << e.what() << '\n';
}
}
bool FifoAcceptor::start() {
// in case there was no proper shutdown.
removeFifoFiles();
if (mkfifo(_acceptorName.data(), 0666) == -1 && errno != EEXIST) {
LogError << strerror(errno) << '-' << _acceptorName << '\n';
return false;
}
return true;
}
void FifoAcceptor::stop() {
_stopped = true;
Fifo::onExit(_acceptorName);
}
void FifoAcceptor::removeFifoFiles() {
std::string_view fifoDirectoryNameV(Options::_fifoDirectoryName.data(),Options::_fifoDirectoryName.size());
for(auto const& entry : std::filesystem::directory_iterator(fifoDirectoryNameV))
try {
if (entry.is_fifo())
std::filesystem::remove(entry);
}
catch (const std::exception& e) {
Warn << e.what() << '\n';
}
}
} // end of namespace fifo