package network.client; import com.google.protobuf.*; import com.ljsd.jieling.netty.cocdex.Tea; import com.ljsd.jieling.protocols.CommonProto; import com.ljsd.jieling.protocols.HeroInfoProto; import com.ljsd.jieling.protocols.MessageTypeProto; import com.ljsd.jieling.protocols.PlayerInfoProto; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; public class NettyClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); private static int uid = 40000072; private static int msgIndex = 1; private static int token=543242; public static void main(String[] args) throws InterruptedException, InvalidProtocolBufferException { EventLoopGroup group = new NioEventLoopGroup(1); // try{ Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .handler(new NettyTCPClientInitializer()); Channel ch = b.connect("127.0.0.1",16080).sync().channel(); NettyTCPClientHandler handler = ch.pipeline().get(NettyTCPClientHandler.class); //------------------------------------------测试类型------------------------------------------------------- // GameProto.GameMessage gameMessage = new NettyClient().sendLoginMessageTest();//登陆请求 PlayerInfoProto.GetPlayerInfoRequest gameMessage = new NettyClient().sendRegisterMessageTest();//注册请求 LOGGER.info(gameMessage.toString()); //--------------------------------------------------------------------------------------------------------- handler.sendRequest_jieling(makeFinalMessage(gameMessage, MessageTypeProto.MessageType.LOGIN_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(getHero(), MessageTypeProto.MessageType.GET_HEROINFO_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(getRandomHero(), MessageTypeProto.MessageType.HERO_RAND_REQQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(gmRequest(11001), MessageTypeProto.MessageType.GM_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(useAndPriceItemRequest(0,11001,2), MessageTypeProto.MessageType.USER_AND_PRICE_ITEM_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(heroComperRequest(1001,10), MessageTypeProto.MessageType.HERO_COMPOSE_REQUEST_VALUE)); // }10052 // handler.sendRequest_jieling(makeFinalMessage(gmRequest(10052), MessageTypeProto.MessageType.GM_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(gmRequest(10052), MessageTypeProto.MessageType.GM_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(upHeroLevel("10000072010015487337620000"), MessageTypeProto.MessageType.UP_HERO_LEVEL_REQUEST_VALUE)); /* try { Map config = STableManager.getConfig(SCHero.class); for(Integer heroId : config.keySet()){ handler.sendRequest_jieling(makeFinalMessage(gmRequest(heroId), MessageTypeProto.MessageType.GM_REQUEST_VALUE)); } } catch (Exception e) { e.printStackTrace(); }*/ handler.sendRequest_jieling(makeFinalMessage(createEquip(3001), MessageTypeProto.MessageType.WORKSHOP_EQUIP_CREATE_REQUEST_VALUE)); } private static MessageLite heroComperRequest(int itemId, int itemNum) { CommonProto.Item item = CommonProto.Item.newBuilder() .setItemId(itemId) .setItemNum(itemNum) .build(); return HeroInfoProto.HeroComposeRequest.newBuilder().setItem(item).build(); } private static MessageLite useAndPriceItemRequest(int type ,int itemId , int itemNum) { CommonProto.Item item = CommonProto.Item.newBuilder() .setItemId(itemId) .setItemNum(itemNum) .build(); return PlayerInfoProto.UseAndPriceItemRequest.newBuilder().setType(type).setItem(item).build(); } public static HeroInfoProto.GetHeroListInfoRequest getHero(){ return HeroInfoProto.GetHeroListInfoRequest.newBuilder().setNum(1).setStr("111").build(); } public static HeroInfoProto.HeroRandRequest getRandomHero(){ return HeroInfoProto.HeroRandRequest.newBuilder().setType(11).build(); } private static byte[] makeFinalMessage(MessageLite request, int msgNum) { byte[] messageData = request.toByteArray(); byte[] packetData = new byte[messageData.length + 16]; Tea.intToByte(packetData, 0, uid); //uid Tea.intToByte(packetData, 4, token); //token Tea.intToByte(packetData, 8,msgNum); //msgId Tea.intToByte(packetData, 12, msgIndex); // msgIndex System.arraycopy(messageData, 0, packetData, 16, messageData.length); int[] secretKey = Tea.KEY; byte[] finalData = Tea.encrypt2(packetData, secretKey); return finalData; } public static CommonProto.GMCommand gmRequest(int itemId){ return CommonProto.GMCommand.newBuilder().setCommand("2#"+itemId+"#1").build(); } public HeroInfoProto.UpHeroStarRequest upHeroStar(){ String heroId = ""; List consumeMaterialsList = new ArrayList<>(); consumeMaterialsList.add(HeroInfoProto.ConsumeMaterial.newBuilder().setPosition(1).addHeroIds("").build()); consumeMaterialsList.add(HeroInfoProto.ConsumeMaterial.newBuilder().setPosition(2).addHeroIds("").build()); consumeMaterialsList.add(HeroInfoProto.ConsumeMaterial.newBuilder().setPosition(3).addHeroIds("").build()); consumeMaterialsList.add(HeroInfoProto.ConsumeMaterial.newBuilder().setPosition(4).addHeroIds("").build()); return HeroInfoProto.UpHeroStarRequest.newBuilder().setHeroId(heroId).addAllConsumeMaterials(consumeMaterialsList).build(); } public static HeroInfoProto.UpHeroLevelRequest upHeroLevel(String heroId){ return HeroInfoProto.UpHeroLevelRequest.newBuilder().setHeroId(heroId).build(); } private PlayerInfoProto.GetPlayerInfoRequest sendRegisterMessageTest(){ LOGGER.info("register"); PlayerInfoProto.GetPlayerInfoRequest registerRequest = PlayerInfoProto.GetPlayerInfoRequest .newBuilder() // .setNum(888) // .setStr("666") // .setStr1("222") .build(); return registerRequest; } /** * 工坊 */ private static PlayerInfoProto.WorkShopEquipCreateRequest createEquip(int equipTid){ return PlayerInfoProto.WorkShopEquipCreateRequest.newBuilder().setEquipTid(equipTid).build(); } }