Example #1
0
    number2Buffer: function(number, bytes) {
        if (number < 0) {
            throw new Error('"number" must greater than or equal to zero.');
        }
        bytes = bytes || 8;
        // 转换为16进制字符串
        var hex = number.toString(16);

        // ------- 将hex length补充成bytes x 2,不足则高位补0,超过则去掉高位
        var length = hex.length;
        var targetLength = bytes * 2;
        if (length < targetLength) {
            var i = targetLength - length;
            while (i > 0) {
                hex = '0' + hex;
                i--;
            }

        } else if (length > targetLength) {
            hex = hex.substring(length - targetLength);
        }

        // ------ 填充到buffer里,高位在前
        var buffer = new Buffer(bytes);
        var offset = 0;
        while (offset < bytes) {
            var bn = Number("0x" + hex.substring(offset * 2, (offset * 2) + 2));
            buffer.writeUInt8(bn, offset);
            offset++;
        }

        return buffer;
    },
Example #2
0
  this.deflate(dict, function (err, chunks, size) {
    if (err) return callback(err);

    var offset = type === 'SYN_STREAM' ? 18 : 14,
        total = (type === 'SYN_STREAM' ? 10 : 6) + size,
        frame = new Buffer(offset + size);;

    frame.writeUInt16BE(0x8002, 0, true); // Control + Version
    frame.writeUInt16BE(type === 'SYN_STREAM' ? 1 : 2, 2, true); // type
    frame.writeUInt32BE(total & 0x00ffffff, 4, true); // No flag support
    frame.writeUInt32BE(id & 0x7fffffff, 8, true); // Stream-ID

    if (type === 'SYN_STREAM') {
      frame[4] = 2;
      frame.writeUInt32BE(assoc & 0x7fffffff, 12, true); // Stream-ID
    }

    frame.writeUInt8(priority & 0x3, 16, true); // Priority

    for (var i = 0; i < chunks.length; i++) {
      chunks[i].copy(frame, offset);
      offset += chunks[i].length;
    }

    callback(null, frame);
  });
Example #3
0
var arrayBufferToBuffer = function(arrayBuffer) {
  var buffer = new Buffer(arrayBuffer.byteLength);
  var uint8Array = new Uint8Array(arrayBuffer);
  for(var i = 0; i < uint8Array.length; i++) {
    buffer.writeUInt8(uint8Array[i], i, true);
  }
  return buffer;
};
Example #4
0
    packHeader: function(command, bodyLength, status) {
        if (!bodyLength) {
            bodyLength = 0;
        }

        if (!status) {
            status = 0;
        }

        // ----------- 存放1字节的command和1字节的status
        var buffer = new Buffer(2);
        buffer.writeUInt8(command, 0);
        buffer.writeUInt8(status, 1);

        // 生成8bytes存放body length的buffer
        var blBuffer = helpers.number2Buffer(bodyLength, 8);

        // 拼接
        return Buffer.concat([blBuffer, buffer]);
    },
Example #5
0
Notification.prototype.toNetwork = function () {
    var data = null
    ,   token = this.device.toNetwork()
    ,   tokenLength = token.length
    ,   payloadString = this.payloadString()
    ,   payloadLength = Buffer.byteLength(payloadString, this.encoding);

    var token_and_payload_size = 2 + tokenLength + 2 + payloadLength;
    
    if (typeof(this.identifier) !== "undefined") { /* extended notification format */

        data = new Buffer(1 + 4 + 4 + token_and_payload_size);
        
        data.writeUInt8(1,                  0);                                     
        data.writeUInt32BE(this.identifier, 1);                                     
        data.writeUInt32BE(this.expiry,     5); // Apple's doc says it could be negative but the C example uses uint32_t ??!
                                            // Comments ?...
                                            //
                                            // SOURCE: You can specify zero or a value less than zero to request that APNs not store the notification at all.
                                            //
                                            //     /* expiry date network order */
                                            //     memcpy(binaryMessagePt, &networkOrderExpiryEpochUTC, sizeof(uint32_t));

    } else { /* simple notification format */

        data = new Buffer(1 + token_and_payload_size);

        data.writeUInt8(0, 0);
        
    }

    var token_start_index = data.length - token_and_payload_size;

    data.writeUInt16BE(tokenLength,     token_start_index);
    token.copy(data,                    token_start_index + 2);
    data.writeUInt16BE(payloadLength,   token_start_index + 2 + tokenLength);
    data.write(payloadString,           token_start_index + 2 + tokenLength + 2, payloadLength, this.encoding);

    return data;
};
Example #6
0
test('test that create-delta works as expected', function(assert) {
  var size = 1024 + ((Math.random() * 1024) | 0)
    , from = new Buffer(size)
    , to = new Buffer(size)
    , val


  for(var i = 0; i < size; ++i) {
    val = Math.random() * 0xFF & 0xFF
    from.writeUInt8(val, i)
    to.writeUInt8(Math.random() > 0.4 ? val : (val + 1) & 0xFF, i)
  }

  var delta = create(from, to) 
    , undeltad = apply(delta, from)

  for(var i = 0; i < size; ++i) {
    assert.equal(undeltad[i], to[i])
  }

  assert.end()
})
Example #7
0
Framer.prototype.dataFrame = function dataFrame(id, fin, data) {
  if (!fin && !data.length) return [];

  var frame = new Buffer(8 + data.length);

  frame.writeUInt32BE(id & 0x7fffffff, 0, true);
  frame.writeUInt32BE(data.length & 0x00ffffff, 4, true);
  frame.writeUInt8(fin ? 0x01 : 0x0, 4, true);

  if (data.length) data.copy(frame, 8);

  return frame;
};
Example #8
0
Packet.prototype.toBuffer = function() {
    var i,
        data = this.getData(),
        serviceType = this._serviceType,
        totalLength = 6 + data.length,
        buffer = new Buffer(totalLength);

    buffer[0] = 0x06; // header length
    buffer[1] = 0x10; // protocol version
    buffer[2] = (serviceType >> 8) & 0xff;
    buffer[3] = serviceType & 0xff;
    buffer[4] = (totalLength >> 8) & 0xff;
    buffer[5] = totalLength & 0xff;

    for (i = 0; i < data.length; i++) {
        buffer.writeUInt8(data[i], i + 6);
    }

    return buffer;
};
	web_socket.on('message', function(data) {
		// console.log("message from client: " + data.msg);

		// var buf = new Buffer(data.msg);

		var buf = new Buffer(data.msg.length);

		for (var i=0; i<data.msg.length; i++){
			buf.writeUInt8(parseInt(data.msg[i]), i);
		}
		
		// console.log("buffer sent: " + buf);



		socket.send(buf, 0, buf.length, 8888, "10.118.73.220", function(err, bytes) {
			if(err) console.log('error: ' + err);
			// else console.log('successful');
		});
	});
