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

View File

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