Browse Source

Simplify send buffer logic

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
344243db92
  1. 34
      lib/net.js

34
lib/net.js

@ -57,15 +57,15 @@ var ioWatchers = new FreeList("iowatcher", 100, function () {
return new IOWatcher();
});
var nb = 0;
var buffers = new FreeList("buffer", 100, function (l) {
return new process.Buffer(1000);
return new process.Buffer(500);
});
// Allocated on demand.
var recvBuffer = null;
function allocRecvBuffer () {
recvBuffer = new process.Buffer(8*1024);
recvBuffer = new process.Buffer(40*1024);
recvBuffer.used = 0;
}
@ -192,19 +192,9 @@ Socket.prototype._sendString = function (data, encoding) {
if (self.sendQueue.length == 0) {
buffer = self._allocateSendBuffer();
} else {
// walk through the sendQueue, find the buffer with free space
for (var i = 0; i < self.sendQueue.length; i++) {
if (self.sendQueue[i].used == 0) {
buffer = self.sendQueue[i];
break;
}
}
// if we didn't find one, take the last
// TODO what if this isn't empty but encoding == fd ?
if (!buffer) {
buffer = self._sendQueueLast();
// if last buffer is used up
if (buffer.length == buffer.used) buffer = self._allocateSendBuffer();
if (buffer.used == buffer.length) {
buffer = self._allocateSendBuffer();
}
}
@ -279,17 +269,7 @@ Socket.prototype.send = function (data, encoding) {
var inserted = false;
data.sent = 0;
data.used = data.length;
for (var i = 0; i < self.sendQueue.length; i++) {
if (self.sendQueue[i].used == 0) {
// if found, insert the data there
self.sendQueue.splice(i, 0, data);
inserted = true;
break;
}
}
if (!inserted) self.sendQueue.push(data);
self.sendQueueSize += data.used;
}
return this.flush();
@ -322,7 +302,7 @@ Socket.prototype.flush = function () {
var self = this;
var bytesWritten;
while (self.sendQueue.length > 0) {
while (self.sendQueue.length) {
if (!self.writable) throw new Error('Socket is not writable');
var b = self.sendQueue[0];
@ -333,7 +313,7 @@ Socket.prototype.flush = function () {
}
if (b.sent == b.used) {
// this can be improved - save the buffer for later?
// shift!
self.sendQueue.shift();
buffers.free(b);
continue;

Loading…
Cancel
Save