src/core/model/unix-fd-reader.cc
author Vedran Mileti? <rivanvx@gmail.com>
Sat, 01 Sep 2012 20:57:21 +0200
changeset 9063 32755d0516f4
parent 7431 17902e1844be
child 9134 7a750f032acd
permissions -rw-r--r--
Bug 1237 - code cleanups related to includes
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
{
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    43
  m_evpipe[0] = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    44
  m_evpipe[1] = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    45
}
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
FdReader::~FdReader ()
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    48
{
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    49
  Stop ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    50
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    51
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    52
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
    53
{
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    54
  int tmp;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    55
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    56
  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
    57
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    58
  // 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
    59
  tmp = pipe (m_evpipe);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    60
  if (tmp == -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    61
    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Mileti? <rivanvx@gmail.com>
parents: 7431
diff changeset
    62
      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
    63
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    64
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    65
  // 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
    66
  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
    67
  if (tmp == -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    68
    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Mileti? <rivanvx@gmail.com>
parents: 7431
diff changeset
    69
      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
    70
    }
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7169
diff changeset
    71
  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
    72
    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Mileti? <rivanvx@gmail.com>
parents: 7431
diff changeset
    73
      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
    74
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    75
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    76
  m_fd = fd;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    77
  m_readCallback = readCallback;
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
  //
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    80
  // 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
    81
  // 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
    82
  // 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
    83
  // proceeding.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    84
  //
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    85
  if (!m_destroyEvent.IsRunning ())
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    86
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    87
      // 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
    88
      // deallocated before the destroy-time event fires
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    89
      this->Ref ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    90
      m_destroyEvent =
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    91
        Simulator::ScheduleDestroy (&FdReader::DestroyEvent, this);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    92
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    93
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    94
  //
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    95
  // 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
    96
  //
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    97
  NS_LOG_LOGIC ("Spinning up read thread");
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    98
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    99
  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
   100
  m_readThread->Start ();
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
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   103
void FdReader::DestroyEvent (void)
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
  Stop ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   106
  this->Unref ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   107
}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   108
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   109
void FdReader::Stop (void)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   110
{
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   111
  m_stop = true;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   112
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   113
  // signal the read thread
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   114
  if (m_evpipe[1] != -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   115
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   116
      char zero = 0;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   117
      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
   118
      if (len != sizeof (zero))
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Mileti? <rivanvx@gmail.com>
parents: 7431
diff changeset
   119
        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
   120
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   121
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   122
  // join the read thread
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   123
  if (m_readThread != 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   124
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   125
      m_readThread->Join ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   126
      m_readThread = 0;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   127
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   128
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   129
  // 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
   130
  if (m_evpipe[1] != -1)
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   131
    {
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   132
      close (m_evpipe[1]);
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   133
      m_evpipe[1] = -1;
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   134
    }
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   135
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   136
  // 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
   137
  if (m_evpipe[0] != -1)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   138
    {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   139
      close (m_evpipe[0]);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   140
      m_evpipe[0] = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   141
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   142
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   143
  // reset everything else
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   144
  m_fd = -1;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   145
  m_readCallback.Nullify ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   146
  m_stop = false;
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
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   149
// This runs in a separate thread
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   150
void FdReader::Run (void)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   151
{
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   152
  int nfds;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   153
  fd_set rfds;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   154
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   155
  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
   156
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   157
  FD_ZERO (&rfds);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   158
  FD_SET (m_fd, &rfds);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   159
  FD_SET (m_evpipe[0], &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
  for (;;)
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
      int r;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   164
      fd_set readfds = rfds;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   165
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   166
      r = select (nfds, &readfds, NULL, NULL, NULL);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   167
      if (r == -1 && errno != EINTR)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   168
        {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Mileti? <rivanvx@gmail.com>
parents: 7431
diff changeset
   169
          NS_FATAL_ERROR ("select() failed: " << std::strerror (errno));
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   170
        }
6747
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
      if (FD_ISSET (m_evpipe[0], &readfds))
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   173
        {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   174
          // drain the event pipe
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   175
          for (;;)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   176
            {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   177
              char buf[1024];
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   178
              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
   179
              if (len == 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   180
                {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   181
                  NS_FATAL_ERROR ("event pipe closed");
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
              if (len < 0)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   184
                {
7431
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   185
                  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
   186
                    {
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   187
                      break;
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   188
                    }
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   189
                  else
17902e1844be bug 1220: FdReader always stops with NS_FATAL_ERROR
Tom Goff <tgoff@tgoff.net>
parents: 7385
diff changeset
   190
                    {
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Mileti? <rivanvx@gmail.com>
parents: 7431
diff changeset
   191
                      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
   192
                    }
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   193
                }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   194
            }
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   195
        }
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   196
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   197
      if (m_stop)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   198
        {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   199
          // this thread is done
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   200
          break;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   201
        }
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 (FD_ISSET (m_fd, &readfds))
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
          struct FdReader::Data data = DoRead ();
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   206
          // reading stops when m_len is zero
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   207
          if (data.m_len == 0)
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
              break;
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
          // 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
   212
          // is ignored if m_len is negative)
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   213
          else 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
              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
   216
            }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   217
        }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   218
    }
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   219
}
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
} // namespace ns3