From 01f0d514c73a05c374eb6581bee9515af67fc9b5 Mon Sep 17 00:00:00 2001 From: zhangshanxue Date: Wed, 25 Mar 2020 10:12:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ljsd/jieling/NetWorkFactory.java | 4 +- .../jieling/netty/server/AbstractClient.java | 40 ++++------- .../jieling/netty/server/NettyGameClient.java | 71 ++++--------------- 3 files changed, 29 insertions(+), 86 deletions(-) diff --git a/netty/src/main/java/com/ljsd/jieling/NetWorkFactory.java b/netty/src/main/java/com/ljsd/jieling/NetWorkFactory.java index 6370d350f..8655359f3 100644 --- a/netty/src/main/java/com/ljsd/jieling/NetWorkFactory.java +++ b/netty/src/main/java/com/ljsd/jieling/NetWorkFactory.java @@ -21,7 +21,7 @@ public class NetWorkFactory { return new NettyGameServer(cfg, handler); } - public static NettyGameClient getClient(MessageLite gameMessage, EndPortCfg cfg, INetWorkHandler handler) throws ConnectException { - return new NettyGameClient(gameMessage, cfg, handler); + public static NettyGameClient getClient(EndPortCfg cfg, INetWorkHandler handler) throws ConnectException { + return new NettyGameClient(cfg, handler); } } diff --git a/netty/src/main/java/com/ljsd/jieling/netty/server/AbstractClient.java b/netty/src/main/java/com/ljsd/jieling/netty/server/AbstractClient.java index 12c08127b..4f196f51c 100644 --- a/netty/src/main/java/com/ljsd/jieling/netty/server/AbstractClient.java +++ b/netty/src/main/java/com/ljsd/jieling/netty/server/AbstractClient.java @@ -22,7 +22,6 @@ import java.util.concurrent.ConcurrentHashMap; public abstract class AbstractClient extends AbstractEndport { protected Logger LOGGER = LoggerFactory.getLogger(AbstractClient.class); //不对外暴露接口 - private T gameSession; private InetSocketAddress localAddress; final int readTimeout; final int writeTimeout; @@ -49,39 +48,31 @@ public abstract class AbstractClient extends Abstrac @Override public void connected(IChannel channel) { - if (handler instanceof INetSession) { - gameSession = ((INetSession) handler).createGameSession(channel, handler); - if (null != gameSession) { - LOGGER.info("seission" + gameSession + "channelActive: " + gameSession.getChannel().getRemoteAddress() + " ,uid: " + - gameSession.getUid() + " ,areaId: " + gameSession.getAreaId()); - this.gameSession = gameSession; - String sessionId = gameSession.getId(); - channel.setBindSessionId(sessionId); - gameSession.open(); - } - } +// if (handler instanceof INetSession) { +// gameSession = ((INetSession) handler).createGameSession(channel, handler); +// if (null != gameSession) { +// LOGGER.info("seission" + gameSession + "channelActive: " + gameSession.getChannel().getRemoteAddress() + " ,uid: " + +// gameSession.getUid() + " ,areaId: " + gameSession.getAreaId()); +// this.gameSession = gameSession; +// String sessionId = gameSession.getId(); +// channel.setBindSessionId(sessionId); +// gameSession.open(); +// } +// } } @Override public void disconnected(IChannel channel) { if (handler instanceof INetConnet) { - if (null != this.gameSession) { - LOGGER.info("channelInactive->uid={},seission={},remoteAddress={}", - gameSession.getUid(), gameSession, gameSession.getChannel().getRemoteAddress()); - ((INetConnet) handler).offLine(gameSession); - gameSession.close(); - gameSession = null; - } + ((INetConnet) handler).offLine(null); } } @Override public void received(IChannel channel, Object msg) { - if (null != gameSession) { - gameSession.received(msg); - } + ((INetReceived) handler).onReceive(null,msg); } @Override @@ -92,11 +83,8 @@ public abstract class AbstractClient extends Abstrac @Override public void idle(IChannel channel) { - if (handler instanceof INetConnet) { - if (null != gameSession) { - ((INetConnet) handler).readIdel(gameSession); - } + ((INetConnet) handler).readIdel(null); } } diff --git a/netty/src/main/java/com/ljsd/jieling/netty/server/NettyGameClient.java b/netty/src/main/java/com/ljsd/jieling/netty/server/NettyGameClient.java index 05be349a2..5e215b38e 100644 --- a/netty/src/main/java/com/ljsd/jieling/netty/server/NettyGameClient.java +++ b/netty/src/main/java/com/ljsd/jieling/netty/server/NettyGameClient.java @@ -1,58 +1,32 @@ package com.ljsd.jieling.netty.server; import com.google.protobuf.MessageLite; -import com.ljsd.jieling.netty.NettyGameSession; -import com.ljsd.jieling.netty.cocdex.Int32FrameDecoder; -import com.ljsd.jieling.netty.cocdex.Int32LengthFieldPrepender; -import com.ljsd.jieling.netty.cocdex.PacketNetData; +import com.ljsd.jieling.netty.cocdex.TeaDecoder; +import com.ljsd.jieling.netty.cocdex.TeaEncoder; import com.ljsd.jieling.netty.config.EndPortCfg; import com.ljsd.jieling.netty.handler.GameMessageHandler; -import com.ljsd.jieling.netty.handler.ServerResponseMessageHandler; -import com.ljsd.jieling.protocol.*; -import com.ljsd.jieling.session.GameSession; -import com.ljsd.jieling.session.GameSessionFactory; +import com.ljsd.jieling.protocol.INetWorkHandler; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.protobuf.ProtobufDecoder; -import io.netty.handler.codec.protobuf.ProtobufEncoder; import io.netty.handler.timeout.IdleStateHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import util.StringUtil; -import util.TimeUtils; -import java.lang.reflect.Type; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; public class NettyGameClient extends AbstractClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyGameClient.class); private static Bootstrap bootstrap = null; - //key位IP+Port的组合 - private static Map channels = new ConcurrentHashMap<>(); - private final MessageLite gameMessage; - -// public NettyGameClient(int readTimeout, int writeTimeout, -// int allTimeout, MessageLite gameMessage, ProtocolsAbstract protocols, GameSessionFactory sessionFactory) { -// super(readTimeout, writeTimeout, allTimeout); -// -// this.protocols = protocols; -// this.sessionFactory = sessionFactory; -// } -// - - public NettyGameClient( MessageLite gameMessage, EndPortCfg endPortCfg, INetWorkHandler channelHandler) { - super(endPortCfg, channelHandler); - this.gameMessage = gameMessage; + public NettyGameClient(EndPortCfg endPortCfg, INetWorkHandler iNetWorkHandler) { + super(endPortCfg, iNetWorkHandler); } - public void start() { + public Channel start() { try { EventLoopGroup group = new NioEventLoopGroup(1); bootstrap = new Bootstrap(); @@ -69,41 +43,20 @@ public class NettyGameClient extends AbstractClient { protected void initChannel(Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); - - p.addLast(new Int32FrameDecoder()); - p.addLast(new ProtobufDecoder(gameMessage)); - + p.addLast(new TeaDecoder()); // 心跳检测处理 p.addLast(new IdleStateHandler(readTimeout, writeTimeout, allTimeout, TimeUnit.SECONDS)); - - p.addLast(new Int32LengthFieldPrepender()); - p.addLast(new ProtobufEncoder()); - - p.addLast(new ServerResponseMessageHandler(NettyGameClient.this)); + p.addLast(new TeaEncoder()); + p.addLast(new GameMessageHandler(NettyGameClient.this)); +// p.addLast(new ServerResponseMessageHandler(NettyGameClient.this)); } }); + return bootstrap.connect(getLocalAddress().getHostName(),getLocalAddress().getPort()).sync().channel();//"39.100.38.60",16080).sync().channel(); } catch (Exception e) { LOGGER.error("start->msg={}", e.getMessage(), e); } - } - public static Channel getChannel(String serverIp, int port) { - try { - String key = serverIp + ":" + port; - Channel channel = channels.get(key); - if(channel == null || !channel.isOpen() || !channel.isActive()) { - channel = bootstrap.connect(serverIp, port).sync().channel(); - channels.put(key, channel); - } - return channel; - } catch (Exception e) { - LOGGER.error("getChannel->serverIp={},port={},msg={}", serverIp, port, e.getMessage(), e); - } return null; } - public static Channel getChannel(String serverIpPort) { - String strs[] = serverIpPort.split(":"); - return getChannel(strs[0], Integer.parseInt(strs[1])); - } @Override public void close() { @@ -111,4 +64,6 @@ public class NettyGameClient extends AbstractClient { } + + }