2010-12-30 14:19:48 -08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010 The Boeing Company
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* 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; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Tom Goff <thomas.goff@boeing.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-08-02 01:59:36 -07:00
|
|
|
#include "fatal-error.h"
|
2022-10-08 21:13:26 -03:00
|
|
|
#include "fd-reader.h"
|
2022-10-07 20:08:35 +00:00
|
|
|
#include "log.h"
|
2011-08-02 01:59:36 -07:00
|
|
|
#include "simple-ref-count.h"
|
|
|
|
|
#include "simulator.h"
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
#include <cerrno>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <unistd.h> // close()
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2014-12-18 15:12:35 -08:00
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
* \ingroup system
|
|
|
|
|
* ns3::FdReader implementation.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
namespace ns3
|
|
|
|
|
{
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_COMPONENT_DEFINE("FdReader");
|
2014-09-26 15:51:00 -07:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
FdReader::FdReader()
|
|
|
|
|
: m_fd(-1),
|
|
|
|
|
m_stop(false),
|
|
|
|
|
m_destroyEvent()
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
m_evpipe[0] = -1;
|
|
|
|
|
m_evpipe[1] = -1;
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
FdReader::~FdReader()
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
Stop();
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
FdReader::Start(int fd, Callback<void, uint8_t*, ssize_t> readCallback)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this << fd << &readCallback);
|
|
|
|
|
int tmp;
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_ASSERT_MSG(!m_readThread.joinable(), "read thread already exists");
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// create a pipe for inter-thread event notification
|
|
|
|
|
tmp = pipe(m_evpipe);
|
|
|
|
|
if (tmp == -1)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_FATAL_ERROR("pipe() failed: " << std::strerror(errno));
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// make the read end non-blocking
|
|
|
|
|
tmp = fcntl(m_evpipe[0], F_GETFL);
|
|
|
|
|
if (tmp == -1)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_FATAL_ERROR("fcntl() failed: " << std::strerror(errno));
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
if (fcntl(m_evpipe[0], F_SETFL, tmp | O_NONBLOCK) == -1)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_FATAL_ERROR("fcntl() failed: " << std::strerror(errno));
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
m_fd = fd;
|
|
|
|
|
m_readCallback = readCallback;
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
//
|
|
|
|
|
// We're going to spin up a thread soon, so we need to make sure we have
|
|
|
|
|
// a way to tear down that thread when the simulation stops. Do this by
|
|
|
|
|
// scheduling a "destroy time" method to make sure the thread exits before
|
|
|
|
|
// proceeding.
|
|
|
|
|
//
|
|
|
|
|
if (!m_destroyEvent.IsRunning())
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
// hold a reference to ensure that this object is not
|
|
|
|
|
// deallocated before the destroy-time event fires
|
|
|
|
|
this->Ref();
|
|
|
|
|
m_destroyEvent = Simulator::ScheduleDestroy(&FdReader::DestroyEvent, this);
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
//
|
|
|
|
|
// Now spin up a thread to read from the fd
|
|
|
|
|
//
|
|
|
|
|
NS_LOG_LOGIC("Spinning up read thread");
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
m_readThread = std::thread(&FdReader::Run, this);
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
FdReader::DestroyEvent()
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
Stop();
|
|
|
|
|
this->Unref();
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
FdReader::Stop()
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
m_stop = true;
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// signal the read thread
|
|
|
|
|
if (m_evpipe[1] != -1)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
char zero = 0;
|
|
|
|
|
ssize_t len = write(m_evpipe[1], &zero, sizeof(zero));
|
|
|
|
|
if (len != sizeof(zero))
|
2020-03-25 15:12:18 -07:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_WARN("incomplete write(): " << std::strerror(errno));
|
2020-03-25 15:12:18 -07:00
|
|
|
}
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// join the read thread
|
|
|
|
|
if (m_readThread.joinable())
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
m_readThread.join();
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// close the write end of the event pipe
|
|
|
|
|
if (m_evpipe[1] != -1)
|
2011-08-16 10:54:12 -07:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
close(m_evpipe[1]);
|
|
|
|
|
m_evpipe[1] = -1;
|
2011-08-16 10:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// close the read end of the event pipe
|
|
|
|
|
if (m_evpipe[0] != -1)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
close(m_evpipe[0]);
|
|
|
|
|
m_evpipe[0] = -1;
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
// reset everything else
|
|
|
|
|
m_fd = -1;
|
|
|
|
|
m_readCallback.Nullify();
|
|
|
|
|
m_stop = false;
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This runs in a separate thread
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
FdReader::Run()
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
int nfds;
|
|
|
|
|
fd_set rfds;
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
nfds = (m_fd > m_evpipe[0] ? m_fd : m_evpipe[0]) + 1;
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
|
FD_SET(m_fd, &rfds);
|
|
|
|
|
FD_SET(m_evpipe[0], &rfds);
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
for (;;)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
int r;
|
|
|
|
|
fd_set readfds = rfds;
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
r = select(nfds, &readfds, nullptr, nullptr, nullptr);
|
|
|
|
|
if (r == -1 && errno != EINTR)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_FATAL_ERROR("select() failed: " << std::strerror(errno));
|
2011-05-13 14:52:27 -04:00
|
|
|
}
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
if (FD_ISSET(m_evpipe[0], &readfds))
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
// drain the event pipe
|
|
|
|
|
for (;;)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
char buf[1024];
|
|
|
|
|
ssize_t len = read(m_evpipe[0], buf, sizeof(buf));
|
|
|
|
|
if (len == 0)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_FATAL_ERROR("event pipe closed");
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
if (len < 0)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
|
2011-08-16 10:54:12 -07:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
break;
|
2011-08-16 10:54:12 -07:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
else
|
2011-08-16 10:54:12 -07:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_FATAL_ERROR("read() failed: " << std::strerror(errno));
|
2011-08-16 10:54:12 -07:00
|
|
|
}
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
}
|
2011-05-13 14:52:27 -04:00
|
|
|
}
|
2010-12-30 14:19:48 -08:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
if (m_stop)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
// this thread is done
|
|
|
|
|
break;
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
if (FD_ISSET(m_fd, &readfds))
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
struct FdReader::Data data = DoRead();
|
|
|
|
|
// reading stops when m_len is zero
|
|
|
|
|
if (data.m_len == 0)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
break;
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
// the callback is only called when m_len is positive (data
|
|
|
|
|
// is ignored if m_len is negative)
|
|
|
|
|
else if (data.m_len > 0)
|
2010-12-30 14:19:48 -08:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
m_readCallback(data.m_buf, data.m_len);
|
2010-12-30 14:19:48 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ns3
|