A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
30 namespace ns3 {
31 
40 class FdReader : public SimpleRefCount<FdReader>
41 {
42 public:
43  FdReader();
44  virtual ~FdReader();
45 
54  void Start (int fd, Callback<void, uint8_t *, ssize_t> readCallback);
55 
60  void Stop (void);
61 
62 protected:
63 
68  struct Data
69  {
70  Data () : m_buf (0), m_len (0) {}
71  Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {}
72  uint8_t *m_buf;
73  ssize_t m_len;
74  };
75 
90  virtual FdReader::Data DoRead (void) = 0;
91 
96  int m_fd;
97 
98 private:
99 
100  void Run (void);
101  void DestroyEvent (void);
102 
105  int m_evpipe[2]; // pipe used to signal events between threads
106  bool m_stop; // true means the read thread should stop
108 };
109 
110 } // namespace ns3
111 
112 #endif /* UNIX_FD_READER_H */
void Start(int fd, Callback< void, uint8_t *, ssize_t > readCallback)
Start a new read thread.
void DestroyEvent(void)
A structure representing data read.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
virtual ~FdReader()
Callback< void, uint8_t *, ssize_t > m_readCallback
void Run(void)
A class that asynchronously reads from a file descriptor.
void Stop(void)
Stop the read thread and reset internal state.
Ptr< SystemThread > m_readThread
int m_fd
The file descriptor to read from.
EventId m_destroyEvent
Data(uint8_t *buf, ssize_t len)
an identifier for simulation events.
Definition: event-id.h:46
virtual FdReader::Data DoRead(void)=0
The read implementation.
A template-based reference counting class.