-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.h
65 lines (49 loc) · 2.01 KB
/
connection.h
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
#ifndef CONNECTION_H_
#define CONNECTION_H_
#include <future>
#include <memory>
#include <string>
#include <thread>
#include "message.pb.h"
#include "storage.h"
class Connection {
public:
// Establishes connection and runs an event loop for it in a separate thread.
// Does not perform any additional operations (like authentication).
Connection(absl::flat_hash_map<std::string,
std::unique_ptr<Storage>>* storage,
int socket_fd);
// Closes connection.
~Connection();
Connection(const Connection& other) = delete;
Connection& operator=(const Connection& other) = delete;
bool IsClosed() const;
private:
// Waits for the input data and processes it. Returns when the client
// initiates shutdown.
void RunEventLoop();
// Handles the received message. Analyzes its content and current state of
// the cryptography parameters fields, and runs a corresponding helper.
void HandleReceivedMessage(const Message& message);
// Handles a message with the RSA public key. Sets it to the internal field
// of the class or returns an error, if some key is already set.
void HandleRsaKeyMessage(const RsaPublicKey& rsa_public_key);
// Handles a message with the session key (assuming that it's session key
// request). Generates a session key, encrypts it with the RSA public key and
// sends it to the caller.
void HandleSessionKeyMessage(const SessionKey& session_key);
// Handles a message with the data operation. Performs necessary storage
// operations and returns OK, an error or some content to the caller.
void HandleDataOperationMessage(const DataOperation& data_operation);
private:
const int socket_fd_;
// RSA public key -> client's storage
absl::flat_hash_map<std::string, std::unique_ptr<Storage>>* storage_;
std::string rsa_public_key_;
std::string aes_encryption_key_;
int64_t aes_key_expiration_time_ = 0;
std::thread thread_;
std::promise<void> is_closed_;
std::future<void> is_closed_future_;
};
#endif // CONNECTION_H_