Catch exception.

This commit is contained in:
Miao Jiang 2013-02-22 02:07:26 +08:00
parent 279e9f04cc
commit 8f707b34b1
3 changed files with 38 additions and 24 deletions

View file

@ -1,6 +1,6 @@
// Generated by ToffeeScript 1.4.0 // Generated by ToffeeScript 1.4.0
(function() { (function() {
var Curl, id, m, p; var Curl, curls, id, m, p;
try { try {
Curl = require(__dirname + '/../build/Release/node-curl').Curl; Curl = require(__dirname + '/../build/Release/node-curl').Curl;
@ -53,16 +53,29 @@
DEBUG: 'DEBUG' DEBUG: 'DEBUG'
}; };
id = 0;
curls = {};
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 = function(chunk) {
return callback.call(_this, chunk);
};
break; break;
case 'error': case 'error':
this.on_error = callback; this.on_error = function(e) {
delete curls[_this.id];
return callback.call(_this, e);
};
break; break;
case 'end': case 'end':
this.on_end = callback; this.on_end = function() {
delete curls[_this.id];
return callback.call(_this);
};
break; break;
default: default:
throw new Error("invalid event type " + event); throw new Error("invalid event type " + event);
@ -71,13 +84,13 @@
}; };
Curl.prototype.close = function() { Curl.prototype.close = function() {
delete curls[this.id];
return this.close_(); 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;

View file

@ -42,28 +42,37 @@ Curl.user_options =
RAW: 'RAW' RAW: 'RAW'
DEBUG: 'DEBUG' DEBUG: 'DEBUG'
id = 0
curls = {}
# on 'data' must be returns the chunk length # on 'data' must be returns the chunk length
Curl::on = (event, callback) -> Curl::on = (event, callback) ->
switch event switch event
when 'data' when 'data'
# (Buffer chunk) -> # (Buffer chunk) ->
@on_write = callback @on_write = (chunk) =>
callback.call @, chunk
when 'error' when 'error'
# (Error error) -> # (Error error) ->
@on_error = callback @on_error = (e) =>
delete curls[@id]
callback.call @, e
when 'end' when 'end'
# () -> # () ->
@on_end = callback @on_end = =>
delete curls[@id]
callback.call @
else else
throw new Error("invalid event type #{event}") throw new Error("invalid event type #{event}")
@ @
Curl::close = () -> Curl::close = () ->
delete curls[@id]
@close_() @close_()
id = 0
Curl::perform = -> Curl::perform = ->
@id = ++id @id = ++id
curls[@id] = @
@perform_() @perform_()
Curl.process() Curl.process()
@ @

View file

@ -29,14 +29,12 @@ 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), ref_no(0) : in_curlm(false)
{ {
++count; ++count;
v8::V8::AdjustAmountOfExternalAllocatedMemory(2*4096); v8::V8::AdjustAmountOfExternalAllocatedMemory(2*4096);
@ -121,7 +119,11 @@ class NodeCurl
{ {
node::Buffer * buffer = node::Buffer::New(data, n); node::Buffer * buffer = node::Buffer::New(data, n);
v8::Handle<v8::Value> argv[] = { buffer->handle_ }; v8::Handle<v8::Value> argv[] = { buffer->handle_ };
n = cb->ToObject()->CallAsFunction(handle, 1, argv)->Int32Value(); v8::Handle<v8::Value> rt = cb->ToObject()->CallAsFunction(handle, 1, argv);
if (rt.IsEmpty())
return 0;
else
return rt->Int32Value();
} }
return n; return n;
} }
@ -330,12 +332,6 @@ class NodeCurl
curl->on_end(&msg_copy); curl->on_end(&msg_copy);
else else
curl->on_error(&msg_copy); curl->on_error(&msg_copy);
--curl->ref_no;
if (curl->ref_no == 0)
curl->ref.Dispose();
} }
} }
} }
@ -360,10 +356,6 @@ 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();
} }