package network.client; import com.google.protobuf.*; import com.ljsd.jieling.netty.cocdex.Tea; import com.ljsd.jieling.protocols.GMCommandProto; 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; public class NettyClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); private static int uid = 10000001; 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",18090).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(gmRequest(), MessageTypeProto.MessageType.GM_COMMAND_VALUE)); } 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 GMCommandProto.GMCommand gmRequest(){ return GMCommandProto.GMCommand.newBuilder().setCommand("10000001#0#1#100").build(); } private PlayerInfoProto.GetPlayerInfoRequest sendRegisterMessageTest(){ LOGGER.info("register"); PlayerInfoProto.GetPlayerInfoRequest registerRequest = PlayerInfoProto.GetPlayerInfoRequest .newBuilder() // .setNum(888) // .setStr("666") // .setStr1("222") .build(); return registerRequest; } }