Improve code.
This commit is contained in:
parent
648e170fa8
commit
78a53743de
6 changed files with 41 additions and 41 deletions
|
@ -19,7 +19,7 @@ curl.on('data', function(chunk) {
|
||||||
});
|
});
|
||||||
|
|
||||||
curl.on('error', function(e) {
|
curl.on('error', function(e) {
|
||||||
p("error: " + e.message)
|
p("error: " + e.message);
|
||||||
curl.close();
|
curl.close();
|
||||||
once();
|
once();
|
||||||
});
|
});
|
||||||
|
|
28
lib/Curl.js
28
lib/Curl.js
|
@ -1,6 +1,6 @@
|
||||||
// Generated by ToffeeScript 1.4.0
|
// Generated by ToffeeScript 1.4.0
|
||||||
(function() {
|
(function() {
|
||||||
var Curl, curls, id, m, p;
|
var Curl, id, m, p;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
|
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
|
||||||
|
@ -8,10 +8,6 @@
|
||||||
Curl = require(__dirname + '/../build/default/node-curl').Curl;
|
Curl = require(__dirname + '/../build/default/node-curl').Curl;
|
||||||
}
|
}
|
||||||
|
|
||||||
curls = {};
|
|
||||||
|
|
||||||
id = 0;
|
|
||||||
|
|
||||||
Curl.prototype.setopt_user_ = function(option_id, value) {
|
Curl.prototype.setopt_user_ = function(option_id, value) {
|
||||||
return this.options[option_id] = value;
|
return this.options[option_id] = value;
|
||||||
};
|
};
|
||||||
|
@ -58,26 +54,15 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
Curl.prototype.on = function(event, callback) {
|
Curl.prototype.on = function(event, callback) {
|
||||||
var _this = this;
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case 'data':
|
case 'data':
|
||||||
this.on_write = callback;
|
this.on_write = callback;
|
||||||
break;
|
break;
|
||||||
case 'error':
|
case 'error':
|
||||||
this.on_error = function() {
|
this.on_error = callback;
|
||||||
var rt;
|
|
||||||
rt = callback();
|
|
||||||
curls[_this.id] = null;
|
|
||||||
return rt;
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
case 'end':
|
case 'end':
|
||||||
this.on_end = function() {
|
this.on_end = callback;
|
||||||
var rt;
|
|
||||||
rt = callback();
|
|
||||||
curls[_this.id] = null;
|
|
||||||
return rt;
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error("invalid event type " + event);
|
throw new Error("invalid event type " + event);
|
||||||
|
@ -85,9 +70,14 @@
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Curl.prototype.close = function() {
|
||||||
|
return this.close_();
|
||||||
|
};
|
||||||
|
|
||||||
|
id = 0;
|
||||||
|
|
||||||
Curl.prototype.perform = function() {
|
Curl.prototype.perform = function() {
|
||||||
this.id = ++id;
|
this.id = ++id;
|
||||||
curls[this.id] = this;
|
|
||||||
this.perform_();
|
this.perform_();
|
||||||
Curl.process();
|
Curl.process();
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -3,8 +3,6 @@ try
|
||||||
catch e
|
catch e
|
||||||
{Curl} = require __dirname + '/../build/default/node-curl'
|
{Curl} = require __dirname + '/../build/default/node-curl'
|
||||||
|
|
||||||
curls = {}
|
|
||||||
id = 0
|
|
||||||
Curl::setopt_user_ = (option_id, value) ->
|
Curl::setopt_user_ = (option_id, value) ->
|
||||||
@options[option_id] = value
|
@options[option_id] = value
|
||||||
|
|
||||||
|
@ -52,24 +50,20 @@ Curl::on = (event, callback) ->
|
||||||
@on_write = callback
|
@on_write = callback
|
||||||
when 'error'
|
when 'error'
|
||||||
# (Error error) ->
|
# (Error error) ->
|
||||||
@on_error = =>
|
@on_error = callback
|
||||||
rt = callback()
|
|
||||||
curls[@id] = null
|
|
||||||
rt
|
|
||||||
|
|
||||||
when 'end'
|
when 'end'
|
||||||
# () ->
|
# () ->
|
||||||
@on_end = =>
|
@on_end = callback
|
||||||
rt = callback()
|
|
||||||
curls[@id] = null
|
|
||||||
rt
|
|
||||||
else
|
else
|
||||||
throw new Error("invalid event type #{event}")
|
throw new Error("invalid event type #{event}")
|
||||||
@
|
@
|
||||||
|
|
||||||
|
Curl::close = () ->
|
||||||
|
@close_()
|
||||||
|
|
||||||
|
id = 0
|
||||||
Curl::perform = ->
|
Curl::perform = ->
|
||||||
@id = ++id
|
@id = ++id
|
||||||
curls[@id] = @
|
|
||||||
@perform_()
|
@perform_()
|
||||||
Curl.process()
|
Curl.process()
|
||||||
@
|
@
|
||||||
|
@ -96,5 +90,6 @@ Curl.process = ->
|
||||||
Curl.in_process = false
|
Curl.in_process = false
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = Curl
|
module.exports = Curl
|
||||||
# vim: sw=2 ts=2 sts=2 expandtab :
|
# vim: sw=2 ts=2 sts=2 expandtab :
|
||||||
|
|
|
@ -50,8 +50,6 @@
|
||||||
if ((_ref = this.options) == null) {
|
if ((_ref = this.options) == null) {
|
||||||
this.options = {};
|
this.options = {};
|
||||||
}
|
}
|
||||||
c = this.curl_;
|
|
||||||
c.chunks = [];
|
|
||||||
length = 0;
|
length = 0;
|
||||||
this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug;
|
this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug;
|
||||||
this.effectiveOptions = {};
|
this.effectiveOptions = {};
|
||||||
|
@ -69,6 +67,8 @@
|
||||||
this.setOptions({
|
this.setOptions({
|
||||||
URL: this.url
|
URL: this.url
|
||||||
});
|
});
|
||||||
|
c = this.curl_;
|
||||||
|
c.chunks = [];
|
||||||
c.on('data', function(chunk) {
|
c.on('data', function(chunk) {
|
||||||
curl.log("receive " + chunk.length + " bytes");
|
curl.log("receive " + chunk.length + " bytes");
|
||||||
c.chunks.push(chunk);
|
c.chunks.push(chunk);
|
||||||
|
|
|
@ -30,13 +30,9 @@ class CurlBuilder
|
||||||
[@url, @options] = args
|
[@url, @options] = args
|
||||||
@options ?= {}
|
@options ?= {}
|
||||||
|
|
||||||
c = @curl_
|
|
||||||
c.chunks = []
|
|
||||||
length = 0
|
length = 0
|
||||||
|
|
||||||
@debug = @defaultOptions.DEBUG ? @options.DEBUG ? @debug
|
@debug = @defaultOptions.DEBUG ? @options.DEBUG ? @debug
|
||||||
|
|
||||||
|
|
||||||
@effectiveOptions = {}
|
@effectiveOptions = {}
|
||||||
for k, v of @defaultOptions
|
for k, v of @defaultOptions
|
||||||
@effectiveOptions[k] = v
|
@effectiveOptions[k] = v
|
||||||
|
@ -47,6 +43,8 @@ class CurlBuilder
|
||||||
@setOptions @effectiveOptions
|
@setOptions @effectiveOptions
|
||||||
@setOptions {URL: @url}
|
@setOptions {URL: @url}
|
||||||
|
|
||||||
|
c = @curl_
|
||||||
|
c.chunks = []
|
||||||
c.on 'data', (chunk) ->
|
c.on 'data', (chunk) ->
|
||||||
curl.log "receive #{chunk.length} bytes"
|
curl.log "receive #{chunk.length} bytes"
|
||||||
c.chunks.push chunk
|
c.chunks.push chunk
|
||||||
|
|
|
@ -29,12 +29,14 @@ class NodeCurl
|
||||||
CURL * curl;
|
CURL * curl;
|
||||||
v8::Persistent<v8::Object> handle;
|
v8::Persistent<v8::Object> handle;
|
||||||
|
|
||||||
|
v8::Persistent<v8::Object> ref;
|
||||||
|
int ref_no;
|
||||||
bool in_curlm;
|
bool in_curlm;
|
||||||
std::vector<curl_slist*> slists;
|
std::vector<curl_slist*> slists;
|
||||||
std::map<int, std::string> strings;
|
std::map<int, std::string> strings;
|
||||||
|
|
||||||
NodeCurl(v8::Handle<v8::Object> object)
|
NodeCurl(v8::Handle<v8::Object> object)
|
||||||
: in_curlm(false)
|
: in_curlm(false), ref_no(0)
|
||||||
{
|
{
|
||||||
++count;
|
++count;
|
||||||
v8::V8::AdjustAmountOfExternalAllocatedMemory(2*4096);
|
v8::V8::AdjustAmountOfExternalAllocatedMemory(2*4096);
|
||||||
|
@ -329,7 +331,11 @@ class NodeCurl
|
||||||
else
|
else
|
||||||
curl->on_error(&msg_copy);
|
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<v8::Value> perform(const v8::Arguments & args)
|
static v8::Handle<v8::Value> perform(const v8::Arguments & args)
|
||||||
{
|
{
|
||||||
NodeCurl *curl = unwrap(args.This());
|
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);
|
CURLMcode code = curl_multi_add_handle(curlm, curl->curl);
|
||||||
if (code != CURLM_OK)
|
if (code != CURLM_OK)
|
||||||
{
|
{
|
||||||
|
@ -347,6 +359,11 @@ class NodeCurl
|
||||||
}
|
}
|
||||||
curl->in_curlm = true;
|
curl->in_curlm = true;
|
||||||
++running_handles;
|
++running_handles;
|
||||||
|
|
||||||
|
if (curl->ref_no == 0)
|
||||||
|
curl->ref=v8::Persistent<v8::Object>::New(curl->handle);
|
||||||
|
++curl->ref_no;
|
||||||
|
|
||||||
return args.This();
|
return args.This();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -386,7 +403,7 @@ class NodeCurl
|
||||||
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_double_" , getinfo_double);
|
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_double_" , getinfo_double);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_slist_" , getinfo_slist);
|
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 , "process_" , process);
|
||||||
NODE_SET_METHOD(t , "get_count" , get_count);
|
NODE_SET_METHOD(t , "get_count" , get_count);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue