Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
quic: fixes and refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed May 22, 2019
1 parent 877b6b4 commit 0f657ab
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 361 deletions.
9 changes: 5 additions & 4 deletions lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ class QuicSocket extends EventEmitter {
return;
this.#state = kSocketClosing;

// Otherwise, gracefully close each QuicSession, with
// [kMaybeDestroy]() being called after each closes.
const maybeDestroy = this[kMaybeDestroy].bind(this);

// If there are no sessions, call [kMaybeDestroy]()
// immediately to destroy the QuicSocket
if (this.#sessions.size === 0) {
Expand All @@ -648,9 +652,6 @@ class QuicSocket extends EventEmitter {
return;
}

// Otherwise, gracefully close each QuicSession, with
// [kMaybeDestroy]() being called after each closes.
const maybeDestroy = this[kMaybeDestroy].bind(this);
for (const session of this.#sessions)
session.close(maybeDestroy);
}
Expand Down Expand Up @@ -1444,8 +1445,8 @@ class QuicStream extends Duplex {
const handle = this[kHandle];
if (handle !== undefined) {
this[kHandle] = undefined;
handle[owner_symbol] = undefined;
handle.destroy();
handle[owner_symbol] = undefined;
}
callback(error);
}
Expand Down
20 changes: 9 additions & 11 deletions lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,21 @@ function onWriteComplete(status) {
this.callback(null);
}

function createWriteWrap(handle) {
function createWriteWrap(handle, callback) {
const req = new WriteWrap();

req.handle = handle;
req.oncomplete = onWriteComplete;
req.async = false;
req.bytes = 0;
req.buffer = null;
req.callback = callback;

return req;
}

function writevGeneric(self, data, cb) {
const req = createWriteWrap(self[kHandle]);
const req = createWriteWrap(self[kHandle], cb);
const allBuffers = data.allBuffers;
var chunks;
var i;
Expand All @@ -126,29 +127,26 @@ function writevGeneric(self, data, cb) {
// Retain chunks
if (err === 0) req._chunks = chunks;

afterWriteDispatched(self, req, err, cb);
afterWriteDispatched(self, req, err);
return req;
}

function writeGeneric(self, data, encoding, cb) {
const req = createWriteWrap(self[kHandle]);
const req = createWriteWrap(self[kHandle], cb);
const err = handleWriteReq(req, data, encoding);

afterWriteDispatched(self, req, err, cb);
afterWriteDispatched(self, req, err);
return req;
}

function afterWriteDispatched(self, req, err, cb) {
function afterWriteDispatched(self, req, err) {
req.bytes = streamBaseState[kBytesWritten];
req.async = !!streamBaseState[kLastWriteWasAsync];

if (err !== 0)
return self.destroy(errnoException(err, 'write', req.error), cb);

if (!req.async) {
cb();
} else {
req.callback = cb;
if (!req.async && typeof req.callback === 'function') {
req.callback();
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/node_quic_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ class QuicBuffer : public MemoryRetainer {
CHECK_EQ(length_, 0);
}

inline uint64_t Copy(
uv_buf_t* bufs,
size_t nbufs) {
uint64_t total = 0;
for (size_t n = 0; n < nbufs; n++) {
MallocedBuffer<uint8_t> data(bufs[n].len);
memcpy(data.data, bufs[n].base, bufs[n].len);
total += bufs[n].len;
Push(std::move(data));
}
return total;
}

// Push one or more uv_buf_t instances into the buffer.
// the done_cb callback will be invoked when the last
// uv_buf_t in the bufs array is consumed and popped out
Expand Down
Loading

0 comments on commit 0f657ab

Please sign in to comment.