com.fastchar.socket.server.tcp.FastTcpSocketServerChannelHandler Maven / Gradle / Ivy
package com.fastchar.socket.server.tcp;
import com.fastchar.core.FastChar;
import com.fastchar.socket.FastSocketServerConfig;
import com.fastchar.socket.core.FastTcpSocket;
import com.fastchar.socket.interfaces.IFastTcpSocketServerListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @author 沈建(Janesen)
* @date 2021/3/31 09:31
*/
public class FastTcpSocketServerChannelHandler extends SimpleChannelInboundHandler {
private static final ConcurrentMap FAST_TCP_SOCKET_CONCURRENT_MAP = new ConcurrentHashMap<>();
private void notifyOpen(ChannelHandlerContext ctx) {
FastTcpSocket tcpSocket = new FastTcpSocket(ctx.channel());
FAST_TCP_SOCKET_CONCURRENT_MAP.put(ctx.channel().id().asShortText(), tcpSocket);
List iFastTcpSocketByTexts = FastChar.getOverrides().singleInstances(false, IFastTcpSocketServerListener.class);
for (IFastTcpSocketServerListener iFastTcpSocketByText : iFastTcpSocketByTexts) {
if (iFastTcpSocketByText == null) {
continue;
}
iFastTcpSocketByText.onOpen(tcpSocket);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
if (FastChar.getConfig(FastSocketServerConfig.class).isDebug()) {
FastChar.getLog().info(this.getClass(), "channelRead:" + ctx);
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
notifyOpen(ctx);
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
super.handlerAdded(ctx);
notifyOpen(ctx);
}
@Override
protected void messageReceived(ChannelHandlerContext ctx, String o) throws Exception {
List iFastTcpSocketByTexts = FastChar.getOverrides().singleInstances(false, IFastTcpSocketServerListener.class);
for (IFastTcpSocketServerListener iFastTcpSocketByText : iFastTcpSocketByTexts) {
if (iFastTcpSocketByText == null) {
continue;
}
iFastTcpSocketByText.onMessage(FAST_TCP_SOCKET_CONCURRENT_MAP.get(ctx.channel().id().asShortText()), o);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
notifyClose(ctx, null, 0);
}
private void notifyClose(ChannelHandlerContext ctx, Throwable cause, int code) {
List iFastTcpSocketByTexts = FastChar.getOverrides().singleInstances(false, IFastTcpSocketServerListener.class);
for (IFastTcpSocketServerListener iFastTcpSocketByText : iFastTcpSocketByTexts) {
if (iFastTcpSocketByText == null) {
continue;
}
iFastTcpSocketByText.onClose(FAST_TCP_SOCKET_CONCURRENT_MAP.get(ctx.channel().id().asShortText()), cause, code);
}
FAST_TCP_SOCKET_CONCURRENT_MAP.remove(ctx.channel().id().asShortText());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
ctx.close();
notifyClose(ctx, cause, -1);
}
}