-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectionManager.hpp
65 lines (53 loc) · 2 KB
/
ConnectionManager.hpp
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
#pragma once
#include <boost/asio.hpp>
#include <functional>
#include <memory>
#include <thread>
#include "NetworkErrors.hpp"
#include "Packet.hpp"
using namespace PacketUtils;
class Response;
class TransferInfoManager;
class ConnectionManager;
// For more information on the following functions, see the readme.md file
void handleNetworkException(ConnectionManager &con,
const ErrorsModule::NetworkException &e);
class ConnectionManager
{
public:
explicit ConnectionManager(const TransferInfoManager &info);
~ConnectionManager();
std::unique_ptr<Response> connectSendReceiveDisconnect(
const Packet &packet);
private:
std::string ip_address_str_;
int port_number_;
boost::asio::ip::tcp::endpoint end_point_;
boost::asio::io_context io_context_;
std::shared_ptr<boost::asio::ip::tcp::socket> socket_;
// connection functions
void initNetworking();
boost::asio::ip::address convertIPAddress(
const std::string &ip_address_str);
void converIPAndCreateEndpoint();
void createSocket();
void handleSocketError(const boost::system::system_error &e);
void handleConnectionError(const boost::system::system_error &e) const;
void connect();
void disconnect();
void closeAndExit();
// sending and receiving functions
void sendPacket(const Packet &packet);
bool executeAndCheck(
const std::function<void(std::unique_ptr<Response> &)> &operation,
std::unique_ptr<Response> &outResponse, int maxRetryCount);
bool isFailureStatusCode(uint16_t statusCode) const;
std::unique_ptr<Response> receiveData();
// handlers
void handleOperationWithRetry(
const std::function<void(std::unique_ptr<Response> &)> &operation,
std::unique_ptr<Response> &outResponse);
void handleFailureStatus(const std::unique_ptr<Response> &response);
friend void handleNetworkException(ConnectionManager &con,
const ErrorsModule::NetworkException &e);
};