Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 326 lines (230 sloc) 7.49 kb
1ae1489 @ry Abstract StreamWrap from TCPWrap
ry authored
1 #include <node.h>
2 #include <node_buffer.h>
0c7bf81 @ry Abstract out HandleWrap class
ry authored
3 #include <handle_wrap.h>
1ae1489 @ry Abstract StreamWrap from TCPWrap
ry authored
4 #include <stream_wrap.h>
5 #include <req_wrap.h>
6
7
8 namespace node {
9
10
11 #define SLAB_SIZE (1024 * 1024)
12 #define MIN(a, b) ((a) < (b) ? (a) : (b))
13
14
15 using v8::Object;
16 using v8::Handle;
17 using v8::Local;
18 using v8::Persistent;
19 using v8::Value;
20 using v8::HandleScope;
21 using v8::FunctionTemplate;
22 using v8::String;
23 using v8::Function;
24 using v8::TryCatch;
25 using v8::Context;
26 using v8::Arguments;
27 using v8::Integer;
28
29
30 #define UNWRAP \
31 assert(!args.Holder().IsEmpty()); \
32 assert(args.Holder()->InternalFieldCount() > 0); \
33 StreamWrap* wrap = \
34 static_cast<StreamWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
35 if (!wrap) { \
36 SetErrno(UV_EBADF); \
37 return scope.Close(Integer::New(-1)); \
38 }
39
40
41 typedef class ReqWrap<uv_shutdown_t> ShutdownWrap;
42 typedef class ReqWrap<uv_write_t> WriteWrap;
43
44
45 static size_t slab_used;
46 static uv_stream_t* handle_that_last_alloced;
0c7bf81 @ry Abstract out HandleWrap class
ry authored
47 static Persistent<String> slab_sym;
48 static Persistent<String> buffer_sym;
49 static Persistent<String> write_queue_size_sym;
50 static bool initialized;
1ae1489 @ry Abstract StreamWrap from TCPWrap
ry authored
51
52
53 void StreamWrap::Initialize(Handle<Object> target) {
54 if (initialized) {
55 return;
56 } else {
57 initialized = true;
58 }
59
60 HandleScope scope;
61
0c7bf81 @ry Abstract out HandleWrap class
ry authored
62 HandleWrap::Initialize(target);
63
1ae1489 @ry Abstract StreamWrap from TCPWrap
ry authored
64 slab_sym = Persistent<String>::New(String::NewSymbol("slab"));
65 buffer_sym = Persistent<String>::New(String::NewSymbol("buffer"));
66 write_queue_size_sym =
67 Persistent<String>::New(String::NewSymbol("writeQueueSize"));
68 }
69
70
0c7bf81 @ry Abstract out HandleWrap class
ry authored
71 StreamWrap::StreamWrap(Handle<Object> object, uv_stream_t* stream)
72 : HandleWrap(object, (uv_handle_t*)stream) {
1ae1489 @ry Abstract StreamWrap from TCPWrap
ry authored
73 stream_ = stream;
74 stream->data = this;
75 }
76
77
78 void StreamWrap::UpdateWriteQueueSize() {
6050af4 @ry net_uv: properly initialize writeQueueSize
ry authored
79 HandleScope scope;
1ae1489 @ry Abstract StreamWrap from TCPWrap
ry authored
80 object_->Set(write_queue_size_sym, Integer::New(stream_->write_queue_size));
81 }
82
83
84 Handle<Value> StreamWrap::ReadStart(const Arguments& args) {
85 HandleScope scope;
86
87 UNWRAP
88
89 int r = uv_read_start(wrap->stream_, OnAlloc, OnRead);
90
91 // Error starting the tcp.
92 if (r) SetErrno(uv_last_error().code);
93
94 return scope.Close(Integer::New(r));
95 }
96
97
98 Handle<Value> StreamWrap::ReadStop(const Arguments& args) {
99 HandleScope scope;
100
101 UNWRAP
102
103 int r = uv_read_stop(wrap->stream_);
104
105 // Error starting the tcp.
106 if (r) SetErrno(uv_last_error().code);
107
108 return scope.Close(Integer::New(r));
109 }
110
111
112 inline char* StreamWrap::NewSlab(Handle<Object> global,
113 Handle<Object> wrap_obj) {
114 Buffer* b = Buffer::New(SLAB_SIZE);
115 global->SetHiddenValue(slab_sym, b->handle_);
116 assert(Buffer::Length(b) == SLAB_SIZE);
117 slab_used = 0;
118 wrap_obj->SetHiddenValue(slab_sym, b->handle_);
119 return Buffer::Data(b);
120 }
121
122
123 uv_buf_t StreamWrap::OnAlloc(uv_stream_t* handle, size_t suggested_size) {
124 HandleScope scope;
125
126 StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
127 assert(wrap->stream_ == handle);
128
129 char* slab = NULL;
130
131 Handle<Object> global = Context::GetCurrent()->Global();
132 Local<Value> slab_v = global->GetHiddenValue(slab_sym);
133
134 if (slab_v.IsEmpty()) {
135 // No slab currently. Create a new one.
136 slab = NewSlab(global, wrap->object_);
137 } else {
138 // Use existing slab.
139 Local<Object> slab_obj = slab_v->ToObject();
140 slab = Buffer::Data(slab_obj);
141 assert(Buffer::Length(slab_obj) == SLAB_SIZE);
142 assert(SLAB_SIZE >= slab_used);
143
144 // If less than 64kb is remaining on the slab allocate a new one.
145 if (SLAB_SIZE - slab_used < 64 * 1024) {
146 slab = NewSlab(global, wrap->object_);
147 } else {
148 wrap->object_->SetHiddenValue(slab_sym, slab_obj);
149 }
150 }
151
152 uv_buf_t buf;
153 buf.base = slab + slab_used;
154 buf.len = MIN(SLAB_SIZE - slab_used, suggested_size);
155
156 wrap->slab_offset_ = slab_used;
157 slab_used += buf.len;
158
159 handle_that_last_alloced = handle;
160
161 return buf;
162 }
163
164 void StreamWrap::OnRead(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
165 HandleScope scope;
166
167 StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
168
169 // We should not be getting this callback if someone as already called
170 // uv_close() on the handle.
171 assert(wrap->object_.IsEmpty() == false);
172
173 // Remove the reference to the slab to avoid memory leaks;
174 Local<Value> slab_v = wrap->object_->GetHiddenValue(slab_sym);
175 wrap->object_->SetHiddenValue(slab_sym, v8::Null());
176
177 if (nread < 0) {
178 // EOF or Error
179 if (handle_that_last_alloced == handle) {
180 slab_used -= buf.len;
181 }
182
183 SetErrno(uv_last_error().code);
184 MakeCallback(wrap->object_, "onread", 0, NULL);
185 return;
186 }
187
188 assert(nread <= buf.len);
189
190 if (handle_that_last_alloced == handle) {
191 slab_used -= (buf.len - nread);
192 }
193
194 if (nread > 0) {
195 Local<Value> argv[3] = {
196 slab_v,
197 Integer::New(wrap->slab_offset_),
198 Integer::New(nread)
199 };
200 MakeCallback(wrap->object_, "onread", 3, argv);
201 }
202 }
203
204
205 Handle<Value> StreamWrap::Write(const Arguments& args) {
206 HandleScope scope;
207
208 UNWRAP
209
210 // The first argument is a buffer.
211 assert(Buffer::HasInstance(args[0]));
212 Local<Object> buffer_obj = args[0]->ToObject();
213
214 size_t offset = 0;
215 size_t length = Buffer::Length(buffer_obj);
216
217 if (args.Length() > 1) {
218 offset = args[1]->IntegerValue();
219 }
220
221 if (args.Length() > 2) {
222 length = args[2]->IntegerValue();
223 }
224
225 WriteWrap* req_wrap = new WriteWrap();
226
227 req_wrap->object_->SetHiddenValue(buffer_sym, buffer_obj);
228
229 uv_buf_t buf;
230 buf.base = Buffer::Data(buffer_obj) + offset;
231 buf.len = length;
232
233 int r = uv_write(&req_wrap->req_, wrap->stream_, &buf, 1, StreamWrap::AfterWrite);
234
235 req_wrap->Dispatched();
236
237 wrap->UpdateWriteQueueSize();
238
239 if (r) {
240 SetErrno(uv_last_error().code);
241 delete req_wrap;
242 return scope.Close(v8::Null());
243 } else {
244 return scope.Close(req_wrap->object_);
245 }
246 }
247
248
249 void StreamWrap::AfterWrite(uv_write_t* req, int status) {
250 WriteWrap* req_wrap = (WriteWrap*) req->data;
251 StreamWrap* wrap = (StreamWrap*) req->handle->data;
252
253 HandleScope scope;
254
255 // The wrap and request objects should still be there.
256 assert(req_wrap->object_.IsEmpty() == false);
257 assert(wrap->object_.IsEmpty() == false);
258
259 if (status) {
260 SetErrno(uv_last_error().code);
261 }
262
263 wrap->UpdateWriteQueueSize();
264
265 Local<Value> argv[4] = {
266 Integer::New(status),
267 Local<Value>::New(wrap->object_),
268 Local<Value>::New(req_wrap->object_),
269 req_wrap->object_->GetHiddenValue(buffer_sym),
270 };
271
272 MakeCallback(req_wrap->object_, "oncomplete", 4, argv);
273
274 delete req_wrap;
275 }
276
277
278 Handle<Value> StreamWrap::Shutdown(const Arguments& args) {
279 HandleScope scope;
280
281 UNWRAP
282
283 ShutdownWrap* req_wrap = new ShutdownWrap();
284
285 int r = uv_shutdown(&req_wrap->req_, wrap->stream_, AfterShutdown);
286
287 req_wrap->Dispatched();
288
289 if (r) {
290 SetErrno(uv_last_error().code);
291 delete req_wrap;
292 return scope.Close(v8::Null());
293 } else {
294 return scope.Close(req_wrap->object_);
295 }
296 }
297
298
299 void StreamWrap::AfterShutdown(uv_shutdown_t* req, int status) {
300 ReqWrap<uv_shutdown_t>* req_wrap = (ReqWrap<uv_shutdown_t>*) req->data;
301 StreamWrap* wrap = (StreamWrap*) req->handle->data;
302
303 // The wrap and request objects should still be there.
304 assert(req_wrap->object_.IsEmpty() == false);
305 assert(wrap->object_.IsEmpty() == false);
306
307 HandleScope scope;
308
309 if (status) {
310 SetErrno(uv_last_error().code);
311 }
312
313 Local<Value> argv[3] = {
314 Integer::New(status),
315 Local<Value>::New(wrap->object_),
316 Local<Value>::New(req_wrap->object_)
317 };
318
319 MakeCallback(req_wrap->object_, "oncomplete", 3, argv);
320
321 delete req_wrap;
322 }
323
324
325 }
Something went wrong with that request. Please try again.