How to use the bytebuffer.allocate function in bytebuffer

To help you get started, we’ve selected a few bytebuffer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Voxelum / minecraft-launcher-core-node / src / libs / client.ts View on Github external
private handlePacket(buffer: Buffer) {
        if (!this.cache) {
            const incoming = ByteBuffer.wrap(buffer);
            this.remaining = incoming.readVarint32();
            this.cache = ByteBuffer.allocate(this.remaining);
            this.remaining -= incoming.remaining();
            this.cache.append(incoming.slice(incoming.offset));
        } else {
            this.cache.append(buffer);
            this.remaining -= buffer.byteLength;
            if (this.remaining <= 0) {
                const packetId = this.cache.readByte();
                this.emit("packet", this.cache.slice(this.cache.offset));
                // handler.accept(this.cache.slice(this.cache.offset))
            }
        }
    }
}
github DoctorMcKay / node-steam-user / components / messages.js View on Github external
// Clean up old job callbacks after 2 minutes
		setTimeout(() => delete this._jobs[jobIdSource], 1000 * 60 * 2);
	}

	let emsgName = EMsg[emsg] || emsg;
	if (emsg == EMsg.ServiceMethodCallFromClient && header.proto && header.proto.target_job_name) {
		emsgName = header.proto.target_job_name;
	}

	this.emit(VERBOSE_EMSG_LIST.includes(emsg) ? 'debug-verbose' : 'debug', 'Sending message: ' + emsgName);

	// Make the header
	let hdrBuf;
	if (header.msg == EMsg.ChannelEncryptResponse) {
		// since we're setting up the encrypted channel, we use this very minimal header
		hdrBuf = ByteBuffer.allocate(4 + 8 + 8, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg);
		hdrBuf.writeUint64(header.targetJobID || JOBID_NONE);
		hdrBuf.writeUint64(jobIdSource || header.sourceJobID || JOBID_NONE);
	} else if (header.proto) {
		// if we have a protobuf header, use that
		header.proto.client_sessionid = this._sessionID || 0;
		header.proto.steamid = (this.steamID || this._tempSteamID).getSteamID64();
		header.proto.jobid_source = jobIdSource || header.proto.jobid_source || header.sourceJobID || JOBID_NONE;
		header.proto.jobid_target = header.proto.jobid_target || header.targetJobID || JOBID_NONE;
		let hdrProtoBuf = exports.encodeProto(Schema.CMsgProtoBufHeader, header.proto);
		hdrBuf = ByteBuffer.allocate(4 + 4 + hdrProtoBuf.length, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg | PROTO_MASK);
		hdrBuf.writeUint32(hdrProtoBuf.length);
		hdrBuf.append(hdrProtoBuf);
	} else {
		// this is the standard non-protobuf extended header
github DoctorMcKay / node-steam-user / components / logon.js View on Github external
function createMachineID(val_bb3, val_ff2, val_3b3) {
	// Machine IDs are binary KV objects with root key MessageObject and three hashes named BB3, FF2, and 3B3.
	// I don't feel like writing a proper BinaryKV serializer, so this will work fine.

	let buffer = ByteBuffer.allocate(155, ByteBuffer.LITTLE_ENDIAN);
	buffer.writeByte(0); // 1 byte, total 1
	buffer.writeCString("MessageObject"); // 14 bytes, total 15

	buffer.writeByte(1); // 1 byte, total 16
	buffer.writeCString("BB3"); // 4 bytes, total 20
	buffer.writeCString(sha1(val_bb3)); // 41 bytes, total 61

	buffer.writeByte(1); // 1 byte, total 62
	buffer.writeCString("FF2"); // 4 bytes, total 66
	buffer.writeCString(sha1(val_ff2)); // 41 bytes, total 107

	buffer.writeByte(1); // 1 byte, total 108
	buffer.writeCString("3B3"); // 4 bytes, total 112
	buffer.writeCString(sha1(val_3b3)); // 41 bytes, total 153

	buffer.writeByte(8); // 1 byte, total 154
github DoctorMcKay / node-steam-user / components / messages.js View on Github external
hdrBuf.writeUint64(header.targetJobID || JOBID_NONE);
		hdrBuf.writeUint64(jobIdSource || header.sourceJobID || JOBID_NONE);
	} else if (header.proto) {
		// if we have a protobuf header, use that
		header.proto.client_sessionid = this._sessionID || 0;
		header.proto.steamid = (this.steamID || this._tempSteamID).getSteamID64();
		header.proto.jobid_source = jobIdSource || header.proto.jobid_source || header.sourceJobID || JOBID_NONE;
		header.proto.jobid_target = header.proto.jobid_target || header.targetJobID || JOBID_NONE;
		let hdrProtoBuf = exports.encodeProto(Schema.CMsgProtoBufHeader, header.proto);
		hdrBuf = ByteBuffer.allocate(4 + 4 + hdrProtoBuf.length, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg | PROTO_MASK);
		hdrBuf.writeUint32(hdrProtoBuf.length);
		hdrBuf.append(hdrProtoBuf);
	} else {
		// this is the standard non-protobuf extended header
		hdrBuf = ByteBuffer.allocate(4 + 1 + 2 + 8 + 8 + 1 + 8 + 4, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg);
		hdrBuf.writeByte(36);
		hdrBuf.writeUint16(2);
		hdrBuf.writeUint64(header.targetJobID || JOBID_NONE);
		hdrBuf.writeUint64(jobIdSource || header.sourceJobID || JOBID_NONE);
		hdrBuf.writeByte(239);
		hdrBuf.writeUint64((this.steamID || this._tempSteamID).getSteamID64());
		hdrBuf.writeUint32(this._sessionID || 0);
	}

	this._connection.send(Buffer.concat([hdrBuf.flip().toBuffer(), body]));
};
github DoctorMcKay / node-steam-user / components / messages.js View on Github external
// Make the header
	let hdrBuf;
	if (header.msg == EMsg.ChannelEncryptResponse) {
		// since we're setting up the encrypted channel, we use this very minimal header
		hdrBuf = ByteBuffer.allocate(4 + 8 + 8, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg);
		hdrBuf.writeUint64(header.targetJobID || JOBID_NONE);
		hdrBuf.writeUint64(jobIdSource || header.sourceJobID || JOBID_NONE);
	} else if (header.proto) {
		// if we have a protobuf header, use that
		header.proto.client_sessionid = this._sessionID || 0;
		header.proto.steamid = (this.steamID || this._tempSteamID).getSteamID64();
		header.proto.jobid_source = jobIdSource || header.proto.jobid_source || header.sourceJobID || JOBID_NONE;
		header.proto.jobid_target = header.proto.jobid_target || header.targetJobID || JOBID_NONE;
		let hdrProtoBuf = exports.encodeProto(Schema.CMsgProtoBufHeader, header.proto);
		hdrBuf = ByteBuffer.allocate(4 + 4 + hdrProtoBuf.length, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg | PROTO_MASK);
		hdrBuf.writeUint32(hdrProtoBuf.length);
		hdrBuf.append(hdrProtoBuf);
	} else {
		// this is the standard non-protobuf extended header
		hdrBuf = ByteBuffer.allocate(4 + 1 + 2 + 8 + 8 + 1 + 8 + 4, ByteBuffer.LITTLE_ENDIAN);
		hdrBuf.writeUint32(header.msg);
		hdrBuf.writeByte(36);
		hdrBuf.writeUint16(2);
		hdrBuf.writeUint64(header.targetJobID || JOBID_NONE);
		hdrBuf.writeUint64(jobIdSource || header.sourceJobID || JOBID_NONE);
		hdrBuf.writeByte(239);
		hdrBuf.writeUint64((this.steamID || this._tempSteamID).getSteamID64());
		hdrBuf.writeUint32(this._sessionID || 0);
	}
