Skip to content

Commit

Permalink
trial to write to buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
c3wenjiaowang committed Jun 17, 2023
1 parent c0bb5e2 commit 563ba6e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
21 changes: 15 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const timeStamp = new Date().toISOString();
const net = require('net');
let clientConnected = false;
let client;
let buffer = '';

const responseFolder = path.join(os.tmpdir(), 'sync_rpc_responses');
if (!fs.existsSync(responseFolder)) {
Expand Down Expand Up @@ -214,12 +215,24 @@ function sendMessage(input) {
);
} else {
input.client = 'ready';
client.on('data', function(data) {
buffer += data.toString('utf8');
if (/\r\nRESPONSE\sEND\r\n/.test(buffer)) {
res = buffer.substring(0, buffer.length-14).trim();
buffer = '';
}
});
client.write(JSON.stringify(input).replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') + '\r\n');
res = readFromFile();
// res = readFromFile();

}
try {
return JSON.parse(res.toString('utf8'));
if (res != null) {
return JSON.parse(res.toString('utf8'));
} else {
res = {};
}
} catch (ex) {
if (res.error) {
if (typeof res.error === 'string') res.error = new Error(res.error);
Expand Down Expand Up @@ -252,10 +265,6 @@ function extractValue(msg) {
return msg.v;
}
function createClient(filename, args) {
// process.on('SIGSEGV', () => {
// logToFile(`Segmentation Fault occurred.`, localClientLog);
// process.exit(1);
// });
const id = extractValue(sendMessage({t: 1, f: filename, a: args}));
return function(args) {
return extractValue(sendMessage({t: 0, i: id, a: args}));
Expand Down
22 changes: 15 additions & 7 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ const index = [0];
const NULL_PROMISE = Promise.resolve(null);
const server = net.createServer({allowHalfOpen: true}, c => {
let responded = false;
function respond(data) {
function respond(data, end) {
if (responded) return;
responded = true;
c.end(JSON.stringify(data));
c.write(JSON.stringify(data));
if (end) {
c.end();
} else {
c.write("\r\nRESPONSE END\r\n");
}
}

let buffer = '';
c.on('error', function(err) {
respond({s: false, v: {code: err.code, message: err.message}});
respond({s: false, v: {code: err.code, message: err.message}}, true);
});
c.on('data', function(data) {
buffer += data.toString('utf8');
Expand All @@ -57,23 +62,26 @@ const server = net.createServer({allowHalfOpen: true}, c => {
return init(req.f, req.a);
}
console.log(`sending msg #${index[0]} to c3server.`);
console.log(`modules length: ${modules.length}`)
return modules[req.i](req.a);
}).then(
function(response) {
console.log(`sending response #${index[0]} to local client.`);
if (req.client != 'ready') {
respond({s: true, v: response});
respond({s: true, v: response}, true);
} else {
writeToFile(JSON.stringify({s: true, v: response}));
respond({s: true, v: response}, false);
// writeToFile(JSON.stringify({s: true, v: response}));
}
console.log(`sent response #${index[0]} to local client.`);
},
function(err) {
console.error(`error: ${err.message} in local server.`);
if (req.client != 'ready') {
respond({s: false, v: {code: err.code, message: err.message}});
respond({s: false, v: {code: err.code, message: err.message}}, true);
} else {
writeToFile(JSON.stringify({s: false, v: {code: err.code, message: err.message}}));
respond({s: false, v: {code: err.code, message: err.message}}, false);
// writeToFile(JSON.stringify({s: false, v: {code: err.code, message: err.message}}));
}
}
);
Expand Down

0 comments on commit 563ba6e

Please sign in to comment.