2019-01-16 16:19:44 +08:00
|
|
|
package network.client;
|
2018-12-27 17:54:23 +08:00
|
|
|
|
2019-01-16 16:19:44 +08:00
|
|
|
import com.google.protobuf.*;
|
|
|
|
import com.ljsd.jieling.netty.cocdex.Tea;
|
2019-01-22 16:30:21 +08:00
|
|
|
import com.ljsd.jieling.protocols.CommonProto;
|
2019-01-18 09:58:37 +08:00
|
|
|
import com.ljsd.jieling.protocols.HeroInfoProto;
|
2019-01-16 16:19:44 +08:00
|
|
|
import com.ljsd.jieling.protocols.MessageTypeProto;
|
2018-12-27 17:54:23 +08:00
|
|
|
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);
|
|
|
|
|
2019-01-24 14:35:59 +08:00
|
|
|
private static int uid = 10000026;
|
2019-01-16 16:19:44 +08:00
|
|
|
private static int msgIndex = 1;
|
|
|
|
private static int token=543242;
|
|
|
|
|
2018-12-27 17:54:23 +08:00
|
|
|
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());
|
|
|
|
|
2019-01-23 10:41:10 +08:00
|
|
|
Channel ch = b.connect("127.0.0.1",16080).sync().channel();
|
2018-12-27 17:54:23 +08:00
|
|
|
|
|
|
|
NettyTCPClientHandler handler = ch.pipeline().get(NettyTCPClientHandler.class);
|
2019-01-16 16:19:44 +08:00
|
|
|
//------------------------------------------测试类型-------------------------------------------------------
|
2018-12-27 17:54:23 +08:00
|
|
|
// GameProto.GameMessage gameMessage = new NettyClient().sendLoginMessageTest();//登陆请求
|
2019-01-17 20:50:13 +08:00
|
|
|
PlayerInfoProto.GetPlayerInfoRequest gameMessage = new NettyClient().sendRegisterMessageTest();//注册请求
|
2018-12-27 17:54:23 +08:00
|
|
|
LOGGER.info(gameMessage.toString());
|
2019-01-16 16:19:44 +08:00
|
|
|
//---------------------------------------------------------------------------------------------------------
|
2018-12-27 17:54:23 +08:00
|
|
|
|
2019-01-16 16:19:44 +08:00
|
|
|
handler.sendRequest_jieling(makeFinalMessage(gameMessage, MessageTypeProto.MessageType.LOGIN_REQUEST_VALUE));
|
2019-01-23 10:41:10 +08:00
|
|
|
// handler.sendRequest_jieling(makeFinalMessage(getHero(), MessageTypeProto.MessageType.GET_HEROINFO_REQUEST_VALUE));
|
|
|
|
// handler.sendRequest_jieling(makeFinalMessage(getRandomHero(), MessageTypeProto.MessageType.HERO_RAND_REQQUEST_VALUE));
|
2019-01-24 14:35:59 +08:00
|
|
|
// for (int i = 1000; i< 1500;i++){
|
|
|
|
handler.sendRequest_jieling(makeFinalMessage(gmRequest(0), MessageTypeProto.MessageType.GM_REQUEST_VALUE));
|
|
|
|
// }
|
2019-01-07 09:53:06 +08:00
|
|
|
|
2019-01-16 16:19:44 +08:00
|
|
|
}
|
2018-12-27 17:54:23 +08:00
|
|
|
|
2019-01-18 12:06:45 +08:00
|
|
|
public static HeroInfoProto.GetHeroListInfoRequest getHero(){
|
|
|
|
return HeroInfoProto.GetHeroListInfoRequest.newBuilder().setNum(1).setStr("111").build();
|
2019-01-18 09:58:37 +08:00
|
|
|
}
|
2019-01-21 15:31:02 +08:00
|
|
|
public static HeroInfoProto.HeroRandRequest getRandomHero(){
|
|
|
|
return HeroInfoProto.HeroRandRequest.newBuilder().setType(11).build();
|
|
|
|
|
|
|
|
}
|
2019-01-18 09:58:37 +08:00
|
|
|
|
2019-01-16 16:19:44 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-12-27 17:54:23 +08:00
|
|
|
|
2019-01-23 10:41:10 +08:00
|
|
|
public static CommonProto.GMCommand gmRequest(int itemId){
|
2019-01-24 14:35:59 +08:00
|
|
|
return CommonProto.GMCommand.newBuilder().setCommand("3#"+1+"#10000").build();
|
2018-12-27 17:54:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-17 20:50:13 +08:00
|
|
|
private PlayerInfoProto.GetPlayerInfoRequest sendRegisterMessageTest(){
|
2018-12-27 17:54:23 +08:00
|
|
|
LOGGER.info("register");
|
2019-01-17 20:50:13 +08:00
|
|
|
PlayerInfoProto.GetPlayerInfoRequest registerRequest = PlayerInfoProto.GetPlayerInfoRequest
|
2018-12-27 17:54:23 +08:00
|
|
|
.newBuilder()
|
2019-01-05 09:24:33 +08:00
|
|
|
// .setNum(888)
|
|
|
|
// .setStr("666")
|
|
|
|
// .setStr1("222")
|
2018-12-27 17:54:23 +08:00
|
|
|
.build();
|
|
|
|
|
|
|
|
return registerRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|