-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.h
More file actions
50 lines (45 loc) · 1.38 KB
/
Copy pathconnection.h
File metadata and controls
50 lines (45 loc) · 1.38 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
#ifndef CONNECTION_H
#define CONNECTION_H
#include "../spolks1/sockportable.h"
#include "../spolks1/socket.h"
#include "../spolks1/transfer.h"
#include "../spolks1/client.h"
#include "../spolks1/tcpclient.h"
#include "../spolks1/udpclient.h"
#include <chrono>
#include <thread>
#include <functional>
class Connection
{
size_t timeoutWait = 10;
size_t reconnectTries = 10;
SocketType type;
Socket socket;
std::string ip;
unsigned short port;
Transfer transfer;
Client* client;
Header header;
OperationResult repeatIfFailed(std::function<OperationResult()> task);
std::function<void(Connection&)> onReconnectCallback = [](Connection&) {};
public:
Connection() {}
Connection(const std::string& ip, unsigned short port, SocketType type = SocketType::TCP);
~Connection();
OperationResult Send(Buffer& data, size_t size);
std::string getLine();
OperationResult getData(Buffer& buff, size_t size, bool noRepeat = false);
bool IsConnected();
void setOnReconnectCallback(const std::function<void(Connection&)>& callback);
size_t getMaxPackageSize();
bool Reconnect();
void setIp(const std::string& ip);
void setPort(unsigned short port);
template<typename T>
OperationResult Send(const T& data)
{
Buffer buff(&data, sizeof(data));
return Send(buff, sizeof(T));
}
};
#endif // CONNECTION_H