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
35typedef SSIZE_T ssize_t;
36#endif
37
44namespace ns3
45{
46
56class FdReader : public SimpleRefCount<FdReader>
57{
58 public:
60 FdReader();
62 virtual ~FdReader();
63
72 void Start(int fd, Callback<void, uint8_t*, ssize_t> readCallback);
73
78 void Stop();
79
80#ifdef __WIN32__
84 static bool winsock_initialized;
85#endif
86 protected:
90 struct Data
91 {
94 : m_buf(nullptr),
95 m_len(0)
96 {
97 }
98
105 Data(uint8_t* buf, ssize_t len)
106 : m_buf(buf),
107 m_len(len)
108 {
109 }
110
112 uint8_t* m_buf;
114 ssize_t m_len;
115 };
116
130 virtual FdReader::Data DoRead() = 0;
131
135 int m_fd;
136
137 private:
139 void Run();
141 void DestroyEvent();
142
145
147 std::thread m_readThread;
148
150 int m_evpipe[2];
152 bool m_stop;
153
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