author | Josh Pelkey <jpelkey@gatech.edu> |
Fri, 13 May 2011 14:52:27 -0400 | |
changeset 7169 | 358f71a624d8 |
parent 6821 | 203367ae7433 |
child 7353 | 09fccf6195ea |
permissions | -rw-r--r-- |
6747
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
2 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
3 |
/* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
4 |
* Copyright (c) 2010 The Boeing Company |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
5 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
6 |
* This program is free software; you can redistribute it and/or modify |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
7 |
* it under the terms of the GNU General Public License version 2 as |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
8 |
* published by the Free Software Foundation; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
9 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
14 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
18 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
19 |
* Author: Tom Goff <thomas.goff@boeing.com> |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
20 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
21 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
22 |
#ifndef UNIX_FD_READER_H |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
23 |
#define UNIX_FD_READER_H |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
24 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
25 |
#include <stdint.h> |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
26 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
27 |
#include "ns3/callback.h" |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
28 |
#include "ns3/system-thread.h" |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
29 |
#include "ns3/event-id.h" |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
30 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
31 |
namespace ns3 { |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
32 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
33 |
/** |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
34 |
* \brief A class that asynchronously reads from a file descriptor. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
35 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
36 |
* This class can be used to start a system thread that reads from a |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
37 |
* given file descriptor and invokes a given callback when data is |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
38 |
* received. This class handles thread management automatically but |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
39 |
* the \p DoRead() method must be implemented by a subclass. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
40 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
41 |
class FdReader : public SimpleRefCount<FdReader> |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
42 |
{ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
43 |
public: |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
44 |
FdReader(); |
6749 | 45 |
virtual ~FdReader(); |
6747
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
46 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
47 |
/** |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
48 |
* Start a new read thread. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
49 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
50 |
* \param fd A valid file descriptor open for reading. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
51 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
52 |
* \param readCallback A callback to invoke when new data is |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
53 |
* available. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
54 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
55 |
void Start (int fd, Callback<void, uint8_t *, ssize_t> readCallback); |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
56 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
57 |
/** |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
58 |
* Stop the read thread and reset internal state. This does not |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
59 |
* close the file descriptor used for reading. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
60 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
61 |
void Stop (void); |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
62 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
63 |
protected: |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
64 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
65 |
/** |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
66 |
* \internal |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
67 |
* \brief A structure representing data read. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
68 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
69 |
struct Data |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
70 |
{ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
71 |
Data () : m_buf (0), m_len (0) {} |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
72 |
Data (uint8_t *buf, ssize_t len) : m_buf (buf), m_len (len) {} |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
73 |
uint8_t *m_buf; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
74 |
ssize_t m_len; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
75 |
}; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
76 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
77 |
/** |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
78 |
* \internal |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
79 |
* \brief The read implementation. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
80 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
81 |
* The value of \p m_len returned controls further processing. The |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
82 |
* callback function is only invoked when \p m_len is positive; any |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
83 |
* data read is not processed when \p m_len is negative; reading |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
84 |
* stops when \p m_len is zero. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
85 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
86 |
* The management of memory associated with \p m_buf must be |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
87 |
* compatible with the read callback. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
88 |
* |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
89 |
* \return A structure representing what was read. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
90 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
91 |
virtual FdReader::Data DoRead (void) = 0; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
92 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
93 |
/** |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
94 |
* \internal |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
95 |
* \brief The file descriptor to read from. |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
96 |
*/ |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
97 |
int m_fd; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
98 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
99 |
private: |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
100 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
101 |
void Run (void); |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
102 |
void DestroyEvent (void); |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
103 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
104 |
Callback<void, uint8_t *, ssize_t> m_readCallback; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
105 |
Ptr<SystemThread> m_readThread; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
106 |
int m_evpipe[2]; // pipe used to signal events between threads |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
107 |
bool m_stop; // true means the read thread should stop |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
108 |
EventId m_destroyEvent; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
109 |
}; |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
110 |
|
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
111 |
} // namespace ns3 |
9dccf3624839
bug 903: TapBridge doesn't close cleanly
Tom Goff <thomas.goff@boeing.com>
parents:
diff
changeset
|
112 |
|
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
113 |
#endif // UNIX_FD_READER_H |