-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.cpp
More file actions
145 lines (131 loc) · 3.77 KB
/
connection.cpp
File metadata and controls
145 lines (131 loc) · 3.77 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
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "connection.h"
#include <iostream>
#include "../spolks1/helpers.h"
Connection::Connection(const std::string& ip, unsigned short port, SocketType type)
{
this->ip = ip;
this->port = port;
this->type = type;
if (type == SocketType::TCP)
{
socket.Create(SocketType::TCP);
socket.Connect(ip, port);
socket.SetReadTimeout(1);
socket.SetWriteTimeout(1);
client = new TCPClient(socket);
}
else
{
socket.Create(SocketType::UDP);
socket.SetReadTimeout(5);
client = new UDPClient(socket, ip, port);
}
sockets::setSendBuffSize(socket.getSocket(), 2000000);
std::cerr << "Buff size: " << sockets::getSendBuffSize(socket.getSocket()) << std::endl;
}
Connection::~Connection()
{
delete client;
}
size_t Connection::getMaxPackageSize()
{
if (type == SocketType::TCP)
return 2000000;
return 1400;
}
bool Connection::Reconnect()
{
if (!client->isReachable())
{
if (asBool(client->getSocket().Connect(ip, port)))
onReconnectCallback(*this);
else
{
std::cerr << "Cannot reconnect" << std::endl;
return false;
}
}
return true;
}
void Connection::setIp(const std::string & ip)
{
this->ip = ip;
((UDPClient*)client)->setIp(ip);
}
void Connection::setPort(unsigned short port)
{
this->port = port;
((UDPClient*)client)->setPort(port);
}
bool Connection::IsConnected()
{
return client->isReachable();
}
void Connection::setOnReconnectCallback(const std::function<void(Connection&)>& callback)
{
onReconnectCallback = callback;
}
OperationResult Connection::repeatIfFailed(std::function<OperationResult()> task)
{
size_t time = 0;
size_t reconnects = 0;
OperationResult result = OperationResult::PartiallyFinished;
bool connected = true;
while (reconnects < reconnectTries)
{
time = 0;
if (connected)
while (time < timeoutWait)
{
result = task();
while (result == OperationResult::Timeout || result == OperationResult::PartiallyFinished)
result = task();
if (result == OperationResult::Success)
return result;
std::cerr << "Got error retrying " << helpers::integral(result) << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
time++;
}
connected = Reconnect();
std::cerr << "Reconnecting" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
reconnectTries++;
}
return result;
}
OperationResult Connection::Send(Buffer& data, size_t size)
{
return repeatIfFailed([&]() { return client->Send(size, data); });
}
OperationResult Connection::getData(Buffer& buff, size_t size, bool noRepeat)
{
if (noRepeat)
{
OperationResult r = client->Recieve(size, buff);
client->endTransfer();
return r;
}
return repeatIfFailed([&]() { return client->Recieve(size, buff); });
}
std::string Connection::getLine()
{
char tmp[1001] = {0};
std::string message = "";
repeatIfFailed([&]() {
if (client->Recieve(65000, false) == OperationResult::Error)
return OperationResult::Error;
client->endTransfer();
client->getState().message << (char*)client->getState().buff.getData();
client->getState().buff.Clear(true);
memset(tmp, 0, 1000);
client->getState().message.getline(tmp, 1000, '\n');
if (client->getState().message.eof())
{
client->getState().message.clear();
return OperationResult::PartiallyFinished;
}
message = tmp;
return OperationResult::Success;
});
return message;
}