From 3eae0c46b5c4262ee3c719cabce2f2904e063be2 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 31 Jul 2020 19:27:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cm/socket/client/MessageSocketClient.java | 6 +- .../handler/MessageClientPingHandler.java | 9 +- .../service/AbstractMessageClientService.java | 8 +- .../com/cm/socket/consts/ISocketConst.java | 9 ++ .../com/cm/socket/decoder/MessageDecoder.java | 60 +++++++-- .../com/cm/socket/encoder/MessageEncoder.java | 6 +- .../com/cm/socket/enums/SocketCodeEnum.java | 27 ++++ .../cm/socket/enums/SocketMessageEnum.java | 19 +-- .../socket/enums/SocketTypeMessageEnum.java | 34 +++++ .../main/java/com/cm/socket/pojo/Message.java | 15 ++- .../java/com/cm/socket/pojo/SocketClient.java | 36 +++++ .../java/com/cm/socket/pojo/SocketResult.java | 58 +++++++++ .../socket/service/AbstractSocketService.java | 123 ++++++++++++++++++ .../com/cm/socket/service/ISocketService.java | 33 +++++ 14 files changed, 398 insertions(+), 45 deletions(-) create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/enums/SocketCodeEnum.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/enums/SocketTypeMessageEnum.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketClient.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketResult.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/service/AbstractSocketService.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/service/ISocketService.java diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java index f67dd5d..2934125 100644 --- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java +++ b/cloud-common-socket-client/src/main/java/com/cm/socket/client/MessageSocketClient.java @@ -67,15 +67,15 @@ public class MessageSocketClient implements Runnable { public static void main(String[] args) { MessageSocketClientConfig messageSocketClientConfig = new MessageSocketClientConfig(); messageSocketClientConfig.setHost("127.0.0.1"); - messageSocketClientConfig.setPort(9999); + messageSocketClientConfig.setPort(8888); messageSocketClientConfig.setDelayPingSeconds(3); - messageSocketClientConfig.setMaxReconnectCount(10); + messageSocketClientConfig.setMaxReconnectCount(5); messageSocketClientConfig.setReconnectTimeStep(1); AbstractMessageClientService abstractMessageClientService = new AbstractMessageClientService() { @Override public void readMessage(String message) { - System.out.println(message); + System.out.println("读取数据:" + message); } }; diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java index fbb9f18..ff263b6 100644 --- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java +++ b/cloud-common-socket-client/src/main/java/com/cm/socket/client/handler/MessageClientPingHandler.java @@ -3,6 +3,7 @@ package com.cm.socket.client.handler; import com.cm.socket.client.MessageSocketClient; import com.cm.socket.client.config.MessageSocketClientConfig; import com.cm.socket.enums.SocketMessageEnum; +import com.cm.socket.enums.SocketTypeMessageEnum; import com.cm.socket.pojo.Message; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; @@ -42,10 +43,10 @@ public class MessageClientPingHandler extends SimpleChannelInboundHandler scheduledFuture = channel.eventLoop().schedule(() -> { if (channel.isActive()) { Message ping = new Message(); - ping.setStart(SocketMessageEnum.MESSAGE_TYPE_START.getType()); - ping.setType(SocketMessageEnum.MESSAGE_TYPE_PING.getType()); + ping.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType()); + ping.setType(SocketTypeMessageEnum.MESSAGE_TYPE_PING.getType()); ping.setContent(SocketMessageEnum.MESSAGE_PING.getMessage()); channel.writeAndFlush(ping); } else { diff --git a/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java b/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java index 013eb03..7688d05 100644 --- a/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java +++ b/cloud-common-socket-client/src/main/java/com/cm/socket/client/service/AbstractMessageClientService.java @@ -1,11 +1,9 @@ package com.cm.socket.client.service; -import com.cm.socket.enums.SocketMessageEnum; +import com.cm.socket.enums.SocketTypeMessageEnum; import com.cm.socket.pojo.Message; import io.netty.channel.ChannelHandlerContext; -import java.nio.channels.Channel; - /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -30,8 +28,8 @@ public abstract class AbstractMessageClientService { return; } Message message = new Message(); - message.setStart(SocketMessageEnum.MESSAGE_TYPE_START.getType()); - message.setType(SocketMessageEnum.MESSAGE_TYPE_MESSAGE.getType()); + message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType()); + message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_MESSAGE.getType()); message.setContent(sendMessage); channelHandlerContext.writeAndFlush(message); } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java b/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java index 90e0386..69843a1 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/consts/ISocketConst.java @@ -12,4 +12,13 @@ package com.cm.socket.consts; **/ public interface ISocketConst { + /** + * 客户端ID + */ + String CLIENT_ID = "clientId"; + /** + * 客户端密码 + */ + String CLIENT_SECRET = "clientSecret"; + } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java b/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java index 9278516..c08a4b5 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java @@ -21,13 +21,13 @@ import java.util.List; **/ public class MessageDecoder extends ByteToMessageDecoder { /** - * 头部长度 1byte + 1byte + 4byte + * 头部长度 1byte + 1byte + 4byte + 4byte */ - private static final int HEADER_LENGTH = 6; + private static final int HEADER_LENGTH = 10; @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - if(in == null) { + if (in == null) { return; } if (in.readableBytes() < HEADER_LENGTH) { @@ -36,21 +36,53 @@ public class MessageDecoder extends ByteToMessageDecoder { in.markReaderIndex(); byte start = in.readByte(); byte type = in.readByte(); - // 消息体长度 + int tokenBytesLength = in.readInt(); int contentBytesLength = in.readInt(); - if (contentBytesLength <= 0) { - return; - } - if (in.readableBytes() < contentBytesLength) { - in.resetReaderIndex(); - return; - } - byte[] contentBytes = new byte[contentBytesLength]; - in.readBytes(contentBytes); + String token = getToken(in, tokenBytesLength); + String content = getContent(in, contentBytesLength); Message message = new Message(); message.setStart(start); message.setType(type); - message.setContent(new String(contentBytes, CharsetUtil.UTF_8)); + message.setToken(token); + message.setContent(content); out.add(message); } + + /** + * 获取ID + * + * @param in + * @return + */ + private String getToken(ByteBuf in, int tokenBytesLength) { + if (tokenBytesLength <= 0) { + return null; + } + if (in.readableBytes() < tokenBytesLength) { + in.resetReaderIndex(); + return null; + } + byte[] tokenBytes = new byte[tokenBytesLength]; + in.readBytes(tokenBytes); + return new String(tokenBytes, CharsetUtil.UTF_8); + } + + /** + * 获取内容 + * + * @param in + * @return + */ + private String getContent(ByteBuf in, int contentBytesLength) { + if (contentBytesLength <= 0) { + return null; + } + if (in.readableBytes() < contentBytesLength) { + in.resetReaderIndex(); + return null; + } + byte[] contentBytes = new byte[contentBytesLength]; + in.readBytes(contentBytes); + return new String(contentBytes, CharsetUtil.UTF_8); + } } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java b/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java index 4534666..673b02d 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java @@ -5,6 +5,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.util.CharsetUtil; +import org.apache.commons.lang3.StringUtils; import java.nio.charset.Charset; @@ -22,10 +23,13 @@ public class MessageEncoder extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception { - byte[] contentBytes = msg.getContent().getBytes(CharsetUtil.UTF_8); + byte[] tokenBytes = StringUtils.isBlank(msg.getToken()) ? new byte[0] : msg.getToken().getBytes(CharsetUtil.UTF_8); + byte[] contentBytes = StringUtils.isBlank(msg.getContent()) ? new byte[0] : msg.getContent().getBytes(CharsetUtil.UTF_8); out.writeByte(msg.getStart()); out.writeByte(msg.getType()); + out.writeInt(tokenBytes.length); out.writeInt(contentBytes.length); + out.writeBytes(tokenBytes); out.writeBytes(contentBytes); } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketCodeEnum.java b/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketCodeEnum.java new file mode 100644 index 0000000..0edabcd --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketCodeEnum.java @@ -0,0 +1,27 @@ +package com.cm.socket.enums; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: SocketCodeEnum + * @Description: + * @Author: WangGeng + * @Date: 2020/7/28 4:19 下午 + * @Version: 1.0 + **/ +public enum SocketCodeEnum { + + SUCCESS(200), + ERROR(400); + + private int code; + + SocketCodeEnum(int code) { + this.code = code; + } + + public int getCode() { + return code; + } +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketMessageEnum.java b/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketMessageEnum.java index bc7cdee..c6073d7 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketMessageEnum.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketMessageEnum.java @@ -4,36 +4,23 @@ package com.cm.socket.enums; * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 * - * @ClassName: ISocketMessageEnum - * @Description: + * @ClassName: SocketMessageEnum + * @Description: socket信息 * @Author: WangGeng - * @Date: 2020/7/6 9:26 上午 + * @Date: 2020/7/28 7:38 下午 * @Version: 1.0 **/ public enum SocketMessageEnum { - MESSAGE_TYPE_START((byte) (0xAA << 4)), - MESSAGE_TYPE_PING((byte) 0x01), - MESSAGE_TYPE_PONG((byte) 0x02), - MESSAGE_TYPE_MESSAGE((byte) 0x03), MESSAGE_PING("PING"), MESSAGE_PONG("PONG"); - private byte type; private String message; - SocketMessageEnum(byte type) { - this.type = type; - } - SocketMessageEnum(String message) { this.message = message; } - public byte getType() { - return type; - } - public String getMessage() { return message == null ? "" : message.trim(); } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketTypeMessageEnum.java b/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketTypeMessageEnum.java new file mode 100644 index 0000000..45ba1db --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/enums/SocketTypeMessageEnum.java @@ -0,0 +1,34 @@ +package com.cm.socket.enums; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ISocketMessageEnum + * @Description: + * @Author: WangGeng + * @Date: 2020/7/6 9:26 上午 + * @Version: 1.0 + **/ +public enum SocketTypeMessageEnum { + + MESSAGE_TYPE_START((byte) (0xAA << 4)), + MESSAGE_TYPE_PING((byte) 0x01), + MESSAGE_TYPE_PONG((byte) 0x02), + MESSAGE_TYPE_MESSAGE((byte) 0x03), + MESSAGE_TYPE_EXCEPTION((byte) 0x04), + MESSAGE_TYPE_ERROR((byte) 0x05), + MESSAGE_TYPE_CLIENT_INFO((byte) 0x06), + MESSAGE_TYPE_CLIENT_LOGIN((byte) 0x07); + + private byte type; + + SocketTypeMessageEnum(byte type) { + this.type = type; + } + + public byte getType() { + return type; + } + +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java b/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java index 4e0918b..ca4b084 100644 --- a/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java +++ b/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java @@ -1,5 +1,7 @@ package com.cm.socket.pojo; +import java.io.Serializable; + /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -10,10 +12,12 @@ package com.cm.socket.pojo; * @Date: 2020/7/4 15:56 * @Version: 1.0 **/ -public class Message { +public class Message implements Serializable { + private static final long serialVersionUID = 2041189621546143865L; private byte start; private byte type; + private String token; private String content; public byte getStart() { @@ -32,6 +36,14 @@ public class Message { this.type = type; } + public String getToken() { + return token == null ? "" : token.trim(); + } + + public void setToken(String token) { + this.token = token; + } + public String getContent() { return content == null ? "" : content.trim(); } @@ -39,5 +51,4 @@ public class Message { public void setContent(String content) { this.content = content; } - } diff --git a/cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketClient.java b/cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketClient.java new file mode 100644 index 0000000..009b178 --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketClient.java @@ -0,0 +1,36 @@ +package com.cm.socket.pojo; + +import java.io.Serializable; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: SocketClient + * @Description: 客户端 + * @Author: WangGeng + * @Date: 2020/7/28 3:56 下午 + * @Version: 1.0 + **/ +public class SocketClient implements Serializable { + + private static final long serialVersionUID = -8167337825837171106L; + private String clientId; + private String clientSecret; + + public String getClientId() { + return clientId == null ? "" : clientId.trim(); + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getClientSecret() { + return clientSecret == null ? "" : clientSecret.trim(); + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketResult.java b/cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketResult.java new file mode 100644 index 0000000..370a107 --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/pojo/SocketResult.java @@ -0,0 +1,58 @@ +package com.cm.socket.pojo; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.apache.poi.ss.formula.functions.T; + +import java.util.Collection; +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: SocketResult + * @Description: socket结果 + * @Author: WangGeng + * @Date: 2020/7/28 4:26 下午 + * @Version: 1.0 + **/ +public class SocketResult { + + private int code; + private T data; + private List list; + private String errorMsg; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public String getErrorMsg() { + return errorMsg == null ? "" : errorMsg.trim(); + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/service/AbstractSocketService.java b/cloud-common-socket/src/main/java/com/cm/socket/service/AbstractSocketService.java new file mode 100644 index 0000000..0ed0f19 --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/service/AbstractSocketService.java @@ -0,0 +1,123 @@ +package com.cm.socket.service; + +import com.alibaba.fastjson.JSONObject; +import com.cm.common.base.AbstractService; +import com.cm.socket.enums.SocketCodeEnum; +import com.cm.socket.enums.SocketTypeMessageEnum; +import com.cm.socket.pojo.Message; +import com.cm.socket.pojo.SocketResult; +import io.netty.channel.ChannelHandlerContext; + +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: BaseService + * @Description: service + * @Author: WangGeng + * @Date: 2020/7/28 2:13 下午 + * @Version: 1.0 + **/ +public abstract class AbstractSocketService extends AbstractService implements ISocketService { + + /** + * 读取信息业务 + * + * @param channelHandlerContext + * @param readMessage + */ + protected abstract void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage); + + /** + * 输出信息 + * + * @param channelHandlerContext + * @param socketTypeMessageEnum + * @param socketResult + */ + protected void writeMessage(ChannelHandlerContext channelHandlerContext, SocketTypeMessageEnum socketTypeMessageEnum, SocketResult socketResult) { + writeMessage(channelHandlerContext, socketTypeMessageEnum, JSONObject.toJSONString(socketResult)); + } + + /** + * 输出信息 + * + * @param channelHandlerContext + * @param socketTypeMessageEnum + * @param content + */ + protected void writeMessage(ChannelHandlerContext channelHandlerContext, SocketTypeMessageEnum socketTypeMessageEnum, String content) { + Message message = new Message(); + message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType()); + message.setType(socketTypeMessageEnum.getType()); + message.setToken(channelHandlerContext.channel().id().asShortText()); + message.setContent(content); + write(channelHandlerContext, message); + } + + /** + * 输出错误信息 + * + * @param channelHandlerContext + * @param errorMessage + */ + protected void writeError(ChannelHandlerContext channelHandlerContext, String errorMessage) { + Message message = new Message(); + message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType()); + message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType()); + message.setContent(JSONObject.toJSONString(errorResult(errorMessage))); + write(channelHandlerContext, message); + } + + /** + * 输出数据 + * + * @param channelHandlerContext + * @param writeMessage + */ + protected void write(ChannelHandlerContext channelHandlerContext, Message writeMessage) { + channelHandlerContext.channel().writeAndFlush(writeMessage); + } + + /** + * 成功对象结果 + * + * @param data + * @return + */ + protected SocketResult successResult(T data) { + SocketResult socketResult = new SocketResult<>(); + socketResult.setCode(SocketCodeEnum.SUCCESS.getCode()); + socketResult.setData(data); + return socketResult; + } + + /** + * 成功列表结果 + * + * @param list + * @return + */ + protected SocketResult successResultList(List list) { + SocketResult socketResult = new SocketResult<>(); + socketResult.setCode(SocketCodeEnum.SUCCESS.getCode()); + socketResult.setList(list); + return socketResult; + } + + /** + * 错误结果 + * + * @param errorMsg + * @return + */ + protected SocketResult errorResult(String errorMsg) { + SocketResult socketResult = new SocketResult<>(); + socketResult.setCode(SocketCodeEnum.ERROR.getCode()); + socketResult.setErrorMsg(errorMsg); + return socketResult; + } + +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/service/ISocketService.java b/cloud-common-socket/src/main/java/com/cm/socket/service/ISocketService.java new file mode 100644 index 0000000..05ff4b3 --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/service/ISocketService.java @@ -0,0 +1,33 @@ +package com.cm.socket.service; + +import com.cm.socket.pojo.Message; +import io.netty.channel.ChannelHandlerContext; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: ISocketServer + * @Description: Socket业务接口 + * @Author: WangGeng + * @Date: 2020/7/28 2:41 下午 + * @Version: 1.0 + **/ +public interface ISocketService { + + /** + * 读取数据 + * + * @param channelHandlerContext + * @param readMessage + */ + void readMessage(ChannelHandlerContext channelHandlerContext, Message readMessage); + + /** + * 自动回复数据 + * + * @param channelHandlerContext + */ + void autoReply(ChannelHandlerContext channelHandlerContext); + +}