网络修改

back_recharge
zhangshanxue 2020-03-25 10:12:08 +08:00
parent f8251f06cf
commit 01f0d514c7
3 changed files with 29 additions and 86 deletions

View File

@ -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);
}
}

View File

@ -22,7 +22,6 @@ import java.util.concurrent.ConcurrentHashMap;
public abstract class AbstractClient<T extends NettyGameSession> 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<T extends NettyGameSession> extends Abstrac
@Override
public void connected(IChannel channel) {
if (handler instanceof INetSession) {
gameSession = ((INetSession<T>) 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<T>) 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<T extends NettyGameSession> extends Abstrac
@Override
public void idle(IChannel channel) {
if (handler instanceof INetConnet) {
if (null != gameSession) {
((INetConnet) handler).readIdel(gameSession);
}
((INetConnet) handler).readIdel(null);
}
}

View File

@ -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<String, Channel> channels = new ConcurrentHashMap<>();
private final MessageLite gameMessage;
// public NettyGameClient(int readTimeout, int writeTimeout,
// int allTimeout, MessageLite gameMessage, ProtocolsAbstract protocols, GameSessionFactory<T> 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 {
}
}