协议解析
parent
4255e37558
commit
6631ea50f3
|
@ -1,19 +1,67 @@
|
|||
package com.ljsd.jieling.handler;
|
||||
|
||||
import com.google.protobuf.*;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.PlayerInfoProto;
|
||||
|
||||
public abstract class BaseHandler {
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public abstract class BaseHandler<T extends GeneratedMessage> {
|
||||
|
||||
Class<T> protoClazz;
|
||||
|
||||
public abstract MessageTypeProto.MessageType getMessageCode();
|
||||
|
||||
public void execute(ISession session, PacketNetData netData) throws Exception {
|
||||
|
||||
//一般错误处理,消息验证
|
||||
process(session, netData);
|
||||
if (null == protoClazz && getTClassIsNull()) {
|
||||
process(session, netData);
|
||||
}else {
|
||||
processWithProto(session, netData);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void process(ISession iSession, PacketNetData netData) throws Exception;
|
||||
@SuppressWarnings("unchecked")
|
||||
private void processWithProto(ISession iSession, PacketNetData netData) {
|
||||
try {
|
||||
Field field = protoClazz.getField("PARSER");
|
||||
Parser<T> parser = (Parser) field.get(protoClazz);
|
||||
processWithProto(iSession, parser.parseFrom(netData.parseClientProtoNetData()));
|
||||
} catch (Exception var4) {
|
||||
System.out.print("解析协议失败" + var4);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 兼容老的类
|
||||
* process/processWithProto只需要重写一个
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* process/processWithProto只需要重写一个
|
||||
*/
|
||||
public void processWithProto(ISession iSession, T proto) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private boolean getTClassIsNull() {
|
||||
Type superClass = this.getClass().getGenericSuperclass();
|
||||
if (superClass instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) superClass;
|
||||
protoClazz = (Class<T>) parameterizedType.getActualTypeArguments()[0];
|
||||
return null == protoClazz;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,23 +1,21 @@
|
|||
package com.ljsd.jieling.handler;
|
||||
|
||||
import com.ljsd.jieling.logic.activity.ActivityLogic;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.PlayerInfoProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ForceRankHandler extends BaseHandler {
|
||||
public class ForceRankHandler extends BaseHandler<PlayerInfoProto.GetForceRankInfoRequest> {
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.GETF_FORCE_RANK_INFO_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
byte[] bytes = netData.parseClientProtoNetData();
|
||||
PlayerInfoProto.GetForceRankInfoRequest getForceRankInfoRequest = PlayerInfoProto.GetForceRankInfoRequest.parseFrom(bytes);
|
||||
ActivityLogic.getInstance().getForceRank(iSession,getForceRankInfoRequest.getPage(),getForceRankInfoRequest.getActiviteId());
|
||||
public void processWithProto(ISession iSession, PlayerInfoProto.GetForceRankInfoRequest proto) throws Exception {
|
||||
ActivityLogic.getInstance().getForceRank(iSession,proto.getPage(),proto.getActiviteId());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue