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 * 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 */
24typedef SSIZE_T ssize_t;
25#endif
26
27/**
28 * \file
29 * \ingroup system
30 * ns3::FdReader declaration.
31 */
32
33namespace 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 */
45class 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. */
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. */
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 */
148};
149
150} // namespace ns3
151
152#endif /* FD_READER_H */
Declaration of the various callback functions.
Callback template class.
Definition callback.h:422
An identifier for simulation events.
Definition event-id.h:45
A class that asynchronously reads from a file descriptor.
Definition fd-reader.h:46
int m_evpipe[2]
Pipe used to signal events between threads.
Definition fd-reader.h:139
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:141
EventId m_destroyEvent
The event scheduled for destroy time which will invoke DestroyEvent and halt the thread.
Definition fd-reader.h:147
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:133
std::thread m_readThread
The thread doing the read, created and launched by Start().
Definition fd-reader.h:136
int m_fd
The file descriptor to read from.
Definition fd-reader.h:124
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:80
ssize_t m_len
The size of the read data buffer, in bytes.
Definition fd-reader.h:103
Data(uint8_t *buf, ssize_t len)
Construct from a buffer of a given length.
Definition fd-reader.h:94
Data()
Default constructor, with null buffer and zero length.
Definition fd-reader.h:82
uint8_t * m_buf
The read data buffer.
Definition fd-reader.h:101