Transfers: ZMODEM's repeated ZRINIT and ZRPOS no longer restart it, YMODEM's batch end

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
Claude committed 2026-10-16 22:56:41 +00:00
1 parent 88c1890e24
commit 885aea6898
2 files changed
+39 -18

No files matched your search

@@ -242,7 +242,12 @@ class XYModem(
}
// the batch over: an empty block 0
if (waitForStart() != true) throw IOException("the receiver didn't ask for the end")
sendBlock(0, ByteArray(128), true)
// (rb exits as it takes it: its ACK can be lost with it, and every file is in)
val end = blockBytes(0, ByteArray(128), true)
for (attempt in 0 until 3) {
io.write(end)
if (answer(allowC = true) == ACK) return
}
}
/** C (CRC: true) or NAK (checksum: false) from the receiver; null if neither came */
@@ -291,6 +296,17 @@ class XYModem(
/** a block sent until it's acknowledged */
private fun sendBlock(n: Int, data: ByteArray, useCrc: Boolean) {
val bytes = blockBytes(n, data, useCrc)
var errors = 0
while (true) {
io.write(bytes)
if (answer(allowC = n == 0) == ACK) return
if (++errors > MAX_ERRORS) throw IOException("too many errors on the line")
report { it.copy(errors = it.errors + 1) }
}
}
private fun blockBytes(n: Int, data: ByteArray, useCrc: Boolean): ByteArray {
val out = java.io.ByteArrayOutputStream()
out.write(if (data.size == 1024) STX else SOH)
out.write(n and 0xff)
@@ -303,14 +319,7 @@ class XYModem(
} else {
out.write(data.sumOf { it.toInt() and 0xff } and 0xff)
}
val bytes = out.toByteArray()
var errors = 0
while (true) {
io.write(bytes)
if (answer(allowC = n == 0) == ACK) return
if (++errors > MAX_ERRORS) throw IOException("too many errors on the line")
report { it.copy(errors = it.errors + 1) }
}
return out.toByteArray()
}
/**
@@ -455,17 +455,25 @@ class ZModem(
("${f.size.coerceAtLeast(0)} ${java.lang.Long.toOctalString(f.mtime.coerceAtLeast(0))} 0 0 $filesLeft $bytesLeft").toByteArray() + 0
var start = -1L
var tries = 0
while (start < 0) {
send@ while (start < 0) {
sendBin(Header.flags(ZFILE, ZCBIN))
sendData(info, info.size, ZCRCW)
val h = readHeaderOrNull()
when (h?.type) {
ZRPOS -> start = h.pos
ZSKIP -> return true
ZCRC -> sendHex(Header.pos(ZCRC, 0))
ZCAN, ZABORT -> throw TransferCancelled("cancelled by the receiver")
ZFERR -> throw IOException("the receiver couldn't write the file")
else -> if (++tries > MAX_ERRORS) throw IOException("the receiver didn't answer the file")
while (true) {
val h = readHeaderOrNull()
when (h?.type) {
ZRPOS -> { start = h.pos; break@send }
ZSKIP -> return true
ZCRC -> sendHex(Header.pos(ZCRC, 0))
ZCAN, ZABORT -> throw TransferCancelled("cancelled by the receiver")
ZFERR -> throw IOException("the receiver couldn't write the file")
// (a ZRINIT repeated, from before the ZFILE: not an answer to it; sending it
// again would get two ZRPOS, and each one a restart)
ZRINIT -> {}
else -> {
if (++tries > MAX_ERRORS) throw IOException("the receiver didn't answer the file")
continue@send
}
}
}
}
val reader = Reader(f)
@@ -532,6 +540,10 @@ class ZModem(
if (++errors > MAX_ERRORS) throw IOException("too many errors on the line")
report { it.copy(errors = it.errors + 1) }
at = to
// (what the receiver said before it saw this restart: its ZRPOSes for the same
// error, which would each be another restart)
Thread.sleep(20)
io.purge()
}
resend@ while (true) {
r.seek(at)