Skip to content

Commit

Permalink
refactor(srtdroid): change recv return type to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Jun 27, 2024
1 parent 003d8a6 commit 8860779
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RecvFile(

socket.send(sendFileName)

val fileSize = Longs.fromByteArray(socket.recv(Longs.BYTES).second.reversedArray())
val fileSize = Longs.fromByteArray(socket.recv(Longs.BYTES).reversedArray())

// Where file will be written
val recvFile = File(recvFileDir, recvFileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ class SendFile(
val clientSocket = peer.first

// Get file name length
var pair = clientSocket.recv(Ints.BYTES)
val res = pair.first
val fileNameLength = Ints.fromByteArray(pair.second.reversedArray())
var array = clientSocket.recv(Ints.BYTES)
val fileNameLength = Ints.fromByteArray(array.reversedArray())
when {
res > 0 -> Log.i(TAG, "File name is $fileNameLength char long")
array.isNotEmpty() -> Log.i(TAG, "File name is $fileNameLength char long")
}

// Get file name
pair = clientSocket.recv(fileNameLength)
val fileName = String(pair.second)
array = clientSocket.recv(fileNameLength)
val fileName = String(array)
Log.i(TAG, "File name is $fileName")

val file = File("$fileSendDir/$fileName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SocketRecvTest {
sleep(1000)
socket.connect(InetAddress.getLoopbackAddress(), server.port)

val actualArray = socket.recv(arraySize).second
val actualArray = socket.recv(arraySize)

val numOfSentBytes = futureResult.get(1000, TimeUnit.MILLISECONDS)
Assert.assertEquals(arraySize, numOfSentBytes)
Expand All @@ -72,7 +72,7 @@ class SocketRecvTest {
val futureResult = server.enqueue(expectedArray)
socket.connect(InetAddress.getLoopbackAddress(), server.port)

val actualArray = socket.recv(arraySize, msgCtrl = MsgCtrl(ttl = 100)).second
val actualArray = socket.recv(arraySize, msgCtrl = MsgCtrl(ttl = 100))

val numOfSentBytes = futureResult.get(1000, TimeUnit.MILLISECONDS)
Assert.assertEquals(arraySize, numOfSentBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class SocketSendTest {
serverSocket.listen(1)
val pair = serverSocket.accept()
val comSocket = pair.first
val byteArray = comSocket.recv(receiveByteCount).second
val byteArray = comSocket.recv(receiveByteCount)
comSocket.close()
byteArray
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,11 @@ private constructor(private val srtsocket: Int) : Closeable {
* **See Also:** [srt_recv](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_recv)
*
* @param size Size of the expected message.
* @return a pair containing the number of bytes received and the [ByteArray] message.
* @return the [ByteArray] that contains the received message.
* @throws SocketException if it has failed to send message
* @throws SocketTimeoutException if a timeout has been triggered
*/
fun recv(size: Int): Pair<Int, ByteArray> {
fun recv(size: Int): ByteArray {
val pair = nativeRecv(size)
val byteReceived = pair.first
when {
Expand All @@ -809,7 +809,7 @@ private constructor(private val srtsocket: Int) : Closeable {
throw SocketTimeoutException(ErrorType.ESCLOSED.toString())
}

else -> return pair
else -> return pair.second.sliceArray(0 until byteReceived)
}
}

Expand All @@ -827,15 +827,15 @@ private constructor(private val srtsocket: Int) : Closeable {
* @param buffer the [ByteArray] where received data are copied to.
* @param offset the offset in the specified [buffer].
* @param byteCount the size of the specified [buffer].
* @return a pair containing the number of bytes received and the [ByteArray] message. The [ByteArray] points to the [buffer].
* @return the number of bytes received.
* @throws SocketException if it has failed to send message
* @throws SocketTimeoutException if a timeout has been triggered
*/
fun recv(
buffer: ByteArray,
offset: Int = 0,
byteCount: Int = buffer.size
): Pair<Int, ByteArray> {
): Int {
val pair = nativeRecv(buffer, offset, byteCount)
val byteReceived = pair.first
when {
Expand All @@ -847,7 +847,7 @@ private constructor(private val srtsocket: Int) : Closeable {
throw SocketTimeoutException(ErrorType.ESCLOSED.toString())
}

else -> return pair
else -> return pair.first
}
}

Expand All @@ -860,11 +860,11 @@ private constructor(private val srtsocket: Int) : Closeable {
*
* @param size Size of the expected message.
* @param msgCtrl the [MsgCtrl] that contains extra parameter
* @return a pair containing the number of bytes received and the [ByteArray] message.
* @return the [ByteArray] that contains the received message.
* @throws SocketException if it has failed to send message
* @throws SocketTimeoutException if a timeout has been triggered
*/
fun recv(size: Int, msgCtrl: MsgCtrl): Pair<Int, ByteArray> {
fun recv(size: Int, msgCtrl: MsgCtrl): ByteArray {
val pair = nativeRecv(size, msgCtrl)
val byteReceived = pair.first
when {
Expand All @@ -876,7 +876,7 @@ private constructor(private val srtsocket: Int) : Closeable {
throw SocketTimeoutException(ErrorType.ESCLOSED.toString())
}

else -> return pair
else -> return pair.second
}
}

Expand All @@ -896,7 +896,7 @@ private constructor(private val srtsocket: Int) : Closeable {
* @param offset the offset in the specified [buffer].
* @param byteCount the size of the specified [buffer].
* @param msgCtrl the [MsgCtrl] that contains extra parameter
* @return a pair containing the number of bytes received and the [ByteArray] message. The [ByteArray] points to the [buffer].
* @return the number of bytes received.
* @throws SocketException if it has failed to send message
* @throws SocketTimeoutException if a timeout has been triggered
*/
Expand All @@ -905,7 +905,7 @@ private constructor(private val srtsocket: Int) : Closeable {
offset: Int = 0,
byteCount: Int = buffer.size,
msgCtrl: MsgCtrl
): Pair<Int, ByteArray> {
): Int {
val pair = nativeRecv(buffer, offset, byteCount, msgCtrl)
val byteReceived = pair.first
when {
Expand All @@ -917,7 +917,7 @@ private constructor(private val srtsocket: Int) : Closeable {
throw SocketTimeoutException(ErrorType.ESCLOSED.toString())
}

else -> return pair
else -> return pair.first
}
}

Expand Down Expand Up @@ -945,32 +945,35 @@ private constructor(private val srtsocket: Int) : Closeable {
}

override fun read(): Int {
val pair = if (msgCtrl != null) {
socket.recv(1, msgCtrl)
} else {
socket.recv(1)
}
val byteReceived = pair.first
val byteArray = pair.second
return if (byteReceived > 0) {
byteArray[0].toInt()
} else {
-1
try {
val array = if (msgCtrl != null) {
socket.recv(1, msgCtrl)
} else {
socket.recv(1)
}
return if (array.isNotEmpty()) {
array[0].toInt()
} else {
-1
}
} catch (e: SocketTimeoutException) {
return -1
}
}

override fun read(buffer: ByteArray, offset: Int, byteCount: Int): Int {
require(byteCount >= 0) { "byteCount must be positive" }
if (byteCount == 0) {
return 0
}

val pair = if (msgCtrl != null) {
val bytesRead = if (msgCtrl != null) {
socket.recv(buffer, offset, byteCount, msgCtrl)
} else {
socket.recv(buffer, offset, byteCount)
}

return pair.first
return bytesRead
}
}

Expand Down Expand Up @@ -1025,7 +1028,7 @@ private constructor(private val srtsocket: Int) : Closeable {
* @throws SocketTimeoutException if a timeout has been triggered
* @see [recvFile]
*/
fun sendFile(file: File, offset: Long = 0, size: Long, block: Int = 364000) =
fun sendFile(file: File, offset: Long = 0, size: Long = file.length(), block: Int = 364000) =
sendFile(file.path, offset, size, block)

/**
Expand Down

0 comments on commit 8860779

Please sign in to comment.