OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 The Boeing Company |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: Tom Goff <thomas.goff@boeing.com> |
| 19 */ |
| 20 |
| 21 #ifndef FD_READER_H |
| 22 #define FD_READER_H |
| 23 |
| 24 #include <stdint.h> |
| 25 |
| 26 #include "callback.h" |
| 27 #include "system-thread.h" |
| 28 #include "event-id.h" |
| 29 |
| 30 #include "ns3/ns3-module.h" |
| 31 #define NS3_MODULE NS3_CORE_MODULE |
| 32 #include "ns3/ns3-export.h" |
| 33 |
| 34 /** |
| 35 * \file |
| 36 * \ingroup system |
| 37 * ns3::FdReader declaration. |
| 38 */ |
| 39 |
| 40 namespace ns3 { |
| 41 |
| 42 /** |
| 43 * \ingroup system |
| 44 * \brief A class that asynchronously reads from a file descriptor. |
| 45 * |
| 46 * This class can be used to start a system thread that reads from a |
| 47 * given file descriptor and invokes a given callback when data is |
| 48 * received. This class handles thread management automatically but |
| 49 * the \p DoRead() method must be implemented by a subclass. |
| 50 */ |
| 51 class NS3_EXPORT FdReader : public SimpleRefCount<FdReader> |
| 52 { |
| 53 public: |
| 54 /** Constructor. */ |
| 55 FdReader(); |
| 56 /** Destructor. */ |
| 57 virtual ~FdReader(); |
| 58 |
| 59 /** |
| 60 * Start a new read thread. |
| 61 * |
| 62 * \param [in] fd A valid file descriptor open for reading. |
| 63 * |
| 64 * \param [in] readCallback A callback to invoke when new data is |
| 65 * available. |
| 66 */ |
| 67 void Start (int fd, Callback<void, uint8_t *, ssize_t> readCallback); |
| 68 |
| 69 /** |
| 70 * Stop the read thread and reset internal state. This does not |
| 71 * close the file descriptor used for reading. |
| 72 */ |
| 73 void Stop (void); |
| 74 |
| 75 protected: |
| 76 |
| 77 /** |
| 78 * \brief A structure representing data read. |
| 79 */ |
| 80 struct Data |
| 81 { |
| 82 /** Default constructor, with null buffer and zero length. */ |
| 83 Data () : m_buf (0), m_len (0) {} |
| 84 /** |
| 85 * Construct from a buffer of a given length. |
| 86 * |
| 87 * \param [in] buf The buffer. |
| 88 * \param [in] len The size of the buffer, in bytes. |
| 89 */ |
| 90 Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {} |
| 91 /** The read data buffer. */ |
| 92 uint8_t *m_buf; |
| 93 /** The size of the read data buffer, in bytes. */ |
| 94 ssize_t m_len; |
| 95 }; |
| 96 |
| 97 /** |
| 98 * \brief The read implementation. |
| 99 * |
| 100 * The value of \p m_len returned controls further processing. The |
| 101 * callback function is only invoked when \p m_len is positive; any |
| 102 * data read is not processed when \p m_len is negative; reading |
| 103 * stops when \p m_len is zero. |
| 104 * |
| 105 * The management of memory associated with \p m_buf must be |
| 106 * compatible with the read callback. |
| 107 * |
| 108 * \return A structure representing what was read. |
| 109 */ |
| 110 virtual FdReader::Data DoRead (void) = 0; |
| 111 |
| 112 /** |
| 113 * \brief The file descriptor to read from. |
| 114 */ |
| 115 int m_fd; |
| 116 |
| 117 private: |
| 118 |
| 119 /** The asynchronous function which performs the read. */ |
| 120 void Run (void); |
| 121 /** Event handler scheduled for destroy time to halt the thread. */ |
| 122 void DestroyEvent (void); |
| 123 |
| 124 /** The main thread callback function to invoke when we have data. */ |
| 125 Callback<void, uint8_t *, ssize_t> m_readCallback; |
| 126 ·· |
| 127 /** The thread doing the read, created and launched by Start(). */ |
| 128 Ptr<SystemThread> m_readThread; |
| 129 |
| 130 /** Pipe used to signal events between threads. */ |
| 131 int m_evpipe[2]; |
| 132 /** Signal the read thread to stop. */ |
| 133 bool m_stop; |
| 134 ·· |
| 135 /** |
| 136 * The event scheduled for destroy time which will invoke DestroyEvent |
| 137 * and halt the thread. |
| 138 */ |
| 139 EventId m_destroyEvent; |
| 140 }; |
| 141 |
| 142 } // namespace ns3 |
| 143 |
| 144 #endif /* FD_READER_H */ |
OLD | NEW |