netty-调整包结构

This commit is contained in:
limqhz
2020-07-06 17:37:40 +08:00
parent 8f36ff33f0
commit 4e0eeb86ff
15 changed files with 38 additions and 268 deletions

View File

@@ -1,6 +1,6 @@
package com.sv.netty.config;
import com.sv.netty.netty.ChannelParam;
import com.sv.netty.netty.message.ChannelParam;
import io.netty.util.AttributeKey;
/**

View File

@@ -1,13 +1,10 @@
package com.sv.netty.controller;
import com.sv.entity.face.FaceRecognizeResponse;
import com.sv.netty.netty.MemberDto;
import com.sv.netty.netty.ResponseDTO;
import com.sv.netty.service.MessageService;
import com.sv.netty.dto.ResponseDTO;
import com.sv.netty.service.impl.TcpMessageHandlerAdapter;
import com.sv.service.oms.DeviceService;
import com.sv.service.oms.VenueService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -1,17 +1,11 @@
package com.sv.netty.controller;
import com.sv.entity.face.FaceRecognizeResponse;
import com.sv.netty.config.ErrorCode;
import com.sv.netty.netty.ResponseDTO;
import com.sv.netty.service.impl.TcpMessageHandlerAdapter;
import com.sv.service.oms.DeviceService;
import com.sv.service.oms.VenueService;
import com.sv.netty.dto.ResponseDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class QRCodeControler {

View File

@@ -1,148 +0,0 @@
package com.sv.netty.netty;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 会员基本信息
* MemberDto.java
*
* @author peakren
* @date 2018/12/20 8:39 PM
*/
public class MemberDto implements Serializable {
/**
* 头像
*/
private String avatar;
/**
* 姓名
*/
private String name;
/**
* 手机号码
*/
private String mobile;
/**
* 余额
*/
private BigDecimal amount;
/**
* 是否第一次进入
*/
private boolean first = false;
/**
* 场地价格
*/
private BigDecimal placePrice;
/**
* 场地名称
*/
private String placeName;
/**
* 会员卡名称
*/
private String cardName;
private String message;
/**
* 1成功进场 0不允许进场
*/
private int code;
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getPlaceName() {
return placeName;
}
public void setPlaceName(String placeName) {
this.placeName = placeName;
}
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public BigDecimal getPlacePrice() {
return placePrice;
}
public void setPlacePrice(BigDecimal placePrice) {
this.placePrice = placePrice;
}
public boolean isFirst() {
return first;
}
public void setFirst(boolean first) {
this.first = first;
}
}

View File

