Skip to content

Commit

Permalink
release: 2.1.1
Browse files Browse the repository at this point in the history
- Fixes Concurrent Modification on Broadcast
- Increase netty version to 4.1.97-Final
  • Loading branch information
1zun4 committed Nov 25, 2024
1 parent af40145 commit 9d8f65f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ val authorName = "ccbluex"
val projectUrl = "https://github.com/ccbluex/netty-httpserver"

group = "net.ccbluex"
version = "2.1.0"
version = "2.1.1"

repositories {
mavenCentral()
Expand All @@ -30,7 +30,7 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
// https://mvnrepository.com/artifact/io.netty/netty-all
implementation("io.netty:netty-all:4.1.82.Final")
implementation("io.netty:netty-all:4.1.97.Final")
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation("com.google.code.gson:gson:2.10.1")
// https://mvnrepository.com/artifact/org.apache.tika/tika-core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal class HttpServerHandler(private val server: HttpServer) : ChannelInboun
handshaker.handshake(ctx.channel(), msg)
}

server.webSocketController.activeContexts += ctx
server.webSocketController.addContext(ctx)
} else {
val requestContext = RequestContext(
msg.method(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package net.ccbluex.netty.http.websocket

import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
import io.netty.handler.codec.http.websocketx.WebSocketFrame
import java.util.concurrent.CopyOnWriteArrayList

/**
* Controller for handling websocket connections.
Expand All @@ -32,7 +32,7 @@ class WebSocketController {
* Keeps track of all connected websocket connections to the server.
* This is used to broadcast messages to all connected clients.
*/
val activeContexts = mutableListOf<ChannelHandlerContext>()
private val activeContexts = CopyOnWriteArrayList<ChannelHandlerContext>()

/**
* Broadcasts a message to all connected clients.
Expand All @@ -53,10 +53,30 @@ class WebSocketController {
/**
* Closes all active contexts.
*/
fun closeAll() {
activeContexts.forEach { handlerContext ->
handlerContext.channel().close()
fun disconnect() {
activeContexts.removeIf { handlerContext ->
runCatching {
handlerContext.channel().close().sync()
}.isSuccess
}
}

/**
* Adds a new context to the list of active contexts.
*
* @param context The context to add.
*/
fun addContext(context: ChannelHandlerContext) {
activeContexts.add(context)
}

/**
* Removes a context from the list of active contexts.
*
* @param context The context to remove.
*/
fun removeContext(context: ChannelHandlerContext) {
activeContexts.remove(context)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal class WebSocketHandler(private val server: HttpServer) : ChannelInbound
ctx.channel().writeAndFlush(msg.retainedDuplicate())
ctx.channel().close().sync()

server.webSocketController.activeContexts -= ctx
server.webSocketController.removeContext(ctx)
logger.debug("WebSocket closed due to ${msg.reasonText()} (${msg.statusCode()})")
}
else -> logger.error("Unknown WebSocketFrame type: ${msg.javaClass.name}")
Expand Down

0 comments on commit 9d8f65f

Please sign in to comment.