Skip to content

Commit 9d8f65f

Browse files
committed
release: 2.1.1
- Fixes Concurrent Modification on Broadcast - Increase netty version to 4.1.97-Final
1 parent af40145 commit 9d8f65f

4 files changed

Lines changed: 29 additions & 9 deletions

File tree

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ val authorName = "ccbluex"
1111
val projectUrl = "https://github.com/ccbluex/netty-httpserver"
1212

1313
group = "net.ccbluex"
14-
version = "2.1.0"
14+
version = "2.1.1"
1515

1616
repositories {
1717
mavenCentral()
@@ -30,7 +30,7 @@ dependencies {
3030
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
3131
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
3232
// https://mvnrepository.com/artifact/io.netty/netty-all
33-
implementation("io.netty:netty-all:4.1.82.Final")
33+
implementation("io.netty:netty-all:4.1.97.Final")
3434
// https://mvnrepository.com/artifact/com.google.code.gson/gson
3535
implementation("com.google.code.gson:gson:2.10.1")
3636
// https://mvnrepository.com/artifact/org.apache.tika/tika-core

src/main/kotlin/net/ccbluex/netty/http/HttpServerHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal class HttpServerHandler(private val server: HttpServer) : ChannelInboun
8282
handshaker.handshake(ctx.channel(), msg)
8383
}
8484

85-
server.webSocketController.activeContexts += ctx
85+
server.webSocketController.addContext(ctx)
8686
} else {
8787
val requestContext = RequestContext(
8888
msg.method(),

src/main/kotlin/net/ccbluex/netty/http/websocket/WebSocketController.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package net.ccbluex.netty.http.websocket
2121

2222
import io.netty.channel.ChannelHandlerContext
2323
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
24-
import io.netty.handler.codec.http.websocketx.WebSocketFrame
24+
import java.util.concurrent.CopyOnWriteArrayList
2525

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

3737
/**
3838
* Broadcasts a message to all connected clients.
@@ -53,10 +53,30 @@ class WebSocketController {
5353
/**
5454
* Closes all active contexts.
5555
*/
56-
fun closeAll() {
57-
activeContexts.forEach { handlerContext ->
58-
handlerContext.channel().close()
56+
fun disconnect() {
57+
activeContexts.removeIf { handlerContext ->
58+
runCatching {
59+
handlerContext.channel().close().sync()
60+
}.isSuccess
5961
}
6062
}
6163

64+
/**
65+
* Adds a new context to the list of active contexts.
66+
*
67+
* @param context The context to add.
68+
*/
69+
fun addContext(context: ChannelHandlerContext) {
70+
activeContexts.add(context)
71+
}
72+
73+
/**
74+
* Removes a context from the list of active contexts.
75+
*
76+
* @param context The context to remove.
77+
*/
78+
fun removeContext(context: ChannelHandlerContext) {
79+
activeContexts.remove(context)
80+
}
81+
6282
}

src/main/kotlin/net/ccbluex/netty/http/websocket/WebSocketHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal class WebSocketHandler(private val server: HttpServer) : ChannelInbound
5151
ctx.channel().writeAndFlush(msg.retainedDuplicate())
5252
ctx.channel().close().sync()
5353

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

0 commit comments

Comments
 (0)