src/core/model/unix-fd-reader.h
author Vedran Miletić <rivanvx@gmail.com>
Tue, 02 Aug 2011 17:42:33 -0400
changeset 7385 10beb0e53130
parent 7383 c5e131450339
child 11045 6024c150e4c8
permissions -rw-r--r--
standardize emacs c++ mode comments
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
 * Copyright (c) 2010 The Boeing Company
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     4
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     5
 * 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
     6
 * 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
     7
 * published by the Free Software Foundation;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     8
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
     9
 * 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
    10
 * 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
    11
 * 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
    12
 * GNU General Public License for more details.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    13
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    14
 * 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
    15
 * 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
    16
 * 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
    17
 *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    18
 * Author: Tom Goff <thomas.goff@boeing.com>
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    19
 */
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
#ifndef UNIX_FD_READER_H
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    22
#define UNIX_FD_READER_H
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    23
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    24
#include <stdint.h>
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    25
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7353
diff changeset
    26
#include "callback.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7353
diff changeset
    27
#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: 7353
diff changeset
    28
#include "event-id.h"
6747
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    29
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    30
namespace ns3 {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    31
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
 * \brief A class that asynchronously reads from a file descriptor.
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
 * This class can be used to start a system thread that reads from a
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    36
 * given file descriptor and invokes a given callback when data is
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    37
 * received.  This class handles thread management automatically but
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    38
 * the \p DoRead() method must be implemented by a subclass.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    39
 */
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    40
class FdReader : public SimpleRefCount<FdReader>
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    41
{
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    42
public:
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    43
  FdReader();
6749
c8a649315434 make destructor virtual
Tom Henderson <tomh@tomh.org>
parents: 6747
diff changeset
    44
  virtual ~FdReader();
6747
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
   * Start a new read thread.
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
   * \param fd A valid file descriptor open for reading.
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
   * \param readCallback A callback to invoke when new data is
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    52
   * available.
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 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
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    56
  /**
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    57
   * Stop the read thread and reset internal state.  This does not
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    58
   * close the file descriptor used for reading.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    59
   */
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    60
  void Stop (void);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    61
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    62
protected:
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
   * \internal
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    66
   * \brief A structure representing data read.
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
  struct Data
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    69
  {
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    70
    Data () : m_buf (0), m_len (0) {}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    71
    Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {}
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    72
    uint8_t *m_buf;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    73
    ssize_t m_len;
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
  /**
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    77
   * \internal
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    78
   * \brief The read implementation.
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
   * The value of \p m_len returned controls further processing.  The
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    81
   * callback function is only invoked when \p m_len is positive; any
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    82
   * data read is not processed when \p m_len is negative; reading
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    83
   * stops when \p m_len is zero.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    84
   *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    85
   * The management of memory associated with \p m_buf must be
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    86
   * compatible with the read callback.
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    87
   *
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    88
   * \return A structure representing what was read.
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
  virtual FdReader::Data DoRead (void) = 0;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    91
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
   * \internal
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
    94
   * \brief The file descriptor to read from.
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
  int m_fd;
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
private:
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
  void Run (void);
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   101
  void DestroyEvent (void);
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
  Callback<void, uint8_t *, ssize_t> m_readCallback;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   104
  Ptr<SystemThread> m_readThread;
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   105
  int m_evpipe[2];           // pipe used to signal events between threads
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   106
  bool m_stop;               // true means the read thread should stop
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   107
  EventId m_destroyEvent;
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
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   110
} // namespace ns3
9dccf3624839 bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff changeset
   111
7353
09fccf6195ea bug 1203: Inconsistently named ifndef/define macros in ns-3 headers
Vedran Miletic <rivanvx@gmail.com>
parents: 7169
diff changeset
   112
#endif /* UNIX_FD_READER_H */