@@ -1,6 +1,6 @@
package com.sv.netty.netty;
import com.sv.netty.utils.CommonUtils;
import com.sv.netty.netty.message.BaseDto;
import com.sv.netty.utils.JsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
@@ -19,11 +19,11 @@ import java.nio.charset.Charset;
*/
public class MessageEncoder extends MessageToByteEncoder<BaseDto> {
private final static int MESSAGE_LENGTH = 4;
private final static int MESSAGE_SEQNO = 8;
private final static int MESSAGE_HEAD = 4;
private final static String mSeqno = "doll";
private final static int MAGIC_WORD = 0x9DDD;
/**
* TODO 客户端没用就删了
*/
// private final static String mSeqno = "doll";
private final static String DELIMITER_WORD = "$_$";
private static Logger logger = LoggerFactory.getLogger(MessageEncoder.class);
@@ -33,11 +33,8 @@ public class MessageEncoder extends MessageToByteEncoder<BaseDto> {
protected void encode(ChannelHandlerContext ctx, BaseDto msg, ByteBuf out) throws Exception {
String message = JsonUtils.encode(msg);
logger.info("send message content:" + message);
byte[] bodys = message.getBytes(charset.name());
int len = bodys.length;
out.writeInt(MAGIC_WORD); //发送预留数据字节码
out.writeInt(len); //发送head字节码
out.writeBytes(bodys); //发送消息内容
message = message + DELIMITER_WORD;
byte[] content = message.getBytes(charset.name());
out.writeBytes(content); //发送消息内容
}
}

View File

@@ -1,60 +0,0 @@
package com.sv.netty.netty;
import org.springframework.util.Assert;
import java.util.LinkedHashMap;
public class ResponseDTO extends LinkedHashMap<String, Object> {
public static final String ERR_CODE = "err_code";
public static final String ERR_MSG = "err_msg";
public static final String TIMESTAMP = "timestamp";
private static final long serialVersionUID = 8410965932046471023L;
public ResponseDTO() {
this(0, "OK");
}
public ResponseDTO(Integer errCode, String errMsg) {
this(errCode, errMsg, System.currentTimeMillis() / 1000L);
}
public ResponseDTO(Integer errCode, String errMsg, Long timestamp) {
this.addAttribute("err_code", errCode);
this.addAttribute("err_msg", errMsg);
this.addAttribute("timestamp", timestamp);
}
public ResponseDTO(Integer errCode, String errMsg, String attrName, Object attrValue) {
this(errCode, errMsg);
this.addAttribute(attrName, attrValue);
}
public static ResponseDTO ok() {
return new ResponseDTO();
}
public static ResponseDTO ok(String msg) {
ResponseDTO ret = ok();
ret.setErrorMsg(msg);
return ret;
}
public ResponseDTO addAttribute(String attrName, Object attrValue) {
Assert.notNull(attrName, "属性名称不能为空");
this.put(attrName, attrValue);
return this;
}
public void setErrorCode(Integer errCode) {
this.addAttribute("err_code", errCode);
}
public void setErrorMsg(String errMsg) {
this.addAttribute("err_msg", errMsg);
}
}

View File

@@ -2,6 +2,8 @@ package com.sv.netty.netty;
import com.sv.netty.config.Constant;
import com.sv.netty.config.SpringContextHolder;
import com.sv.netty.netty.message.BaseDto;
import com.sv.netty.netty.message.ChannelParam;
import com.sv.netty.service.MessageService;
import io.netty.channel.*;
import io.netty.handler.timeout.IdleState;
@@ -38,17 +40,6 @@ public class ServerHandler extends SimpleChannelInboundHandler<BaseDto> {
super.channelActive(ctx);
}
/**
* 读取客户端发送的消息
* @param ctx 上下文对象 管道pipeline通道channel , 地址
* @param msg 客户端发送的数据
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, BaseDto msg) throws Exception {
super.channelRead(ctx, msg);
@@ -100,12 +91,11 @@ public class ServerHandler extends SimpleChannelInboundHandler<BaseDto> {
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
super.exceptionCaught(ctx, cause);
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.error("ServerHandler exceptionCaught",cause);
Channel channel = ctx.channel();
if(channel.isActive()) {
// 错误产生,关闭连接
ctx.close();
}
}

View File

@@ -30,7 +30,7 @@ public class ServerProtocolInitializer extends ChannelInitializer<SocketChannel>
ChannelPipeline pipeline = ch.pipeline();
logger.info("ServerProtocolInitializer");
// 通过指定的长度来标识整包的信息,这样就可以自动的处理粘包和半包的问题
// pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0));
pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0));
pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT));
pipeline.addLast(new MessageDecoder());
pipeline.addLast(new MessageEncoder());

View File

@@ -1,4 +1,4 @@
package com.sv.netty.netty;
package com.sv.netty.netty.message;
import com.sv.entity.Device;

View File

@@ -1,7 +1,7 @@
package com.sv.netty.netty;
package com.sv.netty.netty.message;
/**
* 会话中存储的对象
* 会话中存储的客户端对象
*
* @author peakren
* @since 16/05/2017 11:09 PM

View File

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

View File

@@ -1,4 +1,6 @@
package com.sv.netty.netty;
package com.sv.netty.netty.message;
import com.sv.netty.netty.message.BaseDto;
public class MessageDto extends BaseDto {

View File

@@ -1,14 +1,14 @@
package com.sv.netty.netty;
package com.sv.netty.netty.message;
/**
* 请求门禁的灯光管理
*/
public class Result {
/**
* Code : 0
* Message : success
* Data : false
*/
private int Code;
private String Message;
private boolean Data;

View File

@@ -1,6 +1,6 @@
package com.sv.netty.service;
import com.sv.netty.netty.MemberDto;
import com.sv.netty.dto.MemberDto;
import io.netty.channel.Channel;
/**

View File

@@ -2,14 +2,16 @@ package com.sv.netty.service.impl;
import com.enums.FaceRecognizeEnum;
import com.sv.dto.EnterResult;
import com.sv.dto.api.MemberMessageDto;
import com.sv.entity.Device;
import com.sv.entity.Member;
import com.sv.entity.MemberEnterVenueLog;
import com.sv.entity.Venue;
import com.sv.entity.face.FaceRecognizeResponse;
import com.sv.netty.config.Constant;
import com.sv.netty.netty.*;
import com.sv.netty.dto.MemberDto;
import com.sv.netty.netty.message.BaseDto;
import com.sv.netty.netty.message.Cmd;
import com.sv.netty.netty.message.MessageDto;
import com.sv.netty.netty.message.Result;
import com.sv.netty.service.MessageService;
import com.sv.netty.utils.JsonMapper;
import com.sv.service.api.MemberEnterVenueLogService;
@@ -17,28 +19,20 @@ import com.sv.service.api.MemberService;
import com.sv.service.api.VenueService;
import com.sv.service.oms.ConfigService;
import com.sv.service.oms.DeviceService;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.oms.entity.sys.Admin;
import com.ydd.oms.entity.sys.Config;
import io.netty.channel.Channel;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -297,6 +291,10 @@ public class TcpMessageHandlerAdapter implements MessageService {
}
}
/**
* 控制硬件,门禁灯光控制
* @param number
*/
public void sendNumberChange(Integer number) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);