-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathNwDaemons.cpp
344 lines (270 loc) · 9.61 KB
/
NwDaemons.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//==============================================================================
//
// NwDaemons.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC 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 Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "NwDaemons.h"
#include "Deferred.h"
#include <chrono>
#include <ostream>
#include <ratio>
#include <string>
#include "DaemonRegistry.h"
#include "Debug.h"
#include "Duration.h"
#include "Formatters.h"
#include "IpPortRegistry.h"
#include "Singleton.h"
#include "TcpIoThread.h"
#include "TcpIpService.h"
#include "UdpIoThread.h"
#include "UdpIpService.h"
using namespace NodeBase;
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NetworkBase
{
class IoThreadRecreator : public Deferred
{
public:
// Recreates an I/O thread for SERVICE and PORT, after TIMEOUT seconds,
// on behalf of DAEMON.
//
IoThreadRecreator(IoDaemon* daemon,
const IpService* service, ipport_t port, uint32_t timeout);
// Not subclassed.
//
~IoThreadRecreator();
// Overridden to display member variables.
//
void Display(ostream& stream,
const string& prefix, const Flags& options) const override;
// Overridden for patching.
//
void Patch(sel_t selector, void* arguments) override;
private:
// Creates the I/O thread.
//
void EventHasOccurred(Event event) override;
// The daemon for the I/O thread.
//
IoDaemon* const daemon_;
// The service for the I/O thread.
//
const IpService* const service_;
// The port for the I/O thread.
//
const ipport_t port_;
};
//------------------------------------------------------------------------------
IoThreadRecreator::IoThreadRecreator(IoDaemon* daemon,
const IpService* service, ipport_t port, uint32_t timeout) :
Deferred(*Singleton<IpPortRegistry>::Instance(), timeout, false),
daemon_(daemon),
service_(service),
port_(port)
{
Debug::ft("IoThreadRecreator.ctor");
}
//------------------------------------------------------------------------------
IoThreadRecreator::~IoThreadRecreator()
{
Debug::ftnt("IoThreadRecreator.dtor");
daemon_->RecreatorDeleted();
}
//------------------------------------------------------------------------------
void IoThreadRecreator::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Deferred::Display(stream, prefix, options);
stream << prefix << "daemon : " << strObj(daemon_) << CRLF;
stream << prefix << "service : " << strObj(service_) << CRLF;
stream << prefix << "port : " << port_ << CRLF;
}
//------------------------------------------------------------------------------
void IoThreadRecreator::EventHasOccurred(Event event)
{
Debug::ft("IoThreadRecreator.EventHasOccurred");
daemon_->CreateIoThread(service_, port_);
}
//------------------------------------------------------------------------------
void IoThreadRecreator::Patch(sel_t selector, void* arguments)
{
Deferred::Patch(selector, arguments);
}
//==============================================================================
IoDaemon::IoDaemon(c_string name, const IpService* service, ipport_t port) :
Daemon(name, 1, true),
service_(service),
port_(port),
lastCreation_(SteadyTime::Now()),
backoffSecs_(0),
recreator_(nullptr)
{
Debug::ft("IoDaemon.ctor");
}
//------------------------------------------------------------------------------
fn_name IoDaemon_CreateIoThread = "IoDaemon.CreateIoThread";
Thread* IoDaemon::CreateIoThread(const IpService* service, ipport_t port)
{
Debug::ft(IoDaemon_CreateIoThread);
Debug::SwLog(IoDaemon_CreateIoThread, strOver(this), service->Protocol());
return nullptr;
}
//------------------------------------------------------------------------------
constexpr uint32_t BackOffSecs = 4;
const msecs_t MinExitTime(4000);
Thread* IoDaemon::CreateThread()
{
Debug::ft("IoDaemon.CreateThread");
// If the thread was last created more than 4 seconds ago, create it
// immediately. Reset the backoff time to 4 seconds so that we will
// wait to recreate it if it exits quickly.
//
auto now = SteadyTime::Now();
if((now - lastCreation_) > MinExitTime)
{
backoffSecs_ = BackOffSecs;
lastCreation_ = now;
return CreateIoThread(service_, port_);
}
// The last thread exited quickly, so unless a work item already exists
// as the result of a previous thread also having exited, queue one to
// recreate the thread after the backoff time. If that thread also exits
// quickly, the backoff time will be doubled (but capped at 64 seconds).
//
if(recreator_ == nullptr)
{
recreator_ = new IoThreadRecreator(this, service_, port_, backoffSecs_);
if(backoffSecs_ < 64) backoffSecs_ <<= 1;
}
return nullptr;
}
//------------------------------------------------------------------------------
void IoDaemon::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Daemon::Display(stream, prefix, options);
stream << prefix << "service : " << strObj(service_) << CRLF;
stream << prefix << "port : " << port_ << CRLF;
stream << prefix << "backoffSecs : " << backoffSecs_ << CRLF;
}
//------------------------------------------------------------------------------
void IoDaemon::Patch(sel_t selector, void* arguments)
{
Daemon::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
void IoDaemon::RecreatorDeleted()
{
Debug::ft("IoDaemon.RecreatorDeleted");
recreator_ = nullptr;
}
//==============================================================================
fixed_string TcpIoDaemonName = "tcp";
//------------------------------------------------------------------------------
//
// Returns the name for the daemon that manages the TCP I/O thread on PORT.
//
static string MakeTcpName(ipport_t port)
{
Debug::ft("NetworkBase.MakeTcpName");
// A Daemon requires a unique name, so append the port number
// to the basic name.
//
string name(TcpIoDaemonName);
name.push_back('_');
name.append(std::to_string(port));
return name;
}
//------------------------------------------------------------------------------
TcpIoDaemon::TcpIoDaemon(const TcpIpService* service, ipport_t port) :
IoDaemon(MakeTcpName(port).c_str(), service, port)
{
Debug::ft("TcpIoDaemon.ctor");
}
//------------------------------------------------------------------------------
Thread* TcpIoDaemon::CreateIoThread(const IpService* service, ipport_t port)
{
Debug::ft("TcpIoDaemon.CreateIoThread");
return new TcpIoThread
(this, static_cast<const TcpIpService*>(service), port);
}
//------------------------------------------------------------------------------
TcpIoDaemon* TcpIoDaemon::GetDaemon(const TcpIpService* service, ipport_t port)
{
Debug::ft("TcpIoDaemon.GetDaemon");
auto reg = Singleton<DaemonRegistry>::Instance();
auto name = MakeTcpName(port);
auto daemon = static_cast<TcpIoDaemon*>(reg->FindDaemon(name.c_str()));
if(daemon != nullptr) return daemon;
return new TcpIoDaemon(service, port);
}
//------------------------------------------------------------------------------
void TcpIoDaemon::Patch(sel_t selector, void* arguments)
{
IoDaemon::Patch(selector, arguments);
}
//==============================================================================
fixed_string UdpIoDaemonName = "udp";
//------------------------------------------------------------------------------
//
// Returns the name for the daemon that manages the UDP I/O thread on PORT.
//
static string MakeUdpName(ipport_t port)
{
Debug::ft("NetworkBase.MakeUdpName");
// A Daemon requires a unique name, so append the port number
// to the basic name.
//
string name(UdpIoDaemonName);
name.push_back('_');
name.append(std::to_string(port));
return name;
}
//------------------------------------------------------------------------------
UdpIoDaemon::UdpIoDaemon(const UdpIpService* service, ipport_t port) :
IoDaemon(MakeUdpName(port).c_str(), service, port)
{
Debug::ft("UdpIoDaemon.ctor");
}
//------------------------------------------------------------------------------
Thread* UdpIoDaemon::CreateIoThread(const IpService* service, ipport_t port)
{
Debug::ft("UdpIoDaemon.CreateIoThread");
return new UdpIoThread
(this, static_cast<const UdpIpService*>(service), port);
}
//------------------------------------------------------------------------------
UdpIoDaemon* UdpIoDaemon::GetDaemon(const UdpIpService* service, ipport_t port)
{
Debug::ft("UdpIoDaemon.GetDaemon");
auto reg = Singleton<DaemonRegistry>::Instance();
auto name = MakeUdpName(port);
auto daemon = static_cast<UdpIoDaemon*>(reg->FindDaemon(name.c_str()));
if(daemon != nullptr) return daemon;
return new UdpIoDaemon(service, port);
}
//------------------------------------------------------------------------------
void UdpIoDaemon::Patch(sel_t selector, void* arguments)
{
IoDaemon::Patch(selector, arguments);
}
}