reconnect server

back_recharge
wangyuan 2019-08-26 14:50:13 +08:00
parent 28067420f0
commit 70d2f8165a
4 changed files with 65 additions and 6 deletions

View File

@ -139,6 +139,13 @@ public class RedisKey {
public static final String MONSTER_ATTACK_RANK = "MONSTER_ATTACK_RANK";
//掉线缓存indication
public static final String INDICATION_OFFLINE = "INDICATION_OFFLINE";
public static final String RESPONSE_OFFLINE = "RESPONSE_OFFLINE";
public static final String SESSION_OFFLINE = "SESSION_OFFLINE";
public static String getKey(String type, String key, boolean withoutServerId) {
if (withoutServerId) {
return GameApplication.areaId + Delimiter_colon + type + Delimiter_colon + String.valueOf(key);

View File

@ -1,6 +1,7 @@
package com.ljsd.jieling.network.server;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.protobuf.GeneratedMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.ljsd.GameApplication;
@ -49,6 +50,7 @@ import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@ -207,6 +209,10 @@ public class ProtocolsManager implements ProtocolsAbstract {
if(b){
return;
}
if( packetNetData.getMsgId()== MessageTypeProto.MessageType.RECONNECT_REQUEST_VALUE){
processReconnect(session);
return;
}
//LOGGER.info("process the uid={},the msg={},the index={}",packetNetData.getUserId(), MessageTypeProto.MessageType.valueOf(packetNetData.getMsgId()),packetNetData.getIndex());
BaseHandler baseHandler = handlers.get(packetNetData.getMsgId());
if (baseHandler == null) {
@ -234,6 +240,37 @@ public class ProtocolsManager implements ProtocolsAbstract {
}
public void processReconnect(ISession session){
String key = RedisUtil.getInstence().getKey(RedisKey.INDICATION_OFFLINE, Integer.toString(session.getUid()));
Type type = new TypeToken<Map<Integer, ISession.IndicationMsg>>(){}.getType();
Map<Integer, ISession.IndicationMsg> map= gson.fromJson(RedisUtil.getInstence().get(key).toString(),type);
int maxMsgId = 0;
if(map!=null && map.isEmpty()){
for(Map.Entry<Integer,ISession.IndicationMsg> info : map.entrySet()){
Integer msgIndex = info.getKey();
session.writeAndFlush(info.getValue());
session.putBackIndicationToMap(msgIndex,info.getValue().getMsg());
if(maxMsgId<msgIndex){
maxMsgId = msgIndex;
}
}
RedisUtil.getInstence().del(key);
}
key = RedisUtil.getInstence().getKey(RedisKey.RESPONSE_OFFLINE, Integer.toString(session.getUid()));
type = new TypeToken<Map<Integer, byte[]>>(){}.getType();
Map<Integer, byte[]> response = gson.fromJson(RedisUtil.getInstence().get(key).toString(), type);
session.setFiveReady(1);
session.updateIndicationIndex(maxMsgId);
if(response!=null&&!response.isEmpty()){
session.setIndexToMessage(new ConcurrentHashMap<Integer, byte[]> (response));
session.setIndex(response.keySet().iterator().next());
RedisUtil.getInstence().del(key);
}
MessageUtil.sendMessageWithoutBack(session,1, MessageTypeProto.MessageType.RECONNECT_RESPONSE_VALUE,null,true);
}
private void refreshToken(ISession session) {
@ -348,7 +385,6 @@ public class ProtocolsManager implements ProtocolsAbstract {
}
GameMessageHandler.currentSessions.remove(session.getUid());
try {
OnlineUserManager.userOffline(session.getUid());
PlayerManager playerInfoManager = user.getPlayerInfoManager();
@ -359,6 +395,14 @@ public class ProtocolsManager implements ProtocolsAbstract {
ActivityLogic.getInstance().updateActivityMissionProgress(user, ActivityType.OnlineReward,(int)(onlineTime/TimeUtils.ONE_SECOND));
GuildLogic.updateMyPos(user);
MongoUtil.getInstence().lastUpdate();
ConcurrentHashMap<Integer, ISession.IndicationMsg> indexToIndication = session.getIndexToIndication();
if(!indexToIndication.isEmpty() ){
String key = RedisUtil.getInstence().getKey(RedisKey.INDICATION_OFFLINE, Integer.toString(session.getUid()));
RedisUtil.getInstence().set(key,gson.toJson(indexToIndication));
}
String key = RedisUtil.getInstence().getKey(RedisKey.RESPONSE_OFFLINE, Integer.toString(session.getUid()));
RedisUtil.getInstence().set(key,gson.toJson(session.getIndexToMessage()));
// 下线处理的逻辑需要添加在这里
} catch (Exception e) {
e.printStackTrace();

View File

@ -146,6 +146,10 @@ public class ISession extends NettyGameSession {
this.fiveReady = fiveReady;
}
public void updateIndicationIndex(int index){
this.indicationIndex.set(index);
}
public byte[] getFiveByteBuf() {
return fiveByteBuf;
}
@ -154,9 +158,4 @@ public class ISession extends NettyGameSession {
this.fiveByteBuf = fiveByteBuf;
}
public static void main(String[] args) {
AtomicInteger a= new AtomicInteger(1);
System.out.println(a.incrementAndGet());
System.out.println(a.get());
}
}

View File

@ -103,6 +103,15 @@ public class MessageUtil {
session.putBackMassageToMap(byteBuf);
}
public static void sendMessageWithoutBack(ISession session, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) {
byte[] byteBuf = wrappedBuffer(session.getUid(), session.getToken(), session.getIndex(), result, msgId, generatedMessage);
if (flush) {
session.writeAndFlush(byteBuf);
} else {
session.write(byteBuf);
}
}
public static void sendIndicationMessage(ISession session, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) {
if(session.getFiveReady() == 0){