ref[net]: 重构net

This commit is contained in:
jaysunxiao
2021-10-13 23:36:23 +08:00
parent 526a59d88b
commit cf3b715222
16 changed files with 34 additions and 34 deletions
@@ -14,7 +14,7 @@
package com.zfoo.net.core;
import com.zfoo.net.NetContext;
import com.zfoo.net.handler.BaseDispatcherHandler;
import com.zfoo.net.handler.BaseRouteHandler;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.IOUtils;
@@ -72,7 +72,7 @@ public abstract class AbstractClient implements IClient {
if (channelFuture.isSuccess()) {
if (channelFuture.channel().isActive()) {
var channel = channelFuture.channel();
var session = BaseDispatcherHandler.initChannel(channel);
var session = BaseRouteHandler.initChannel(channel);
NetContext.getSessionManager().addClientSession(session);
logger.info("TcpClient started at [{}]", channel.localAddress());
return session;
@@ -14,7 +14,7 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.GatewayDispatcherHandler;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.tcp.TcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.model.Session;
@@ -59,7 +59,7 @@ public class GatewayServer extends AbstractServer {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
channel.pipeline().addLast(new TcpCodecHandler());
channel.pipeline().addLast(new GatewayDispatcherHandler(packetFilter));
channel.pipeline().addLast(new GatewayRouteHandler(packetFilter));
}
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.GatewayDispatcherHandler;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.model.Session;
@@ -69,7 +69,7 @@ public class WebsocketGatewayServer extends AbstractServer {
channel.pipeline().addLast(new WebSocketServerProtocolHandler("/websocket"));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new GatewayDispatcherHandler(packetFilter));
channel.pipeline().addLast(new GatewayRouteHandler(packetFilter));
}
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.GatewayDispatcherHandler;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.model.Session;
@@ -87,7 +87,7 @@ public class WebsocketSslGatewayServer extends AbstractServer {
channel.pipeline().addLast(new WebSocketServerProtocolHandler("/"));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new GatewayDispatcherHandler(packetFilter));
channel.pipeline().addLast(new GatewayRouteHandler(packetFilter));
}
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.http;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.ServerDispatcherHandler;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.http.HttpCodecHandler;
import com.zfoo.net.packet.model.DecodedPacketInfo;
import com.zfoo.protocol.util.IOUtils;
@@ -57,7 +57,7 @@ public class HttpServer extends AbstractServer {
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new HttpCodecHandler(uriResolver));
channel.pipeline().addLast(new ServerDispatcherHandler());
channel.pipeline().addLast(new ServerRouteHandler());
}
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.tcp;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.handler.ClientDispatcherHandler;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.tcp.TcpCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import com.zfoo.util.net.HostAndPort;
@@ -45,7 +45,7 @@ public class TcpClient extends AbstractClient {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
channel.pipeline().addLast(new ClientIdleHandler());
channel.pipeline().addLast(new TcpCodecHandler());
channel.pipeline().addLast(new ClientDispatcherHandler());
channel.pipeline().addLast(new ClientRouteHandler());
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.tcp;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.ServerDispatcherHandler;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.tcp.TcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.util.net.HostAndPort;
@@ -44,7 +44,7 @@ public class TcpServer extends AbstractServer {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
channel.pipeline().addLast(new TcpCodecHandler());
channel.pipeline().addLast(new ServerDispatcherHandler());
channel.pipeline().addLast(new ServerRouteHandler());
}
}
}
@@ -15,8 +15,8 @@ package com.zfoo.net.core.udp;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.handler.BaseDispatcherHandler;
import com.zfoo.net.handler.ClientDispatcherHandler;
import com.zfoo.net.handler.BaseRouteHandler;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.udp.UdpCodecHandler;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.exception.ExceptionUtils;
@@ -55,7 +55,7 @@ public class UdpClient extends AbstractClient {
if (channelFuture.isSuccess()) {
if (channelFuture.channel().isActive()) {
var channel = channelFuture.channel();
var session = BaseDispatcherHandler.initChannel(channel);
var session = BaseRouteHandler.initChannel(channel);
NetContext.getSessionManager().addClientSession(session);
logger.info("UdpClient started at [{}]", channel.localAddress());
return session;
@@ -81,7 +81,7 @@ public class UdpClient extends AbstractClient {
@Override
protected void initChannel(Channel channel) {
channel.pipeline().addLast(new UdpCodecHandler());
channel.pipeline().addLast(new ClientDispatcherHandler());
channel.pipeline().addLast(new ClientRouteHandler());
}
}
@@ -13,7 +13,7 @@
package com.zfoo.net.core.udp;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.ServerDispatcherHandler;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.udp.UdpCodecHandler;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.Bootstrap;
@@ -76,7 +76,7 @@ public class UdpServer extends AbstractServer {
@Override
protected void initChannel(Channel channel) {
channel.pipeline().addLast(new UdpCodecHandler());
channel.pipeline().addLast(new ServerDispatcherHandler());
channel.pipeline().addLast(new ServerRouteHandler());
}
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.handler.ClientDispatcherHandler;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
@@ -54,7 +54,7 @@ public class WebsocketClient extends AbstractClient {
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new ClientDispatcherHandler());
channel.pipeline().addLast(new ClientRouteHandler());
}
}
@@ -14,7 +14,7 @@
package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.ServerDispatcherHandler;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
@@ -56,7 +56,7 @@ public class WebsocketServer extends AbstractServer {
channel.pipeline().addLast(new ChunkedWriteHandler());
// 编解码WebSocketFrame二进制协议
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new ServerDispatcherHandler());
channel.pipeline().addLast(new ServerRouteHandler());
}
}
@@ -32,9 +32,9 @@ import org.slf4j.LoggerFactory;
* @version 3.0
*/
@ChannelHandler.Sharable
public class BaseDispatcherHandler extends ChannelInboundHandlerAdapter {
public class BaseRouteHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = LoggerFactory.getLogger(BaseDispatcherHandler.class);
private static final Logger logger = LoggerFactory.getLogger(BaseRouteHandler.class);
public static final AttributeKey<Session> SESSION_KEY = AttributeKey.valueOf("session");
@@ -28,9 +28,9 @@ import org.slf4j.LoggerFactory;
* @version 3.0
*/
@ChannelHandler.Sharable
public class ClientDispatcherHandler extends BaseDispatcherHandler {
public class ClientRouteHandler extends BaseRouteHandler {
private static final Logger logger = LoggerFactory.getLogger(ClientDispatcherHandler.class);
private static final Logger logger = LoggerFactory.getLogger(ClientRouteHandler.class);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
@@ -44,13 +44,13 @@ import java.util.function.BiFunction;
* @version 3.0
*/
@ChannelHandler.Sharable
public class GatewayDispatcherHandler extends ServerDispatcherHandler {
public class GatewayRouteHandler extends ServerRouteHandler {
private static final Logger logger = LoggerFactory.getLogger(GatewayDispatcherHandler.class);
private static final Logger logger = LoggerFactory.getLogger(GatewayRouteHandler.class);
private BiFunction<Session, IPacket, Boolean> packetFilter;
public GatewayDispatcherHandler(BiFunction<Session, IPacket, Boolean> packetFilter) {
public GatewayRouteHandler(BiFunction<Session, IPacket, Boolean> packetFilter) {
this.packetFilter = packetFilter;
}
@@ -27,9 +27,9 @@ import org.slf4j.LoggerFactory;
* @version 3.0
*/
@ChannelHandler.Sharable
public class ServerDispatcherHandler extends BaseDispatcherHandler {
public class ServerRouteHandler extends BaseRouteHandler {
private static final Logger logger = LoggerFactory.getLogger(ServerDispatcherHandler.class);
private static final Logger logger = LoggerFactory.getLogger(ServerRouteHandler.class);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
@@ -19,7 +19,7 @@ import com.zfoo.protocol.util.StringUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import static com.zfoo.net.handler.BaseDispatcherHandler.SESSION_KEY;
import static com.zfoo.net.handler.BaseRouteHandler.SESSION_KEY;
/**
* @author jaysunxiao