-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.cpp
More file actions
132 lines (126 loc) · 3.87 KB
/
Session.cpp
File metadata and controls
132 lines (126 loc) · 3.87 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
131
132
/*
* 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();
switch(Options::_primaryEncryptor) {
case CRYPTO::CRYPTOSODIUM:
{
_primarySodiumEncryptor = std::make_shared<CryptoSodium>(primaryPubKeyAes,
primarySignatureWithKey);
CryptoWeakSodiumPtr weak = _primarySodiumEncryptor;
if (CryptoSodiumPtr encryptor = weak.lock()) {
_encryptorContainer = encryptor;
_primaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
break;
case CRYPTO::CRYPTOPP:
{
_primaryCryptoppEncryptor = std::make_shared<CryptoPlPl>(primaryPubKeyAes,
primarySignatureWithKey);
CryptoWeakPlPlPtr weak = _primaryCryptoppEncryptor;
if (CryptoPlPlPtr encryptor = weak.lock()) {
_encryptorContainer = encryptor;
_primaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
break;
default:
break;
}
if (Options::_doubleEncryption) {
switch (Options::_secondaryEncryptor) {
case CRYPTO::CRYPTOSODIUM:
{
_secondarySodiumEncryptor = std::make_shared<CryptoSodium>(secondaryPubKeyAes,
secondarySignatureWithKey);
CryptoWeakSodiumPtr weak = _secondarySodiumEncryptor;
if (CryptoSodiumPtr encryptor = weak.lock()) {
_secondaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
break;
case CRYPTO::CRYPTOPP:
{
_secondaryCryptoppEncryptor = std::make_shared<CryptoPlPl>(secondaryPubKeyAes,
secondarySignatureWithKey);
CryptoWeakPlPlPtr weak = _secondaryCryptoppEncryptor;
if (CryptoPlPlPtr encryptor = weak.lock()) {
_secondaryPubKeyAes = encryptor->_encodedPubKeyAes;
}
}
break;
default:
break;
}
}
}
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 };
std::string_view dataView =
compressEncrypt(_encryptorContainer,
_buffer,
header,
_responseData,
ServerOptions::_doEncrypt,
ServerOptions::_compressionLevel);
header = { HEADERTYPE::SESSION, dataView.size(), 0,
ServerOptions::_compressor, DIAGNOSTICS::NONE, status, 0, 0 };
return { header, dataView };
}
bool Session::processTask() {
decryptDecompress(_encryptorContainer, _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) {
Info << "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;
}
}