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