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 #include <thread>
26 
27 #include "callback.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)
80  {}
87  Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len)
88  {}
90  uint8_t *m_buf;
92  ssize_t m_len;
93  };
94 
108  virtual FdReader::Data DoRead (void) = 0;
109 
113  int m_fd;
114 
115 private:
116 
118  void Run (void);
120  void DestroyEvent (void);
121 
124 
126  std::thread m_readThread;
127 
129  int m_evpipe[2];
131  bool m_stop;
132 
138 };
139 
140 } // namespace ns3
141 
142 #endif /* UNIX_FD_READER_H */
Declaration of the various callback functions.
An identifier for simulation events.
Definition: event-id.h:54
A class that asynchronously reads from a file descriptor.
int m_evpipe[2]
Pipe used to signal events between threads.
void Stop(void)
Stop the read thread and reset internal state.
FdReader()
Constructor.
bool m_stop
Signal the read thread to stop.
virtual FdReader::Data DoRead(void)=0
The read implementation.
void DestroyEvent(void)
Event handler scheduled for destroy time to halt the thread.
EventId m_destroyEvent
The event scheduled for destroy time which will invoke DestroyEvent and halt the thread.
void Start(int fd, Callback< void, uint8_t *, ssize_t > readCallback)
Start a new read thread.
void Run(void)
The asynchronous function which performs the read.
std::thread m_readThread
The thread doing the read, created and launched by Start().
int m_fd
The file descriptor to read from.
virtual ~FdReader()
Destructor.
Callback< void, uint8_t *, ssize_t > m_readCallback
The main thread callback function to invoke when we have data.
A template-based reference counting class.
ns3::EventId declarations.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
A structure representing data read.
ssize_t m_len
The size of the read data buffer, in bytes.
Data(uint8_t *buf, ssize_t len)
Construct from a buffer of a given length.
Data()
Default constructor, with null buffer and zero length.
uint8_t * m_buf
The read data buffer.