-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathIoThread.h
132 lines (113 loc) · 4.08 KB
/
IoThread.h
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
//==============================================================================
//
// IoThread.h
//
// 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/>.
//
#ifndef IOTHREAD_H_INCLUDED
#define IOTHREAD_H_INCLUDED
#include "Thread.h"
#include <cstddef>
#include "NwTypes.h"
#include "SteadyTime.h"
#include "SysIpL3Addr.h"
#include "SysTypes.h"
//------------------------------------------------------------------------------
namespace NetworkBase
{
// For efficiency, the preferred I/O design is one in which messages destined
// for applications are "pushed" directly into an input handler, either from
// an interrupt service routine or the task that implements the IP stack.
// However, the standard practice in many platforms is to "pull" messages
// using recvfrom (or something similar). The recvfrom is performed by an
// I/O thread which then pushes messages into the appropriate input handler.
//
class IoThread : public NodeBase::Thread
{
friend class IpPort;
public:
//> The maximum receive buffer size for a socket (in bytes).
//
static const size_t MaxRxBuffSize;
//> The maximum transmit buffer size for a socket (in bytes).
//
static const size_t MaxTxBuffSize;
// Overridden to display member variables.
//
void Display(std::ostream& stream,
const std::string& prefix, const NodeBase::Flags& options) const override;
// Overridden for patching.
//
void Patch(sel_t selector, void* arguments) override;
protected:
// Creates an I/O thread, managed by DAEMON, that receives messages on
// PORT on behalf of SERVICE. Protected because this class is virtual.
//
IoThread(NodeBase::Daemon* daemon, const IpService* service, ipport_t port);
// Protected to restrict deletion. Virtual to allow subclassing.
//
virtual ~IoThread();
// Once a subclass has received a message and set txAddr_, rxAddr_, and
// ticks0_ accordingly, it invokes this to wrap the message and pass it to
// PORT's input handler. SOURCE and SIZE identify the message's location.
//
void InvokeHandler
(const IpPort& port, const NodeBase::byte_t* source, size_t size) const;
// Returns true after pausing when the thread has run locked for more
// than PERCENT of the maximum time allowed.
//
bool ConditionalPause(NodeBase::word percent);
// Overridden to survive warm restarts.
//
bool ExitOnRestart(NodeBase::RestartLevel level) const override;
// The port on which the thread receives messages.
//
const ipport_t port_;
// The IpPort registered against port_. Must be set by a subclass
// constructor.
//
IpPort* ipPort_;
// This element's address.
//
SysIpL2Addr self_;
// The number of messages received during the current work interval.
//
size_t recvs_;
// The (peer) address that sent the current incoming message.
//
SysIpL3Addr txAddr_;
// The address on which the current message arrived. When TCP
// is used, rxAddr_.socket_ identifies the connection's socket.
//
SysIpL3Addr rxAddr_;
// The time when the current message arrived.
//
NodeBase::SteadyTime::Point time_;
// The buffer for receiving messages.
//
NodeBase::byte_t* buffer_;
private:
// The size of the receive buffer for the socket bound against port_.
//
size_t rxSize_;
// The size of the transmit buffer for the socket bound against port_.
//
size_t txSize_;
};
}
#endif