function Socket(options) { Socket.super_.call (this); this.requests = []; this.buffer = new Buffer ((options && options.bufferSize) ? options.bufferSize : 4096); this.recvPaused = false; this.sendPaused = true; this.wrap = new raw.SocketWrap ( ((options && options.protocol) ? options.protocol : 0), ((options && options.addressFamily) ? options.addressFamily : AddressFamily.IPv4) ); var me = this; this.wrap.on ("sendReady", this.onSendReady.bind (me)); this.wrap.on ("recvReady", this.onRecvReady.bind (me)); this.wrap.on ("error", this.onError.bind (me)); this.wrap.on ("close", this.onClose.bind (me)); }
n/a
createChecksum = function () { var sum = 0; for (var i = 0; i < arguments.length; i++) { var object = arguments[i]; if (object instanceof Buffer) { sum = raw.createChecksum (sum, object, 0, object.length); } else { sum = raw.createChecksum (sum, object.buffer, object.offset, object.length); } } return sum; }
...
Both buffers will be treated as one, i.e. as if the data at offset `20` in
`tcp_packet` had followed all data in `pseudo_header` - as if they were one
buffer.
## raw.writeChecksum (buffer, offset, checksum)
The `writeChecksum()` function writes a checksum created by the
`raw.createChecksum()` function to the [Node.js][nodejs] `Buffer` object
`buffer` at offsets `offset` and `offset` + 1.
The following example generates and writes a checksum at offset `2` in a
[Node.js][nodejs] `Buffer` object:
raw.writeChecksum (buffer, 2, raw.createChecksum (buffer));
...
createSocket = function (options) { return new Socket (options || {}); }
n/a
htonl = function () { [native code] }
n/a
htons = function () { [native code] }
n/a
ntohl = function () { [native code] }
n/a
ntohs = function () { [native code] }
n/a
writeChecksum = function (buffer, offset, checksum) { buffer.writeUInt8 ((checksum & 0xff00) >> 8, offset); buffer.writeUInt8 (checksum & 0xff, offset + 1); return buffer; }
n/a
function Socket(options) { Socket.super_.call (this); this.requests = []; this.buffer = new Buffer ((options && options.bufferSize) ? options.bufferSize : 4096); this.recvPaused = false; this.sendPaused = true; this.wrap = new raw.SocketWrap ( ((options && options.protocol) ? options.protocol : 0), ((options && options.addressFamily) ? options.addressFamily : AddressFamily.IPv4) ); var me = this; this.wrap.on ("sendReady", this.onSendReady.bind (me)); this.wrap.on ("recvReady", this.onRecvReady.bind (me)); this.wrap.on ("error", this.onError.bind (me)); this.wrap.on ("close", this.onClose.bind (me)); }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
close = function () { this.wrap.close (); return this; }
n/a
getOption = function (level, option, value, length) { return this.wrap.getOption (level, option, value, length); }
n/a
onClose = function () { this.emit ("close"); }
n/a
onError = function (error) { this.emit ("error", error); this.close (); }
n/a
onRecvReady = function () { var me = this; try { this.wrap.recv (this.buffer, function (buffer, bytes, source) { var newBuffer = buffer.slice (0, bytes); me.emit ("message", newBuffer, source); }); } catch (error) { me.emit ("error", error); } }
n/a
onSendReady = function () { if (this.requests.length > 0) { var me = this; var req = this.requests.shift (); try { if (req.beforeCallback) req.beforeCallback (); this.wrap.send (req.buffer, req.offset, req.length, req.address, function (bytes) { req.afterCallback.call (me, null, bytes); }); } catch (error) { req.afterCallback.call (me, error, 0); } } else { if (! this.sendPaused) this.pauseSend (); } }
...
* Default protocol option to `createSession()` was incorrect in the README.md
* The `session.on("message")` example used `message` instead of `buffer` in
the README.md
## Version 1.1.3 - 04/03/2013
* `raw.Socket.onSendReady()` emit's an error when `raw.SocketWrap.send()`
throws an exception when it should call the `req.callback` callback
* Added the `pauseRecv()`, `resumeRecv()`, `pauseSend()` and `resumeSend()`
methods
[net-ping]: https://npmjs.org/package/net-ping "net-ping"
## Version 1.1.4 - 05/03/2013
...
pauseRecv = function () { this.recvPaused = true; this.wrap.pause (this.recvPaused, this.sendPaused); return this; }
n/a
pauseSend = function () { this.sendPaused = true; this.wrap.pause (this.recvPaused, this.sendPaused); return this; }
n/a
resumeRecv = function () { this.recvPaused = false; this.wrap.pause (this.recvPaused, this.sendPaused); return this; }
n/a
resumeSend = function () { this.sendPaused = false; this.wrap.pause (this.recvPaused, this.sendPaused); return this; }
n/a
send = function (buffer, offset, length, address, beforeCallback, afterCallback) { if (! afterCallback) { afterCallback = beforeCallback; beforeCallback = null; } if (length + offset > buffer.length) { afterCallback.call (this, new Error ("Buffer length '" + buffer.length + "' is not large enough for the specified offset '" + offset + "' plus length '" + length + "'")); return this; } if (! net.isIP (address)) { afterCallback.call (this, new Error ("Invalid IP address '" + address + "'")); return this; } var req = { buffer: buffer, offset: offset, length: length, address: address, afterCallback: afterCallback, beforeCallback: beforeCallback }; this.requests.push (req); if (this.sendPaused) this.resumeSend (); return this; }
...
* Default protocol option to `createSession()` was incorrect in the README.md
* The `session.on("message")` example used `message` instead of `buffer` in
the README.md
## Version 1.1.3 - 04/03/2013
* `raw.Socket.onSendReady()` emit's an error when `raw.SocketWrap.send()`
throws an exception when it should call the `req.callback` callback
* Added the `pauseRecv()`, `resumeRecv()`, `pauseSend()` and `resumeSend()`
methods
[net-ping]: https://npmjs.org/package/net-ping "net-ping"
## Version 1.1.4 - 05/03/2013
...
setOption = function (level, option, value, length) { if (arguments.length > 3) this.wrap.setOption (level, option, value, length); else this.wrap.setOption (level, option, value); }
n/a