forked from cculianu/Fulcrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractConnection.cpp
280 lines (260 loc) · 10.3 KB
/
AbstractConnection.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Fulcrum - A fast & nimble SPV Server for Bitcoin Cash
// Copyright (C) 2019-2020 Calin A. Culianu <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program (see LICENSE.txt). If not, see
// <https://www.gnu.org/licenses/>.
//
#include "AbstractConnection.h"
#include "Util.h"
#include <QTcpSocket>
#include <QSslSocket>
#include <QHostAddress>
#include <cassert>
AbstractConnection::AbstractConnection(quint64 id_in, QObject *parent, qint64 maxBuffer)
: QObject(parent), IdMixin(id_in), MAX_BUFFER(maxBuffer)
{
assert(qobj()); // Runtime check that derived class followed the rules outlined at the top of Mixins.h
}
/// this should only be called from our thread, because it accesses socket which should only be touched from thread
QString AbstractConnection::prettyName(bool dontTouchSocket, bool showId) const
{
QString type = socket && !dontTouchSocket ? (dynamic_cast<QSslSocket *>(socket) ? "SSL" : "TCP") : "(NoSocket)";
QString port = socket && !dontTouchSocket && socket->peerPort() ? QString(":%1").arg(socket->peerPort()) : "";
QString ip = socket && !dontTouchSocket && !socket->peerAddress().isNull() ? socket->peerAddress().toString() : "";
QString idStr = showId ? QString(" (id: %1)").arg(id) : QString();
return QString("%1 %2%3 %4%5").arg(type).arg(!objectName().isNull()?objectName():"(AbstractSocket)").arg(idStr).arg(ip).arg(port);
}
bool AbstractConnection::isGood() const
{
return status == Connected;
}
bool AbstractConnection::isStale() const
{
return isGood() && Util::getTime() - lastGood > stale_threshold;
}
// The below 4 will only return valid results if this->thread() == QThread::currentThread(), and if socket != nullptr
QHostAddress AbstractConnection::localAddress() const
{
QHostAddress ret;
if (thread() == QThread::currentThread() && socket) {
ret = socket->localAddress();
}
return ret;
}
quint16 AbstractConnection::localPort() const
{
quint16 ret{};
if (thread() == QThread::currentThread() && socket) {
ret = socket->localPort();
}
return ret;
}
QHostAddress AbstractConnection::peerAddress() const
{
QHostAddress ret;
if (thread() == QThread::currentThread() && socket) {
ret = socket->peerAddress();
}
return ret;
}
quint16 AbstractConnection::peerPort() const
{
quint16 ret{};
if (thread() == QThread::currentThread() && socket) {
ret = socket->peerPort();
}
return ret;
}
bool AbstractConnection::isSsl() const
{
bool ret{};
if (thread() == QThread::currentThread() && socket) {
ret = dynamic_cast<QSslSocket *>(socket) != nullptr;
}
return ret;
}
void AbstractConnection::do_disconnect(bool graceful)
{
status = status == Bad ? Bad : NotConnected; // try and keep Bad status around so EXMgr can decide when to reconnect based on it
if (socket) {
if (!graceful) {
Debug() << __FUNCTION__ << " (abort)";
socket->abort(); // this will set status too because state change, but we set it first above to be paranoid
} else {
socket->disconnectFromHost();
Debug() << __FUNCTION__ << " (graceful)";
}
}
}
namespace {
void setSockOpts(QAbstractSocket *socket) {
if (socket) {
// don't we want to disable KeepAliveOption ? it appears to eat some bandwidth .. 1 packet per second on Windows.
//socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); // from Qt docs: required on Windows before connection
socket->setSocketOption(QAbstractSocket::SocketOption::LowDelayOption, 1); // disable Nagling for lower latency
}
}
}
void AbstractConnection::socketConnectSignals()
{
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(on_error(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(on_socketState(QAbstractSocket::SocketState)));
connect(socket, &QAbstractSocket::connected, this, [this]{on_connected();});
setSockOpts(socket); // from Qt docs: required on Windows before connection
}
bool AbstractConnection::do_write(const QByteArray & data)
{
QString err = "";
if (!socket) {
err = " called with no socket! FIXME!";
} else if (QThread::currentThread() != thread()) {
err = " called from another thread! FIXME!";
}
if (!err.isEmpty()) {
Error() << __FUNCTION__ << " (" << objectName() << ") " << err << " id=" << id;
return false;
}
auto data2write = writeBackLog + data;
qint64 written = socket->write(data2write);
if (written < 0) {
Error() << __FUNCTION__ << " error on write " << socket->error() << " (" << socket->errorString() << ") id=" << id;
do_disconnect();
return false;
} else if (written < data2write.length()) {
writeBackLog = data2write.mid(int(written));
}
nSent += written;
if (writeBackLog.length() > MAX_BUFFER) {
Error() << __FUNCTION__ << " MAX_BUFFER reached on write (" << MAX_BUFFER << ") id=" << id;
do_disconnect();
return false;
}
return true;
}
void AbstractConnection::slot_on_readyRead() { on_readyRead(); }
void AbstractConnection::on_connected()
{
// runs in our thread's context
Debug() << __FUNCTION__;
connectedTS = Util::getTime();
setSockOpts(socket); // ensure nagling disabled
socket->setReadBufferSize(MAX_BUFFER); // ensure memory exhaustion from peer can't happen in case we're too busy to read.
connectedConns.push_back(connect(this, &AbstractConnection::send, this, &AbstractConnection::do_write));
connectedConns.push_back(connect(socket, SIGNAL(readyRead()), this, SLOT(slot_on_readyRead())));
if (dynamic_cast<QSslSocket *>(socket)) {
// for some reason Qt can't find this old-style signal for QSslSocket so we do the below.
// Additionally, bytesWritten is never emitted for QSslSocket, violating OOP! Thanks Qt. :P
connectedConns.push_back(connect(socket, SIGNAL(encryptedBytesWritten(qint64)), this, SLOT(on_bytesWritten())));
} else {
connectedConns.push_back(connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(on_bytesWritten())));
}
connectedConns.push_back(
connect(socket, &QAbstractSocket::disconnected, this, [this]{
Debug() << prettyName() << " socket disconnected";
for (const auto & connection : connectedConns) {
QObject::disconnect(connection);
}
connectedConns.clear(); // be sure to empty the list out when we are done!
stopTimer(pingTimer); // kill the ping timer (method from TimersByNameMixin)
on_disconnected();
emit lostConnection(this);
// todo: put stuff to queue up a reconnect sometime later?
})
);
{ // set up the "pingTimer"
auto on_pingTimer = [this]{
if (Util::getTime() - lastGood > pingtime_ms)
// only call do_ping if we've been idle for longer than pingtime_ms
do_ping();
return true;
};
const int period_ms = pingtime_ms/* 1 minute */ / 2;
callOnTimerSoon(period_ms, pingTimer, on_pingTimer, true, Qt::TimerType::VeryCoarseTimer); // method inherited from TimersByNameMixin
}
}
void AbstractConnection::on_disconnected()
{
++nDisconnects;
}
void AbstractConnection::on_socketState(QAbstractSocket::SocketState s)
{
Debug() << prettyName() << " socket state: " << s;
switch (s) {
case QAbstractSocket::ConnectedState:
status = Connected;
break;
case QAbstractSocket::HostLookupState:
case QAbstractSocket::ConnectingState:
status = Connecting;
break;
case QAbstractSocket::UnconnectedState:
case QAbstractSocket::ClosingState:
default:
status = NotConnected;
break;
}
}
void AbstractConnection::on_bytesWritten()
{
Trace() << __FUNCTION__;
if (!writeBackLog.isEmpty() && status == Connected && socket) {
Debug() << prettyName() << " writeBackLog size: " << writeBackLog.length();
do_write();
}
}
void AbstractConnection::do_ping()
{
Debug() << __FUNCTION__ << " " << prettyName() << " stub ...";
}
void AbstractConnection::on_error(QAbstractSocket::SocketError err)
{
Warning() << prettyName() << ": error " << err << " (" << (lastSocketError = (socket ? socket->errorString() : "(null)")) << ")";
++nSocketErrors;
do_disconnect();
}
/// call this only from this object's thread
auto AbstractConnection::stats() const -> Stats
{
QVariantMap m;
m["name"] = objectName();
m["id"] = id;
m["connectedTime"] = isGood() ? QVariant(double(Util::getTime() - connectedTS)/1e3) : QVariant();
m["nBytesSent"] = nSent.load();
m["nBytesReceived"] = nReceived.load();
m["idleTime"] = isGood() ? QVariant(double(Util::getTime() - lastGood)/1e3) : QVariant();
m["lastSocketError"] = lastSocketError;
m["nDisconnects"] = nDisconnects.load();
m["nSocketErrors"] = nSocketErrors.load();
m["writeBackLog"] = writeBackLog.size();
m["readBytesAvailable"] = socket ? socket->bytesAvailable() : 0;
auto atl = activeTimers();
QVariantMap timerMap;
for (const auto & name : atl)
timerMap[name] = timerInterval(name);
m["activeTimers"] = timerMap;
m["remote"] = [this]() -> QVariant {
if (QString addr; socket && !(addr=socket->peerAddress().toString()).isNull()) {
return QString("%1:%2").arg(addr).arg(socket->peerPort());
}
return QVariant(); // null
}();
m["local"] = [this]() -> QVariant {
if (QString addr; socket && !(addr=socket->localAddress().toString()).isNull()) {
return QString("%1:%2").arg(addr).arg(socket->localPort());
}
return QVariant(); // null
}();
return m;
}