Skip to content

Instantly share code, notes, and snippets.

@scaret
Last active February 24, 2019 06:18
Show Gist options
  • Save scaret/cfcd34641908abfb2712577ea3be798a to your computer and use it in GitHub Desktop.
Save scaret/cfcd34641908abfb2712577ea3be798a to your computer and use it in GitHub Desktop.
var res = new MyBuf(msg);
res.skip(2); // length
res.skip(2); // uri
res.skip(2); // server_type
res.skip(4); // flag
var code = parseInt(res.readUInt32LE()); // code
if (code > 0) { // error code
var exe_error_str = VOCS_CODE_MAP[code];
if (exe_error_str) {
return callback(exe_error_str);
} else {
var err = `UNKNOWN_ERROR_${code}`;
return callback(err);
}
}
var cid = res.readUInt32LE();
var uid = res.readUInt32LE();
res.skip(4);
res.skip(4); // time stamp
var len = res.readUInt16LE();
var channel_name = res.read(len);
var gateway_count = res.readUInt16LE();
// Logger.info(`Return from gwcs - cname: ${channel_name}, cid: ${cid}, uid: ${uid}`);
// get gateway list from the response
var gateway_list = [];
for (var i = 0; i < gateway_count; i++) {
var gateway_info = {};
var ip_len = res.readUInt16LE(); // len
var ip_str = res.read(ip_len);
gateway_info.port = parseInt(ip_str.split(":")[1]); // port
if (!gateway_info.port) {
Logger.error(`Error IP:port ${ip_str}`);
continue;
}
gateway_info.ip = ip_str.split(":")[0]; // ip
gateway_list.push(gateway_info);
}
var details = {};
res.skip(6); // whatever
var unilbs_ip_len = res.readUInt16LE();
details["unilbs_ip"] = res.read(unilbs_ip_len);
res.skip(4); // whatever
var carrier_len = res.readUInt16LE();
details["carrier"] = res.read(carrier_len);
res.skip(4); // whatever
var region_len = res.readUInt16LE();
details["region"] = res.read(region_len);
// process gateway_list
var available_gateway_list = gateway_list.map(function(gateway_info){
var domain = details.region == "CN" ? GATEWAY_DOMAIN_CN : GATEWAY_DOMAIN;
var host = `webrtc-${gateway_info.ip.split(".").join("-")}.${domain}:${gateway_info.port}`;
return host;
});
if (available_gateway_list.length) {
var result = {
'gateway_addr': available_gateway_list,
'cid': cid,
'uid': uid
};
callback(null, result);
} else {
callback(NO_GATEWAY_AVAILABLE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment