Improve code.

This commit is contained in:
Miao Jiang 2013-02-21 19:05:32 +08:00
parent 648e170fa8
commit 78a53743de
6 changed files with 41 additions and 41 deletions

View file

@ -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();
});

View file

@ -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;

View file

@ -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 :

View file

@ -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);

View file

@ -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

View file

@ -29,12 +29,14 @@ class NodeCurl
CURL * curl;
v8::Persistent<v8::Object> handle;
v8::Persistent<v8::Object> ref;
int ref_no;
bool in_curlm;
std::vector<curl_slist*> slists;
std::map<int, std::string> strings;
NodeCurl(v8::Handle<v8::Object> 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<v8::Value> 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<v8::Object>::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);