Skip to content

Commit 78c090c

Browse files
authored
Expose lavalink sessions for plugins (#1042)
1 parent 0736cc4 commit 78c090c

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

LavalinkServer/src/main/java/lavalink/server/io/SocketServer.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package lavalink.server.io
2424

2525
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager
2626
import dev.arbjerg.lavalink.api.AudioPluginInfoModifier
27+
import dev.arbjerg.lavalink.api.ISocketServer
2728
import dev.arbjerg.lavalink.api.PluginEventHandler
2829
import dev.arbjerg.lavalink.protocol.v4.Message
2930
import dev.arbjerg.lavalink.protocol.v4.PlayerState
@@ -46,11 +47,11 @@ final class SocketServer(
4647
koeOptions: KoeOptions,
4748
private val eventHandlers: List<PluginEventHandler>,
4849
private val pluginInfoModifiers: List<AudioPluginInfoModifier>
49-
) : TextWebSocketHandler() {
50+
) : TextWebSocketHandler(), ISocketServer {
5051

5152
// sessionID <-> Session
52-
val contextMap = ConcurrentHashMap<String, SocketContext>()
53-
private val resumableSessions = mutableMapOf<String, SocketContext>()
53+
override val sessions = ConcurrentHashMap<String, SocketContext>()
54+
override val resumableSessions = mutableMapOf<String, SocketContext>()
5455
private val koe = Koe.koe(koeOptions)
5556
private val statsCollector = StatsCollector(this)
5657
private val charPool = ('a'..'z') + ('0'..'9')
@@ -81,12 +82,12 @@ final class SocketServer(
8182
var sessionId: String
8283
do {
8384
sessionId = List(16) { charPool.random() }.joinToString("")
84-
} while (contextMap[sessionId] != null)
85+
} while (sessions[sessionId] != null)
8586
return sessionId
8687
}
8788

8889
val contexts: Collection<SocketContext>
89-
get() = contextMap.values
90+
get() = sessions.values
9091

9192
@Suppress("UastIncorrectHttpHeaderInspection")
9293
override fun afterConnectionEstablished(session: WebSocketSession) {
@@ -100,7 +101,7 @@ final class SocketServer(
100101

101102
if (resumable != null) {
102103
session.attributes["sessionId"] = resumable.sessionId
103-
contextMap[resumable.sessionId] = resumable
104+
sessions[resumable.sessionId] = resumable
104105
resumable.resume(session)
105106
log.info("Resumed session with id $sessionId")
106107
resumable.eventEmitter.onWebSocketOpen(true)
@@ -123,7 +124,7 @@ final class SocketServer(
123124
eventHandlers,
124125
pluginInfoModifiers
125126
)
126-
contextMap[sessionId] = socketContext
127+
sessions[sessionId] = socketContext
127128
socketContext.sendMessage(Message.Serializer, Message.ReadyEvent(false, sessionId))
128129
socketContext.eventEmitter.onWebSocketOpen(false)
129130
if (clientName != null) {
@@ -140,7 +141,7 @@ final class SocketServer(
140141
}
141142

142143
override fun afterConnectionClosed(session: WebSocketSession, status: CloseStatus) {
143-
val context = contextMap.remove(session.attributes["sessionId"]) ?: return
144+
val context = sessions.remove(session.attributes["sessionId"]) ?: return
144145
if (context.resumable) {
145146
resumableSessions.remove(context.sessionId)?.let { removed ->
146147
log.warn(

LavalinkServer/src/main/java/lavalink/server/util/util.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fun getRootCause(throwable: Throwable?): Throwable {
122122
}
123123

124124
fun socketContext(socketServer: SocketServer, sessionId: String) =
125-
socketServer.contextMap[sessionId] ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Session not found")
125+
socketServer.sessions[sessionId] ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Session not found")
126126

127127
fun existingPlayer(socketContext: SocketContext, guildId: Long) =
128128
socketContext.players[guildId] ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Player not found")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dev.arbjerg.lavalink.api
2+
3+
/**
4+
* Represents a Lavalink server which handles WebSocket connections.
5+
*/
6+
interface ISocketServer {
7+
/**
8+
* A map of all active sessions by their session id.
9+
*/
10+
val sessions: Map<String, ISocketContext>
11+
12+
/**
13+
* A map of all resumable sessions by their session id.
14+
* A session is resumable if the client configured resuming and has disconnected.
15+
*/
16+
val resumableSessions: Map<String, ISocketContext>
17+
}

0 commit comments

Comments
 (0)