A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Tom Goff <thomas.goff@boeing.com>
7
*/
8
9
#ifndef FD_READER_H
10
#define FD_READER_H
11
12
#include "
callback.h
"
13
#include "
event-id.h
"
14
15
#include <cstdint>
16
#include <thread>
17
18
#ifdef __WIN32__
19
#include <BaseTsd.h>
20
21
/**
22
* Signed version of size_t
23
*/
24
typedef
SSIZE_T ssize_t;
25
#endif
26
27
/**
28
* \file
29
* \ingroup system
30
* ns3::FdReader declaration.
31
*/
32
33
namespace
ns3
34
{
35
36
/**
37
* \ingroup system
38
* \brief A class that asynchronously reads from a file descriptor.
39
*
40
* This class can be used to start a system thread that reads from a
41
* given file descriptor and invokes a given callback when data is
42
* received. This class handles thread management automatically but
43
* the \pname{DoRead()} method must be implemented by a subclass.
44
*/
45
class
FdReader
:
public
SimpleRefCount
<FdReader>
46
{
47
public
:
48
/** Constructor. */
49
FdReader
();
50
/** Destructor. */
51
virtual
~FdReader
();
52
53
/**
54
* Start a new read thread.
55
*
56
* \param [in] fd A valid file descriptor open for reading.
57
*
58
* \param [in] readCallback A callback to invoke when new data is
59
* available.
60
*/
61
void
Start
(
int
fd,
Callback<void, uint8_t*, ssize_t>
readCallback);
62
63
/**
64
* Stop the read thread and reset internal state. This does not
65
* close the file descriptor used for reading.
66
*/
67
void
Stop
();
68
69
#ifdef __WIN32__
70
/**
71
* Keeps track if the Winsock library has been initialized.
72
*/
73
static
bool
winsock_initialized;
74
#endif
75
protected
:
76
/**
77
* \brief A structure representing data read.
78
*/
79
struct
Data
80
{
81
/** Default constructor, with null buffer and zero length. */
82
Data
()
83
:
m_buf
(nullptr),
84
m_len
(0)
85
{
86
}
87
88
/**
89
* Construct from a buffer of a given length.
90
*
91
* \param [in] buf The buffer.
92
* \param [in] len The size of the buffer, in bytes.
93
*/
94
Data
(uint8_t* buf, ssize_t len)
95
:
m_buf
(buf),
96
m_len
(len)
97
{
98
}
99
100
/** The read data buffer. */
101
uint8_t*
m_buf
;
102
/** The size of the read data buffer, in bytes. */
103
ssize_t
m_len
;
104
};
105
106
/**
107
* \brief The read implementation.
108
*
109
* The value of \pname{m_len} returned controls further processing. The
110
* callback function is only invoked when \pname{m_len} is positive; any
111
* data read is not processed when \pname{m_len} is negative; reading
112
* stops when \pname{m_len} is zero.
113
*
114
* The management of memory associated with \pname{m_buf} must be
115
* compatible with the read callback.
116
*
117
* \return A structure representing what was read.
118
*/
119
virtual
FdReader::Data
DoRead
() = 0;
120
121
/**
122
* \brief The file descriptor to read from.
123
*/
124
int
m_fd
;
125
126
private
:
127
/** The asynchronous function which performs the read. */
128
void
Run
();
129
/** Event handler scheduled for destroy time to halt the thread. */
130
void
DestroyEvent
();
131
132
/** The main thread callback function to invoke when we have data. */
133
Callback<void, uint8_t*, ssize_t>
m_readCallback
;
134
135
/** The thread doing the read, created and launched by Start(). */
136
std::thread
m_readThread
;
137
138
/** Pipe used to signal events between threads. */
139
int
m_evpipe
[2];
140
/** Signal the read thread to stop. */
141
bool
m_stop
;
142
143
/**
144
* The event scheduled for destroy time which will invoke DestroyEvent
145
* and halt the thread.
146
*/
147
EventId
m_destroyEvent
;
148
};
149
150
}
// namespace ns3
151
152
#endif
/* FD_READER_H */
callback.h
Declaration of the various callback functions.
ns3::Callback
Callback template class.
Definition
callback.h:422
ns3::EventId
An identifier for simulation events.
Definition
event-id.h:45
ns3::FdReader
A class that asynchronously reads from a file descriptor.
Definition
fd-reader.h:46
ns3::FdReader::m_evpipe
int m_evpipe[2]
Pipe used to signal events between threads.
Definition
fd-reader.h:139
ns3::FdReader::FdReader
FdReader()
Constructor.
Definition
unix-fd-reader.cc:34
ns3::FdReader::Run
void Run()
The asynchronous function which performs the read.
Definition
unix-fd-reader.cc:154
ns3::FdReader::Stop
void Stop()
Stop the read thread and reset internal state.
Definition
unix-fd-reader.cc:110
ns3::FdReader::m_stop
bool m_stop
Signal the read thread to stop.
Definition
fd-reader.h:141
ns3::FdReader::m_destroyEvent
EventId m_destroyEvent
The event scheduled for destroy time which will invoke DestroyEvent and halt the thread.
Definition
fd-reader.h:147
ns3::FdReader::DoRead
virtual FdReader::Data DoRead()=0
The read implementation.
ns3::FdReader::Start
void Start(int fd, Callback< void, uint8_t *, ssize_t > readCallback)
Start a new read thread.
Definition
unix-fd-reader.cc:51
ns3::FdReader::DestroyEvent
void DestroyEvent()
Event handler scheduled for destroy time to halt the thread.
Definition
unix-fd-reader.cc:102
ns3::FdReader::m_readCallback
Callback< void, uint8_t *, ssize_t > m_readCallback
The main thread callback function to invoke when we have data.
Definition
fd-reader.h:133
ns3::FdReader::m_readThread
std::thread m_readThread
The thread doing the read, created and launched by Start().
Definition
fd-reader.h:136
ns3::FdReader::m_fd
int m_fd
The file descriptor to read from.
Definition
fd-reader.h:124
ns3::FdReader::~FdReader
virtual ~FdReader()
Destructor.
Definition
unix-fd-reader.cc:44
ns3::SimpleRefCount
A template-based reference counting class.
Definition
simple-ref-count.h:70
event-id.h
ns3::EventId declarations.
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::FdReader::Data
A structure representing data read.
Definition
fd-reader.h:80
ns3::FdReader::Data::m_len
ssize_t m_len
The size of the read data buffer, in bytes.
Definition
fd-reader.h:103
ns3::FdReader::Data::Data
Data(uint8_t *buf, ssize_t len)
Construct from a buffer of a given length.
Definition
fd-reader.h:94
ns3::FdReader::Data::Data
Data()
Default constructor, with null buffer and zero length.
Definition
fd-reader.h:82
ns3::FdReader::Data::m_buf
uint8_t * m_buf
The read data buffer.
Definition
fd-reader.h:101
src
core
model
fd-reader.h
Generated on Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0