github DoctorMcKay / node-steam-user / components / appauth.js View on Github external
let buildToken = () => {
				let gcToken = this._gcTokens.splice(0, 1)[0];
				let buffer = ByteBuffer.allocate(4 + gcToken.length + 4 + 24 + 4 + ticket.length, ByteBuffer.LITTLE_ENDIAN);
				buffer.writeUint32(gcToken.length);
				buffer.append(gcToken);
				buffer.writeUint32(24); // length of the session header, which is always 24 bytes
				buffer.writeUint32(1); // unknown 1
				buffer.writeUint32(2); // unknown 2
				buffer.writeUint32(StdLib.IPv4.stringToInt(this.publicIP)); // external IP
				buffer.writeUint32(0); // filler
				buffer.writeUint32(Date.now() - this._connectTime); // timestamp
				buffer.writeUint32(++this._connectionCount); // connection count
				buffer.writeUint32(ticket.length);
				buffer.append(ticket);

				buffer = buffer.flip().toBuffer();

				// We need to activate our ticket
				this.validateAuthTickets(appid, buffer, () => {
github Voxelum / minecraft-launcher-core-node / src / libs / client.ts View on Github external
return new Promise((resolve, reject) => {
        const buffer = ByteBuffer.allocate(256);
        // packet id
        buffer.writeByte(0x00);
        // protocol version
        buffer.writeVarint32(210);
        writeString(buffer, host);

        buffer.writeShort(port & 0xffff);
        buffer.writeVarint32(1);
        buffer.flip();
        const handshakeBuf = ByteBuffer.allocate(buffer.limit + 8);
        handshakeBuf.writeVarint32(buffer.limit);
        handshakeBuf.append(buffer);
        handshakeBuf.flip();
        connection.write(Buffer.from(handshakeBuf.toArrayBuffer()));
        connection.write(Buffer.from([0x01, 0x00]));
        let remain: number | undefined;
        let msg: ByteBuffer;
        const listener = (incoming: Buffer) => {
            const inbuf = ByteBuffer.wrap(incoming);
            if (remain === undefined) {
                remain = inbuf.readVarint32();
                msg = ByteBuffer.allocate(remain);
                remain -= inbuf.remaining();
                msg.append(inbuf.slice(inbuf.offset));
            } else {
                msg.append(inbuf);
github Cirrus-Link / Sparkplug / stand_alone_examples / js_example / lib / kurapayload.js View on Github external
(function () {
    var ProtoBuf = require("protobufjs"),
        ByteBuffer = require("bytebuffer"),
        tempBuffer = ByteBuffer.allocate(1024);
    
    var builder = ProtoBuf.loadProtoFile('../../kurapayload.proto'),
        KuraDataTypes = builder.build('kuradatatypes');
        KuraPayload = KuraDataTypes.KuraPayload,
        KuraMetric = KuraPayload.KuraMetric,
        KuraPosition = KuraPayload.KuraPosition,
        ValueType = KuraMetric.ValueType;
    
    exports.generateKuraPayload = function(object) {
        var newPayload = new KuraPayload(object.timestamp);
        
        // Build up the position
        if (object.position !== undefined && object.position !== null) {
            var position = object.position,
                newPosition = new KuraPosition(position.latitude, position.longitude);
github leonardosnt / java-class-tools / test / tests.js View on Github external
function compareFloatBits(f1, f2) {
  let buf1 = ByteBuffer.allocate(4), buf2 = ByteBuffer.allocate(4)
  buf1.writeFloat(f1).flip();
  buf2.writeFloat(f2).flip();
  return buf1.buffer.equals(buf2.buffer);
}
github heatcrypto / heat-sdk / src / socket.ts View on Github external
.then(websocket => {
          let buffer = ByteBuffer.allocate(4 + 4 + 2 + request.byteLength).order(
            ByteBuffer.LITTLE_ENDIAN
          )
          buffer.writeInt32(MAGIC_NUM)
          buffer.writeInt32(callId)
          buffer.writeInt16(method)
          buffer.append(request)
          websocket.send(buffer.buffer)
        })
        .catch(reject)