Added LinkType to Channel.

This commit is contained in:
Koen J 2025-04-14 15:19:16 +02:00
parent b460f9915d
commit 0ef1f2d40f
2 changed files with 9 additions and 15 deletions

View file

@ -17,6 +17,7 @@ interface IChannel : AutoCloseable {
fun setDataHandler(onData: ((SyncSocketSession, IChannel, UByte, UByte, ByteBuffer) -> Unit)?)
fun send(opcode: UByte, subOpcode: UByte = 0u, data: ByteBuffer? = null)
fun setCloseHandler(onClose: ((IChannel) -> Unit)?)
val linkType: LinkType
}
class ChannelSocket(private val session: SyncSocketSession) : IChannel {
@ -24,6 +25,7 @@ class ChannelSocket(private val session: SyncSocketSession) : IChannel {
override val remoteVersion: Int? get() = session.remoteVersion
private var onData: ((SyncSocketSession, IChannel, UByte, UByte, ByteBuffer) -> Unit)? = null
private var onClose: ((IChannel) -> Unit)? = null
override val linkType: LinkType get() = LinkType.Direct
override var authorizable: IAuthorizable?
get() = session.authorizable
@ -83,6 +85,7 @@ class ChannelRelayed(
override var remoteVersion: Int? = null
private set
override var syncSession: SyncSession? = null
override val linkType: LinkType get() = LinkType.Relayed
private var onData: ((SyncSocketSession, IChannel, UByte, UByte, ByteBuffer) -> Unit)? = null
private var onClose: ((IChannel) -> Unit)? = null

View file

@ -35,26 +35,18 @@ class SyncSession : IAuthorizable {
val linkType: LinkType get()
{
var hasRelayed = false
var hasDirect = false
var linkType = LinkType.None
synchronized(_channels)
{
for (channel in _channels)
{
if (channel is ChannelRelayed)
hasRelayed = true
if (channel is ChannelSocket)
hasDirect = true
if (hasRelayed && hasDirect)
if (channel.linkType == LinkType.Direct)
return LinkType.Direct
if (channel.linkType == LinkType.Relayed)
linkType = LinkType.Relayed
}
}
if (hasRelayed)
return LinkType.Relayed
if (hasDirect)
return LinkType.Direct
return LinkType.None
return linkType
}
var connected: Boolean = false
@ -212,8 +204,7 @@ class SyncSession : IAuthorizable {
}
fun send(opcode: UByte, subOpcode: UByte, data: ByteBuffer? = null) {
//TODO: Prioritize local connections
val channels = synchronized(_channels) { _channels.toList() }
val channels = synchronized(_channels) { _channels.sortedBy { it.linkType.ordinal }.toList() }
if (channels.isEmpty()) {
//TODO: Should this throw?
Logger.v(TAG, "Packet was not sent (opcode = $opcode, subOpcode = $subOpcode) due to no connected sockets")