想把这个加密文本绕过去改成代码内定义,求支援
package org.come.socket;
import io.netty.channel.Channel;
import org.come.action.MapAction;
import org.come.bean.Account;
import io.netty.channel.ChannelHandlerContext;
public class SendMessageUntil {
// 重新连接标识
public static boolean CanDoConnectOrNo = true;
// 发送信息给服务端的(登录注册信息发送通道)
public static ChannelHandlerContext SendMesToServer;
static String FB = "\n";
/** zrikka 2020 0414 */
public static String gameServerKey = "";
public static String accServerKey = "";
public static String project = "";
public static Account account = null;
public static void toServer(final String sendmes) {
if (MapAction.flagAction.get(gameServerKey)) {
MapAction.nettyAction.get(gameServerKey).writeAndFlush(sendmes + FB);
}
}
// 与 账号服务器 的交互
public static void loginToServer(String sendmes) {
if (MapAction.flagAction.get(accServerKey)) {
MapAction.nettyAction.get(accServerKey).writeAndFlush(sendmes + FB);
}
}
/** 实例化连接 */
public static void getClientUntil() throws Exception {
if (SendMesToServer == null) {
// 启动服务器连接
GameClient client = new GameClient();
client.start();
}
}
public static void getClientUntil(String ip, int port) throws Exception {
if (SendMesToServer == null) {
// 启动服务器连接
GameClient client = new GameClient(ip,port);
client.start();
}
}
/** 实例化连接 */
}
package org.come.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.come.until.AESUtil;
import org.come.until.CreateTextUtil;
import org.come.until.Util;
import javax.sound.sampled.Port;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.charset.Charset;
import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class GameClient {
//TODO 客户端版本号 对应服务端配置文件两端不一致则不允许登录
public static String VERSION = "version861808";
public static String BT = "哈哈";
public static String atid;
public static String userid;
public static int lianhua = 0;
private NioEventLoopGroup workGroup = new NioEventLoopGroup();
private Channel channel;
private Bootstrap bootstrap;
public static String[] potAndIpStrings;
// 私密钥匙
private static String privateStr;
/** zrikka 2020-0414 */
public static String username;
public static String userpasw;
private String ip;// netty ip
private int port;// netty 端口
private String serverType;// 服务器类型
/******/
/** Mac地址 */
public static String Mac;
/** 断线重连信息 */
public static String DXCL = null;
public static BigDecimal tempreId = null;
/** zrikka 2020-0414 */
public GameClient() {
ip = "127.0.0.1";
this.port = 7100;
}
public GameClient(String ip ,int port) {
this.ip = ip;
this.port = port;
}
public static String getPrivateStr() {
return privateStr;
}
public static void setPrivateStr(String privateStr) {
GameClient.privateStr = privateStr;
}
/**
public static void main(String[] args) throws Exception {
String str = AESUtil.AESJDKDncode(CreateTextUtil.getContent("resource/other/opacity-inif.pat"),
">LA~h4FNKPMJW077jxO");
privateStr = AESUtil.AESJDKDncode(CreateTextUtil.getContent("resource/other/opacity-selfinif.pat"), str);
System.out.println(AESUtil.AESJDKDncode(CreateTextUtil.getContent("resource/other/init.txt"), privateStr));
}
*/
public void start() {
try {
bootstrap = new Bootstrap();
bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);// 0拷贝
bootstrap.group(workGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline p = socketChannel.pipeline();
// 以("\n")为结尾分割的 解码器
p.addLast("framer",
new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()));
// 字符串解码和编码
p.addLast("decoder", new StringDecoder(Charset.forName("utf-8")));
p.addLast("encoder", new StringEncoder(Charset.forName("utf-8")));
p.addLast(new IdleStateHandler(0, 30, 0, TimeUnit.SECONDS));
p.addLast(new GameClientHandler(GameClient.this));
}
});
bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT);
bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
doConnect();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 初始化次数
int a = 0;
protected void doConnect() throws Exception {
if (channel != null && channel.isActive()) {
return;
}
SendMessageUntil.gameServerKey = ip + "|" + port;
ChannelFuture future = bootstrap.connect(ip, port);
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture futureListener) throws Exception {
if (futureListener.isSuccess()) {
// 重置次数
a = 0;
channel = futureListener.channel();
/** zrikka 2020 0414 **/
// if ("login".equals(serverType)) {
// return;
// }
/**********/
// 进行断线重新连接
if (DXCL != null) {
System.err.println("断线重新连接");
try {
Thread.sleep(100 + Util.random.nextInt(40) * 50);
} catch (Exception e) {
// TODO: handle exception
}
// 设置返回服务器的信息
String serverMes = Agreement.getAgreement().enterGameAgreement(DXCL);
channel.writeAndFlush(serverMes + "\n");
}
} else {
futureListener.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {
try {
// 重连超过30次关闭游戏
if (a <= 30) {
a++;
doConnect();
} else {
System.exit(0);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 10, TimeUnit.SECONDS);
}
}
});
}
/**
* 服务器ip和端口类
*
* @throws Exception
*/
public static String[] getServerIpAndPort() throws Exception {
Mac = getLocalMac();
// 128位公钥
String str = AESUtil.AESJDKDncode(CreateTextUtil.getContent("resource/other/opacity-inif.pat"),
">LA~h4FNKPMJW077jxO");
privateStr = AESUtil.AESJDKDncode(CreateTextUtil.getContent("resource/other/opacity-selfinif.pat"), str);
String msg1 = AESUtil.AESJDKDncode(CreateTextUtil.getContent("resource/other/init.txt"), privateStr);
System.out.println(privateStr);
System.out.println(msg1);
String mes1[] = msg1.toString().split(";");
int sum = Integer.parseInt(mes1[0].split("=")[1]);
String portAndIp[] = new String[(sum) * 8];
for (int i = 0; i < portAndIp.length; i++) {
if (i + 1 < mes1.length) {
portAndIp[i] = mes1[i + 1].split("=")[1];
}
System.out.println(portAndIp);
}
return portAndIp;
}
/** 转区格式 */
/** 获取mac地址 */
private static String getLocalMac() throws Exception {
// TODO Auto-generated method stub
// 获取网卡,获取地址
InetAddress ia = InetAddress.getLocalHost();
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// 字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
sb.append("0" + str);
} else {
sb.append(str);
}
}
return sb.toString().toUpperCase();
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
因为是对串口返回的数据是以ReadExisting()来进行读取;
官网解释:
在编码的基础上,读取 SerialPort 对象的流和输入缓冲区中所有立即可用的字节。
返回 String
SerialPort 对象的流和输入缓冲区的内容。
1.当下位机返回的数据量大时,接受到的数据可能会发生错乱。比如A发送的指令本应该是返回给A的,结果因为B也做了发送指令的操作,此时原本是返回给A的数据被B接收到。
解决办法:对串口读写加上线程锁,这样就能确保每次的读写不会错乱
2.或是有特殊的帧头 帧尾,这时就需要对数据进行一个拆包的操作
(略...)