11/*
22The MIT License
33
4- Copyright (c) 2016-2023 kong <congcoi123@gmail.com>
4+ Copyright (c) 2016-2025 kong <congcoi123@gmail.com>
55
66Permission is hereby granted, free of charge, to any person obtaining a copy
77of this software and associated documentation files (the "Software"), to deal
@@ -24,12 +24,11 @@ of this software and associated documentation files (the "Software"), to deal
2424
2525package com .tenio .core .api .implement ;
2626
27- import com .tenio .common .configuration . Configuration ;
27+ import com .tenio .common .data . DataCollection ;
2828import com .tenio .common .logger .SystemLogger ;
2929import com .tenio .core .api .ServerApi ;
30- import com .tenio .core .configuration .constant .CoreConstant ;
31- import com .tenio .core .configuration .define .CoreConfigurationType ;
3230import com .tenio .core .configuration .define .ServerEvent ;
31+ import com .tenio .core .entity .Channel ;
3332import com .tenio .core .entity .Player ;
3433import com .tenio .core .entity .Room ;
3534import com .tenio .core .entity .define .mode .ConnectionDisconnectMode ;
@@ -40,6 +39,7 @@ of this software and associated documentation files (the "Software"), to deal
4039import com .tenio .core .entity .define .result .PlayerLeftRoomResult ;
4140import com .tenio .core .entity .define .result .PlayerLoggedInResult ;
4241import com .tenio .core .entity .define .result .RoomCreatedResult ;
42+ import com .tenio .core .entity .manager .ChannelManager ;
4343import com .tenio .core .entity .manager .PlayerManager ;
4444import com .tenio .core .entity .manager .RoomManager ;
4545import com .tenio .core .entity .setting .InitialRoomSetting ;
@@ -48,12 +48,12 @@ of this software and associated documentation files (the "Software"), to deal
4848import com .tenio .core .exception .CreatedRoomException ;
4949import com .tenio .core .exception .PlayerJoinedRoomException ;
5050import com .tenio .core .exception .RemovedNonExistentPlayerException ;
51- import com .tenio .core .network .configuration .SocketConfiguration ;
5251import com .tenio .core .network .entity .session .Session ;
5352import com .tenio .core .server .Server ;
5453import java .io .IOException ;
5554import java .util .Iterator ;
5655import java .util .List ;
56+ import java .util .Map ;
5757import java .util .Objects ;
5858import java .util .Optional ;
5959
@@ -137,11 +137,18 @@ public void logout(Player player, ConnectionDisconnectMode connectionDisconnectM
137137
138138 try {
139139 if (player .containsSession () && player .getSession ().isPresent ()) {
140+ // check process on method InternalProcessorServiceImpl#processSessionWillBeClosed
140141 player .getSession ().get ().close (connectionDisconnectMode , playerDisconnectMode );
141142 } else {
143+ // unsubscribe it from all channels
144+ unsubscribeFromAllChannels (player );
145+ // player should leave room (if applicable) first
146+ if (player .isInRoom ()) {
147+ leaveRoom (player , PlayerLeaveRoomMode .LOG_OUT );
148+ }
142149 getEventManager ().emit (ServerEvent .DISCONNECT_PLAYER , player , playerDisconnectMode );
143150 String removedPlayer = player .getIdentity ();
144- getPlayerManager ().removePlayerByIdentity (player . getIdentity () );
151+ getPlayerManager ().removePlayerByIdentity (removedPlayer );
145152 debug ("DISCONNECTED PLAYER" , "Player " , removedPlayer , " was removed" );
146153 player .clean ();
147154 }
@@ -318,17 +325,48 @@ public void removeRoom(Room room, RoomRemoveMode removeRoomMode) {
318325 }
319326
320327 @ Override
321- public int getCurrentAvailableUdpPort ( ) {
322- return server . getDatagramChannelManager ().getCurrentAvailableUdpPort ( );
328+ public void createChannel ( String id , String description ) {
329+ getChannelManager ().createChannel ( id , description );
323330 }
324331
325332 @ Override
326- public int getCurrentAvailableKcpPort () {
327- Configuration configuration = server .getConfiguration ();
328- if (Objects .isNull (configuration .get (CoreConfigurationType .NETWORK_KCP ))) {
329- return CoreConstant .NULL_PORT_VALUE ;
330- }
331- return Integer .parseInt (((SocketConfiguration ) (configuration .get (CoreConfigurationType .NETWORK_KCP ))).port ());
333+ public void removeChannel (String id ) {
334+ getChannelManager ().removeChannel (id );
335+ }
336+
337+ @ Override
338+ public void subscribeToChannel (Channel channel , Player player ) {
339+ getChannelManager ().subscribe (channel , player );
340+ }
341+
342+ @ Override
343+ public void unsubscribeFromChannel (Channel channel , Player player ) {
344+ getChannelManager ().unsubscribe (channel , player );
345+ }
346+
347+ @ Override
348+ public void unsubscribeFromAllChannels (Player player ) {
349+ getChannelManager ().unsubscribe (player );
350+ }
351+
352+ @ Override
353+ public void broadcastToChannel (Channel channel , DataCollection message ) {
354+ getChannelManager ().broadcast (channel , message );
355+ }
356+
357+ @ Override
358+ public Map <String , Channel > getSubscribedChannelsForPlayer (Player player ) {
359+ return getChannelManager ().getSubscribedChannelsForPlayer (player );
360+ }
361+
362+ @ Override
363+ public int getUdpPort () {
364+ return server .getDatagramChannelManager ().getUdpPort ();
365+ }
366+
367+ @ Override
368+ public int getKcpPort () {
369+ return server .getDatagramChannelManager ().getKcpPort ();
332370 }
333371
334372 @ Override
@@ -357,4 +395,8 @@ private PlayerManager getPlayerManager() {
357395 private RoomManager getRoomManager () {
358396 return server .getRoomManager ();
359397 }
398+
399+ private ChannelManager getChannelManager () {
400+ return server .getChannelManager ();
401+ }
360402}
0 commit comments