src/core/model/unix-fd-reader.cc
author Maja Grubišić <maja.grubisic@live.com>
Sat, 10 Nov 2012 19:16:38 +0100
changeset 9134 7a750f032acd
parent 9063 32755d0516f4
child 10968 2d29fee2b7b8
permissions -rw-r--r--
Clean up function logging of core module.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7385
10beb0e53130 standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents: 7383
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     2
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     3
/*
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     4
 * Copyright (c) 2010 The Boeing Company
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     5
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     9
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    13
 * GNU General Public License for more details.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    14
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    18
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    19
 * Author: Tom Goff <thomas.goff@boeing.com>
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    20
 */
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    21
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
    22
#include <cerrno>
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
    23
#include <cstring>
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    24
#include <unistd.h>
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    25
#include <fcntl.h>
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    26
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7256
diff changeset
    27
#include "log.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7256
diff changeset
    28
#include "fatal-error.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7256
diff changeset
    29
#include "simple-ref-count.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7256
diff changeset
    30
#include "system-thread.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7256
diff changeset
    31
#include "simulator.h"
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    32
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    33
#include "unix-fd-reader.h"
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    34
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    35
NS_LOG_COMPONENT_DEFINE ("FdReader");
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    36
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    37
namespace ns3 {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    38
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    39
FdReader::FdReader ()
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    40
  : m_fd (-1), m_readCallback (0), m_readThread (0), m_stop (false),
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    41
    m_destroyEvent ()
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    42
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    43
  NS_LOG_FUNCTION (this);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    44
  m_evpipe[0] = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    45
  m_evpipe[1] = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    46
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    47
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    48
FdReader::~FdReader ()
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    49
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    50
  NS_LOG_FUNCTION (this);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    51
  Stop ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    52
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    53
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    54
void FdReader::Start (int fd, Callback<void, uint8_t *, ssize_t> readCallback)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    55
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    56
  NS_LOG_FUNCTION (this << fd << &readCallback);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    57
  int tmp;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    58
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    59
  NS_ASSERT_MSG (m_readThread == 0, "read thread already exists");
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    60
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    61
  // create a pipe for inter-thread event notification
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    62
  tmp = pipe (m_evpipe);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    63
  if (tmp == -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    64
    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
    65
      NS_FATAL_ERROR ("pipe() failed: " << std::strerror (errno));
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    66
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    67
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    68
  // make the read end non-blocking
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7169
diff changeset
    69
  tmp = fcntl (m_evpipe[0], F_GETFL);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    70
  if (tmp == -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    71
    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
    72
      NS_FATAL_ERROR ("fcntl() failed: " << std::strerror (errno));
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    73
    }
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7169
diff changeset
    74
  if (fcntl (m_evpipe[0], F_SETFL, tmp | O_NONBLOCK) == -1)
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    75
    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
    76
      NS_FATAL_ERROR ("fcntl() failed: " << std::strerror (errno));
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    77
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    78
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    79
  m_fd = fd;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    80
  m_readCallback = readCallback;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    81
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    82
  //
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    83
  // We're going to spin up a thread soon, so we need to make sure we have
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    84
  // a way to tear down that thread when the simulation stops.  Do this by
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    85
  // scheduling a "destroy time" method to make sure the thread exits before
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    86
  // proceeding.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    87
  //
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    88
  if (!m_destroyEvent.IsRunning ())
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    89
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    90
      // hold a reference to ensure that this object is not
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    91
      // deallocated before the destroy-time event fires
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    92
      this->Ref ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    93
      m_destroyEvent =
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    94
        Simulator::ScheduleDestroy (&FdReader::DestroyEvent, this);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    95
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    96
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    97
  //
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    98
  // Now spin up a thread to read from the fd
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    99
  //
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   100
  NS_LOG_LOGIC ("Spinning up read thread");
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   101
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   102
  m_readThread = Create<SystemThread> (MakeCallback (&FdReader::Run, this));
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   103
  m_readThread->Start ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   104
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   105
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   106
void FdReader::DestroyEvent (void)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   107
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   108
  NS_LOG_FUNCTION (this);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   109
  Stop ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   110
  this->Unref ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   111
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   112
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   113
void FdReader::Stop (void)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   114
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   115
  NS_LOG_FUNCTION (this);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   116
  m_stop = true;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   117
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   118
  // signal the read thread
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   119
  if (m_evpipe[1] != -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   120
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   121
      char zero = 0;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   122
      ssize_t len = write (m_evpipe[1], &zero, sizeof (zero));
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   123
      if (len != sizeof (zero))
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
   124
        NS_LOG_WARN ("incomplete write(): " << std::strerror (errno));
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   125
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   126
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   127
  // join the read thread
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   128
  if (m_readThread != 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   129
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   130
      m_readThread->Join ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   131
      m_readThread = 0;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   132
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   133
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   134
  // close the write end of the event pipe
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   135
  if (m_evpipe[1] != -1)
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   136
    {
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   137
      close (m_evpipe[1]);
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   138
      m_evpipe[1] = -1;
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   139
    }
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   140
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   141
  // close the read end of the event pipe
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   142
  if (m_evpipe[0] != -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   143
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   144
      close (m_evpipe[0]);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   145
      m_evpipe[0] = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   146
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   147
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   148
  // reset everything else
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   149
  m_fd = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   150
  m_readCallback.Nullify ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   151
  m_stop = false;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   152
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   153
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   154
// This runs in a separate thread
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   155
void FdReader::Run (void)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   156
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   157
  NS_LOG_FUNCTION (this);
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   158
  int nfds;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   159
  fd_set rfds;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   160
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   161
  nfds = (m_fd > m_evpipe[0] ? m_fd : m_evpipe[0]) + 1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   162
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   163
  FD_ZERO (&rfds);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   164
  FD_SET (m_fd, &rfds);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   165
  FD_SET (m_evpipe[0], &rfds);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   166
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   167
  for (;;)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   168
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   169
      int r;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   170
      fd_set readfds = rfds;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   171
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   172
      r = select (nfds, &readfds, NULL, NULL, NULL);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   173
      if (r == -1 && errno != EINTR)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   174
        {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
   175
          NS_FATAL_ERROR ("select() failed: " << std::strerror (errno));
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   176
        }
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   177
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   178
      if (FD_ISSET (m_evpipe[0], &readfds))
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   179
        {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   180
          // drain the event pipe
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   181
          for (;;)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   182
            {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   183
              char buf[1024];
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   184
              ssize_t len = read (m_evpipe[0], buf, sizeof (buf));
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   185
              if (len == 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   186
                {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   187
                  NS_FATAL_ERROR ("event pipe closed");
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   188
                }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   189
              if (len < 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   190
                {
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   191
                  if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   192
                    {
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   193
                      break;
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   194
                    }
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   195
                  else
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   196
                    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7431
diff changeset
   197
                      NS_FATAL_ERROR ("read() failed: " << std::strerror (errno));
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   198
                    }
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   199
                }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   200
            }
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   201
        }
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   202
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   203
      if (m_stop)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   204
        {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   205
          // this thread is done
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   206
          break;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   207
        }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   208
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   209
      if (FD_ISSET (m_fd, &readfds))
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   210
        {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   211
          struct FdReader::Data data = DoRead ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   212
          // reading stops when m_len is zero
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   213
          if (data.m_len == 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   214
            {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   215
              break;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   216
            }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   217
          // the callback is only called when m_len is positive (data
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   218
          // is ignored if m_len is negative)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   219
          else if (data.m_len > 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   220
            {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   221
              m_readCallback (data.m_buf, data.m_len);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   222
            }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   223
        }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   224
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   225
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   226
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   227
} // namespace ns3