Skip to content

Commit

Permalink
try send within same event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Apr 30, 2024
1 parent a8eda0c commit 4b9f5d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
37 changes: 25 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Socket extends EventEmitter {
this.wrap.send (req.buffer, req.offset, req.length,
req.address, function (bytes) {
req.afterCallback.call (me, null, bytes);
});
}, false);
} catch (error) {
req.afterCallback.call (me, error, 0);
}
Expand Down Expand Up @@ -159,18 +159,31 @@ class Socket extends EventEmitter {
return this;
}

const req = {
buffer: buffer,
offset: offset,
length: length,
address: address,
afterCallback: afterCallback,
beforeCallback: beforeCallback
};
this.requests.push (req);

if (this.sendPaused)
const req = { buffer, offset, length, address, afterCallback, beforeCallback };

if (this.sendPaused) {
if (req.beforeCallback)
req.beforeCallback ();

try {
let sent = this.wrap.send (req.buffer, req.offset, req.length, req.address, (bytes) => {
req.afterCallback.call (this, null, bytes);
}, true);

if (sent) {
return this
}
} catch(error){
req.afterCallback.call (this, error, 0);
return
}

this.requests.push (req);

this.resumeSend ();
} else {
this.requests.push (req);
}

return this;
}
Expand Down
15 changes: 13 additions & 2 deletions src/raw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,10 @@ NAN_METHOD(SocketWrap::Send) {
uint32_t length;
int rc;
char *data;
bool try_send;

if (info.Length () < 5) {
Nan::ThrowError("Five arguments are required");
if (info.Length () < 6) {
Nan::ThrowError("Six arguments are required");
return;
}

Expand All @@ -811,6 +812,11 @@ NAN_METHOD(SocketWrap::Send) {
Nan::ThrowTypeError("Callback argument must be a function");
return;
}

if (! info[5]->IsBoolean ()) {
Nan::ThrowTypeError("Try argument must be a boolean");
return;
}

rc = socket->CreateSocket ();
if (rc != 0) {
Expand All @@ -821,6 +827,7 @@ NAN_METHOD(SocketWrap::Send) {
buffer = info[0]->ToObject (context).ToLocalChecked();
offset = Nan::To<Uint32>(info[1]).ToLocalChecked()->Value();
length = Nan::To<Uint32>(info[2]).ToLocalChecked()->Value();
try_send = info[5]->ToBoolean (isolate)->Value ();

data = node::Buffer::Data (buffer) + offset;

Expand Down Expand Up @@ -890,6 +897,10 @@ NAN_METHOD(SocketWrap::Send) {
}

if (rc == SOCKET_ERROR) {
if(try_send){
info.GetReturnValue().Set(Nan::New<Boolean>(false));
return;
}
Nan::ThrowError(raw_strerror (SOCKET_ERRNO));
return;
}
Expand Down

0 comments on commit 4b9f5d9

Please sign in to comment.