A Discrete-Event Network Simulator
API
unix-fd-reader.h
Go to the documentation of this file.
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 UNIX_FD_READER_H
22 #define UNIX_FD_READER_H
23 
24 #include <stdint.h>
25 
26 #include "callback.h"
27 #include "system-thread.h"
28 #include "event-id.h"
29 
36 namespace ns3 {
37 
47 class FdReader : public SimpleRefCount<FdReader>
48 {
49 public:
51  FdReader();
53  virtual ~FdReader();
54 
63  void Start (int fd, Callback<void, uint8_t *, ssize_t> readCallback);
64 
69  void Stop (void);
70 
71 protected:
72 
76  struct Data
77  {
79  Data () : m_buf (0), m_len (0) {}
86  Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {}
88  uint8_t *m_buf;
90  ssize_t m_len;
91  };
92 
106  virtual FdReader::Data DoRead (void) = 0;
107 
111  int m_fd;
112 
113 private:
114 
116  void Run (void);
118  void DestroyEvent (void);
119 
122 
125 
127  int m_evpipe[2];
129  bool m_stop;
130 
136 };
137 
138 } // namespace ns3
139 
140 #endif /* UNIX_FD_READER_H */
void Start(int fd, Callback< void, uint8_t *, ssize_t > readCallback)
Start a new read thread.
void DestroyEvent(void)
Event handler scheduled for destroy time to halt the thread.
A structure representing data read.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
virtual ~FdReader()
Destructor.
System-independent thread class ns3::SystemThread declaration.
Callback< void, uint8_t *, ssize_t > m_readCallback
The main thread callback function to invoke when we have data.
bool m_stop
Signal the read thread to stop.
void Run(void)
The asynchronous function which performs the read.
A class that asynchronously reads from a file descriptor.
Declaration of the various callback functions.
void Stop(void)
Stop the read thread and reset internal state.
Ptr< SystemThread > m_readThread
The thread doing the read, created and launched by Start().
int m_fd
The file descriptor to read from.
FdReader()
Constructor.
uint8_t * m_buf
The read data buffer.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
EventId m_destroyEvent
The event scheduled for destroy time which will invoke DestroyEvent and halt the thread.
Data(uint8_t *buf, ssize_t len)
Construct from a buffer of a given length.
Data()
Default constructor, with null buffer and zero length.
An identifier for simulation events.
Definition: event-id.h:53
virtual FdReader::Data DoRead(void)=0
The read implementation.
int m_evpipe[2]
Pipe used to signal events between threads.
ns3::EventId declarations.
A template-based reference counting class.
ssize_t m_len
The size of the read data buffer, in bytes.