netty-调证测试类的方法(模拟安卓客户端)

This commit is contained in:
limqhz
2020-07-07 18:53:53 +08:00
parent 4436e9e6d9
commit dbf3174de4
17 changed files with 52 additions and 1023 deletions

View File

@@ -1,12 +1,13 @@
package com.test.netty.client;
import com.test.netty.dto.HeartBeat;
import com.test.netty.client.message.Cmd;
import com.test.netty.client.message.HeartBeat;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import com.test.netty.dto.Message;
import com.test.netty.client.message.Message;
/**
* 通讯服务器请求处理

View File

@@ -1,23 +1,26 @@
package com.test.netty.client;
import com.sv.netty.netty.MessageEncoder;
import com.test.netty.client.config.Constant;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
public class ClientInitializer extends ChannelInitializer<SocketChannel> {
private final static int TIME_HEART_BEAT = 20;
private final int READ_TIMEOUT = 60; //读超时时间
public ClientThread.ReConnectHandler reConnectHandler;
public ClientHandler dmClientHandler;
public ClientInitializer(ClientThread.ReConnectHandler handler) {
reConnectHandler = handler;
}
public ClientInitializer(ClientThread.ReConnectHandler handler, ClientHandler dmClientHandler) {
reConnectHandler = handler;
this.dmClientHandler = dmClientHandler;
@@ -28,10 +31,12 @@ public class ClientInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("reconnect", reConnectHandler);
pipeline.addLast("idleStateHandler", new IdleStateHandler(TIME_HEART_BEAT, TIME_HEART_BEAT, TIME_HEART_BEAT));
pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT));
pipeline.addLast(new DelimiterBasedFrameDecoder(2048,
Unpooled.wrappedBuffer(Constant.DELIMITER_WORD.getBytes())));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new MessageEncoder());
pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0));
pipeline.addLast(new MessageDecoder());
pipeline.addLast(new IdleStateHandler(TIME_HEART_BEAT, TIME_HEART_BEAT,TIME_HEART_BEAT));
pipeline.addLast(dmClientHandler);
}

View File

@@ -110,7 +110,7 @@ public class ClientThread extends Thread{
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
System.out.println("inactive");
System.out.println("ReConnectHandler inactive");
ctx.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {

View File

@@ -1,40 +0,0 @@
package com.test.netty.client;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import java.nio.ByteOrder;
/**
* 自定义解码器,解决粘包和分包问题
*
* @author peakren
* @date 07/12/2017 10:03 PM
*/
public class CustomDecoder extends LengthFieldBasedFrameDecoder {
public CustomDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
}
public CustomDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
}
public CustomDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}
public CustomDecoder(ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
super(byteOrder, maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
return super.decode(ctx, in);
}
}

View File

@@ -1,14 +0,0 @@
package com.test.netty.client;
/**
* Created by hehelt on 16/2/26.
*/
public class DataConfig {
public static final int MAGIC_WORD = 0x9DDD;
public static final int MAGIC_WORD_INDEX = 0;
public static final int LENGTH_INDEX = 4;
public static final int DATA_INDEX = 8;
}

View File

