mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-22 03:03:18 +00:00
Added support for IPC concurrency on Windows
This commit is contained in:
@@ -3,52 +3,75 @@ var promise = require('promise');
|
||||
var nodeid = require('_agentNodeId')();
|
||||
var ipcPath = process.platform == 'win32' ? ('\\\\.\\pipe\\' + nodeid + '-DAIPC') : (process.cwd() + '/DAIPC');
|
||||
|
||||
function queryAgent(obj)
|
||||
function dataHandler(chunk)
|
||||
{
|
||||
var len;
|
||||
if (chunk.length < 4) { this.unshift(chunk); return; }
|
||||
if ((len = chunk.readUInt32LE(0)) > chunk.length) { this.unshift(chunk); return; }
|
||||
|
||||
var data = chunk.slice(4, len + 4);
|
||||
var payload = null;
|
||||
try
|
||||
{
|
||||
payload = JSON.parse(data.toString());
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
this.promise._rej('Invalid Response Received');
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
//this.promise._res(payload.result?payload.result:'');
|
||||
this.promise._res(payload.result, this);
|
||||
}
|
||||
catch (x)
|
||||
{
|
||||
}
|
||||
if ((len + 4) < chunk.length) { this.unshift(chunk.slice(4 + len)); }
|
||||
}
|
||||
function queryAgent(obj, prev)
|
||||
{
|
||||
var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
ret._obj = { cmd: 'query', value: obj };
|
||||
ret.client = require('net').createConnection({ path: ipcPath });
|
||||
ret.client.promise = ret;
|
||||
ret.client.on('connect', function ()
|
||||
console.log(obj, prev);
|
||||
if (prev == null)
|
||||
{
|
||||
this.on('data', function (chunk)
|
||||
ret.client = require('net').createConnection({ path: ipcPath });
|
||||
ret.client.on('connect', function ()
|
||||
{
|
||||
var len;
|
||||
if (chunk.length < 4) { this.unshift(chunk); return; }
|
||||
if ((len = chunk.readUInt32LE(0)) > chunk.length) { this.unshift(chunk); return;}
|
||||
console.log('ON CONNECT');
|
||||
this.on('data', dataHandler);
|
||||
this.on('end', function ()
|
||||
{
|
||||
this.promise._rej('closed');
|
||||
});
|
||||
|
||||
var data = chunk.slice(4, len + 4);
|
||||
var payload = null;
|
||||
try
|
||||
{
|
||||
payload = JSON.parse(data.toString());
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
this.promise._rej('Invalid Response Received');
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
//this.promise._res(payload.result?payload.result:'');
|
||||
this.promise._res(payload.result);
|
||||
}
|
||||
catch(x)
|
||||
{
|
||||
}
|
||||
if ((len + 4) < chunk.length) { this.unshift(chunk.slice(4 + len)); }
|
||||
var j = Buffer.from(JSON.stringify(ret._obj));
|
||||
var buf = Buffer.alloc(4 + j.length);
|
||||
buf.writeUInt32LE(j.length + 4, 0);
|
||||
j.copy(buf, 4);
|
||||
this.write(buf);
|
||||
});
|
||||
this.on('end', function ()
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.client = prev;
|
||||
ret.client.removeAllListeners('data');
|
||||
ret.client.removeAllListeners('end');
|
||||
ret.client.on('data', dataHandler);
|
||||
ret.client.on('end', function ()
|
||||
{
|
||||
this.promise._rej('closed');
|
||||
});
|
||||
|
||||
var j = Buffer.from(JSON.stringify(this.promise._obj));
|
||||
var j = Buffer.from(JSON.stringify(ret._obj));
|
||||
var buf = Buffer.alloc(4 + j.length);
|
||||
buf.writeUInt32LE(j.length + 4, 0);
|
||||
j.copy(buf, 4);
|
||||
this.write(buf);
|
||||
});
|
||||
ret.client.write(buf);
|
||||
}
|
||||
ret.client.promise = ret;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
@@ -61,11 +84,11 @@ function start()
|
||||
process._exit();
|
||||
}, 3000);
|
||||
|
||||
queryAgent('connection').then(function (res)
|
||||
queryAgent('connection').then(function (res, connection)
|
||||
{
|
||||
if (res == null) { res = '[NOT CONNECTED]'; }
|
||||
console.log('Mesh Agent connected to: ' + res);
|
||||
return (queryAgent('descriptors'));
|
||||
return (queryAgent('descriptors', connection));
|
||||
}).then(console.log).then(function () { process._exit(); }).catch(function () { process._exit(); });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user