src/core/model/unix-fd-reader.h
changeset 11097 caafe12b0378
parent 11045 6024c150e4c8
child 11131 6a448ac28669
equal deleted inserted replaced
11096:e57bfdb4f12c 11097:caafe12b0378
    25 
    25 
    26 #include "callback.h"
    26 #include "callback.h"
    27 #include "system-thread.h"
    27 #include "system-thread.h"
    28 #include "event-id.h"
    28 #include "event-id.h"
    29 
    29 
       
    30 /**
       
    31  * \ingroup system
       
    32  * \file
       
    33  * Asynchronous reads from a file descriptor, which trigger a Callback.
       
    34  */
       
    35 
    30 namespace ns3 {
    36 namespace ns3 {
    31 
    37 
    32 /**
    38 /**
       
    39  * \ingroup system
    33  * \brief A class that asynchronously reads from a file descriptor.
    40  * \brief A class that asynchronously reads from a file descriptor.
    34  *
    41  *
    35  * This class can be used to start a system thread that reads from a
    42  * This class can be used to start a system thread that reads from a
    36  * given file descriptor and invokes a given callback when data is
    43  * given file descriptor and invokes a given callback when data is
    37  * received.  This class handles thread management automatically but
    44  * received.  This class handles thread management automatically but
    38  * the \p DoRead() method must be implemented by a subclass.
    45  * the \p DoRead() method must be implemented by a subclass.
    39  */
    46  */
    40 class FdReader : public SimpleRefCount<FdReader>
    47 class FdReader : public SimpleRefCount<FdReader>
    41 {
    48 {
    42 public:
    49 public:
       
    50   /** Constructor. */
    43   FdReader();
    51   FdReader();
       
    52   /** Destructor. */
    44   virtual ~FdReader();
    53   virtual ~FdReader();
    45 
    54 
    46   /**
    55   /**
    47    * Start a new read thread.
    56    * Start a new read thread.
    48    *
    57    *
    64   /**
    73   /**
    65    * \brief A structure representing data read.
    74    * \brief A structure representing data read.
    66    */
    75    */
    67   struct Data
    76   struct Data
    68   {
    77   {
       
    78     /** Default constructor, with null buffer and zero length. */
    69     Data () : m_buf (0), m_len (0) {}
    79     Data () : m_buf (0), m_len (0) {}
       
    80     /**
       
    81      * Construct from a buffer of a given length.
       
    82      *
       
    83      * \param buf The buffer.
       
    84      * \param len The size of the buffer, in bytes.
       
    85      */
    70     Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {}
    86     Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {}
       
    87     /** The read data buffer. */
    71     uint8_t *m_buf;
    88     uint8_t *m_buf;
       
    89     /** The size of the read data buffer, in bytes. */
    72     ssize_t m_len;
    90     ssize_t m_len;
    73   };
    91   };
    74 
    92 
    75   /**
    93   /**
    76    * \brief The read implementation.
    94    * \brief The read implementation.
    92    */
   110    */
    93   int m_fd;
   111   int m_fd;
    94 
   112 
    95 private:
   113 private:
    96 
   114 
       
   115   /** The asynchronous function which performs the read. */
    97   void Run (void);
   116   void Run (void);
       
   117   /** Event handler scheduled for destroy time to halt the thread. */
    98   void DestroyEvent (void);
   118   void DestroyEvent (void);
    99 
   119 
       
   120   /** The main thread callback function to invoke when we have data. */
   100   Callback<void, uint8_t *, ssize_t> m_readCallback;
   121   Callback<void, uint8_t *, ssize_t> m_readCallback;
       
   122   
       
   123   /** The thread doing the read, created and launched by Start(). */
   101   Ptr<SystemThread> m_readThread;
   124   Ptr<SystemThread> m_readThread;
   102   int m_evpipe[2];           // pipe used to signal events between threads
   125 
   103   bool m_stop;               // true means the read thread should stop
   126   /** Pipe used to signal events between threads. */
       
   127   int m_evpipe[2];
       
   128   /** Signal the read thread to stop. */
       
   129   bool m_stop;
       
   130   
       
   131   /**
       
   132    * The event scheduled for destroy time which will invoke DestroyEvent
       
   133    * and halt the thread.
       
   134    */
   104   EventId m_destroyEvent;
   135   EventId m_destroyEvent;
   105 };
   136 };
   106 
   137 
   107 } // namespace ns3
   138 } // namespace ns3
   108 
   139