mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2025-08-05 15:49:22 +00:00
Added timeouts to casting flow.
This commit is contained in:
parent
ed2aa848da
commit
386c58d4ad
3 changed files with 40 additions and 20 deletions
|
@ -9,7 +9,6 @@ import java.net.InetAddress
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.net.Socket
|
import java.net.Socket
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.charset.Charset
|
|
||||||
|
|
||||||
|
|
||||||
private const val IPV4_PART_COUNT = 4;
|
private const val IPV4_PART_COUNT = 4;
|
||||||
|
@ -216,13 +215,15 @@ private fun ByteArray.toInetAddress(): InetAddress {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getConnectedSocket(addresses: List<InetAddress>, port: Int): Socket? {
|
fun getConnectedSocket(addresses: List<InetAddress>, port: Int): Socket? {
|
||||||
|
val timeout = 5000
|
||||||
|
|
||||||
if (addresses.isEmpty()) {
|
if (addresses.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addresses.size == 1) {
|
if (addresses.size == 1) {
|
||||||
try {
|
try {
|
||||||
return Socket(addresses[0], port);
|
return Socket().apply { this.connect(InetSocketAddress(addresses[0], port), timeout) };
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
//Ignored.
|
//Ignored.
|
||||||
}
|
}
|
||||||
|
@ -249,7 +250,7 @@ fun getConnectedSocket(addresses: List<InetAddress>, port: Int): Socket? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.connect(InetSocketAddress(address, port));
|
socket.connect(InetSocketAddress(address, port), timeout);
|
||||||
|
|
||||||
synchronized(syncObject) {
|
synchronized(syncObject) {
|
||||||
if (connectedSocket == null) {
|
if (connectedSocket == null) {
|
||||||
|
|
|
@ -307,7 +307,7 @@ class ChromecastCastingDevice : CastingDevice {
|
||||||
try {
|
try {
|
||||||
val connectedSocket = getConnectedSocket(adrs.toList(), port);
|
val connectedSocket = getConnectedSocket(adrs.toList(), port);
|
||||||
if (connectedSocket == null) {
|
if (connectedSocket == null) {
|
||||||
Thread.sleep(3000);
|
Thread.sleep(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,7 +347,7 @@ class ChromecastCastingDevice : CastingDevice {
|
||||||
Logger.i(TAG, "Failed to connect to Chromecast.", e);
|
Logger.i(TAG, "Failed to connect to Chromecast.", e);
|
||||||
|
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
Thread.sleep(3000);
|
Thread.sleep(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -363,7 +363,7 @@ class ChromecastCastingDevice : CastingDevice {
|
||||||
_socket?.close();
|
_socket?.close();
|
||||||
|
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
Thread.sleep(3000);
|
Thread.sleep(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,7 +415,7 @@ class ChromecastCastingDevice : CastingDevice {
|
||||||
Logger.i(TAG, "Socket disconnected.");
|
Logger.i(TAG, "Socket disconnected.");
|
||||||
|
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
Thread.sleep(3000);
|
Thread.sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.i(TAG, "Stopped connection loop.");
|
Logger.i(TAG, "Stopped connection loop.");
|
||||||
|
|
|
@ -32,6 +32,7 @@ import java.io.DataOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.math.BigInteger
|
import java.math.BigInteger
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
|
import java.net.InetSocketAddress
|
||||||
import java.net.Socket
|
import java.net.Socket
|
||||||
import java.security.KeyFactory
|
import java.security.KeyFactory
|
||||||
import java.security.KeyPair
|
import java.security.KeyPair
|
||||||
|
@ -241,29 +242,40 @@ class FCastCastingDevice : CastingDevice {
|
||||||
|
|
||||||
val thread = _thread
|
val thread = _thread
|
||||||
if (thread == null || !thread.isAlive) {
|
if (thread == null || !thread.isAlive) {
|
||||||
Log.i(TAG, "Restarting thread because the thread has died")
|
Log.i(TAG, "(Re)starting thread because the thread has died")
|
||||||
|
|
||||||
_scopeIO?.cancel();
|
_scopeIO?.let {
|
||||||
|
it.cancel()
|
||||||
Logger.i(TAG, "Cancelled previous scopeIO because a new one is starting.")
|
Logger.i(TAG, "Cancelled previous scopeIO because a new one is starting.")
|
||||||
|
}
|
||||||
|
|
||||||
_scopeIO = CoroutineScope(Dispatchers.IO);
|
_scopeIO = CoroutineScope(Dispatchers.IO);
|
||||||
|
|
||||||
_thread = Thread {
|
_thread = Thread {
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
|
Log.i(TAG, "Connection thread started.")
|
||||||
|
|
||||||
|
var connectedSocket: Socket? = null
|
||||||
while (_scopeIO?.isActive == true) {
|
while (_scopeIO?.isActive == true) {
|
||||||
try {
|
try {
|
||||||
val connectedSocket = getConnectedSocket(adrs.toList(), port);
|
Log.i(TAG, "getConnectedSocket.")
|
||||||
if (connectedSocket == null) {
|
|
||||||
Thread.sleep(3000);
|
val resultSocket = getConnectedSocket(adrs.toList(), port);
|
||||||
|
|
||||||
|
if (resultSocket == null) {
|
||||||
|
Log.i(TAG, "Connection failed, waiting 1 seconds.")
|
||||||
|
Thread.sleep(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
usedRemoteAddress = connectedSocket.inetAddress;
|
Log.i(TAG, "Connection succeeded.")
|
||||||
localAddress = connectedSocket.localAddress;
|
|
||||||
connectedSocket.close();
|
connectedSocket = resultSocket
|
||||||
|
usedRemoteAddress = connectedSocket.inetAddress
|
||||||
|
localAddress = connectedSocket.localAddress
|
||||||
break;
|
break;
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
Logger.w(ChromecastCastingDevice.TAG, "Failed to get setup initial connection to FastCast device.", e)
|
Logger.w(TAG, "Failed to get setup initial connection to FastCast device.", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,7 +285,14 @@ class FCastCastingDevice : CastingDevice {
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_socket = Socket(usedRemoteAddress, port);
|
if (connectedSocket != null) {
|
||||||
|
Logger.i(TAG, "Using connected socket.");
|
||||||
|
_socket = connectedSocket
|
||||||
|
connectedSocket = null
|
||||||
|
} else {
|
||||||
|
Logger.i(TAG, "Using new socket.");
|
||||||
|
_socket = Socket().apply { this.connect(InetSocketAddress(usedRemoteAddress, port), 5000) };
|
||||||
|
}
|
||||||
Logger.i(TAG, "Successfully connected to FastCast at $usedRemoteAddress:$port");
|
Logger.i(TAG, "Successfully connected to FastCast at $usedRemoteAddress:$port");
|
||||||
|
|
||||||
_outputStream = DataOutputStream(_socket?.outputStream);
|
_outputStream = DataOutputStream(_socket?.outputStream);
|
||||||
|
@ -283,7 +302,7 @@ class FCastCastingDevice : CastingDevice {
|
||||||
Logger.i(TAG, "Failed to connect to FastCast.", e);
|
Logger.i(TAG, "Failed to connect to FastCast.", e);
|
||||||
|
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
Thread.sleep(3000);
|
Thread.sleep(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,7 +362,7 @@ class FCastCastingDevice : CastingDevice {
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionState = CastConnectionState.CONNECTING;
|
connectionState = CastConnectionState.CONNECTING;
|
||||||
Thread.sleep(3000);
|
Thread.sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.i(TAG, "Stopped connection loop.");
|
Logger.i(TAG, "Stopped connection loop.");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue