com.neko233.virtualRouter.server.strategy.impl.ServerSendMessageStrategy.kt Maven / Gradle / Ivy
package com.neko233.virtualRouter.server.strategy.impl
import com.neko233.virtualRouter.core.businessHandler.HandleRouteStrategy
import com.neko233.virtualRouter.core.constant.RouteMessageTypeEnum
import com.neko233.virtualRouter.core.RouteMessage
import com.neko233.virtualRouter.core.RouterSession
import com.neko233.virtualRouter.core.RouterSessionManager
import io.netty.channel.Channel
import org.slf4j.LoggerFactory
class ServerSendMessageStrategy : HandleRouteStrategy {
companion object {
val LOGGER = LoggerFactory.getLogger(ServerSendMessageStrategy::class.java)!!
}
override fun type(): RouteMessageTypeEnum {
return RouteMessageTypeEnum.MessageData
}
override fun handleReq(
routeMessage: RouteMessage,
channel: Channel,
) {
val fromRouteId = routeMessage.fromRouteId
val toRouteId = routeMessage.toRouteId
// 找到目标节点
val toRouteNodeSession: RouterSession? = RouterSessionManager.Instance.getSession(toRouteId)
if (toRouteNodeSession == null) {
LOGGER.error("route message error! to routeId offline. from={}, to={}, msgType={}, data={}",
routeMessage.fromRouteId,
routeMessage.toRouteId,
routeMessage.messageType,
routeMessage.data
)
return
}
LOGGER.debug(
"转发路由消息. from={}, to={}, msgType={}, data={}",
fromRouteId,
toRouteId,
routeMessage.messageType,
routeMessage.data
)
// 发送给目标 session
toRouteNodeSession.writeAndFlush(routeMessage)
}
}