将VMSConnection模块加入min-dev-java,以支持与VMS进行通信,执行注册、登录等功能

This commit is contained in:
free will committed 2021-07-12 21:44:18 +08:00
1 parent a26f908470
commit ba105bdc3c
36 files changed
+2961 -2

No files matched your search

@@ -0,0 +1,76 @@
package VMSConnection.Encoding;
import VMSConnection.Utils.ByteHelper;
import java.io.IOException;
import java.nio.channels.SocketChannel;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 19:47 2021/6/24
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class BC_Encoder {
/**
* Encode 将消息编码: 加一个长度头
* @param message
* @return
*/
public static byte[] Encode(byte[] message){
byte[] length= ByteHelper.int32ToByteArray(message.length);
byte[] pkg=new byte[4+ message.length];
System.arraycopy(length,0,pkg,0,4);
System.arraycopy(message,0,pkg,4,message.length);
return pkg;
}
/**
* 解码消息:去掉长度头
* @param channel
* @return
*/
public static byte[] DecodeTcp(SocketChannel channel){
if(!channel.isConnected()){
// 重新连接
try {
System.out.println("DecodeTcp-重新连接: "+channel.getRemoteAddress());
channel.connect(channel.getRemoteAddress());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
System.out.println("start read...");
// 读取消息的长度
byte[] buf=new byte[4];
try {
int revL=channel.socket().getInputStream().read(buf);
// channel.read(ByteBuffer.wrap(buf));
if(revL<0){
System.out.println("read len < 0");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
int length=ByteHelper.byteArrayToInt32(buf);
System.out.println("read 111: "+length);
// 读取真正的消息数据
byte[] pkg=new byte[length];
try {
int revL=channel.socket().getInputStream().read(pkg);
if(revL<0){
System.out.println("read len < 0");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
System.out.println("read 222: "+new String(pkg));
return pkg;
}
}