updates to go with updates to the base stations.

main
inmysocks 2026-08-11 18:54:47 +02:00
parent 98150ad7f2
commit d1f2d52be5
1 changed files with 38 additions and 20 deletions

View File

@ -28,7 +28,7 @@
} }
//console.log('received chunk: ', chunk) //console.log('received chunk: ', chunk)
const the_header = parse_chunk_header(chunk) const the_header = parse_chunk_header(chunk)
//console.log('header: ', the_header) console.log('header: ', the_header)
if(typeof the_header.message_id !== 'undefined') { if(typeof the_header.message_id !== 'undefined') {
// if this is a new message start a record for it // if this is a new message start a record for it
if(!chunk_record[the_header.message_id]) { if(!chunk_record[the_header.message_id]) {
@ -54,9 +54,9 @@
// if we have all the chunks reconstruct the message and pass it on for processing // if we have all the chunks reconstruct the message and pass it on for processing
if(chunk_record[the_header.message_id].r_chunks === chunk_record[the_header.message_id].num_chunks) { if(chunk_record[the_header.message_id].r_chunks === chunk_record[the_header.message_id].num_chunks) {
//console.log('have all chunks') //console.log('have all chunks')
let reconstructed_message = Buffer.from([]) let reconstructed_message = ''
Object.keys(chunk_record[the_header.message_id].chunks).sort((a,b)=>a-b).forEach(function(thisChunkIndex) { Object.keys(chunk_record[the_header.message_id].chunks).sort((a,b)=>a-b).forEach(function(thisChunkIndex) {
reconstructed_message = Buffer.concat([reconstructed_message, chunk_record[the_header.message_id].chunks[thisChunkIndex]]) reconstructed_message = reconstructed_message + chunk_record[the_header.message_id].chunks[thisChunkIndex]
}) })
if(reconstructed_message) { if(reconstructed_message) {
chunk_record[the_header.message_id].ctime = new Date() // completed time chunk_record[the_header.message_id].ctime = new Date() // completed time
@ -65,7 +65,7 @@
} }
prune_chunk_record() prune_chunk_record()
} }
if(the_header.chunk_length + the_header.header_length < chunk.length) { if(the_header.chunk_length > 0 && the_header.chunk_length + the_header.header_length < chunk.length) {
// if there is more data than has been processed, cut off the bit we have already done and process the rest. // if there is more data than has been processed, cut off the bit we have already done and process the rest.
receive_chunk(chunk.slice(the_header.chunk_length + the_header.header_length), message_handler, rinfo) receive_chunk(chunk.slice(the_header.chunk_length + the_header.header_length), message_handler, rinfo)
} }
@ -76,23 +76,38 @@
if (chunk.length < 9) { if (chunk.length < 9) {
return {} return {}
} }
chunkBuf = Buffer.from(chunk, 'hex') chunkBuf = Buffer.from(chunk, 'hex')//.slice(0,17)
message_id = chunkBuf.readUInt16BE() if (chunkBuf.length >= 9) {
chunk_length = chunkBuf.readUInt16BE(2) message_id = chunkBuf.readUInt16BE()
chunk_version = chunkBuf.readUInt8(4) chunk_length = chunkBuf.readUInt16BE(2)
if (chunk_version == 0x01) { chunk_version = chunkBuf.readUInt8(4)
return { if (chunk_version == 1) {
"message_id": message_id, return {
"chunk_length": chunk_length, "message_id": message_id,
"chunk_version": chunk_version, "chunk_length": chunk_length,
"chunk_index": (chunkBuf.slice(5,7)).readUInt16BE(), "chunk_version": chunk_version,
"num_chunks": (chunkBuf.slice(7,9)).readUInt16BE(), "chunk_index": (chunkBuf.slice(5,7)).readUInt16BE(),
"header_length": 9 "num_chunks": (chunkBuf.slice(7,9)).readUInt16BE(),
} "header_length": 18
}
} else if (chunk_version == 2) {
return {
"message_id": message_id,
"chunk_length": chunk_length,
"chunk_version": chunk_version,
"chunk_index": (chunkBuf.slice(5,7)).readUInt16BE(),
"num_chunks": (chunkBuf.slice(7,9)).readUInt16BE(),
"header_length": 18
}
} else {
// we don't know how to handle this chunk version, so return the chunk length so it can be skipped
return {
"chunk_length": chunk_length
}
}
} else { } else {
// we don't know how to handle this chunk version, so return the chunk length so it can be skipped return {
return { "chunk_length": -1
"chunk_length": chunk_length
} }
} }
} }
@ -102,6 +117,8 @@
const messageChunks = [] const messageChunks = []
const messageLen = message.length const messageLen = message.length
const numChunks = Math.floor(messageLen / max_chunk_length) // we use 1400 instead of 1500 to account for the header and because I am paranoid about sending things that are the max length const numChunks = Math.floor(messageLen / max_chunk_length) // we use 1400 instead of 1500 to account for the header and because I am paranoid about sending things that are the max length
console.log('length: ', messageLen)
console.log('numChunks: ', numChunks)
for(let i = 0; i < numChunks; i++) { for(let i = 0; i < numChunks; i++) {
// 1409 is the chunk length here, we split the message into 1400 byte lengths and add a 9 byte header // 1409 is the chunk length here, we split the message into 1400 byte lengths and add a 9 byte header
// for now the version is always 1 // for now the version is always 1
@ -110,6 +127,7 @@
messageChunks.push(thisHeader.toString('hex') + message.slice(max_chunk_length*i, max_chunk_length*(i+1))) messageChunks.push(thisHeader.toString('hex') + message.slice(max_chunk_length*i, max_chunk_length*(i+1)))
} }
// if the message is less than 1400 bytes than this catches it, and this catches any left over after pulling off all the 1400 byte lengths // if the message is less than 1400 bytes than this catches it, and this catches any left over after pulling off all the 1400 byte lengths
console.log('thingy: ', message.length % max_chunk_length > 0)
if(message.length % max_chunk_length > 0) { if(message.length % max_chunk_length > 0) {
// there is an annoying edge case when the message is an integer multiple of your chunk length and it doesn't have any leftovers which can cause a crash if you don't check for it // there is an annoying edge case when the message is an integer multiple of your chunk length and it doesn't have any leftovers which can cause a crash if you don't check for it
const thisHeader = make_chunk_header(message_id, numChunks+1, numChunks+1, (message.length % max_chunk_length) + 9, 1) const thisHeader = make_chunk_header(message_id, numChunks+1, numChunks+1, (message.length % max_chunk_length) + 9, 1)