@@ -16,39 +16,37 @@ namespace node {
16
16
// Forward declarations
17
17
class StreamBase ;
18
18
19
- template < class Req >
19
+ template < typename Base >
20
20
class StreamReq {
21
21
public:
22
- typedef void (*DoneCb)(Req* req, int status);
23
-
24
- explicit StreamReq (DoneCb cb) : cb_(cb) {
22
+ explicit StreamReq (StreamBase* stream) : stream_(stream) {
25
23
}
26
24
27
25
inline void Done (int status, const char * error_str = nullptr ) {
28
- Req * req = static_cast <Req *>(this );
26
+ Base * req = static_cast <Base *>(this );
29
27
Environment* env = req->env ();
30
28
if (error_str != nullptr ) {
31
29
req->object ()->Set (env->error_string (),
32
30
OneByteString (env->isolate (), error_str));
33
31
}
34
32
35
- cb_ ( req, status);
33
+ req-> OnDone ( status);
36
34
}
37
35
36
+ inline StreamBase* stream () const { return stream_; }
37
+
38
38
private:
39
- DoneCb cb_ ;
39
+ StreamBase* const stream_ ;
40
40
};
41
41
42
42
class ShutdownWrap : public ReqWrap <uv_shutdown_t >,
43
43
public StreamReq<ShutdownWrap> {
44
44
public:
45
45
ShutdownWrap (Environment* env,
46
46
v8::Local<v8::Object> req_wrap_obj,
47
- StreamBase* wrap,
48
- DoneCb cb)
47
+ StreamBase* stream)
49
48
: ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_SHUTDOWNWRAP),
50
- StreamReq<ShutdownWrap>(cb),
51
- wrap_ (wrap) {
49
+ StreamReq<ShutdownWrap>(stream) {
52
50
Wrap (req_wrap_obj, this );
53
51
}
54
52
@@ -60,27 +58,22 @@ class ShutdownWrap : public ReqWrap<uv_shutdown_t>,
60
58
return ContainerOf (&ShutdownWrap::req_, req);
61
59
}
62
60
63
- inline StreamBase* wrap () const { return wrap_; }
64
61
size_t self_size () const override { return sizeof (*this ); }
65
62
66
- private:
67
- StreamBase* const wrap_;
63
+ inline void OnDone (int status); // Just calls stream()->AfterShutdown()
68
64
};
69
65
70
- class WriteWrap : public ReqWrap <uv_write_t >,
71
- public StreamReq<WriteWrap> {
66
+ class WriteWrap : public ReqWrap <uv_write_t >,
67
+ public StreamReq<WriteWrap> {
72
68
public:
73
69
static inline WriteWrap* New (Environment* env,
74
70
v8::Local<v8::Object> obj,
75
- StreamBase* wrap,
76
- DoneCb cb,
71
+ StreamBase* stream,
77
72
size_t extra = 0 );
78
73
inline void Dispose ();
79
74
inline char * Extra (size_t offset = 0 );
80
75
inline size_t ExtraSize () const ;
81
76
82
- inline StreamBase* wrap () const { return wrap_; }
83
-
84
77
size_t self_size () const override { return storage_size_; }
85
78
86
79
static WriteWrap* from_req (uv_write_t * req) {
@@ -91,24 +84,22 @@ class WriteWrap: public ReqWrap<uv_write_t>,
91
84
92
85
WriteWrap (Environment* env,
93
86
v8::Local<v8::Object> obj,
94
- StreamBase* wrap,
95
- DoneCb cb)
87
+ StreamBase* stream)
96
88
: ReqWrap(env, obj, AsyncWrap::PROVIDER_WRITEWRAP),
97
- StreamReq<WriteWrap>(cb),
98
- wrap_ (wrap),
89
+ StreamReq<WriteWrap>(stream),
99
90
storage_size_ (0 ) {
100
91
Wrap (obj, this );
101
92
}
102
93
94
+ inline void OnDone (int status); // Just calls stream()->AfterWrite()
95
+
103
96
protected:
104
97
WriteWrap (Environment* env,
105
98
v8::Local<v8::Object> obj,
106
- StreamBase* wrap,
107
- DoneCb cb,
99
+ StreamBase* stream,
108
100
size_t storage_size)
109
101
: ReqWrap(env, obj, AsyncWrap::PROVIDER_WRITEWRAP),
110
- StreamReq<WriteWrap>(cb),
111
- wrap_(wrap),
102
+ StreamReq<WriteWrap>(stream),
112
103
storage_size_(storage_size) {
113
104
Wrap (obj, this );
114
105
}
@@ -129,7 +120,6 @@ class WriteWrap: public ReqWrap<uv_write_t>,
129
120
// WriteWrap. Ensure this never happens.
130
121
void operator delete (void * ptr) { UNREACHABLE (); }
131
122
132
- StreamBase* const wrap_;
133
123
const size_t storage_size_;
134
124
};
135
125
@@ -151,7 +141,7 @@ class StreamResource {
151
141
void * ctx;
152
142
};
153
143
154
- typedef void (*AfterWriteCb)(WriteWrap* w, void * ctx);
144
+ typedef void (*AfterWriteCb)(WriteWrap* w, int status, void * ctx);
155
145
typedef void (*AllocCb)(size_t size, uv_buf_t * buf, void * ctx);
156
146
typedef void (*ReadCb)(ssize_t nread,
157
147
const uv_buf_t * buf,
@@ -176,9 +166,9 @@ class StreamResource {
176
166
virtual void ClearError ();
177
167
178
168
// Events
179
- inline void OnAfterWrite (WriteWrap* w) {
169
+ inline void OnAfterWrite (WriteWrap* w, int status ) {
180
170
if (!after_write_cb_.is_empty ())
181
- after_write_cb_.fn (w, after_write_cb_.ctx );
171
+ after_write_cb_.fn (w, status, after_write_cb_.ctx );
182
172
}
183
173
184
174
inline void OnAlloc (size_t size, uv_buf_t * buf) {
@@ -208,14 +198,12 @@ class StreamResource {
208
198
inline Callback<ReadCb> read_cb () { return read_cb_; }
209
199
inline Callback<DestructCb> destruct_cb () { return destruct_cb_; }
210
200
211
- private :
201
+ protected :
212
202
Callback<AfterWriteCb> after_write_cb_;
213
203
Callback<AllocCb> alloc_cb_;
214
204
Callback<ReadCb> read_cb_;
215
205
Callback<DestructCb> destruct_cb_;
216
206
uint64_t bytes_read_;
217
-
218
- friend class StreamBase ;
219
207
};
220
208
221
209
class StreamBase : public StreamResource {
@@ -253,6 +241,10 @@ class StreamBase : public StreamResource {
253
241
v8::Local<v8::Object> buf,
254
242
v8::Local<v8::Object> handle);
255
243
244
+ // These are called by the respective {Write,Shutdown}Wrap class.
245
+ virtual void AfterShutdown (ShutdownWrap* req, int status);
246
+ virtual void AfterWrite (WriteWrap* req, int status);
247
+
256
248
protected:
257
249
explicit StreamBase (Environment* env) : env_(env), consumed_(false ) {
258
250
}
@@ -263,10 +255,6 @@ class StreamBase : public StreamResource {
263
255
virtual AsyncWrap* GetAsyncWrap () = 0;
264
256
virtual v8::Local<v8::Object> GetObject ();
265
257
266
- // Libuv callbacks
267
- static void AfterShutdown (ShutdownWrap* req, int status);
268
- static void AfterWrite (WriteWrap* req, int status);
269
-
270
258
// JS Methods
271
259
int ReadStart (const v8::FunctionCallbackInfo<v8::Value>& args);
272
260
int ReadStop (const v8::FunctionCallbackInfo<v8::Value>& args);
0 commit comments