Skip to content

Instantly share code, notes, and snippets.

@totherik
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totherik/b66c911147c85ad48a5a to your computer and use it in GitHub Desktop.
Save totherik/b66c911147c85ad48a5a to your computer and use it in GitHub Desktop.
Brainstorming read/write socket implementations.
'use strict';
var ToughSocket = require('./socket');
var socket = new ToughSocket(8124, 'localhost', { resetTimeout: 1000 });
socket.on('data', function (chunk) {
console.log('read', chunk.toString('utf8'));
});
(function write() {
socket.write('foo', function (err) {
console.log(err || 'write foo');
setTimeout(write, 10);
});
})();
'use strict';
var Net = require('net');
var Util = require('util');
var Levee = require('./');
var Events = require('events');
function ToughSocket(port, host, options) {
ToughSocket.super_.apply(this);
var self = this;
this._ended = false;
this._port = port || 24224;
this._host = host || 'localhost';
this._socket = null;
this._read = Levee.createEmitterBreaker(Object.create(this, {
_socket: {
get: function () {
return self._socket;
},
set: function (value) {
self._socket = value;
}
},
execute: {
value: function (/*bytes, cb*/) {
var args, cb;
args = Array.prototype.slice.call(arguments);
cb = args.pop();
cb(null, this.read.apply(this, args));
}
}
}), options);
this._write = Levee.createEmitterBreaker(Object.create(this, {
_socket: {
get: function () {
return self._socket;
},
set: function (value) {
self._socket = value;
}
},
execute: {
value: function write(data, cb) {
this.write(data, cb);
}
}
}), options);
}
Util.inherits(ToughSocket, Events.EventEmitter);
ToughSocket.prototype.read = function (size, cb) {
this._read.run.apply(this._read, arguments);
};
ToughSocket.prototype.write = function (data, cb) {
this._write.run(data, cb);
};
ToughSocket.prototype.end = function (data) {
this.write(data, Function.prototype);
this._ended = true;
if (this._socket) {
this._socket.end();
}
};
ToughSocket.prototype.getInstance = function (cb) {
var self = this;
if (this._ended) {
cb(null, null);
return;
}
function onerror(err) {
self._socket.destroy();
self._socket = null;
setImmediate(function () {
cb(err);
});
}
if (this._socket && !this._socket.writable) {
this._socket.destroy();
this._socket = null;
this.getInstance(cb);
return;
}
if (!this._socket) {
console.log('create');
this._socket = new Net.Socket();
this._socket.once('error', onerror);
this._socket.on('data', this.emit.bind(this, 'data'));
this._socket.connect(this._port, this._host, function () {
this.removeListener('error', onerror);
cb(null, this);
});
return;
}
setImmediate(function () {
cb(null, self._socket);
});
};
module.exports = ToughSocket;
var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment