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)
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 this is a new message start a record for it
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(chunk_record[the_header.message_id].r_chunks === chunk_record[the_header.message_id].num_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) {
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) {
chunk_record[the_header.message_id].ctime = new Date() // completed time
@ -65,7 +65,7 @@
}
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.
receive_chunk(chunk.slice(the_header.chunk_length + the_header.header_length), message_handler, rinfo)
}
@ -76,23 +76,38 @@
if (chunk.length < 9) {
return {}
}
chunkBuf = Buffer.from(chunk, 'hex')
message_id = chunkBuf.readUInt16BE()
chunk_length = chunkBuf.readUInt16BE(2)
chunk_version = chunkBuf.readUInt8(4)
if (chunk_version == 0x01) {
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": 9
}
chunkBuf = Buffer.from(chunk, 'hex')//.slice(0,17)
if (chunkBuf.length >= 9) {
message_id = chunkBuf.readUInt16BE()
chunk_length = chunkBuf.readUInt16BE(2)
chunk_version = chunkBuf.readUInt8(4)
if (chunk_version == 1) {
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 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 {
// 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
return {
"chunk_length": -1
}
}
}
@ -102,6 +117,8 @@
const messageChunks = []
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
console.log('length: ', messageLen)
console.log('numChunks: ', numChunks)
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
// 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)))
}
// 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) {
// 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)