當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript node-opcua-buffer-utils.createFastUninitializedBuffer函數代碼示例

本文整理匯總了TypeScript中node-opcua-buffer-utils.createFastUninitializedBuffer函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript createFastUninitializedBuffer函數的具體用法?TypeScript createFastUninitializedBuffer怎麽用?TypeScript createFastUninitializedBuffer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了createFastUninitializedBuffer函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: constructor

 constructor(data: undefined | Buffer | number) {
     if (data === undefined) {
         this.buffer = createFastUninitializedBuffer(1024);
     } else if (typeof data === "number") {
         this.buffer = createFastUninitializedBuffer(data);
     } else {
         assert(data instanceof Buffer);
         this.buffer = data;
     }
     this.length = 0;
 }
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:11,代碼來源:binaryStream.ts

示例2: randomByteString

export function randomByteString(value: any, len: number): ByteString {
    len = len || getRandomInt(1, 200);
    const b = createFastUninitializedBuffer(len);
    for (let i = 0; i < len; i++) {
        b.writeUInt8(getRandomInt(0, 255), i);
    }
    return b;
}
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:8,代碼來源:byte_string.ts

示例3: packTcpMessage

export function packTcpMessage(msgType: string, encodableObject: BaseUAObject): Buffer {

    assert(is_valid_msg_type(msgType));

    const messageChunk = createFastUninitializedBuffer(encodableObject.binaryStoreSize() + 8);
    // encode encodeableObject in a packet
    const stream = new BinaryStream(messageChunk);
    encodeMessage(msgType, encodableObject, stream);

    return messageChunk;

}
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:12,代碼來源:tools.ts

示例4: createChunk

    /**
     * ```createChunk``` is used to construct a pre-allocated chunk to store up to ```length``` bytes of data.
     * The created chunk includes a prepended header for ```chunk_type``` of size ```self.headerSize```.
     *
     * @method createChunk
     * @param msgType
     * @param chunkType {String} chunk type. should be 'F' 'C' or 'A'
     * @param length
     * @return {Buffer} a buffer object with the required length representing the chunk.
     *
     * Note:
     *  - only one chunk can be created at a time.
     *  - a created chunk should be committed using the ```write``` method before an other one is created.
     */
    public createChunk(msgType: string, chunkType: string, length: number): Buffer {

        assert(msgType === "MSG");
        assert(this._pendingBuffer === undefined, "createChunk has already been called ( use write first)");

        const totalLength = length + this.headerSize;
        const buffer = createFastUninitializedBuffer(totalLength);
        writeTCPMessageHeader("MSG", chunkType, totalLength, buffer);

        this._pendingBuffer = buffer;

        return buffer;
    }
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:27,代碼來源:tcp_transport.ts

示例5: _append

    /**
     * append a message chunk
     * @method _append
     * @param chunk
     * @private
     */
    private _append(chunk: Buffer): boolean {

        if (this._hasReceivedError) {
            // the message builder is in error mode and further message chunks should be discarded.
            return false;
        }

        this.messageChunks.push(chunk);
        this.totalMessageSize += chunk.length;

        const binaryStream = new BinaryStream(chunk);

        if (!this._read_headers(binaryStream)) {
            return false;
        }

        assert(binaryStream.length >= 12);

        // verify message chunk length
        if (this.messageHeader.length !== chunk.length) {
            // tslint:disable:max-line-length
            return this._report_error(
              `Invalid messageChunk size: the provided chunk is ${chunk.length} bytes long but header specifies ${this.messageHeader.length}`);
        }

        // the start of the message body block
        const offsetBodyStart = binaryStream.length;

        // the end of the message body block
        const offsetBodyEnd = binaryStream.buffer.length;

        this.totalBodySize += (offsetBodyEnd - offsetBodyStart);
        this.offsetBodyStart = offsetBodyStart;

        // add message body to a queue
        // note : Buffer.slice create a shared memory !
        //        use Buffer.clone
        const sharedBuffer = chunk.slice(offsetBodyStart, offsetBodyEnd);
        const clonedBuffer = createFastUninitializedBuffer(sharedBuffer.length);

        sharedBuffer.copy(clonedBuffer, 0, 0);
        this.blocks.push(clonedBuffer);

        return true;
    }
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:51,代碼來源:message_builder_base.ts

示例6: write

    public write(buffer: Buffer, length?: number) {

        length = length || buffer.length;
        assert(buffer instanceof Buffer || (buffer === null));
        assert(length > 0);

        let l = length;
        let inputCursor = 0;

        while (l > 0) {
            assert(length - inputCursor !== 0);

            if (this.cursor === 0) {
                this._push_pending_chunk(false);
            }

            // space left in current chunk
            const spaceLeft = this.maxBodySize - this.cursor;

            const nbToWrite = Math.min(length - inputCursor, spaceLeft);

            this.chunk = this.chunk || createFastUninitializedBuffer(this.chunkSize);

            if (buffer) {
                buffer.copy(this.chunk!, this.cursor + this.dataOffset, inputCursor, inputCursor + nbToWrite);
            }

            inputCursor += nbToWrite;
            this.cursor += nbToWrite;

            if (this.cursor >= this.maxBodySize) {
                this._post_process_current_chunk();
            }
            l -= nbToWrite;
        }
    }
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:36,代碼來源:chunk_manager.ts

