class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallback) : IPtpIpCommandCallback
{
private val isDumpLog = true
- private var received_total_bytes = 0
- private var received_remain_bytes = 0
+ private var receivedTotalBytes = 0
+ private var receivedRemainBytes = 0
private var receivedFirstData = false
private var byteStream = ByteArrayOutputStream()
{
if (rx_body == null)
{
- Log.v(TAG, " MSG BODY IS NULL.")
+ Log.v(TAG, " MSG BODY IS NULL. $id")
return
}
if (isReceiveMulti)
{
- receivedMessage_multi(id, rx_body)
+ receivedMessageMulti(id, rx_body)
}
else
{
- receivedMessage_single(id, rx_body)
+ receivedMessageSingle(id, rx_body)
}
}
cutHeader(rx_body)
}
- private fun receivedMessage_single(id: Int, rx_body: ByteArray)
+ private fun receivedMessageSingle(id: Int, rx_body: ByteArray)
{
try
{
- Log.v(TAG, "receivedMessage_single() : " + rx_body.size + " bytes.")
+ Log.v(TAG, "receivedMessage_single() : " + rx_body.size + " bytes. id:$id")
if ((isDumpLog)&&(rx_body.size > 64))
{
- SimpleLogDumper.dump_bytes(" LV (FIRST) : ", Arrays.copyOfRange(rx_body, 0, 64))
- SimpleLogDumper.dump_bytes(" LV (-END-) : ", Arrays.copyOfRange(rx_body, rx_body.size - 64, rx_body.size))
+ SimpleLogDumper.dump_bytes(" LV (FIRST) : ", rx_body.copyOfRange(0, 64))
+ SimpleLogDumper.dump_bytes(" LV (-END-) : ", rx_body.copyOfRange(rx_body.size - 64, rx_body.size))
}
callback.onCompleted(rx_body, null)
}
}
}
- private fun receivedMessage_multi(id: Int, rx_body: ByteArray)
+ private fun receivedMessageMulti(id: Int, rx_body: ByteArray)
{
try
{
- Log.v(TAG, " receivedMessage_multi() id[$id] size : ${rx_body.size} ")
+ Log.v(TAG, " receivedMessage_multi() id[$id] size : ${rx_body.size} id:$id")
callback.onCompleted(byteStream.toByteArray(), null)
receivedFirstData = false
- received_remain_bytes = 0
- received_total_bytes = 0
+ receivedRemainBytes = 0
+ receivedTotalBytes = 0
byteStream.reset()
}
catch (e: Exception)
private fun cutHeader(rx_body: ByteArray)
{
val length = rx_body.size
- var data_position = 0
+ var dataPosition = 0
if (!receivedFirstData)
{
// データを最初に読んだとき。ヘッダ部分を読み飛ばす
receivedFirstData = true
- data_position = rx_body[0].toUByte().toInt()
+ dataPosition = rx_body[0].toUByte().toInt()
if (isDumpLog)
{
- Log.v(TAG, " FIRST DATA POS. : $data_position len: $length ");
- SimpleLogDumper.dump_bytes(" [sXXs]", Arrays.copyOfRange(rx_body, 0, (32)));
+ Log.v(TAG, " FIRST DATA POS. : $dataPosition len: $length ");
+ SimpleLogDumper.dump_bytes(" [sXXs]", rx_body.copyOfRange(0, (32)))
}
}
else
{
// 2回目以降の受信データ
- if (received_remain_bytes > 0)
+ if (receivedRemainBytes > 0)
{
// データの読み込みが途中だった場合...
- if (length < received_remain_bytes)
+ if (length < receivedRemainBytes)
{
// 全部コピーする、足りないバイト数は残す
- received_remain_bytes = received_remain_bytes - length
- received_total_bytes = received_total_bytes + rx_body.size
+ receivedRemainBytes -= length
+ receivedTotalBytes += rx_body.size
byteStream.write(rx_body, 0, rx_body.size)
return
}
else
{
- byteStream.write(rx_body, data_position, received_remain_bytes)
- data_position = received_remain_bytes
- received_remain_bytes = 0
+ byteStream.write(rx_body, dataPosition, receivedRemainBytes)
+ dataPosition = receivedRemainBytes
+ receivedRemainBytes = 0
}
}
}
- while (data_position <= length - 12)
+ while (dataPosition <= length - 12)
{
- val body_size = (rx_body[data_position].toUByte()).toInt() + ((rx_body[data_position + 1].toUByte()).toInt() * 256) + ((rx_body[data_position + 2].toUByte()).toInt() * 256 * 256) + ((rx_body[data_position + 3].toUByte()).toInt() * 256 * 256 * 256)
+ val body_size = (rx_body[dataPosition].toUByte()).toInt() + ((rx_body[dataPosition + 1].toUByte()).toInt() * 256) + ((rx_body[dataPosition + 2].toUByte()).toInt() * 256 * 256) + ((rx_body[dataPosition + 3].toUByte()).toInt() * 256 * 256 * 256)
- Log.v(TAG, " XX body_size : ${body_size} [$data_position] ($length) aa: ${rx_body[data_position].toUByte().toInt()} ${rx_body[data_position + 1].toUByte().toInt()} + ${rx_body[data_position + 2].toUByte().toInt()}")
+ Log.v(TAG, " XX body_size : ${body_size} [$dataPosition] ($length) aa: ${rx_body[dataPosition].toUByte().toInt()} ${rx_body[dataPosition + 1].toUByte().toInt()} + ${rx_body[dataPosition + 2].toUByte().toInt()}")
SimpleLogDumper.dump_bytes("XX", Arrays.copyOfRange(rx_body, 0, 32))
if (body_size <= 12)
{
- Log.v(TAG, " ----- BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.size + " ")
+ Log.v(TAG, " ----- BODY SIZE IS SMALL : " + dataPosition + " (" + body_size + ") [" + receivedRemainBytes + "] " + rx_body.size + " ")
break
}
// 受信データ(のヘッダ部分)をダンプする
- Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
- if (data_position + body_size > length)
+ Log.v(TAG, " RX DATA : " + dataPosition + " (" + body_size + ") [" + receivedRemainBytes + "] (" + receivedTotalBytes + ")");
+ if (dataPosition + body_size > length)
{
// データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。
- val copysize = length - (data_position + 12)
- byteStream.write(rx_body, data_position + 12, copysize)
- received_remain_bytes = body_size - copysize - 12 // マイナス12は、ヘッダ分
- received_total_bytes = received_total_bytes + copysize
+ val copysize = length - (dataPosition + 12)
+ byteStream.write(rx_body, dataPosition + 12, copysize)
+ receivedRemainBytes = body_size - copysize - 12 // マイナス12は、ヘッダ分
+ receivedTotalBytes += copysize
//Log.v(TAG, " ----- copy : " + (data_position + (12)) + " " + copysize + " remain : " + received_remain_bytes + " body size : " + body_size);
break
}
try
{
- byteStream.write(rx_body, data_position + 12, body_size - 12)
- data_position = data_position + body_size
- received_total_bytes = received_total_bytes + 12
+ byteStream.write(rx_body, dataPosition + 12, body_size - 12)
+ dataPosition += body_size
+ receivedTotalBytes += 12
//Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - (12)) + " remain : " + received_remain_bytes);
}
catch (e: Exception)
{
- Log.v(TAG, " pos : $data_position size : $body_size length : $length")
+ Log.v(TAG, " pos : $dataPosition size : $body_size length : $length")
e.printStackTrace()
}
}
return (if (callback != null && callback.isReceiveMulti)
{
// 受信したら逐次「受信したよ」と応答するパターン
- receive_multi(command, delayMs)
+ receiveMulti(command, delayMs)
}
else
{
- receive_single(command, delayMs)
+ receiveSingle(command, delayMs)
})
// 受信した後、すべてをまとめて「受信したよ」と応答するパターン
}
- private fun receive_single(command: IPtpIpCommand, delayMs: Int): Boolean
+ private fun receiveSingle(command: IPtpIpCommand, delayMs: Int): Boolean
{
val isDumpReceiveLog = command.dumpLog()
val id = command.id
val callback = command.responseCallback()
try
{
- val receive_message_buffer_size = BUFFER_SIZE
- val byte_array = ByteArray(receive_message_buffer_size)
+ val receiveMessageBufferSize = BUFFER_SIZE
+ val byteArray = ByteArray(receiveMessageBufferSize)
val inputStream = socket?.getInputStream()
if (inputStream == null)
{
}
// 初回データが受信バッファにデータが溜まるまで待つ...
- var read_bytes = waitForReceive(inputStream, delayMs, command.maxRetryCount())
- if (read_bytes < 0)
+ var readBytes = waitForReceive(inputStream, delayMs, command.maxRetryCount())
+ if (readBytes < 0)
{
// リトライオーバー...
Log.v(TAG, " RECEIVE : RETRY OVER...")
// 受信したデータをバッファに突っ込む
val byteStream = ByteArrayOutputStream()
- while (read_bytes > 0)
+ while (readBytes > 0)
{
- read_bytes = inputStream.read(byte_array, 0, receive_message_buffer_size)
- if (read_bytes <= 0)
+ readBytes = inputStream.read(byteArray, 0, receiveMessageBufferSize)
+ if (readBytes <= 0)
{
- Log.v(TAG, " RECEIVED MESSAGE FINISHED ($read_bytes)")
+ Log.v(TAG, " RECEIVED MESSAGE FINISHED ($readBytes)")
break
}
- byteStream.write(byte_array, 0, read_bytes)
+ byteStream.write(byteArray, 0, readBytes)
sleep(delayMs)
- read_bytes = inputStream.available()
+ readBytes = inputStream.available()
}
val outputStream = cutHeader(byteStream)
receivedAllMessage(isDumpReceiveLog, id, outputStream.toByteArray(), callback)
callback?.receivedMessage(id, body)
}
- private fun receive_multi(command: IPtpIpCommand, delayMs: Int): Boolean
+ private fun receiveMulti(command: IPtpIpCommand, delayMs: Int): Boolean
{
//int estimatedSize = command.estimatedReceiveDataSize();
var maxRetryCount = command.maxRetryCount()
// 受信サイズ異常の場合...
if (receivedLength > 0)
{
- SimpleLogDumper.dump_bytes("WRONG DATA : ", Arrays.copyOfRange(byteArray, 0, Math.min(receivedLength, 64)))
+ SimpleLogDumper.dump_bytes("WRONG DATA : ", byteArray.copyOfRange(0, Math.min(receivedLength, 64)))
}
Log.v(TAG, " WRONG LENGTH. : $targetLength READ : $receivedLength bytes.")
callback?.receivedMessage(id, null)
- return false
+ //return (false)
+ // データが不足しているので、もう一度受信待ち
+ return (true)
}
}
Log.v(TAG, " -=-=-=- 1st CALL : read_bytes : " + readBytes + "(" + receivedLength + ") : target_length : " + targetLength + " buffer SIZE : " + byteArray.size)
- callback?.onReceiveProgress(receivedLength, targetLength, Arrays.copyOfRange(byteArray, 0, receivedLength))
+ callback?.onReceiveProgress(receivedLength, targetLength, byteArray.copyOfRange(fromIndex = 0, toIndex = receivedLength))
run {
sleep(delayMs)
}
receivedLength += readBytes
- callback?.onReceiveProgress(receivedLength, targetLength, Arrays.copyOfRange(byteArray, 0, readBytes))
+ callback?.onReceiveProgress(receivedLength, targetLength, byteArray.copyOfRange(0, readBytes))
maxRetryCount = command.maxRetryCount()
run {
{
e.printStackTrace()
}
- return false
+ return (false)
}
private fun parseDataLength(byte_array: ByteArray, read_bytes: Int): Int
{
if (byte_array[offset + 4].toUByte().toInt() == 0x07)
{
- // 前の応答が入っていると考える...
- offset = 14
+ // 前の応答が入っていると考える... レングスバイト分読み飛ばす
+ offset = byte_array[offset].toUByte().toInt()
}
if (byte_array[offset + 4].toUByte().toInt() == 0x09)
{
+ // データバイト...
lenlen = (byte_array[offset + 15].toUByte().toInt() and 0xff shl 24) + (byte_array[offset + 14].toUByte().toInt() and 0xff shl 16) + (byte_array[offset + 13].toUByte().toInt() and 0xff shl 8) + (byte_array[offset + 12].toUByte().toInt() and 0xff)
}
}