天眼封禁优化
parent
4de7b94119
commit
962f0641b7
|
@ -1,6 +1,7 @@
|
||||||
package com.ljsd.jieling.netty.handler;
|
package com.ljsd.jieling.netty.handler;
|
||||||
|
|
||||||
import com.ljsd.jieling.netty.server.SkyEyeClient;
|
import com.ljsd.jieling.netty.server.SkyEyeClient;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.EventLoop;
|
import io.netty.channel.EventLoop;
|
||||||
|
@ -9,45 +10,50 @@ import io.netty.handler.timeout.IdleState;
|
||||||
import io.netty.handler.timeout.IdleStateEvent;
|
import io.netty.handler.timeout.IdleStateEvent;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import rpc.protocols.ChatProto;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@ChannelHandler.Sharable
|
@ChannelHandler.Sharable
|
||||||
public class SkyEyeHandler extends SimpleChannelInboundHandler {
|
public class SkyEyeHandler extends SimpleChannelInboundHandler {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SkyEyeHandler.class);
|
private static final Logger logger = LoggerFactory.getLogger(SkyEyeHandler.class);
|
||||||
long start = 0;
|
private SkyEyeClient cilent;
|
||||||
private long heartbeatCount = 0;
|
|
||||||
|
public SkyEyeHandler(SkyEyeClient cilent){
|
||||||
|
this.cilent = cilent;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
logger.info("天眼激活......");
|
||||||
ctx.fireChannelActive();
|
ctx.fireChannelActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
System.out.println("掉线了"+(System.currentTimeMillis()-start)/1000);
|
logger.info("天眼channelInactive......");
|
||||||
|
EventLoop loop = ctx.channel().eventLoop();
|
||||||
|
loop.schedule(() ->{
|
||||||
|
logger.info("天眼reconnect......");
|
||||||
|
cilent.connect();
|
||||||
|
},1, TimeUnit.SECONDS);
|
||||||
|
ctx.fireChannelInactive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
|
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
System.out.println();
|
ByteBuf in = (ByteBuf)msg;
|
||||||
|
int length = in.readableBytes();
|
||||||
|
if(length<=6){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
byte[] b = new byte[length-6];
|
||||||
|
in.readBytes(6);
|
||||||
|
for(int i =0;i<length-6;i++){
|
||||||
|
b[i] = in.readByte();
|
||||||
|
}
|
||||||
|
ChatProto.ChatV3 v3 = ChatProto.ChatV3.parseFrom(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
|
||||||
// public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
|
||||||
//// ctx.channel().writeAndFlush("TCX心跳");
|
|
||||||
//// System.out.println("发送心跳"+(System.currentTimeMillis()-start)/1000);
|
|
||||||
// super.userEventTriggered(ctx, evt);
|
|
||||||
//// if (evt instanceof IdleStateEvent) {
|
|
||||||
//// IdleStateEvent event = (IdleStateEvent) evt;
|
|
||||||
//// if (event.state().equals(IdleState.READER_IDLE)) {
|
|
||||||
//// System.out.println("长期没收到服务器推送数据");
|
|
||||||
//// //可以选择重新连接
|
|
||||||
//// } else if (event.state().equals(IdleState.WRITER_IDLE)) {
|
|
||||||
//// System.out.println("长期未向服务器发送数据");
|
|
||||||
//// } else if (event.state().equals(IdleState.ALL_IDLE)) {
|
|
||||||
//// System.out.println("ALL");
|
|
||||||
//// }
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ public class SkyEyeClient {
|
||||||
private static Bootstrap bootstrap = null;
|
private static Bootstrap bootstrap = null;
|
||||||
public static Channel channel;
|
public static Channel channel;
|
||||||
private EventLoopGroup group = new NioEventLoopGroup();
|
private EventLoopGroup group = new NioEventLoopGroup();
|
||||||
protected final HashedWheelTimer timer = new HashedWheelTimer();
|
|
||||||
private final int port = 8741;
|
private final int port = 8741;
|
||||||
private final String host = "api.gamegs.cn";
|
private final String host = "api.gamegs.cn";
|
||||||
|
|
||||||
|
@ -30,49 +29,43 @@ public class SkyEyeClient {
|
||||||
public void doConnect(EventLoopGroup group){
|
public void doConnect(EventLoopGroup group){
|
||||||
|
|
||||||
// ChannelFuture f = null;
|
// ChannelFuture f = null;
|
||||||
ChannelFuture future=null;
|
bootstrap = new Bootstrap();
|
||||||
Bootstrap bootstrap = new Bootstrap();
|
|
||||||
bootstrap.group(group)
|
bootstrap.group(group)
|
||||||
.option(ChannelOption.SO_KEEPALIVE,true)
|
.option(ChannelOption.SO_KEEPALIVE,true)
|
||||||
.channel(NioSocketChannel.class);
|
.channel(NioSocketChannel.class);
|
||||||
final ConnectionWatchdog watchdog = new ConnectionWatchdog(bootstrap, timer, port,host, true) {
|
|
||||||
@Override
|
|
||||||
public ChannelHandler[] handlers() {
|
|
||||||
return new ChannelHandler[] {
|
|
||||||
this,
|
|
||||||
new SkyEyeHandler()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
synchronized (bootstrap){
|
bootstrap.remoteAddress(host,port);
|
||||||
bootstrap.remoteAddress(host,port);
|
bootstrap.handler(new ChannelInitializer<Channel>() {
|
||||||
bootstrap.handler(new ChannelInitializer<Channel>() {
|
//初始化channel
|
||||||
//初始化channel
|
@Override
|
||||||
@Override
|
protected void initChannel(Channel ch) throws Exception {
|
||||||
protected void initChannel(Channel ch) throws Exception {
|
ch.pipeline().addLast(new SkyEyeHandler(SkyEyeClient.this));
|
||||||
ch.pipeline().addLast(watchdog.handlers());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
future = bootstrap.connect(host,port);
|
|
||||||
}
|
|
||||||
future.sync();
|
|
||||||
|
|
||||||
// f = bootstrap.connect();
|
|
||||||
// f.addListener(new ConnectionListener());
|
|
||||||
channel = future.channel();
|
|
||||||
// channel.closeFuture().sync();
|
|
||||||
} catch (Throwable t) {
|
|
||||||
LOGGER.error("客户端连接失败------------,ip为{},端口为{}",host,port);
|
|
||||||
if (null != future) {
|
|
||||||
try {
|
|
||||||
timer.newTimeout(watchdog,60, TimeUnit.SECONDS);
|
|
||||||
} catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}finally {
|
connect();
|
||||||
// group.shutdownGracefully();
|
// channel.closeFuture().sync();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
LOGGER.error("客户端连接失败------------,ip为{},端口为{}",host,port);
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect(){
|
||||||
|
synchronized (bootstrap) {
|
||||||
|
ChannelFuture future = bootstrap.connect(host, port);
|
||||||
|
channel = future.channel();
|
||||||
|
future.addListener(new ChannelFutureListener() {
|
||||||
|
public void operationComplete(ChannelFuture f) throws Exception {
|
||||||
|
boolean succeed = f.isSuccess();
|
||||||
|
Channel channel = f.channel();
|
||||||
|
if (!succeed) {
|
||||||
|
LOGGER.info("重连失败");
|
||||||
|
f.channel().pipeline().fireChannelInactive();
|
||||||
|
} else {
|
||||||
|
LOGGER.info("重连成功");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.ljsd.jieling.config.skyeye;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableConfigurationProperties(SkyEyeProperties.class)
|
||||||
|
public class SkyEyeAutoConfigration {
|
||||||
|
@Autowired
|
||||||
|
private SkyEyeProperties skyEyeProperties;
|
||||||
|
|
||||||
|
public SkyEyeProperties getSkyEyeProperties() {
|
||||||
|
return skyEyeProperties;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.ljsd.jieling.config.skyeye;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "skyeye")
|
||||||
|
public class SkyEyeProperties {
|
||||||
|
|
||||||
|
private String appId;
|
||||||
|
private String gmAddress;
|
||||||
|
|
||||||
|
public String getAppId() {
|
||||||
|
return appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAppId(String appId) {
|
||||||
|
this.appId = appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGmAddress() {
|
||||||
|
return gmAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGmAddress(String gmAddress) {
|
||||||
|
this.gmAddress = gmAddress;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ljsd.jieling.util;
|
package com.ljsd.jieling.util;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import com.google.protobuf.GeneratedMessage;
|
import com.google.protobuf.GeneratedMessage;
|
||||||
|
@ -7,6 +8,8 @@ import com.googlecode.protobuf.format.JsonFormat;
|
||||||
import com.ljsd.GameApplication;
|
import com.ljsd.GameApplication;
|
||||||
import com.ljsd.SkyEyeService;
|
import com.ljsd.SkyEyeService;
|
||||||
import com.ljsd.jieling.chat.messge.MessageCache;
|
import com.ljsd.jieling.chat.messge.MessageCache;
|
||||||
|
import com.ljsd.jieling.config.skyeye.SkyEyeAutoConfigration;
|
||||||
|
import com.ljsd.jieling.config.skyeye.SkyEyeProperties;
|
||||||
import com.ljsd.jieling.core.GlobalsDef;
|
import com.ljsd.jieling.core.GlobalsDef;
|
||||||
import com.ljsd.jieling.core.SimpleTransaction;
|
import com.ljsd.jieling.core.SimpleTransaction;
|
||||||
import com.ljsd.jieling.db.redis.RedisKey;
|
import com.ljsd.jieling.db.redis.RedisKey;
|
||||||
|
@ -36,6 +39,7 @@ import java.util.*;
|
||||||
public class MessageUtil {
|
public class MessageUtil {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(MessageUtil.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(MessageUtil.class);
|
||||||
|
private static HashMap<String, String> skyEyeExtraMap = new HashMap<>();
|
||||||
|
|
||||||
public static byte[] wrappedBuffer(int uid, int token, int index, int result, int msgId, GeneratedMessage generatedMessage) {
|
public static byte[] wrappedBuffer(int uid, int token, int index, int result, int msgId, GeneratedMessage generatedMessage) {
|
||||||
byte[] backMessage;
|
byte[] backMessage;
|
||||||
|
@ -79,8 +83,9 @@ public class MessageUtil {
|
||||||
return bytes1;
|
return bytes1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] wrappedBufferSE(GeneratedMessage generatedMessage) {
|
public static byte[] wrappedBufferSE(GeneratedMessage generatedMessage,SkyEyeProperties properties) {
|
||||||
byte[] idByte = short2byte((short) 0xd6ac);
|
int b1 = Integer.parseInt(properties.getAppId(),16);
|
||||||
|
byte[] idByte = short2byte((short) b1);
|
||||||
byte[] backMessage = generatedMessage.toByteArray();
|
byte[] backMessage = generatedMessage.toByteArray();
|
||||||
byte[] verifyByte = new byte[2+4+backMessage.length];
|
byte[] verifyByte = new byte[2+4+backMessage.length];
|
||||||
byte[] lengthByte =int2bytes(backMessage.length);
|
byte[] lengthByte =int2bytes(backMessage.length);
|
||||||
|
@ -457,8 +462,10 @@ public class MessageUtil {
|
||||||
if(uid != 0){
|
if(uid != 0){
|
||||||
he = UserManager.getUser(uid);
|
he = UserManager.getUser(uid);
|
||||||
}
|
}
|
||||||
ChatProto.ChatV3.Builder v3 = buildChatV3(msg,me,he,channel);
|
SkyEyeAutoConfigration netServerConfig = ConfigurableApplicationContextManager.getBean(SkyEyeAutoConfigration.class);
|
||||||
byte[] byteBuf = wrappedBufferSE(v3.build());
|
SkyEyeProperties properties = netServerConfig.getSkyEyeProperties();
|
||||||
|
ChatProto.ChatV3.Builder v3 = buildChatV3(msg,me,he,channel,properties);
|
||||||
|
byte[] byteBuf = wrappedBufferSE(v3.build(),properties);
|
||||||
// session.writeAndFlush(byteBuf);
|
// session.writeAndFlush(byteBuf);
|
||||||
// session.putBackMassageToMap(byteBuf);
|
// session.putBackMassageToMap(byteBuf);
|
||||||
ByteBuf bb = PooledByteBufAllocator.DEFAULT.heapBuffer();
|
ByteBuf bb = PooledByteBufAllocator.DEFAULT.heapBuffer();
|
||||||
|
@ -466,7 +473,7 @@ public class MessageUtil {
|
||||||
SkyEyeService.sendMsg(bb);
|
SkyEyeService.sendMsg(bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ChatProto.ChatV3.Builder buildChatV3(String msg,User me,User he,int type){
|
public static ChatProto.ChatV3.Builder buildChatV3(String msg,User me,User he,int type,SkyEyeProperties properties){
|
||||||
ChatProto.ChatV3.Builder v3 = ChatProto.ChatV3.newBuilder();
|
ChatProto.ChatV3.Builder v3 = ChatProto.ChatV3.newBuilder();
|
||||||
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
||||||
v3.setId(uuid);
|
v3.setId(uuid);
|
||||||
|
@ -479,6 +486,10 @@ public class MessageUtil {
|
||||||
ISession iSession = OnlineUserManager.getSessionByUid(me.getId());
|
ISession iSession = OnlineUserManager.getSessionByUid(me.getId());
|
||||||
String ip = iSession.getChannel().getLocalAddress().getHostString();
|
String ip = iSession.getChannel().getLocalAddress().getHostString();
|
||||||
v3.setIp(ip);
|
v3.setIp(ip);
|
||||||
|
skyEyeExtraMap.clear();
|
||||||
|
String url = properties.getGmAddress();
|
||||||
|
skyEyeExtraMap.put("url",url);
|
||||||
|
v3.setExtra(JSON.toJSONString(skyEyeExtraMap));
|
||||||
return v3;
|
return v3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,7 +502,7 @@ public class MessageUtil {
|
||||||
v3.setVipLevel(user.getPlayerInfoManager().getVipLevel());
|
v3.setVipLevel(user.getPlayerInfoManager().getVipLevel());
|
||||||
v3.setZoneId(String.valueOf(GameApplication.serverId));
|
v3.setZoneId(String.valueOf(GameApplication.serverId));
|
||||||
v3.setZoneName(String.valueOf(GameApplication.serverId));
|
v3.setZoneName(String.valueOf(GameApplication.serverId));
|
||||||
v3.setServerId(1+"");
|
v3.setServerId(user.getPlayerInfoManager().getChannel_id());
|
||||||
return v3;
|
return v3;
|
||||||
}
|
}
|
||||||
//高位在前,低位在后
|
//高位在前,低位在后
|
||||||
|
|
Loading…
Reference in New Issue