Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function get doesnt set shm_size and always returns 1024 array #10

Open
enseitankado opened this issue Jun 16, 2020 · 0 comments
Open

Comments

@enseitankado
Copy link

enseitankado commented Jun 16, 2020

SHM_arr = shm.create(4, 'Uint8Array', SHM_SEGMET_KEY);
SHM_arr[0] = Math.floor(Math.random() * 255);
SHM_arr[1] = Math.floor(Math.random() * 255);
node.log(SHM_arr);
SHM_arr = shm.get(SHM_SEGMET_KEY, 'Uint8Array');
node.log(SHM_arr);

Outputs:

16 Jun 05:38:13 - [info] [ss963-driver:4a2c9bf7.0a86dc] 114,50,0,0
16 Jun 05:38:13 - [info] [ss963-driver:4a2c9bf7.0a86dc] 114,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

There are the functions below. In "function create", shm.get sets the shm_size but "function get" doesnt so returns 1024 bytes length array as seen above.

/**

  • Create shared memory segment
  • @param {int} count - number of elements
  • @param {string} typeKey - see keys of BufferType
  • @param {int/null} key - integer key of shared memory segment, or null to autogenerate
  • @return {mixed/null} shared memory buffer/array object, or null on error
  • Class depends on param typeKey: Buffer or descendant of TypedArray
  • Return object has property 'key' - integer key of created shared memory segment
    /
    function create(count, typeKey /
    = 'Buffer'/, key /= null*/) {
    if (typeKey === undefined)
    typeKey = 'Buffer';
    if (key === undefined)
    key = null;
    if (BufferType[typeKey] === undefined)
    throw new Error("Unknown type key " + typeKey);
    if (key !== null) {
    if (!(Number.isSafeInteger(key) && key >= keyMin && key <= keyMax))
    throw new RangeError('Shm key should be ' + keyMin + ' .. ' + keyMax);
    }
    var type = BufferType[typeKey];
    //var size1 = BufferTypeSizeof[typeKey];
    //var size = size1 * count;
    if (!(Number.isSafeInteger(count) && count >= lengthMin && count <= lengthMax))
    throw new RangeError('Count should be ' + lengthMin + ' .. ' + lengthMax);
    let res;
    if (key) {
    res = shm.get(key, count, shm.IPC_CREAT|shm.IPC_EXCL|perm, 0, type);
    } else {
    do {
    key = _keyGen();
    res = shm.get(key, count, shm.IPC_CREAT|shm.IPC_EXCL|perm, 0, type);
    } while(!res);
    }
    if (res) {
    res.key = key;
    }
    return res;
    }

/**

  • Get shared memory segment
  • @param {int} key - integer key of shared memory segment
  • @param {string} typeKey - see keys of BufferType
  • @return {mixed/null} shared memory buffer/array object, see create(), or null on error
    /
    function get(key, typeKey /
    = 'Buffer'*/) {
    if (typeKey === undefined)
    typeKey = 'Buffer';
    if (BufferType[typeKey] === undefined)
    throw new Error("Unknown type key " + typeKey);
    var type = BufferType[typeKey];
    if (!(Number.isSafeInteger(key) && key >= keyMin && key <= keyMax))
    throw new RangeError('Shm key should be ' + keyMin + ' .. ' + keyMax);
    let res = shm.get(key, 0, 0, 0, type);
    if (res) {
    res.key = key;
    }
    return res;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant