Skip to content

Commit

Permalink
🚚 bedrock: move all events to one file
Browse files Browse the repository at this point in the history
  • Loading branch information
MeLike2D committed Apr 14, 2021
1 parent a1e7c93 commit e0689a2
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 167 deletions.
2 changes: 1 addition & 1 deletion Server/src/main/kotlin/obsidian/bedrock/Bedrock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object Bedrock {
"netty" -> ByteBufAllocator.DEFAULT
"unpooled" -> UnpooledByteBufAllocator.DEFAULT
else -> {
logger.warn("Invalid byte buf allocator \"$allocator\", defaulting to the 'pooled' byte buf allocator.")
logger.warn("Invalid byte buf allocator '$allocator', defaulting to the 'pooled' byte buf allocator.")
PooledByteBufAllocator.DEFAULT
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,53 @@
* limitations under the License.
*/

package obsidian.bedrock.event
package obsidian.bedrock

import obsidian.bedrock.BedrockClient
import obsidian.bedrock.MediaConnection
import io.ktor.util.network.*
import obsidian.bedrock.gateway.event.ClientConnect

interface Event {
/**
* The client that emitted this event.
* Media connection
*/
val client: BedrockClient
val mediaConnection: MediaConnection

/**
* Media connection
* Client that emitted this event
*/
val mediaConnection: MediaConnection
val client: BedrockClient
get() = mediaConnection.bedrockClient

/**
* ID of the guild
*/
val guildId: Long
get() = mediaConnection.id
}
}

data class GatewayClosedEvent(
override val mediaConnection: MediaConnection,
val code: Short,
val reason: String?
) : Event

data class GatewayReadyEvent(
override val mediaConnection: MediaConnection,
val ssrc: Int,
val target: NetworkAddress
) : Event

data class HeartbeatSentEvent(
override val mediaConnection: MediaConnection,
val nonce: Long
) : Event

data class HeartbeatAcknowledgedEvent(
override val mediaConnection: MediaConnection,
val nonce: Long
) : Event

data class UserConnectedEvent(
override val mediaConnection: MediaConnection,
val event: ClientConnect
) : Event
26 changes: 18 additions & 8 deletions Server/src/main/kotlin/obsidian/bedrock/MediaConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import kotlinx.coroutines.flow.*
import obsidian.bedrock.codec.Codec
import obsidian.bedrock.codec.OpusCodec
import obsidian.bedrock.codec.framePoller.FramePoller
import obsidian.bedrock.event.Event
import obsidian.bedrock.gateway.MediaGatewayConnection
import obsidian.bedrock.handler.ConnectionHandler
import obsidian.bedrock.media.MediaFrameProvider
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.coroutines.CoroutineContext

Expand Down Expand Up @@ -109,7 +109,10 @@ class MediaConnection(
}

if (connectionHandler != null) {
connectionHandler?.close()
withContext(Dispatchers.IO) {
connectionHandler?.close()
}

connectionHandler = null
}
}
Expand Down Expand Up @@ -159,7 +162,7 @@ class MediaConnection(
}

companion object {
val logger = LoggerFactory.getLogger(MediaConnection::class.java)
val logger: Logger = LoggerFactory.getLogger(MediaConnection::class.java)
}
}

Expand All @@ -171,9 +174,16 @@ class MediaConnection(
*
* @return A [Job] that can be used to cancel any further processing of event [T]
*/
inline fun <reified T> MediaConnection.on(scope: CoroutineScope = this, crossinline block: suspend T.() -> Unit): Job {
return events.buffer(Channel.UNLIMITED).filterIsInstance<T>().onEach { event ->
event.runCatching { block() }
.onFailure { MediaConnection.logger.error(it) }
}.launchIn(scope)
inline fun <reified T : Event> MediaConnection.on(
scope: CoroutineScope = this,
crossinline block: suspend T.() -> Unit
): Job {
return events.buffer(Channel.UNLIMITED)
.filterIsInstance<T>()
.onEach { event ->
event
.runCatching { block() }
.onFailure { MediaConnection.logger.error(it) }
}
.launchIn(scope)
}

This file was deleted.

30 changes: 0 additions & 30 deletions Server/src/main/kotlin/obsidian/bedrock/event/GatewayReadyEvent.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import obsidian.bedrock.MediaConnection
import obsidian.bedrock.VoiceServerInfo
import obsidian.bedrock.codec.OpusCodec
import obsidian.bedrock.crypto.EncryptionMode
import obsidian.bedrock.event.*
import obsidian.bedrock.*
import obsidian.bedrock.gateway.event.*
import obsidian.bedrock.handler.DiscordUDPConnection
import obsidian.bedrock.util.Interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import io.netty.buffer.ByteBuf
import obsidian.bedrock.MediaConnection
import obsidian.bedrock.codec.Codec
import obsidian.bedrock.codec.OpusCodec
import obsidian.bedrock.event.UserConnectedEvent
import obsidian.bedrock.UserConnectedEvent
import obsidian.bedrock.gateway.SpeakingFlags
import obsidian.bedrock.on

Expand Down
4 changes: 0 additions & 4 deletions Server/src/main/kotlin/obsidian/server/io/MagmaClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import kotlinx.serialization.json.Json
import obsidian.bedrock.*
import obsidian.bedrock.event.GatewayClosedEvent
import obsidian.bedrock.event.GatewayReadyEvent
import obsidian.bedrock.event.HeartbeatAcknowledgedEvent
import obsidian.bedrock.event.HeartbeatSentEvent
import obsidian.bedrock.gateway.AbstractMediaGatewayConnection.Companion.asFlow
import obsidian.server.io.Magma.Companion.magma
import obsidian.server.player.Link
Expand Down

0 comments on commit e0689a2

Please sign in to comment.