Repository navigation
Expand file tree
/
Copy pathSession.cpp
More file actions
130 lines (123 loc) · 4.26 KB
/
Copy pathSession.cpp
File metadata and controls
130 lines (123 loc) · 4.26 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (C) 2021 Ilya Entin
*/
#include "Session.h"
#include "Server.h"
#include "ServerOptions.h"
#include "Task.h"
#include "TaskController.h"
#include "Utility.h"
Session::Session(ServerWeakPtr server,
std::string_view primarySignatureWithKey,
std::string_view primaryPubKeyAes,
std::string_view secondarySignatureWithKey,
std::string_view secondaryPubKeyAes)
try :
_task(std::make_shared<Task>(server)),
_server(server) {
_clientId = utility::getUniqueId();
_buffer.reserve(BUFFER_CAPACITY);
switch(Options::_primaryEncryptor) {
case CRYPTO::CRYPTOSODIUM:
{
_primarySodiumEncryptor = std::make_shared<CryptoSodium>(primaryPubKeyAes,
primarySignatureWithKey);
CryptoWeakSodiumPtr weak = _primarySodiumEncryptor;
if (CryptoSodiumPtr encryptor = weak.lock()) {
_primaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
break;
case CRYPTO::CRYPTOPP:
{
_primaryCryptoppEncryptor = std::make_shared<CryptoPlPl>(primaryPubKeyAes,
primarySignatureWithKey);
CryptoWeakPlPlPtr weak = _primaryCryptoppEncryptor;
if (CryptoPlPlPtr encryptor = weak.lock()) {
_primaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
break;
default:
break;
}
_buffer.reserve(BUFFER_CAPACITY);
if (!_primarySodiumEncryptor) {
_primarySodiumEncryptor = std::make_shared<CryptoSodium>(primaryPubKeyAes,
primarySignatureWithKey);
CryptoWeakSodiumPtr weak = _primarySodiumEncryptor;
if (CryptoSodiumPtr encryptor = weak.lock()) {
_primaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
_secondaryCryptoppEncryptor = std::make_shared<CryptoPlPl>(secondaryPubKeyAes,
secondarySignatureWithKey);
CryptoWeakPlPlPtr weak = _secondaryCryptoppEncryptor;
if (CryptoPlPlPtr encryptor = weak.lock()) {
_secondaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
_encryptors = std::make_tuple(_primarySodiumEncryptor, _secondaryCryptoppEncryptor);
}
catch (const std::exception& e) {
LogError << e.what() << '\n';
}
std::pair<HEADER, std::string_view>
Session::buildReply(std::atomic<STATUS>& status) {
_responseData.clear();
const auto& response = _task->getResponse();
for (std::string_view entry : response)
_responseData += entry;
HEADER header =
{ HEADERTYPE::SESSION, _responseData.size(), 0,
ServerOptions::_compressor, DIAGNOSTICS::NONE, status, 0, 0 };
static thread_local std::string encrypted;
encrypted.clear();
encrypted = Options::_doubleEncryption ?
compressDoubleEncrypt(_encryptors, _buffer, header, _responseData, ServerOptions::_doEncrypt, ServerOptions::_compressionLevel) :
compressSingleEncrypt(_encryptors,
Options::_singleEncryptor,
_buffer,
header,
_responseData,
ServerOptions::_doEncrypt,
ServerOptions::_compressionLevel);
std::string_view dataView = encrypted;
header = { HEADERTYPE::SESSION, dataView.size(), 0,
ServerOptions::_compressor, DIAGNOSTICS::NONE, status, 0, 0 };
return { header, dataView };
}
bool Session::processTask() {
Options::_doubleEncryption ?
doubleDecryptDecompress(_encryptors, _buffer, _header, _request) :
singleDecryptDecompress(_encryptors, Options::_singleEncryptor, _buffer, _header, _request);
if (auto taskController = TaskController::getWeakPtr().lock()) {
_task->update(_header, _request);
taskController->processTask(_task);
return true;
}
return false;
}
void Session::displayCapacityCheck(std::string_view type,
unsigned totalNumberObjects,
unsigned numberObjects,
unsigned numberRunningByType,
unsigned maxNumberRunningByType,
STATUS status) {
Logger shortInfo(LOG_LEVEL::INFO, std::clog, false);
shortInfo << "Number " << type << " sessions=" << numberObjects
<< ", Number running " << type << " sessions=" << numberRunningByType
<< ", max number " << type << " running=" << maxNumberRunningByType
<< '\n';
switch (status) {
case STATUS::MAX_OBJECTS_OF_TYPE:
Warn << "\nThe number of " << type << " sessions="
<< numberObjects << " exceeds thread pool capacity." << '\n';
break;
case STATUS::MAX_TOTAL_OBJECTS:
Warn << "\nTotal sessions=" << totalNumberObjects
<< " exceeds system capacity." << '\n';
break;
default:
break;
}
}