2021-01-26 19:45:18 +01:00
|
|
|
package sushi.hardcore.aira.background_service
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer
|
|
|
|
|
|
|
|
class Protocol {
|
|
|
|
companion object {
|
|
|
|
const val MESSAGE: Byte = 0x00
|
|
|
|
const val ASK_NAME: Byte = 0x01
|
|
|
|
const val TELL_NAME: Byte = 0x02
|
|
|
|
const val FILE: Byte = 0x03
|
2021-05-06 22:57:47 +02:00
|
|
|
const val ASK_LARGE_FILES: Byte = 0x04
|
|
|
|
const val ACCEPT_LARGE_FILES: Byte = 0x05
|
2021-01-26 19:45:18 +01:00
|
|
|
const val LARGE_FILE_CHUNK: Byte = 0x06
|
|
|
|
const val ACK_CHUNK: Byte = 0x07
|
2021-05-06 22:57:47 +02:00
|
|
|
const val ABORT_FILES_TRANSFER: Byte = 0x08
|
2021-01-26 19:45:18 +01:00
|
|
|
|
|
|
|
fun askName(): ByteArray {
|
|
|
|
return byteArrayOf(ASK_NAME)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun tellName(name: String): ByteArray {
|
|
|
|
return byteArrayOf(TELL_NAME)+name.toByteArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun newMessage(msg: String): ByteArray {
|
|
|
|
return byteArrayOf(MESSAGE)+msg.toByteArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun newFile(fileName: String, buffer: ByteArray): ByteArray {
|
|
|
|
return byteArrayOf(FILE)+ByteBuffer.allocate(2).putShort(fileName.length.toShort()).array()+fileName.toByteArray()+buffer
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
fun askLargeFiles(files: List<SendFile>): ByteArray {
|
|
|
|
var buff = byteArrayOf(ASK_LARGE_FILES)
|
|
|
|
for (file in files) {
|
|
|
|
buff += ByteBuffer.allocate(8).putLong(file.fileSize).array()
|
|
|
|
buff += ByteBuffer.allocate(2).putShort(file.fileName.length.toShort()).array()
|
|
|
|
buff += file.fileName.toByteArray()
|
|
|
|
}
|
|
|
|
return buff
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
fun acceptLargeFiles(): ByteArray {
|
|
|
|
return byteArrayOf(ACCEPT_LARGE_FILES)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
fun abortFilesTransfer(): ByteArray {
|
|
|
|
return byteArrayOf(ABORT_FILES_TRANSFER)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun ackChunk(): ByteArray {
|
|
|
|
return byteArrayOf(ACK_CHUNK)
|
|
|
|
}
|
2021-05-04 20:01:06 +02:00
|
|
|
|
|
|
|
class SmallFile(val rawFileName: ByteArray, val fileContent: ByteArray)
|
|
|
|
|
|
|
|
fun parseSmallFile(buffer: ByteArray): SmallFile? {
|
|
|
|
if (buffer.size > 3) {
|
|
|
|
val filenameLen = ByteBuffer.wrap(ByteArray(2) +buffer.sliceArray(1..2)).int
|
|
|
|
if (buffer.size > 3+filenameLen) {
|
|
|
|
val rawFileName = buffer.sliceArray(3 until 3+filenameLen)
|
|
|
|
return SmallFile(rawFileName, buffer.sliceArray(3+filenameLen until buffer.size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
fun parseAskFiles(buffer: ByteArray): List<ReceiveFile>? {
|
|
|
|
val files = mutableListOf<ReceiveFile>()
|
|
|
|
var n = 1
|
|
|
|
while (n < buffer.size) {
|
|
|
|
if (buffer.size > n+10) {
|
|
|
|
val fileSize = ByteBuffer.wrap(buffer.sliceArray(n..n+8)).long
|
|
|
|
val fileNameLen = ByteBuffer.wrap(buffer.sliceArray(n+8..n+10)).short
|
|
|
|
if (buffer.size >= n+10+fileNameLen) {
|
|
|
|
val fileName = buffer.sliceArray(n+10 until n+10+fileNameLen).decodeToString()
|
|
|
|
files.add(ReceiveFile(fileName, fileSize))
|
|
|
|
n += 10+fileNameLen
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2021-05-04 20:01:06 +02:00
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
return files
|
2021-05-04 20:01:06 +02:00
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|