Comparing version 0.1.0 to 0.9.0
@@ -113,3 +113,9 @@ /** | ||
module.exports = function(host, port, channel_id) { | ||
module.exports = function(options) { | ||
let {host, port, channel_id, debug=false} = options; | ||
if ( !host || !port || !channel_id ) { | ||
throw new Error("Destination host, port and channel_id must be assigned!"); | ||
} | ||
return new Promise((resolve, reject)=>{ | ||
@@ -145,2 +151,3 @@ const client = Object.create(null); | ||
client.debug = !!debug; | ||
client.id = GenInstId(); | ||
@@ -219,3 +226,8 @@ client.state = 0; | ||
for(const client of clients) { | ||
console.log(client.chunk.toString('hex')); | ||
if ( client.debug ) { | ||
console.log(client.chunk.toString('hex')); | ||
} | ||
if ( client.chunk.length === 0 ) continue; | ||
@@ -222,0 +234,0 @@ reader.bindBuffer(client.chunk, 0); |
@@ -11,5 +11,36 @@ #!/usr/bin/env node | ||
const argv = process.argv.slice(2); | ||
const host = argv[0]||'127.0.0.1'; | ||
const port = (argv[2]||65500)|0; | ||
const config = { host: '127.0.0.1', port: 65500, debug:false }; | ||
{ | ||
const argv = process.argv.slice(2); | ||
while( argv.length > 0 ) { | ||
const option = argv.pop(); | ||
const [arg, assign] = __CMD_SPLIT(option); | ||
switch(arg) { | ||
case "--host": | ||
case "-H": | ||
config.host = assign||config.host; | ||
break; | ||
case "--port": | ||
case "-p": | ||
config.port = (assign||config.port)|0; | ||
break; | ||
case "--debug": | ||
config.debug = true; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
function __CMD_SPLIT(option) { | ||
option = ('' + (option||'')).trim(); | ||
const index = option.indexOf('='); | ||
return index>0?[ | ||
option.substring(0, index), option.substring(index+1) | ||
]:[option, ""]; | ||
} | ||
} | ||
@@ -19,5 +50,3 @@ | ||
const {host, port, debug} = config; | ||
const ZERO_BUFFER = Buffer.alloc(0); | ||
@@ -147,3 +176,21 @@ const MSG_TYPE = { | ||
function ___EAT_HELO_MSG(client) { | ||
const data_chunk = client.chunk; | ||
const reader = new BufferReader(data_chunk); | ||
const cmd = reader.readUInt8(); | ||
if ( cmd === null ) return null; | ||
if ( cmd !== MSG_TYPE.HELO ) return false; | ||
// 0x01 + uint16 + bytes{0,} | ||
let len = reader.readUInt16LE(); | ||
if ( len === null ) return null; | ||
const channel_id = reader.readUTF8String(len); | ||
if ( channel_id === null ) return null; | ||
client.chunk = data_chunk.slice(reader.offset); | ||
return channel_id; | ||
} | ||
function _PARSE_CLIENT_DATA() { | ||
@@ -192,2 +239,3 @@ SERVER_STATE.timeout = null; | ||
client.socket.write(Buffer.from([0x01])); | ||
console.log(`[${GetLocalISOString()}] STAT, ${client.id}, channel:${client.room_id}`); | ||
} | ||
@@ -215,3 +263,6 @@ | ||
console.log(client.chunk.toString('hex')); | ||
if ( debug ) { | ||
console.log(`[${GetLocalISOString()}] DATA, ${client.id}, data:${client.chunk.toString('hex')}`); | ||
} | ||
paired_client.socket.write(client.chunk); | ||
@@ -221,24 +272,2 @@ client.chunk = ZERO_BUFFER; | ||
} | ||
function ___EAT_HELO_MSG(client) { | ||
const data_chunk = client.chunk; | ||
const reader = new BufferReader(data_chunk); | ||
const cmd = reader.readUInt8(); | ||
if ( cmd === null ) return null; | ||
if ( cmd !== MSG_TYPE.HELO ) return false; | ||
// 0x01 + uint16 + bytes{0,} | ||
let len = reader.readUInt16LE(); | ||
if ( len === null ) return null; | ||
const channel_id = reader.readUTF8String(len); | ||
if ( channel_id === null ) return null; | ||
client.chunk = data_chunk.slice(reader.offset); | ||
return channel_id; | ||
} | ||
function _ON_CLIENT_DATA(chunk) { | ||
@@ -256,3 +285,3 @@ const client = PRIVATE.get(this); | ||
client.valid = false; | ||
console.error(`[${client.id}]: ERROR, channel:${client.room_id}, error:${e.message}!`, e); | ||
console.log(`[${GetLocalISOString()}] ERROR, ${client.id}, channel:${client.room_id||'-'}, error:${e.message}!`, e); | ||
@@ -274,3 +303,3 @@ const {room} = client; | ||
client.valid = false; | ||
console.log(`[${client.id}]: CLOSE, channel:${client.room_id}`); | ||
console.log(`[${GetLocalISOString()}] CLOS, ${client.id}, channel:${client.room_id||'-'}`); | ||
@@ -289,2 +318,40 @@ const {room} = client; | ||
} | ||
function GetLocalISOString() { | ||
const now = new Date(); | ||
let offset, zone = now.getTimezoneOffset(); | ||
if ( zone === 0 ) { | ||
offset = 'Z'; | ||
} | ||
else { | ||
const sign = zone > 0 ? '-' : '+'; | ||
zone = Math.abs(zone); | ||
const zone_hour = Math.floor(zone/60); | ||
const zone_min = zone%60; | ||
offset = sign + Padding(zone_hour) + Padding(zone_min); | ||
} | ||
return now.getFullYear() + | ||
'-' + Padding(now.getMonth()+1) + | ||
'-' + Padding(now.getDate()) + | ||
'T' + Padding(now.getHours()) + | ||
':' + Padding(now.getMinutes()) + | ||
':' + Padding(now.getSeconds()) + | ||
'.' + Padding(now.getMilliseconds() % 1000, 3, true) + | ||
offset; | ||
} | ||
function Padding(val, length=2, back=false){ | ||
val = `${val}`; | ||
const remain = length - val.length; | ||
const stuffing = '0'.repeat(remain); | ||
return back ? val + stuffing : stuffing + val; | ||
} | ||
})(); |
{ | ||
"name": "ipevt", | ||
"version": "0.1.0", | ||
"version": "0.9.0", | ||
"description": "A small library that allows peers to communicate via a central server", | ||
@@ -5,0 +5,0 @@ "main": "ipevt-client.js", |
1384
45036
5