xiuwei 3 سال پیش
والد
کامیت
f2122dccae

+ 2 - 2
pom.xml

@@ -6,7 +6,7 @@
 
     <groupId>wei.yigulu</groupId>
     <artifactId>protocol</artifactId>
-    <version>2.1.11</version>
+    <version>2.2.11</version>
     <packaging>pom</packaging>
     <modules>
         <module>protocol-core</module>
@@ -32,7 +32,7 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
-        <protocol.version>2.1.11</protocol.version>
+        <protocol.version>2.2.11</protocol.version>
         <iec104.version>2.4.16</iec104.version>
         <modbus.version>2.2.4</modbus.version>
         <cdt.version>1.0.0</cdt.version>

+ 1 - 1
protocol-all/pom.xml

@@ -6,7 +6,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.1.11</version>
+        <version>2.2.11</version>
     </parent>
 
 

+ 1 - 1
protocol-cdt/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.1.11</version>
+        <version>2.2.11</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
protocol-core/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.1.11</version>
+        <version>2.2.11</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 122 - 0
protocol-core/src/main/java/wei/yigulu/netty/AbstractClientBuilder.java

@@ -0,0 +1,122 @@
+package wei.yigulu.netty;
+
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.EventLoopGroup;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+import wei.yigulu.threadpool.LocalThreadPool;
+
+
+/**
+ * 网络传输层的client
+ *
+ * @author 修唯xiuwei
+ * @version 3.0
+ */
+
+@Accessors(chain = true)
+public abstract class AbstractClientBuilder extends BaseProtocolBuilder {
+
+
+	/**
+	 * Work group
+	 */
+	protected EventLoopGroup workGroup = null;
+	/**
+	 * Bootstrap
+	 */
+	protected Bootstrap bootstrap = null;
+
+	/**
+	 * Future
+	 */
+	@Getter
+	@Setter
+	protected ChannelFuture future;
+	/**
+	 * Connection listener
+	 */
+	protected ChannelFutureListener connectionListener = null;
+
+
+	protected ProtocolChannelInitializer channelInitializer = null;
+
+
+	public void stop() {
+		if (this.future != null) {
+			this.future.removeListener(getOrCreateConnectionListener());
+			if (!this.future.channel().eventLoop().isShutdown()) {
+				this.future.channel().close();
+			}
+		}
+		if (this.workGroup != null) {
+			this.workGroup.shutdownGracefully();
+		}
+		this.bootstrap = null;
+		this.workGroup = null;
+	}
+
+
+	/**
+	 * 创建Master 连接
+	 */
+	public abstract void create();
+
+	/**
+	 * Create by un block
+	 */
+	public void createByUnBlock() {
+		LocalThreadPool.getInstance().getLocalPool().execute(this::create);
+	}
+
+
+	/**
+	 * null则创建,有则获取获取EventLoopGroup 用与bootstrap的绑定
+	 *
+	 * @return or create work group
+	 */
+	public abstract EventLoopGroup getOrCreateWorkGroup();
+
+
+	/**
+	 * null则创建,有则获取获取bootstrap
+	 *
+	 * @return or create bootstrap
+	 */
+	public abstract Bootstrap getOrCreateBootstrap();
+
+
+	/**
+	 * null则创建,有则获取获取ConnectionListener
+	 *
+	 * @return or create connection listener
+	 */
+	public abstract ChannelFutureListener getOrCreateConnectionListener();
+
+	/**
+	 * null则创建,有则获取获取ChannelInitializer
+	 *
+	 * @return or create ChannelInitializer
+	 */
+	protected abstract ProtocolChannelInitializer getOrCreateChannelInitializer();
+
+	/**
+	 * 通道连接成功
+	 */
+	public void connected() {
+
+	}
+
+	/**
+	 * 通道断开连接
+	 */
+	public void disconnected() {
+
+	}
+
+
+}

+ 7 - 104
protocol-core/src/main/java/wei/yigulu/netty/AbstractMasterBuilder.java

@@ -1,16 +1,9 @@
 package wei.yigulu.netty;
 
 