@@ -1,48 +0,0 @@
package com.test.netty.client;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* Created by hehelt on 16/2/26.
* <p/>
* 解码器
*/
public class MessageDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.capacity() >= DataConfig.LENGTH_INDEX) {
int magicWord = in.readInt();
if (magicWord == DataConfig.MAGIC_WORD) {
int length = in.readInt();
byte[] msg = new byte[length];
in.readBytes(msg);
String message = new String(msg, "utf-8");
out.add(message);
}
}
}
/**
* 解析从服务器接受的消息
*
* @param buf
* @return
*/
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
try {
return new String(con, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}

View File

@@ -1,24 +1,30 @@
package com.test.netty.client;
import com.test.netty.dto.JsonMapper;
import com.sv.netty.config.Constant;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import com.test.netty.dto.Message;
import com.test.netty.client.message.Message;
import java.nio.charset.Charset;
/**
* 自定义编码器, 1个字节固定头+4个字节长度+内容
*/
public class MessageEncoder extends MessageToByteEncoder<Message> {
public class MessageEncoder extends MessageToByteEncoder<String> {
private String charset = "utf-8";
Charset charset = Charset.forName("UTF-8");
/**
* 安卓打印日志,本地不需要
*/
private final static String TAG = "MessageEncoder";
@Override
protected void encode(ChannelHandlerContext ctx, Message message, ByteBuf out) throws Exception {
out.writeInt(DataConfig.MAGIC_WORD);
String msg = JsonMapper.toJson(message);
out.writeInt(msg.getBytes(charset).length);
out.writeBytes(msg.getBytes(charset));
protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
// Log.i(TAG, "send data:" + msg);
msg = msg + Constant.DELIMITER_WORD;
byte[] content = msg.getBytes(charset.name());
out.writeBytes(content); //发送消息内容
}
}

View File

@@ -0,0 +1,13 @@
package com.test.netty.client.config;
import com.sv.netty.netty.message.ChannelParam;
import io.netty.util.AttributeKey;
/**
* Created by ranfi on 2/22/16.
*/
public class Constant {
public final static String DELIMITER_WORD = "$_$";
}

View File

@@ -1,4 +1,4 @@
package com.test.netty.dto;
package com.test.netty.client.message;
import com.google.gson.annotations.Expose;

View File

@@ -1,4 +1,4 @@
package com.test.netty.client;
package com.test.netty.client.message;
/**
* 消息协议指令定义

View File

@@ -1,4 +1,4 @@
package com.test.netty.dto;
package com.test.netty.client.message;
import com.google.gson.annotations.Expose;

View File

@@ -1,4 +1,4 @@
package com.test.netty.dto;
package com.test.netty.client.message;
import com.google.gson.annotations.Expose;

View File

@@ -1,6 +1,7 @@
package com.test.netty.dto;
package com.test.netty.client.message;
import com.google.gson.annotations.Expose;
import com.test.netty.dto.FaceImageDto;
/**
* <pre>上位机与通讯服务器的数据协议格式</pre>

View File

@@ -1,142 +0,0 @@
package com.test.netty.dto;
import com.google.gson.annotations.Expose;
public class AddFaceResponse {
/**
* code : 0
* data : {"company_id":1,"id":4,"origin_url":"/static/upload/origin/2018-08-09/v2_1ea4b7847d1ea56b773aec99441af52dcbf9ca7d.jpg","quality":0.992649,"subject_id":null,"url":"/static/upload/photo/2018-08-09/v2_fa9dcfd045ff5232aa446f5645cbb031eef7ac74.jpg","version":7}
* page : {}
*/
@Expose
private int code;
@Expose
private DataBean data;
@Expose
private PageBean page;
@Expose
private String desc;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public PageBean getPage() {
return page;
}
public void setPage(PageBean page) {
this.page = page;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public static class DataBean {
/**
* company_id : 1
* id : 4
* origin_url : /static/upload/origin/2018-08-09/v2_1ea4b7847d1ea56b773aec99441af52dcbf9ca7d.jpg
* quality : 0.992649
* subject_id : null
* url : /static/upload/photo/2018-08-09/v2_fa9dcfd045ff5232aa446f5645cbb031eef7ac74.jpg
* version : 7
*/
@Expose
private int company_id;
@Expose
private int id;
@Expose
private String origin_url;
@Expose
private double quality;
@Expose
private Object subject_id;
@Expose
private String url;
@Expose
private int version;
public int getCompany_id() {
return company_id;
}
public void setCompany_id(int company_id) {
this.company_id = company_id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOrigin_url() {
return origin_url;
}
public void setOrigin_url(String origin_url) {
this.origin_url = origin_url;
}
public double getQuality() {
return quality;
}
public void setQuality(double quality) {
this.quality = quality;
}
public Object getSubject_id() {
return subject_id;
}
public void setSubject_id(Object subject_id) {
this.subject_id = subject_id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
public static class PageBean {
}
}

View File

@@ -1,52 +0,0 @@
package com.test.netty.dto;
import com.google.gson.annotations.Expose;
import java.io.Serializable;
/**
* 会员基本信息
* MemberDto.java
*
* @author peakren
* @date 2018/12/20 8:39 PM
*/
public class FaceImageDto implements Serializable {
@Expose
private Integer faceId;
@Expose
private Integer memberId;
@Expose
private String faceImage;
public Integer getFaceId() {
return faceId;
}
public void setFaceId(Integer faceId) {
this.faceId = faceId;
}
public Integer getMemberId() {
return memberId;
}
public void setMemberId(Integer memberId) {
this.memberId = memberId;
}
public String getFaceImage() {
return faceImage;
}
public void setFaceImage(String faceImage) {
this.faceImage = faceImage;
}
}

File diff suppressed because one or more lines are too long

View File

@@ -1,62 +0,0 @@
package com.test.netty.dto;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
/**
* Created by ranfi on 12/9/14.
*/
public class JsonMapper {
private final static Gson gson;
static {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
builder.excludeFieldsWithoutExposeAnnotation();
gson = builder.create();
}
private JsonMapper() {
}
/**
* Object可以是POJO也可以是Collection或数组。 如果对象为Null, 返回"null". 如果集合为空集合, 返回"[]".
*
* @param obj
* @return
*/
public static String toJson(Object obj) {
return gson.toJson(obj);
}
public static String toJson(Object obj, Type type) {
return gson.toJson(obj, type);
}
/**
* 反序列化POJO或简单Collection如List<String>.
* <p/>
* 如果JSON字符串为Null或"null"字符串, 返回Null. 如果JSON字符串为"[]", 返回空集合.
* <p/>
* 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String, JavaType)
*/
public static <T> T fromJson(String jsonValue, Class<T> clazz) {
return gson.fromJson(jsonValue, clazz);
}
public static <T> T fromJson(String jsonValue, Type type) {
return gson.fromJson(jsonValue, type);
}
public static <T> List<T> fromJson(String jsonValue) {
return gson.fromJson(jsonValue, new TypeToken<List<T>>() {
}.getType());
}
}