示例7: calculateByteLength

        this.length += bufLen;
        return str;
    }
}

/**
 * @function calculateByteLength
 * calculate the size in bytes of a utf8 string
 * @param str {String}
 * @internal
 */
export function calculateByteLength(str: string) {
    // returns the byte length of an utf8 string
    let s = str.length;
    for (let i = str.length - 1; i >= 0; i--) {
        const code = str.charCodeAt(i);
        if (code > 0x7f && code <= 0x7ff) {
            s++;
        } else if (code > 0x7ff && code <= 0xffff) {
            s += 2;
        }
        if (code >= 0xdc00 && code <= 0xdfff) {
            // trail surrogate
            i--;
        }
    }
    return s;
}

const zeroLengthBuffer = createFastUninitializedBuffer(0);
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:30,代碼來源:binaryStream.ts

示例8: createUserNameIdentityToken

function createUserNameIdentityToken(
  session: ClientSessionImpl,
  userName: string | null,
  password: string | null
): UserNameIdentityToken {

    // assert(endpoint instanceof EndpointDescription);
    assert(userName === null || typeof userName === "string");
    assert(password === null || typeof password === "string");
    const endpoint = session.endpoint;
    assert(endpoint instanceof EndpointDescription);

    /**
     * OPC Unified Architecture 1.0.4:  Part 4 155
     * Each UserIdentityToken allowed by an Endpoint shall have a UserTokenPolicy specified in the
     * EndpointDescription. The UserTokenPolicy specifies what SecurityPolicy to use when encrypting
     * or signing. If this SecurityPolicy is omitted then the Client uses the SecurityPolicy in the
     * EndpointDescription. If the matching SecurityPolicy is set to None then no encryption or signature
     * is required.
     *
     */
    const userTokenPolicy = findUserTokenPolicy(endpoint, UserTokenType.UserName);

    // istanbul ignore next
    if (!userTokenPolicy) {
        throw new Error("Cannot find USERNAME user token policy in end point description");
    }

    let securityPolicy = fromURI(userTokenPolicy.securityPolicyUri);

    // if the security policy is not specified we use the session security policy
    if (securityPolicy === SecurityPolicy.Invalid) {
        securityPolicy = session._client._secureChannel.securityPolicy;
    }

    let identityToken;
    let serverCertificate: Buffer | string | null = session.serverCertificate;
    // if server does not provide certificate use unencrypted password
    if (!serverCertificate) {
        identityToken = new UserNameIdentityToken({
            encryptionAlgorithm: null,
            password: Buffer.from(password as string, "utf-8"),
            policyId: userTokenPolicy ? userTokenPolicy!.policyId : null,
            userName
        });
        return identityToken;
    }

    assert(serverCertificate instanceof Buffer);
    serverCertificate = toPem(serverCertificate, "CERTIFICATE");
    const publicKey = extractPublicKeyFromCertificateSync(serverCertificate);

    const serverNonce: Nonce = session.serverNonce || Buffer.alloc(0);
    assert(serverNonce instanceof Buffer);

    // If None is specified for the UserTokenPolicy and SecurityPolicy is None
    // then the password only contains the UTF-8 encoded password.
    // note: this means that password is sent in clear text to the server
    // note: OPCUA specification discourages use of unencrypted password
    //       but some old OPCUA server may only provide this policy and we
    //       still have to support in the client?
    if (securityPolicy === SecurityPolicy.None) {
        identityToken = new UserNameIdentityToken({
            encryptionAlgorithm: null,
            password: Buffer.from(password!, "utf-8"),
            policyId: userTokenPolicy.policyId,
            userName
        });
        return identityToken;
    }

    // see Release 1.02 155 OPC Unified Architecture, Part 4
    const cryptoFactory = getCryptoFactory(securityPolicy);

    // istanbul ignore next
    if (!cryptoFactory) {
        throw new Error(" Unsupported security Policy");
    }

    identityToken = new UserNameIdentityToken({
        encryptionAlgorithm: cryptoFactory.asymmetricEncryptionAlgorithm,
        password: Buffer.from(password as string, "utf-8"),
        policyId: userTokenPolicy.policyId,
        userName
    });

    // now encrypt password as requested
    const lenBuf = createFastUninitializedBuffer(4);
    lenBuf.writeUInt32LE(identityToken.password.length + serverNonce.length, 0);
    const block = Buffer.concat([lenBuf, identityToken.password, serverNonce]);
    identityToken.password = cryptoFactory.asymmetricEncrypt(block, publicKey);

    return identityToken;
}
開發者ID:node-opcua,項目名稱:node-opcua,代碼行數:94,代碼來源:opcua_client_impl.ts


注:本文中的node-opcua-buffer-utils.createFastUninitializedBuffer函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。