From 962f0641b7ccac96b3248496570136461b18ca95 Mon Sep 17 00:00:00 2001 From: mengchengzhen <13682025901@163.com> Date: Tue, 8 Jun 2021 10:41:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A9=E7=9C=BC=E5=B0=81=E7=A6=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jieling/netty/handler/SkyEyeHandler.java | 50 +++++++------ .../jieling/netty/server/SkyEyeClient.java | 71 +++++++++---------- .../config/skyeye/SkyEyeAutoConfigration.java | 16 +++++ .../config/skyeye/SkyEyeProperties.java | 28 ++++++++ .../com/ljsd/jieling/util/MessageUtil.java | 23 ++++-- 5 files changed, 121 insertions(+), 67 deletions(-) create mode 100644 serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeAutoConfigration.java create mode 100644 serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeProperties.java diff --git a/netty/src/main/java/com/ljsd/jieling/netty/handler/SkyEyeHandler.java b/netty/src/main/java/com/ljsd/jieling/netty/handler/SkyEyeHandler.java index 961624816..d6212cc56 100644 --- a/netty/src/main/java/com/ljsd/jieling/netty/handler/SkyEyeHandler.java +++ b/netty/src/main/java/com/ljsd/jieling/netty/handler/SkyEyeHandler.java @@ -1,6 +1,7 @@ package com.ljsd.jieling.netty.handler; import com.ljsd.jieling.netty.server.SkyEyeClient; +import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.EventLoop; @@ -9,45 +10,50 @@ import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import rpc.protocols.ChatProto; + +import java.util.concurrent.TimeUnit; @ChannelHandler.Sharable public class SkyEyeHandler extends SimpleChannelInboundHandler { private static final Logger logger = LoggerFactory.getLogger(SkyEyeHandler.class); - long start = 0; - private long heartbeatCount = 0; + private SkyEyeClient cilent; + + public SkyEyeHandler(SkyEyeClient cilent){ + this.cilent = cilent; + } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { + logger.info("天眼激活......"); ctx.fireChannelActive(); } @Override 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 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() { - //初始化channel - @Override - protected void initChannel(Channel ch) throws Exception { - 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(); + bootstrap.remoteAddress(host,port); + bootstrap.handler(new ChannelInitializer() { + //初始化channel + @Override + protected void initChannel(Channel ch) throws Exception { + ch.pipeline().addLast(new SkyEyeHandler(SkyEyeClient.this)); } - } - }finally { -// group.shutdownGracefully(); + }); + connect(); +// 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("重连成功"); + } + } + }); } } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeAutoConfigration.java b/serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeAutoConfigration.java new file mode 100644 index 000000000..c6b1048fe --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeAutoConfigration.java @@ -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; + } +} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeProperties.java b/serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeProperties.java new file mode 100644 index 000000000..e440edebe --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/config/skyeye/SkyEyeProperties.java @@ -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; + } +} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/MessageUtil.java b/serverlogic/src/main/java/com/ljsd/jieling/util/MessageUtil.java index 2d4cb4afc..47febca01 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/util/MessageUtil.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/MessageUtil.java @@ -1,5 +1,6 @@ package com.ljsd.jieling.util; +import com.alibaba.fastjson.JSON; import com.google.gson.Gson; import com.google.protobuf.ByteString; import com.google.protobuf.GeneratedMessage; @@ -7,6 +8,8 @@ import com.googlecode.protobuf.format.JsonFormat; import com.ljsd.GameApplication; import com.ljsd.SkyEyeService; 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.SimpleTransaction; import com.ljsd.jieling.db.redis.RedisKey; @@ -36,6 +39,7 @@ import java.util.*; public class MessageUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MessageUtil.class); + private static HashMap skyEyeExtraMap = new HashMap<>(); public static byte[] wrappedBuffer(int uid, int token, int index, int result, int msgId, GeneratedMessage generatedMessage) { byte[] backMessage; @@ -79,8 +83,9 @@ public class MessageUtil { return bytes1; } - public static byte[] wrappedBufferSE(GeneratedMessage generatedMessage) { - byte[] idByte = short2byte((short) 0xd6ac); + public static byte[] wrappedBufferSE(GeneratedMessage generatedMessage,SkyEyeProperties properties) { + int b1 = Integer.parseInt(properties.getAppId(),16); + byte[] idByte = short2byte((short) b1); byte[] backMessage = generatedMessage.toByteArray(); byte[] verifyByte = new byte[2+4+backMessage.length]; byte[] lengthByte =int2bytes(backMessage.length); @@ -457,8 +462,10 @@ public class MessageUtil { if(uid != 0){ he = UserManager.getUser(uid); } - ChatProto.ChatV3.Builder v3 = buildChatV3(msg,me,he,channel); - byte[] byteBuf = wrappedBufferSE(v3.build()); + SkyEyeAutoConfigration netServerConfig = ConfigurableApplicationContextManager.getBean(SkyEyeAutoConfigration.class); + SkyEyeProperties properties = netServerConfig.getSkyEyeProperties(); + ChatProto.ChatV3.Builder v3 = buildChatV3(msg,me,he,channel,properties); + byte[] byteBuf = wrappedBufferSE(v3.build(),properties); // session.writeAndFlush(byteBuf); // session.putBackMassageToMap(byteBuf); ByteBuf bb = PooledByteBufAllocator.DEFAULT.heapBuffer(); @@ -466,7 +473,7 @@ public class MessageUtil { 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(); String uuid = UUID.randomUUID().toString().replaceAll("-",""); v3.setId(uuid); @@ -479,6 +486,10 @@ public class MessageUtil { ISession iSession = OnlineUserManager.getSessionByUid(me.getId()); String ip = iSession.getChannel().getLocalAddress().getHostString(); v3.setIp(ip); + skyEyeExtraMap.clear(); + String url = properties.getGmAddress(); + skyEyeExtraMap.put("url",url); + v3.setExtra(JSON.toJSONString(skyEyeExtraMap)); return v3; } @@ -491,7 +502,7 @@ public class MessageUtil { v3.setVipLevel(user.getPlayerInfoManager().getVipLevel()); v3.setZoneId(String.valueOf(GameApplication.serverId)); v3.setZoneName(String.valueOf(GameApplication.serverId)); - v3.setServerId(1+""); + v3.setServerId(user.getPlayerInfoManager().getChannel_id()); return v3; } //高位在前,低位在后