From 78a53743debc32538510aa565e64d839ba82975d Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Thu, 21 Feb 2013 19:05:32 +0800 Subject: [PATCH] Improve code. --- examples/low-level.js | 2 +- lib/Curl.js | 28 +++++++++------------------- lib/Curl.toffee | 19 +++++++------------ lib/CurlBuilder.js | 4 ++-- lib/CurlBuilder.toffee | 6 ++---- src/node-curl.h | 23 ++++++++++++++++++++--- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/examples/low-level.js b/examples/low-level.js index bc72d60..d5e228b 100644 --- a/examples/low-level.js +++ b/examples/low-level.js @@ -19,7 +19,7 @@ curl.on('data', function(chunk) { }); curl.on('error', function(e) { - p("error: " + e.message) + p("error: " + e.message); curl.close(); once(); }); diff --git a/lib/Curl.js b/lib/Curl.js index 1565368..aa11a24 100644 --- a/lib/Curl.js +++ b/lib/Curl.js @@ -1,6 +1,6 @@ // Generated by ToffeeScript 1.4.0 (function() { - var Curl, curls, id, m, p; + var Curl, id, m, p; try { Curl = require(__dirname + '/../build/Release/node-curl').Curl; @@ -8,10 +8,6 @@ Curl = require(__dirname + '/../build/default/node-curl').Curl; } - curls = {}; - - id = 0; - Curl.prototype.setopt_user_ = function(option_id, value) { return this.options[option_id] = value; }; @@ -58,26 +54,15 @@ }; Curl.prototype.on = function(event, callback) { - var _this = this; switch (event) { case 'data': this.on_write = callback; break; case 'error': - this.on_error = function() { - var rt; - rt = callback(); - curls[_this.id] = null; - return rt; - }; + this.on_error = callback; break; case 'end': - this.on_end = function() { - var rt; - rt = callback(); - curls[_this.id] = null; - return rt; - }; + this.on_end = callback; break; default: throw new Error("invalid event type " + event); @@ -85,9 +70,14 @@ return this; }; + Curl.prototype.close = function() { + return this.close_(); + }; + + id = 0; + Curl.prototype.perform = function() { this.id = ++id; - curls[this.id] = this; this.perform_(); Curl.process(); return this; diff --git a/lib/Curl.toffee b/lib/Curl.toffee index 338a1ee..25dcf84 100644 --- a/lib/Curl.toffee +++ b/lib/Curl.toffee @@ -3,8 +3,6 @@ try catch e {Curl} = require __dirname + '/../build/default/node-curl' -curls = {} -id = 0 Curl::setopt_user_ = (option_id, value) -> @options[option_id] = value @@ -52,24 +50,20 @@ Curl::on = (event, callback) -> @on_write = callback when 'error' # (Error error) -> - @on_error = => - rt = callback() - curls[@id] = null - rt - + @on_error = callback when 'end' # () -> - @on_end = => - rt = callback() - curls[@id] = null - rt + @on_end = callback else throw new Error("invalid event type #{event}") @ +Curl::close = () -> + @close_() + +id = 0 Curl::perform = -> @id = ++id - curls[@id] = @ @perform_() Curl.process() @ @@ -96,5 +90,6 @@ Curl.process = -> Curl.in_process = false + module.exports = Curl # vim: sw=2 ts=2 sts=2 expandtab : diff --git a/lib/CurlBuilder.js b/lib/CurlBuilder.js index 15b8b16..ed6448f 100644 --- a/lib/CurlBuilder.js +++ b/lib/CurlBuilder.js @@ -50,8 +50,6 @@ if ((_ref = this.options) == null) { this.options = {}; } - c = this.curl_; - c.chunks = []; length = 0; this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug; this.effectiveOptions = {}; @@ -69,6 +67,8 @@ this.setOptions({ URL: this.url }); + c = this.curl_; + c.chunks = []; c.on('data', function(chunk) { curl.log("receive " + chunk.length + " bytes"); c.chunks.push(chunk); diff --git a/lib/CurlBuilder.toffee b/lib/CurlBuilder.toffee index 1ffbb7c..216ad8b 100644 --- a/lib/CurlBuilder.toffee +++ b/lib/CurlBuilder.toffee @@ -30,13 +30,9 @@ class CurlBuilder [@url, @options] = args @options ?= {} - c = @curl_ - c.chunks = [] length = 0 @debug = @defaultOptions.DEBUG ? @options.DEBUG ? @debug - - @effectiveOptions = {} for k, v of @defaultOptions @effectiveOptions[k] = v @@ -47,6 +43,8 @@ class CurlBuilder @setOptions @effectiveOptions @setOptions {URL: @url} + c = @curl_ + c.chunks = [] c.on 'data', (chunk) -> curl.log "receive #{chunk.length} bytes" c.chunks.push chunk diff --git a/src/node-curl.h b/src/node-curl.h index 9079c2e..715093a 100644 --- a/src/node-curl.h +++ b/src/node-curl.h @@ -29,12 +29,14 @@ class NodeCurl CURL * curl; v8::Persistent handle; + v8::Persistent ref; + int ref_no; bool in_curlm; std::vector slists; std::map strings; NodeCurl(v8::Handle object) - : in_curlm(false) + : in_curlm(false), ref_no(0) { ++count; v8::V8::AdjustAmountOfExternalAllocatedMemory(2*4096); @@ -329,7 +331,11 @@ class NodeCurl else curl->on_error(&msg_copy); - // Handle should not released in curl.process, or will cause segment fault. + --curl->ref_no; + if (curl->ref_no == 0) + curl->ref.Dispose(); + + } } } @@ -340,6 +346,12 @@ class NodeCurl static v8::Handle perform(const v8::Arguments & args) { NodeCurl *curl = unwrap(args.This()); + if (!curl) + return raise("curl is closed."); + + if (curl->in_curlm) + return raise("curl session is running."); + CURLMcode code = curl_multi_add_handle(curlm, curl->curl); if (code != CURLM_OK) { @@ -347,6 +359,11 @@ class NodeCurl } curl->in_curlm = true; ++running_handles; + + if (curl->ref_no == 0) + curl->ref=v8::Persistent::New(curl->handle); + ++curl->ref_no; + return args.This(); } @@ -386,7 +403,7 @@ class NodeCurl NODE_SET_PROTOTYPE_METHOD(t , "getinfo_double_" , getinfo_double); NODE_SET_PROTOTYPE_METHOD(t , "getinfo_slist_" , getinfo_slist); - NODE_SET_PROTOTYPE_METHOD(t, "close", close); + NODE_SET_PROTOTYPE_METHOD(t, "close_", close); NODE_SET_METHOD(t , "process_" , process); NODE_SET_METHOD(t , "get_count" , get_count);