Permalink
Browse files

new put monad

  • Loading branch information...
1 parent deee749 commit f17553f022edd2a7f47a8314a5ecb37f4e9f29b1 @substack substack committed May 31, 2010
Showing with 97 additions and 51 deletions.
  1. +1 −3 README
  2. +38 −48 { → lib}/rfb.js
  3. +58 −0 lib/rfb/put.js
View
4 README
@@ -1,4 +1,2 @@
-RFB Client Library
+RFB client library for node.js
http://www.realvnc.com/docs/rfbproto.pdf
-
-* Not yet complete *
View
@@ -9,6 +9,7 @@ var EventEmitter = require('events').EventEmitter;
var BufferList = require('bufferlist').BufferList;
var Binary = require('bufferlist/binary').Binary;
+var Put = require('rfb/put').Put;
var clientMsgTypes = {
setPixelFormat : 0,
@@ -34,25 +35,6 @@ var encodings = {
zrle : 16
};
-function Word8(x) {
- return String.fromCharCode(x);
-}
-
-function Word16be(x) {
- return String.fromCharCode((x>>8)&0xFF) + String.fromCharCode(x&0xFF);
-}
-
-function Word32be(x) {
- return String.fromCharCode((x>>24)&0xFF) +
- String.fromCharCode((x>>16)&0xFF) +
- String.fromCharCode((x>>8)&0xFF) +
- String.fromCharCode(x&0xFF);
-}
-
-function Pad8() { return "\x00"; }
-function Pad16() { return "\x00\x00"; }
-function Pad24() { return "\x00\x00\x00"; }
-
RFB.prototype = new EventEmitter;
exports.RFB = RFB;
function RFB(opts) {
@@ -111,40 +93,47 @@ function RFB(opts) {
};
this.sendKey = function (key, down) {
- this.send(
- Word8(clientMsgTypes.keyEvent),
- Word8(!!down),
- Pad16(),
- Word32be(key)
- );
+ Put()
+ .word8(clientMsgTypes.keyEvent)
+ .word8(!!down)
+ .pad(2)
+ .word32be(key)
+ .send(this)
+ ;
+ return this;
};
this.sendKeyDown = function (key) {
this.sendKey(key, 1);
+ return this;
};
this.sendKeyUp = function (key) {
this.sendKey(key, 0);
+ return this;
};
this.sendPointer = function (mask, x, y) {
- this.send(
- Word8(clientMsgTypes.pointerEvent),
- Word8(mask),
- Word16be(x),
- Word16be(y)
- );
+ Put()
+ .word8(clientMsgTypes.pointerEvent)
+ .word8(mask)
+ .word16be(x)
+ .word16be(y)
+ .send(this)
+ ;
+ return this;
};
this.fbUpdateRequest = function (x, y, width, height, subscribe) {
- this.send(
- Word8(clientMsgTypes.fbUpdate),
- Word8(subscribe),
- Word16be(x),
- Word16be(y),
- Word16be(width),
- Word16be(height)
- );
+ Put()
+ .word8(clientMsgTypes.fbUpdate)
+ .word8(subscribe)
+ .word16be(x)
+ .word16be(y)
+ .word16be(width)
+ .word16be(height)
+ .send(this)
+ ;
}
this.requestRedrawScreen = function () {
@@ -207,7 +196,7 @@ function Parser (rfb, bufferList) {
sys.log('Security type ' + rfb.securityType + ' not supported');
this.clear();
}
- rfb.send(Word8(secNum));
+ Put().word8(secNum).send(rfb);
})
.flush()
.getWord32be('secRes')
@@ -226,7 +215,7 @@ function Parser (rfb, bufferList) {
.flush()
// init handshake
.tap(function (vars) {
- rfb.send(Word8(rfb.shared));
+ Put().word8(rfb.shared).send(rfb);
})
.getWord16be('fb.Width')
.getWord16be('fb.height')
@@ -249,13 +238,14 @@ function Parser (rfb, bufferList) {
.tap(function (vars) {
rfb.fb.width = vars.fb.width;
rfb.fb.height = vars.fb.height;
- rfb.send(
- Word8(clientMsgTypes.setEncodings),
- Pad8(),
- Word16be(2), // number of encodings following
- Word32be(encodings.raw),
- Word32be(encodings.copyRect)
- );
+ Put()
+ .word8(clientMsgTypes.setEncodings)
+ .pad(1)
+ .word16be(2) // number of encodings following
+ .word32be(encodings.raw)
+ .word32be(encodings.copyRect)
+ .send(rfb)
+ ;
})
.tap(function (vars) {
rfb.requestRedrawScreen();
View
@@ -0,0 +1,58 @@
+var Buffer = require('buffer').Buffer;
+
+exports.Put = Put;
+function Put () {
+ var self = this;
+ if (!(this instanceof Put)) return new Put;
+
+ var words = [];
+ var len = 0;
+
+ this.word8 = function (x) {
+ words.push({ bytes : 1, value : x });
+ len += 1;
+ return this;
+ };
+
+ [16,32,64].forEach(function (bits) {
+ self['word' + String(bits) + 'be'] = function (x) {
+ words.push({ endian : 'big', bytes : bits / 8, value : x });
+ len += bits / 8;
+ return self;
+ };
+ self['word' + String(bits) + 'le'] = function (x) {
+ words.push({ endian : 'little', bytes : bits / 8, value : x });
+ len += bits / 8;
+ return self;
+ };
+ });
+
+ this.pad = function (bytes) {
+ words.push({ endian : 'big', bytes : bytes, value : 0 });
+ len += bytes;
+ return this;
+ };
+
+ this.buffer = function () {
+ var buf = new Buffer(len);
+ var n = 0;
+ words.forEach(function (word) {
+ if (word.endian == 'big') {
+ for (var i = (word.bytes - 1) * 8; i >= 0; i -= 8) {
+ buf[n++] = (word.value >> i) & 0xff;
+ }
+ }
+ else {
+ for (var i = 0; i < word.bytes * 8; i += 8) {
+ buf[n++] = (word.value >> i) & 0xff;
+ }
+ }
+ });
+ return buf;
+ };
+
+ this.send = function (obj) {
+ obj.send(this.buffer());
+ };
+}
+

0 comments on commit f17553f

Please sign in to comment.