-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05bb4d4
commit 5586a5b
Showing
16 changed files
with
1,333 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 37 additions & 29 deletions
66
example/src/main/java/io/github/thibaultbee/srtdroid/example/tests/RecvFile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,64 @@ | ||
package io.github.thibaultbee.srtdroid.example.tests | ||
|
||
import android.util.Log | ||
import com.google.common.primitives.Ints | ||
import com.google.common.primitives.Longs | ||
import io.github.thibaultbee.srtdroid.enums.SockOpt | ||
import io.github.thibaultbee.srtdroid.enums.Transtype | ||
import io.github.thibaultbee.srtdroid.example.Utils | ||
import io.github.thibaultbee.srtdroid.models.Socket | ||
import com.google.common.primitives.Ints | ||
import com.google.common.primitives.Longs | ||
import io.github.thibaultbee.srtdroid.ktx.CoroutineSocket | ||
import io.github.thibaultbee.srtdroid.ktx.extensions.connect | ||
import io.github.thibaultbee.srtdroid.ktx.extensions.isConnected | ||
import io.github.thibaultbee.srtdroid.ktx.extensions.recvFile | ||
import io.github.thibaultbee.srtdroid.ktx.extensions.send | ||
import kotlinx.coroutines.delay | ||
import java.io.File | ||
import java.util.concurrent.ExecutorService | ||
|
||
class RecvFile( | ||
private val sendFileName: String, | ||
private val recvFileName: String, | ||
private val recvFileDir: File, | ||
executorService: ExecutorService, | ||
onSuccess: (String) -> Unit, | ||
onError: (String) -> Unit | ||
) : Test(executorService, onSuccess, onError) { | ||
) : Test { | ||
companion object { | ||
private val TAG = RecvFile::class.simpleName | ||
} | ||
|
||
override val testName: String = this::class.simpleName!! | ||
override val name: String = this::class.simpleName!! | ||
|
||
override fun launchImpl(ip: String, port: Int, socket: Socket) { | ||
override suspend fun run(ip: String, port: Int) { | ||
Log.i(TAG, "Will get file $sendFileName from server") | ||
val socket = CoroutineSocket() | ||
|
||
if (!socket.isValid) { | ||
throw Exception("Invalid socket") | ||
} | ||
try { | ||
socket.setSockFlag(SockOpt.TRANSTYPE, Transtype.FILE) | ||
socket.connect(ip, port) | ||
Log.i( | ||
TAG, | ||
"Is connected: ${socket.isConnected}" | ||
) | ||
|
||
socket.setSockFlag(SockOpt.TRANSTYPE, Transtype.FILE) | ||
socket.connect(ip, port) | ||
// Request server file | ||
socket.send(Ints.toByteArray(sendFileName.length).reversedArray()) | ||
|
||
// Request server file | ||
socket.send(Ints.toByteArray(sendFileName.length).reversedArray()) | ||
socket.send(sendFileName) | ||
|
||
socket.send(sendFileName) | ||
val array = socket.recv(Longs.BYTES) | ||
val fileSize = Longs.fromByteArray(array.reversedArray()) | ||
|
||
val fileSize = Longs.fromByteArray(socket.recv(Longs.BYTES).reversedArray()) | ||
// Where file will be written | ||
val recvFile = File(recvFileDir, recvFileName) | ||
Log.i(TAG, "Receiving file ${recvFile.path}") | ||
if (fileSize != socket.recvFile(recvFile, 0, fileSize)) { | ||
throw Exception("Failed to recv file: ${Utils.getErrorMessage()}") | ||
} | ||
|
||
// Where file will be written | ||
val recvFile = File(recvFileDir, recvFileName) | ||
if (fileSize != socket.recvFile(recvFile, 0, fileSize)) { | ||
throw Exception("Failed to recv file: ${Utils.getErrorMessage()}") | ||
// If session is close too early, last msg will not be receive by server | ||
delay(1000) | ||
Log.i(TAG, "Received file $recvFile") | ||
} catch (e: Exception) { | ||
throw e | ||
} finally { | ||
socket.close() | ||
} | ||
|
||
// If session is close too early, last msg will not be receive by server | ||
Thread.sleep(1000) | ||
|
||
successMsg = "Recv file is in $recvFile" | ||
} | ||
|
||
} |
Oops, something went wrong.