2017-06-27 20:23:09 +01:00
|
|
|
const net = require('net');
|
|
|
|
const RpcMessage = require('./rpc-message');
|
|
|
|
|
|
|
|
let PipePrefix;
|
2017-06-27 21:19:36 +01:00
|
|
|
let PipePostfix;
|
2017-06-27 20:23:09 +01:00
|
|
|
if (process.platform == 'win32') {
|
2017-06-27 21:19:36 +01:00
|
|
|
PipePrefix = '\\\\.\\pipe\\';
|
|
|
|
PipePostfix = '';
|
2017-06-27 20:23:09 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PipePrefix = "/tmp";
|
2017-06-27 21:19:36 +01:00
|
|
|
PipePostfix = '.pipe';
|
2017-06-27 20:23:09 +01:00
|
|
|
}
|
2017-06-29 16:58:03 +01:00
|
|
|
const PipePath = PipePrefix + 'DiscordRpcServer' + PipePostfix;
|
|
|
|
let connections = 0;
|
2017-06-27 20:23:09 +01:00
|
|
|
|
2017-06-29 16:58:03 +01:00
|
|
|
const server = net.createServer(function(sock) {
|
|
|
|
connections += 1;
|
|
|
|
console.log('Server: on connection', connections);
|
|
|
|
let myConnection = connections;
|
2017-06-27 20:23:09 +01:00
|
|
|
|
2017-06-29 16:58:03 +01:00
|
|
|
sock.on('data', function(data) {
|
2017-06-27 20:23:09 +01:00
|
|
|
const msgObj = RpcMessage.deserialize(data);
|
|
|
|
if (msgObj != null) {
|
2017-06-29 16:58:03 +01:00
|
|
|
console.log('Server: on data:', myConnection, msgObj);
|
2017-06-27 20:23:09 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-06-29 16:58:03 +01:00
|
|
|
console.log('Server: got some data', data.toString());
|
2017-06-27 20:23:09 +01:00
|
|
|
}
|
|
|
|
});
|
2017-06-29 16:58:03 +01:00
|
|
|
|
|
|
|
sock.on('end', function() {
|
|
|
|
connections -= 1;
|
|
|
|
console.log('Server: on end', connections);
|
2017-06-27 20:23:09 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.on('close', function(){
|
|
|
|
console.log('Server: on close');
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
server.listen(PipePath, function(){
|
|
|
|
console.log('Server: on listening');
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
console.error('could not start server:', e);
|
2017-06-29 16:58:03 +01:00
|
|
|
}
|