public static WsPacket decodeJson(String stringResult) { try { JSONObject jsObj = JSONObject.fromObject(stringResult); String wsOpCode = jsObj.getString(WSOPCODE); if (wsOpCode == null) { if (WSManager.log != null) { WSManager.log.warn("数据为:" + stringResult + ",无wsOpCode"); } return null; } if (!WSManager.wsOpCodeMap.containsKey(wsOpCode)) { if (WSManager.log != null) { WSManager.log.warn("wsOpCode为:" + wsOpCode + "无对应解析,请及时解决"); } return null; } Class<?> className = WSManager.wsOpCodeMap.get(wsOpCode); Method buildM = className.getDeclaredMethod("newBuilder"); AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null); Message data = PacketUtils.jsonToProtoBuf(stringResult, builder); WsPacket wsPacket = new WsPacket(wsOpCode, data); return wsPacket; } catch (Exception e) { if (WSManager.log != null) { WSManager.log.error("json转换成protobuf异常", e); } return null; } }
private static Builder getBuilder(Class<?> cl) throws Exception { if (cl==null) { return null; } Method meth = cl.getMethod("newBuilder"); if (!Modifier.isStatic(meth.getModifiers()) || !Modifier.isPublic(meth.getModifiers())) { throw new Exception("Not a PB!"); } Object o = meth.invoke(cl); if (!(o instanceof Builder)) { throw new Exception("Not a PB Descriptor!"); } return (Builder) o; }
/** * 将字符串转换成HttpPacket * * @param stringResult * 字符串 * @param isServer * 是不是服务器一般传true * @param httpPacket * 消息包 * @return */ public static boolean decodeJson(String stringResult, boolean isServer, HttpPacket httpPacket) { try { // 转换成json获取hOpCode,如果没有看看头消息有没有 JSONObject jsObj = JSONObject.fromObject(stringResult); String hOpCode; if (jsObj.containsKey(AllowParam.HOPCODE)) { hOpCode = jsObj.getString(AllowParam.HOPCODE); } else if (httpPacket.hSession.headParam.hOpCode != null && !httpPacket.hSession.headParam.hOpCode.equals("")) { hOpCode = httpPacket.hSession.headParam.hOpCode; } else { return false; } // 是否设定相应解析 if (!HttpManager.hOpCodeMap.containsKey(hOpCode)) { if (HttpConfig.log != null) { HttpConfig.log.warn("hOpCode为:" + hOpCode + "无对应解析,请及时解决"); } return false; } // 解析 Class<?>[] classNames = HttpManager.hOpCodeMap.get(hOpCode); Class<?> className; if (isServer) { className = classNames[0]; } else { className = classNames[1]; } Method buildM = className.getDeclaredMethod("newBuilder"); AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null); Message data = PacketUtils.jsonToProtoBuf(stringResult, builder); if (data == null) { return false; } // 设置hOpCode和消息体 httpPacket.sethOpCode(hOpCode); httpPacket.setData(data); return true; } catch (Exception e) { if (HttpConfig.log != null) { HttpConfig.log.error("json转换成protobuf异常", e); } return false; } }
private static BuilderParser getParser(Class<?> cl) throws Exception { if (cl == null) { return null; } Method meth = cl.getMethod("newBuilder"); if (!Modifier.isStatic(meth.getModifiers()) || !Modifier.isPublic(meth.getModifiers())) { throw new Exception("Not a PB!"); } Object o = meth.invoke(cl); if (!(o instanceof Builder)) { throw new Exception("Not a PB Descriptor!"); } return new BuilderParser<>((Builder) o); }
/** * 发送到所有网关连接 */ public void sendAll(short code, Builder<?> messageBuilder) { PBMessage packet = new PBMessage(code); if (messageBuilder != null) { // packet.setMessage(messageBuilder.build()); } gateWay.send(packet); }
/** * 发送黑名单列表给玩家 */ public void sendTotalBlackUser() { BlackInfoListMsg.Builder netMsg = BlackInfoListMsg.newBuilder(); List<String> blackUser = playerInfo.getBlackUserList(); for (String userName : blackUser) { netMsg.addBlackInfoList(userName); } sendPacket(Protocol.S_C_TOTAL_BLACK_USER, netMsg); }
/** * 发送消息包 */ public void sendPacket(short msgId, Builder<?> msgBuilder) { PBMessage packet = new PBMessage(msgId); if (msgBuilder != null) { // packet.setMessage(msgBuilder.build()); } sendPacket(packet); }
/** * 发送各类提示信息给客户端 */ public void sendTips(int tipId, Object... args) { TipInfoMsg.Builder netMsg = TipInfoMsg.newBuilder(); netMsg.setTipId(tipId); for (int index = 0; index < args.length; index++) { netMsg.addContent(String.valueOf(args[index])); } sendPacket(Protocol.S_C_TIP_INFO, netMsg); }
private void syncPlayerProperty(boolean isAllSynch) { if (changeCount.get() <= 0) { PlayerMsg.Builder playerInfoMsg = PlayerMsg.newBuilder(); initPlayerMsg(playerInfoMsg, isAllSynch); sendPacket(Protocol.S_C_PLAYER_INFO, playerInfoMsg); } }
public void pvpRandomItem() { PVPRandomDiceMsg.Builder netMsg = PVPRandomDiceMsg.newBuilder(); int itemIndex = 0; for (int i = 0; i < 7; i++) { int color = ThreadLocalRandom.current().nextInt(1, 5); DiceInfoMsg.Builder diceInfoMsg = DiceInfoMsg.newBuilder(); diceInfoMsg.setQuality(color); int[] aryColor = aryWhite; switch (color) { case 2: aryColor = aryBlack; break; case 3: aryColor = aryGold; break; case 4: aryColor = aryGreen; break; default: break; } int index = ThreadLocalRandom.current().nextInt(0, 6); itemIndex += aryColor[index]; diceInfoMsg.setPointNum(index); netMsg.addDiceInfoList(diceInfoMsg); } int itemId = randomItemIdList.get(itemIndex).type; propMgr.addOnePropToPackage(itemId, 1, ItemChangeType.PVP_RANDOM_DICE_REWARD); netMsg.setItemIndex(itemIndex + 1); sendPacket(Protocol.S_C_PVP_RANDOM_ITEM, netMsg); }
/** * @see com.quancheng.saluki.serializer.IProtobufSerializer#toProtobuf(java.lang.Object) */ @Override @SuppressWarnings({"unchecked", "rawtypes", "unused"}) public Message toProtobuf(Object pojo) throws ProtobufException { try { final Class<?> fromClazz = (Class<?>) pojo.getClass(); final Class<? extends GeneratedMessageV3> protoClazz = ProtobufSerializerUtils.getProtobufClassFromPojoAnno(fromClazz); if (protoClazz == null) { throw new ProtobufAnnotationException( "Doesn't seem like " + fromClazz + " is ProtobufEntity"); } final Map<Field, ProtobufAttribute> protoBufFields = ProtobufSerializerUtils.getAllProtbufFields(fromClazz); if (protoBufFields.isEmpty()) { return null; } final Method newBuilderMethod = protoClazz.getMethod("newBuilder"); final Builder protoObjBuilder = (Builder) newBuilderMethod.invoke(null); for (Entry<Field, ProtobufAttribute> entry : protoBufFields.entrySet()) { final Field field = entry.getKey(); final ProtobufAttribute gpbAnnotation = entry.getValue(); final String fieldName = field.getName(); // 1. Determine validity of value Object value = Pojo2ProtobufHelp.getPojoFieldValue(pojo, gpbAnnotation, field); // If value is null and it is not required, skip, as the default for Protobuf values is null if (value == null) { continue; } // 2. Call recursively if this is a ProtobufEntity value = Pojo2ProtobufHelp.serializeToProtobufEntity(value); // 3. Special recursively if this is a ProtobufEntity if (value instanceof Collection) { value = Pojo2ProtobufHelp.convertCollectionToProtobufs((Collection<Object>) value); if (((Collection) value).isEmpty()) { continue; } } if (value instanceof Map) { value = Pojo2ProtobufHelp.convertMapToProtobufs((Map) value); if (((Map) value).isEmpty()) { continue; } } String setter = ProtobufSerializerUtils.getProtobufSetter(gpbAnnotation, field, value); if (value instanceof Enum) { value = JReflectionUtils.runMethod(value, "getNumber"); setter = setter + "Value"; } Pojo2ProtobufHelp.setProtobufFieldValue(gpbAnnotation, protoObjBuilder, setter, value); } return protoObjBuilder.build(); } catch (Exception e) { throw new ProtobufException( "Could not generate Protobuf object for " + pojo.getClass() + ": " + e, e); } }
public Message get(String key, Builder<?> builder) throws Exception { byte[] data = get(key); return (data == null) ? null : builder.mergeFrom(data).build(); }
public HttpPBPacket(int opcode, Builder<?> builder) { this.opcode = opcode; Message msg = builder.build(); // log.info("[HttpPBPacket][new] \nopode:{}, message:\n[\n{}]" , opcode, msg); this.data = msg.toByteArray(); }
public BuilderParser(Builder builder) { this.builder = builder.clone().clear(); }
public void sendPlayerInfo(boolean isAllSynch) { PlayerMsg.Builder playerInfoMsg = PlayerMsg.newBuilder(); initPlayerMsg(playerInfoMsg, isAllSynch); sendPacket(Protocol.S_C_PLAYER_INFO, playerInfoMsg); }
/** * 消息创建 */ public static PBMessage buildMessage(short code, Builder<?> messageBuilder) { PBMessage response = new PBMessage(code); // response.setMessage(messageBuilder.build()); return response; }