Example #10
0
  socketOnConnect() {
    this.emit('connect');

    if (this.tcp) {
      this.send(this.password, PacketType.AUTH);
    } else if (this.challenge) {
      let str = "challenge rcon\n";
      let sendBuf = new Buffer(str.length + 4);
      sendBuf.writeInt32LE(-1, 0);
      sendBuf.write(str, 4);
      this._sendSocket(sendBuf);
    } else {
      let sendBuf = new Buffer(5);
      sendBuf.writeInt32LE(-1, 0);
      sendBuf.writeUInt8(0, 4);
      this._sendSocket(sendBuf);

      this.hasAuthed = true;
      this.emit('auth');
    }
  }
Example #11
0
    written = buf.write('abcd', 0, 2, encoding);
    console.log(buf);
    assert.equal(written, 2);
    assert.equal(buf[0], 0x61);
    assert.equal(buf[1], 0x00);
    assert.equal(buf[2], 0xFF);
    assert.equal(buf[3], 0xFF);
  });
}

{
  // test offset returns are correct
  const b = new Buffer(16);
  assert.equal(4, b.writeUInt32LE(0, 0));
  assert.equal(6, b.writeUInt16LE(0, 4));
  assert.equal(7, b.writeUInt8(0, 6));
  assert.equal(8, b.writeInt8(0, 7));
  assert.equal(16, b.writeDoubleLE(0, 8));
}

{
  // test unmatched surrogates not producing invalid utf8 output
  // ef bf bd = utf-8 representation of unicode replacement character
  // see https://codereview.chromium.org/121173009/
  const buf = new Buffer('ab\ud800cd', 'utf8');
  assert.equal(buf[0], 0x61);
  assert.equal(buf[1], 0x62);
  assert.equal(buf[2], 0xef);
  assert.equal(buf[3], 0xbf);
  assert.equal(buf[4], 0xbd);
  assert.equal(buf[5], 0x63);
Example #12
0
        // Seriously this is strange, that it is happening only in windows
        message_parser.destroy = function destroy(){
            console.log('Surprise surprise, you are on windows!')
            message_parser.flush();
        }
        parse_new_message(message_parser);
        stream.pipe(message_parser);        
    } else {
        return new Parser(emitter, stream);
    }
}


var ConnectionValidation = new buffer.Buffer(14);
ConnectionValidation.write('IceP');                 // magic
ConnectionValidation.writeUInt8(1, 4);              // Protocol Major version
ConnectionValidation.writeUInt8(0, 5);              // Protocol Minor version
ConnectionValidation.writeUInt8(1, 6);              // Encoding Major version
ConnectionValidation.writeUInt8(0, 7);              // Encoding Minor version
ConnectionValidation.writeUInt8(3, 8);              // "Connection Validation" Type
ConnectionValidation.writeUInt8(0, 9);              // Compression type (always zero for validation)
ConnectionValidation.writeUInt32LE(ice_message_header_length, 10);


var stringified_message_type = function(integer) {
    switch (integer) {
        case 0: return 'request';
        case 1: return 'batch request';
        case 2: return 'reply';
        case 3: return 'validate connection';
        case 4: return 'close connection';
Example #13
0
NetWriter.prototype.writeUInt8 = function (value) {
    var buffer = new Buffer(1);
    buffer.writeUInt8(value, 0, true);
    this.content.push(buffer);
    this.size += 1;
};