A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
unix-fd-reader.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tom Goff <thomas.goff@boeing.com>
7 */
8
9#include "fatal-error.h"
10#include "fd-reader.h"
11#include "log.h"
12#include "simple-ref-count.h"
13#include "simulator.h"
14
15#include <cerrno>
16#include <cstring>
17#include <fcntl.h>
18#include <sys/select.h>
19#include <thread>
20#include <unistd.h> // close()
21
22/**
23 * @file
24 * @ingroup system
25 * ns3::FdReader implementation.
26 */
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("FdReader");
32
34 : m_fd(-1),
35 m_stop(false),
37{
38 NS_LOG_FUNCTION(this);
39 m_evpipe[0] = -1;
40 m_evpipe[1] = -1;
41}
42
44{
45 NS_LOG_FUNCTION(this);
46 Stop();
47}
48
49void
51{
52 NS_LOG_FUNCTION(this << fd << &readCallback);
53 int tmp;
54
55 NS_ASSERT_MSG(!m_readThread.joinable(), "read thread already exists");
56
57 // create a pipe for inter-thread event notification
58 tmp = pipe(m_evpipe);
59 if (tmp == -1)
60 {
61 NS_FATAL_ERROR("pipe() failed: " << std::strerror(errno));
62 }
63
64 // make the read end non-blocking
65 tmp = fcntl(m_evpipe[0], F_GETFL);
66 if (tmp == -1)
67 {
68 NS_FATAL_ERROR("fcntl() failed: " << std::strerror(errno));
69 }
70 if (fcntl(m_evpipe[0], F_SETFL, tmp | O_NONBLOCK) == -1)
71 {
72 NS_FATAL_ERROR("fcntl() failed: " << std::strerror(errno));
73 }
74
75 m_fd = fd;
76 m_readCallback = readCallback;
77
78 //
79 // We're going to spin up a thread soon, so we need to make sure we have
80 // a way to tear down that thread when the simulation stops. Do this by
81 // scheduling a "destroy time" method to make sure the thread exits before
82 // proceeding.
83 //
84 if (!m_destroyEvent.IsPending())
85 {
86 // hold a reference to ensure that this object is not
87 // deallocated before the destroy-time event fires
88 this->Ref();
90 }
91
92 //
93 // Now spin up a thread to read from the fd
94 //
95 NS_LOG_LOGIC("Spinning up read thread");
96
97 m_readThread = std::thread(&FdReader::Run, this);
98}
99
100void
102{
103 NS_LOG_FUNCTION(this);
104 Stop();
105 this->Unref();
106}
107
108void
110{
111 NS_LOG_FUNCTION(this);
112 m_stop = true;
113
114 // signal the read thread
115 if (m_evpipe[1] != -1)
116 {
117 char zero = 0;
118 ssize_t len = write(m_evpipe[1], &zero, sizeof(zero));
119 if (len != sizeof(zero))
120 {
121 NS_LOG_WARN("incomplete write(): " << std::strerror(errno));
122 }
123 }
124
125 // join the read thread
126 if (m_readThread.joinable())
127 {
128 m_readThread.join();
129 }
130
131 // close the write end of the event pipe
132 if (m_evpipe[1] != -1)
133 {
134 close(m_evpipe[1]);
135 m_evpipe[1] = -1;
136 }
137
138 // close the read end of the event pipe
139 if (m_evpipe[0] != -1)
140 {
141 close(m_evpipe[0]);
142 m_evpipe[0] = -1;
143 }
144
145 // reset everything else
146 m_fd = -1;
147 m_readCallback.Nullify();
148 m_stop = false;
149}
150
151// This runs in a separate thread
152void
154{
155 NS_LOG_FUNCTION(this);
156 int nfds;
157 fd_set rfds;
158
159 nfds = (m_fd > m_evpipe[0] ? m_fd : m_evpipe[0]) + 1;
160
161 FD_ZERO(&rfds);
162 FD_SET(m_fd, &rfds);
163 FD_SET(m_evpipe[0], &rfds);
164
165 for (;;)
166 {
167 int r;
168 fd_set readfds = rfds;
169
170 r = select(nfds, &readfds, nullptr, nullptr, nullptr);
171 if (r == -1 && errno != EINTR)
172 {
173 NS_FATAL_ERROR("select() failed: " << std::strerror(errno));
174 }
175
176 if (FD_ISSET(m_evpipe[0], &readfds))
177 {
178 // drain the event pipe
179 for (;;)
180 {
181 char buf[1024];
182 ssize_t len = read(m_evpipe[0], buf, sizeof(buf));
183 if (len == 0)
184 {
185 NS_FATAL_ERROR("event pipe closed");
186 }
187 if (len < 0)
188 {
189 if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
190 {
191 break;
192 }
193 else
194 {
195 NS_FATAL_ERROR("read() failed: " << std::strerror(errno));
196 }
197 }
198 }
199 }
200
201 if (m_stop)
202 {
203 // this thread is done
204 break;
205 }
206
207 if (FD_ISSET(m_fd, &readfds))
208 {
210 // reading stops when m_len is zero
211 if (data.m_len == 0)
212 {
213 break;
214 }
215 // the callback is only called when m_len is positive (data
216 // is ignored if m_len is negative)
217 else if (data.m_len > 0)
218 {
219 m_readCallback(data.m_buf, data.m_len);
220 }
221 }
222 }
223}
224
225} // namespace ns3
Callback template class.
Definition callback.h:422
int m_evpipe[2]
Pipe used to signal events between threads.
Definition fd-reader.h:139
FdReader()
Constructor.
void Run()
The asynchronous function which performs the read.
void Stop()
Stop the read thread and reset internal state.
bool m_stop
Signal the read thread to stop.
Definition fd-reader.h:141
EventId m_destroyEvent
The event scheduled for destroy time which will invoke DestroyEvent and halt the thread.
Definition fd-reader.h:147
virtual FdReader::Data DoRead()=0
The read implementation.
void Start(int fd, Callback< void, uint8_t *, ssize_t > readCallback)
Start a new read thread.
void DestroyEvent()
Event handler scheduled for destroy time to halt the thread.
Callback< void, uint8_t *, ssize_t > m_readCallback
The main thread callback function to invoke when we have data.
Definition fd-reader.h:133
std::thread m_readThread
The thread doing the read, created and launched by Start().
Definition fd-reader.h:136
int m_fd
The file descriptor to read from.
Definition fd-reader.h:124
virtual ~FdReader()
Destructor.
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition simulator.h:612
NS_FATAL_x macro definitions.
ns3::FdReader declaration.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SimpleRefCount declaration and template implementation.
ns3::Simulator declaration.
uint8_t data[writeSize]
A structure representing data read.
Definition fd-reader.h:80