-import io.netty.bootstrap.Bootstrap;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.ChannelFutureListener;
-import io.netty.channel.EventLoopGroup;
-import lombok.Getter;
-import lombok.Setter;
 import lombok.experimental.Accessors;
-import wei.yigulu.threadpool.LocalThreadPool;
 import wei.yigulu.utils.DataConvertor;
 
 
@@ -22,46 +15,7 @@ import wei.yigulu.utils.DataConvertor;
  */
 
 @Accessors(chain = true)
-public abstract class AbstractMasterBuilder extends BaseProtocolBuilder {
-
-
-	/**
-	 * Work group
-	 */
-	protected EventLoopGroup workGroup = null;
-	/**
-	 * Bootstrap
-	 */
-	protected Bootstrap bootstrap = null;
-
-	/**
-	 * Future
-	 */
-	@Getter
-	@Setter
-	protected ChannelFuture future;
-	/**
-	 * Connection listener
-	 */
-	protected ChannelFutureListener connectionListener = null;
-
-
-	protected ProtocolChannelInitializer channelInitializer = null;
-
-
-	public void stop() {
-		if (this.future != null) {
-			this.future.removeListener(getOrCreateConnectionListener());
-			if (!this.future.channel().eventLoop().isShutdown()) {
-				this.future.channel().close();
-			}
-		}
-		if (this.workGroup != null) {
-			this.workGroup.shutdownGracefully();
-		}
-		this.bootstrap = null;
-		this.workGroup = null;
-	}
+public abstract class AbstractMasterBuilder extends AbstractClientBuilder implements MasterInterface {
 
 
 	/**
@@ -69,10 +23,13 @@ public abstract class AbstractMasterBuilder extends BaseProtocolBuilder {
 	 *
 	 * @param bytes
 	 */
+	@Override
 	public void sendFrameToOpposite(byte[] bytes) {
 		if (getFuture() != null && getFuture().channel().isActive()) {
 			getLog().info("se ==> " + DataConvertor.Byte2String(bytes));
 			getFuture().channel().writeAndFlush(Unpooled.copiedBuffer(bytes));
+		}else{
+			throw new RuntimeException("无客户端连接");
 		}
 	}
 
@@ -81,69 +38,15 @@ public abstract class AbstractMasterBuilder extends BaseProtocolBuilder {
 	 *
 	 * @param byteBuf
 	 */
+	@Override
 	public void sendFrameToOpposite(ByteBuf byteBuf) {
 		if (getFuture() != null && getFuture().channel().isActive()) {
 			getLog().info("se ==> " + DataConvertor.ByteBuf2String(byteBuf));
 			getFuture().channel().writeAndFlush(Unpooled.copiedBuffer(byteBuf));
+		}else{
+			throw new RuntimeException("无客户端连接");
 		}
 	}
 
-	/**
-	 * 创建Master 连接
-	 */
-	public abstract void create();
-
-	/**
-	 * Create by un block
-	 */
-	public void createByUnBlock() {
-		LocalThreadPool.getInstance().getLocalPool().execute(this::create);
-	}
-
-
-	/**
-	 * null则创建,有则获取获取EventLoopGroup 用与bootstrap的绑定
-	 *
-	 * @return or create work group
-	 */
-	public abstract EventLoopGroup getOrCreateWorkGroup();
-
-
-	/**
-	 * null则创建,有则获取获取bootstrap
-	 *
-	 * @return or create bootstrap
-	 */
-	public abstract Bootstrap getOrCreateBootstrap();
-
-
-	/**
-	 * null则创建,有则获取获取ConnectionListener
-	 *
-	 * @return or create connection listener
-	 */
-	public abstract ChannelFutureListener getOrCreateConnectionListener();
-
-	/**
-	 * null则创建,有则获取获取ChannelInitializer
-	 *
-	 * @return or create ChannelInitializer
-	 */
-	protected abstract ProtocolChannelInitializer getOrCreateChannelInitializer();
-
-	/**
-	 * 通道连接成功
-	 */
-	public void connected() {
-
-	}
-
-	/**
-	 * 通道断开连接
-	 */
-	public void disconnected() {
-
-	}
-
 
 }

+ 186 - 0
protocol-core/src/main/java/wei/yigulu/netty/AbstractTcpServerBuilder.java

@@ -0,0 +1,186 @@
+package wei.yigulu.netty;
+
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+import wei.yigulu.connectfilterofslave.ConnectFilterManager;
+import wei.yigulu.threadpool.LocalThreadPool;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * TCP网络传输层的server
+ * 向主站上送数据
+ *
+ * @author 修唯xiuwei
+ * @version 3.0
+ */
+@EqualsAndHashCode(callSuper = true)
+@Accessors(chain = true)
+public abstract class AbstractTcpServerBuilder extends BaseProtocolBuilder {
+
+
+	private EventLoopGroup group;
+
+	private ServerBootstrap serverBootstrap;
+
+	@Setter
+	@Getter
+	private int port = 2404;
+
+	@Setter
+	@Getter
+	private String ip = null;
+
+
+	/**
+	 * 连接过滤器管理器
+	 */
+	@Setter
+	@Getter
+	private ConnectFilterManager connectFilterManager = new ConnectFilterManager();
+
+	protected ProtocolChannelInitializer channelInitializer = null;
+
+	/**
+	 * 子channel集合
+	 */
+	@Getter
+	private List<Channel> channels = new ArrayList<>();
+	/**
+	 * 父channel
+	 */
+	@Getter
+	private Channel fatherChannel;
+
+
+	public AbstractTcpServerBuilder(int port) {
+		this.port = port;
+	}
+
+	/**
+	 * 创建104 slave 监听
+	 *
+	 * @throws Exception 异常
+	 */
+	public void create() throws Exception {
+		// 服务器异步创建绑定
+		ChannelFuture cf = getOrCrateServerBootstrap().bind().sync();
+		this.fatherChannel = cf.channel();
+		log.info("Slaver端启动成功;端口" + port);
+		// 关闭服务器通道
+		cf.channel().closeFuture().sync();
+		// 释放线程池资源
+		group.shutdownGracefully().sync();
+	}
+
+
+	/**
+	 * null则创建,有则获取获取ChannelInitializer
+	 *
+	 * @return or create ChannelInitializer
+	 */
+	protected abstract ProtocolChannelInitializer getOrCreateChannelInitializer();
+
+	/**
+	 * 获取ServerBootstrap
+	 * 如果==null 那么就创建
+	 *
+	 * @return
+	 */
+	protected ServerBootstrap getOrCrateServerBootstrap() {
+		if (this.serverBootstrap == null) {
+			AbstractTcpServerBuilder slaverBuilder = this;
+			this.serverBootstrap = new ServerBootstrap();
+			// 绑定线程池
+			this.serverBootstrap.group(getOrCrateLoopGroup())
+					// 指定使用的channel
+					.channel(NioServerSocketChannel.class)
+					// 绑定客户端连接时候触发操作
+					.childHandler(getOrCreateChannelInitializer());
+			// 绑定监听端口
+			if (this.ip != null) {
+				this.serverBootstrap.localAddress(this.ip, this.port);
+			} else {
+				this.serverBootstrap.localAddress(this.port);
+			}
+		}
+		return this.serverBootstrap;
+	}
+
+	/**
+	 * 获取或创建 循环任务组
+	 *
+	 * @return {@link EventLoopGroup}
+	 */
+	protected EventLoopGroup getOrCrateLoopGroup() {
+		if (this.group == null) {
+			this.group = new NioEventLoopGroup();
+		}
+		return this.group;
+	}
+
+
+	/**
+	 * 停止通道监听
+	 */
+	public void stop() {
+		if (this.fatherChannel != null) {
+			this.fatherChannel.close();
+			fatherChannel = null;
+		}
+		for (Channel c : this.channels) {
+			c.close();
+		}
+		this.channels = new ArrayList<>();
+		if (this.group != null) {
+			this.group.shutdownGracefully();
+		}
+
+	}
+
+	/**
+	 * 以非阻塞的方式启动
+	 */
+	public void createByUnBlock() {
+		AbstractTcpServerBuilder s = this;
+		LocalThreadPool.getInstance().getLocalPool().execute(() -> {
+			try {
+				s.create();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		});
+	}
+
+
+	/**
+	 * 当有连接接入时触发
+	 *
+	 * @param ipSocket ip套接字
+	 */
+	public void connected(InetSocketAddress ipSocket) {
+
+	}
+
+	/**
+	 * 当有连接断开时触发
+	 *
+	 * @param ipSocket ip套接字
+	 */
+	public void disconnected(InetSocketAddress ipSocket) {
+
+	}
+
+}

+ 2 - 165
protocol-core/src/main/java/wei/yigulu/netty/AbstractTcpSlaverBuilder.java

@@ -1,22 +1,8 @@
 package wei.yigulu.netty;
 
 
-import io.netty.bootstrap.ServerBootstrap;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
 import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.Setter;
 import lombok.experimental.Accessors;
-import wei.yigulu.connectfilterofslave.ConnectFilterManager;
-import wei.yigulu.threadpool.LocalThreadPool;
-
-import java.net.InetSocketAddress;
-import java.util.ArrayList;
-import java.util.List;
 
 
 /**
@@ -28,159 +14,10 @@ import java.util.List;
  */
 @EqualsAndHashCode(callSuper = true)
 @Accessors(chain = true)
-public abstract class AbstractTcpSlaverBuilder extends BaseProtocolBuilder {
-
-
-	private EventLoopGroup group;
-
-	private ServerBootstrap serverBootstrap;
-
-	@Setter
-	@Getter
-	private int port = 2404;
-
-	@Setter
-	@Getter
-	private String ip = null;
-
-
-	/**
-	 * 连接过滤器管理器
-	 */
-	@Setter
-	@Getter
-	private ConnectFilterManager connectFilterManager = new ConnectFilterManager();
-
-	protected ProtocolChannelInitializer channelInitializer = null;
-
-	/**
-	 * 子channel集合
-	 */
-	@Getter
-	private List<Channel> channels = new ArrayList<>();
-	/**
-	 * 父channel
-	 */
-	@Getter
-	private Channel fatherChannel;
+public abstract class AbstractTcpSlaverBuilder extends AbstractTcpServerBuilder implements SlaverInterface {
 
 
 	public AbstractTcpSlaverBuilder(int port) {
-		this.port = port;
-	}
-
-	/**
-	 * 创建104 slave 监听
-	 *
-	 * @throws Exception 异常
-	 */
-	public void create() throws Exception {
-		// 服务器异步创建绑定
-		ChannelFuture cf = getOrCrateServerBootstrap().bind().sync();
-		this.fatherChannel = cf.channel();
-		log.info("Slaver端启动成功;端口" + port);
-		// 关闭服务器通道
-		cf.channel().closeFuture().sync();
-		// 释放线程池资源
-		group.shutdownGracefully().sync();
-	}
-
-
-	/**
-	 * null则创建,有则获取获取ChannelInitializer
-	 *
-	 * @return or create ChannelInitializer
-	 */
-	protected abstract ProtocolChannelInitializer getOrCreateChannelInitializer();
-
-	/**
-	 * 获取ServerBootstrap
-	 * 如果==null 那么就创建
-	 *
-	 * @return
-	 */
-	protected ServerBootstrap getOrCrateServerBootstrap() {
-		if (this.serverBootstrap == null) {
-			AbstractTcpSlaverBuilder slaverBuilder = this;
-			this.serverBootstrap = new ServerBootstrap();
-			// 绑定线程池
-			this.serverBootstrap.group(getOrCrateLoopGroup())
-					// 指定使用的channel
-					.channel(NioServerSocketChannel.class)
-					// 绑定客户端连接时候触发操作
-					.childHandler(getOrCreateChannelInitializer());
-			// 绑定监听端口
-			if (this.ip != null) {
-				this.serverBootstrap.localAddress(this.ip, this.port);
-			} else {
-				this.serverBootstrap.localAddress(this.port);
-			}
-		}
-		return this.serverBootstrap;
+		super(port);
 	}
-
-	/**
-	 * 获取或创建 循环任务组
-	 *
-	 * @return {@link EventLoopGroup}
-	 */
-	protected EventLoopGroup getOrCrateLoopGroup() {
-		if (this.group == null) {
-			this.group = new NioEventLoopGroup();
-		}
-		return this.group;
-	}
-
-
-	/**
-	 * 停止通道监听
-	 */
-	public void stop() {
-		if (this.fatherChannel != null) {
-			this.fatherChannel.close();
-			fatherChannel = null;
-		}
-		for (Channel c : this.channels) {
-			c.close();
-		}
-		this.channels = new ArrayList<>();
-		if (this.group != null) {
-			this.group.shutdownGracefully();
-		}
-
-	}
-
-	/**
-	 * 以非阻塞的方式启动
-	 */
-	public void createByUnBlock() {
-		AbstractTcpSlaverBuilder s = this;
-		LocalThreadPool.getInstance().getLocalPool().execute(() -> {
-			try {
-				s.create();
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-		});
-	}
-
-
-	/**
-	 * 当有连接接入时触发
-	 *
-	 * @param ipSocket ip套接字
-	 */
-	public void connected(InetSocketAddress ipSocket) {
-
-	}
-
-	/**
-	 * 当有连接断开时触发
-	 *
-	 * @param ipSocket ip套接字
-	 */
-	public void disconnected(InetSocketAddress ipSocket) {
-
-	}
-
 }

+ 0 - 5
protocol-core/src/main/java/wei/yigulu/netty/HSConnectionListener.java

@@ -39,11 +39,6 @@ public class HSConnectionListener implements ChannelFutureListener {
 
 	@Override
 	public void operationComplete(ChannelFuture channelFuture) throws Exception {
-		try {
-			log.warn("通道任务和重连任务是否是一个:" + channelFuture.equals(masterBuilder.getFuture()));
-		}catch ( Exception e){
-
-		}
 		if (channelFuture == null || channelFuture.channel() == null || !channelFuture.channel().isActive()) {
 			this.masterBuilder.getOrCreateWorkGroup().schedule(() -> {
 				try {

+ 26 - 0
protocol-core/src/main/java/wei/yigulu/netty/MasterInterface.java

@@ -0,0 +1,26 @@
+package wei.yigulu.netty;
+
+import io.netty.buffer.ByteBuf;
+
+/**
+ * 协议层的master
+ *
+ * @author: xiuwei
+ * @version:
+ */
+public interface MasterInterface {
+	/**
+	 * 向对端 发送数据帧
+	 *
+	 * @param bytes
+	 */
+	public void sendFrameToOpposite(byte[] bytes);
+
+	/**
+	 * 向对端 发送数据帧
+	 *
+	 * @param byteBuf
+	 */
+	public void sendFrameToOpposite(ByteBuf byteBuf);
+
+}

+ 0 - 5
protocol-core/src/main/java/wei/yigulu/netty/SimpleTcpConnectionListener.java

@@ -37,11 +37,6 @@ public class SimpleTcpConnectionListener implements ChannelFutureListener {
 
 	@Override
 	public void operationComplete(ChannelFuture channelFuture) throws Exception {
-		try {
-			log.warn("通道任务和重连任务是否是一个:" + channelFuture.equals(masterBuilder.getFuture()));
-		}catch ( Exception e){
-
-		}
 		if (channelFuture == null || channelFuture.channel() == null || !channelFuture.channel().isActive()) {
 			this.future = this.masterBuilder.getOrCreateWorkGroup().schedule(() -> {
 				try {

+ 10 - 0
protocol-core/src/main/java/wei/yigulu/netty/SlaverInterface.java

@@ -0,0 +1,10 @@
+package wei.yigulu.netty;
+
+/**
+ * @program: protocol
+ * @description: 协议层的slaver 子站
+ * @author: xiuwei
+ * @create: 2021-06-11 10:39
+ */
+public interface SlaverInterface {
+}

+ 1 - 1
protocol-iec104/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.1.11</version>
+        <version>2.2.11</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
protocol-modbus/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>2.1.11</version>
+        <version>2.2.11</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <version>${modbus.version}</version>