Skip to content

Commit

Permalink
publish disconnection reason
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 31, 2024
1 parent e6c8e00 commit edc8e85
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/LilaHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ final class LilaHandler(
case RoundTourStanding(tourId, data) =>
publish(_.tourStanding(tourId), ClientIn.roundTourStanding(data))
case o: TvSelect => Tv.select(o)
case RoomStop(roomId) =>
case o @ RoomStop(roomId) =>
History.round.stop(Game.Id(roomId.value))
publish(_.room(roomId), ClientCtrl.Disconnect)
publish(_.room(roomId), ClientCtrl.Disconnect(o.toString))
case RoundBotOnline(gameId, color, v) => roundCrowd.botOnline(gameId, color, v)
case GameStart(users) =>
users.foreach: u =>
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Users.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class Users(using scheduler: Scheduler, ec: Executor):

def kick(userId: User.Id): Unit =
Option(users.get(userId)).foreach:
_.foreach { _ ! ClientCtrl.Disconnect }
_.foreach { _ ! ClientCtrl.Disconnect("user kick") }

def setTroll(userId: User.Id, v: IsTroll): Unit =
Option(users.get(userId)).foreach:
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/actor/ChallengeClientActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object ChallengeClientActor:
case None => Behaviors.same
case Some(s) => apply(state.copy(site = s), deps)

case ClientCtrl.Disconnect =>
case ClientCtrl.Disconnect(_) =>
// lila tries to close the round room, because there's no game with that ID yet
// ignore it so we stay connected to the challenge
Behaviors.same
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/actor/ClientActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ object ClientActor:
if state.lastPing < oldSeconds && !deps.req.flag.contains(Flag.api) then Behaviors.stopped
else Behaviors.same

case ClientCtrl.Disconnect =>
deps.clientIn(ClientIn.Disconnect)
case ClientCtrl.Disconnect(reason) =>
deps.clientIn(ClientIn.Disconnect(reason))
Behaviors.stopped

case ClientCtrl.ApiDisconnect =>
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/ipc/ClientIn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import chess.variant.Crazyhouse
import chess.{ Check, Color, Ply }
import play.api.libs.json.*

// messages from lila-ws to the client
sealed trait ClientIn extends ClientMsg:
def write: String

Expand All @@ -21,8 +22,9 @@ object ClientIn:
case object Resync extends ClientIn:
val write = cliMsg("resync")

// triggers actual disconnection
case object Disconnect extends ClientIn:
// instead of sending a message to the client,
// lila-ws will close the connection
case class Disconnect(reason: String) extends ClientIn:
val write = cliMsg("bye") // not actually sent

case class LobbyPong(members: Int, rounds: Int) extends ClientIn:
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/ipc/types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ trait ClientMsg
sealed trait ClientCtrl extends ClientMsg

object ClientCtrl:
case object Disconnect extends ClientCtrl
case object ApiDisconnect extends ClientCtrl
case class Broom(oldSeconds: Int) extends ClientCtrl
case class Disconnect(reason: String) extends ClientCtrl
case object ApiDisconnect extends ClientCtrl
case class Broom(oldSeconds: Int) extends ClientCtrl

object ClientNull extends ClientMsg
case class SetTroll(v: IsTroll) extends ClientMsg
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/netty/ActorChannelConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ final private class ActorChannelConnector(clients: ActorRef[Clients.Control])(us
}

private def emitToChannel(channel: Channel): ClientEmit =
case ipc.ClientIn.Disconnect =>
channel.writeAndFlush(CloseWebSocketFrame()).addListener(ChannelFutureListener.CLOSE)
case ipc.ClientIn.Disconnect(reason) =>
channel
.writeAndFlush(CloseWebSocketFrame(WebSocketCloseStatus(4010, reason)))
.addListener(ChannelFutureListener.CLOSE)
case ipc.ClientIn.RoundPingFrameNoFlush =>
channel.write { PingWebSocketFrame(Unpooled.copyLong(System.currentTimeMillis())) }
case in =>
Expand Down

0 comments on commit edc8e85

Please sign in to comment.