Compare commits

..

No commits in common. "8142655a3722a9920d07a5d3afb7327ce1af6885" and "cb8151889bf04db7aa14f67df0aa5226bcb8d94f" have entirely different histories.

46 changed files with 247 additions and 226 deletions

View File

@ -8,11 +8,12 @@
* TCP协议服务端开发 * TCP协议服务端开发
## 代码仓库 ## 代码仓库
* Gitee仓库地址[http://115.29.108.160:3000/TripartiteOpenSource/netmc](http://115.29.108.160:3000/TripartiteOpenSource/netmc) * Gitee仓库地址[https://gitee.com/yezhihao/netmc/tree/master](https://gitee.com/yezhihao/netmc/tree/master)
* Github仓库地址[http://115.29.108.160:3000/TripartiteOpenSource/netmc](http://115.29.108.160:3000/TripartiteOpenSource/netmc) * Github仓库地址[https://github.com/yezhihao/netmc/tree/master](https://github.com/yezhihao/netmc/tree/master)
## 下载方式 ## 下载方式
* Git下载命令`git clone http://115.29.108.160:3000/TripartiteOpenSource/netmc.git -b master` * Gitee下载命令`git clone https://gitee.com/yezhihao/netmc -b master`
* Github下载命令`git clone https://github.com/yezhihao/netmc -b master`
## 项目结构 ## 项目结构
```sh ```sh
@ -71,4 +72,10 @@ public class JT808Endpoint {
详细的例子请参考Test目录 详细的例子请参考Test目录
当前项目是Fork [剑器近](https://gitee.com/yezhihao) 的 [netmc](https://gitee.com/yezhihao/netmc?_from=gitee_search) 使用该组件的项目:[https://gitee.com/yezhihao/jt808-server/tree/master](https://gitee.com/yezhihao/jt808-server/tree/master)
项目会不定期进行更新建议star和watch一份您的支持是我最大的动力。
如有任何疑问或者BUG请联系我非常感谢。
技术交流QQ群[906230542]

122
pom.xml
View File

@ -1,10 +1,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>io.github.yezhihao</groupId>
<groupId>com.dust.tripartite</groupId>
<artifactId>netmc</artifactId> <artifactId>netmc</artifactId>
<version>2.0.6.RELEASE</version> <version>2.0.4.RELEASE</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Netmc</name> <name>Netmc</name>
@ -19,17 +18,25 @@
</licenses> </licenses>
<scm> <scm>
<url>http://115.29.108.160:3000/TripartiteOpenSource/netmc</url> <url>https://github.com/yezhihao/netmc</url>
<connection>http://115.29.108.160:3000/TripartiteOpenSource/netmc.git</connection> <connection>https://github.com/yezhihao/netmc.git</connection>
</scm> </scm>
<developers>
<developer>
<id>netmc.yezhihao</id>
<name>netmc</name>
<email>zhihao.ye@qq.com</email>
</developer>
</developers>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter> <resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.test.skip>true</maven.test.skip> <maven.test.skip>true</maven.test.skip>
</properties> </properties>
@ -55,26 +62,26 @@
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId> <artifactId>netty-handler</artifactId>
<version>4.1.70.Final</version> <version>4.1.66.Final</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.ben-manes.caffeine</groupId> <groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId> <artifactId>caffeine</artifactId>
<version>3.0.4</version> <version>2.8.8</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>5.3.12</version> <version>5.3.9</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles> <profiles>
<profile> <profile>
<id>nexus-releases</id> <id>nexus-release</id>
<activation> <activation>
<activeByDefault>true</activeByDefault> <activeByDefault>true</activeByDefault>
</activation> </activation>
@ -96,7 +103,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version> <version>3.2.0</version>
<executions> <executions>
<execution> <execution>
<phase>package</phase> <phase>package</phase>
@ -106,20 +113,20 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- <plugin>--> <plugin>
<!-- <groupId>org.apache.maven.plugins</groupId>--> <groupId>org.apache.maven.plugins</groupId>
<!-- <artifactId>maven-gpg-plugin</artifactId>--> <artifactId>maven-gpg-plugin</artifactId>
<!-- <version>3.0.1</version>--> <version>1.6</version>
<!-- <executions>--> <executions>
<!-- <execution>--> <execution>
<!-- <id>sign-artifacts</id>--> <id>sign-artifacts</id>
<!-- <phase>verify</phase>--> <phase>verify</phase>
<!-- <goals>--> <goals>
<!-- <goal>sign</goal>--> <goal>sign</goal>
<!-- </goals>--> </goals>
<!-- </execution>--> </execution>
<!-- </executions>--> </executions>
<!-- </plugin>--> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
@ -136,7 +143,6 @@
<version>2.5.3</version> <version>2.5.3</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version> <version>2.8.2</version>
<executions> <executions>
@ -149,45 +155,43 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- <plugin>--> <plugin>
<!-- <groupId>org.sonatype.plugins</groupId>--> <groupId>org.sonatype.plugins</groupId>
<!-- <artifactId>nexus-staging-maven-plugin</artifactId>--> <artifactId>nexus-staging-maven-plugin</artifactId>
<!-- <version>1.6.8</version>--> <version>1.6.8</version>
<!-- <extensions>true</extensions>--> <extensions>true</extensions>
<!-- <configuration>--> <configuration>
<!-- <serverId>central-nexus</serverId>--> <serverId>central-nexus</serverId>
<!-- <nexusUrl>https://oss.sonatype.org/</nexusUrl>--> <nexusUrl>https://oss.sonatype.org/</nexusUrl>
<!-- <autoReleaseAfterClose>true</autoReleaseAfterClose>--> <autoReleaseAfterClose>true</autoReleaseAfterClose>
<!-- </configuration>--> </configuration>
<!-- </plugin>--> </plugin>
<!-- <plugin>--> <plugin>
<!-- <groupId>org.apache.maven.plugins</groupId>--> <groupId>org.apache.maven.plugins</groupId>
<!-- <artifactId>maven-scm-plugin</artifactId>--> <artifactId>maven-scm-plugin</artifactId>
<!-- <version>1.11.3</version>--> <version>1.11.2</version>
<!-- </plugin>--> </plugin>
</plugins> </plugins>
</build> </build>
<distributionManagement>
<repository>
<id>nexus-release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>nexus-snapshot</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</profile> </profile>
</profiles> </profiles>
<distributionManagement>
<repository>
<id>store</id>
<url>http://117.33.142.185:8081/repository/store/</url>
</repository>
<snapshotRepository>
<id>store</id>
<url>http://117.33.142.185:8081/repository/store/</url>
</snapshotRepository>
</distributionManagement>
<repositories> <repositories>
<repository> <repository>
<id>maven-public</id> <id>central</id>
<url>http://117.33.142.185:8081/repository/maven-public/</url> <name>Maven Central</name>
<snapshots> <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<enabled>true</enabled> <layout>default</layout>
</snapshots>
<releases> <releases>
<enabled>true</enabled> <enabled>true</enabled>
</releases> </releases>

View File

@ -1,13 +1,13 @@
package com.dust.tripartite.netmc; package io.github.yezhihao.netmc;
import com.dust.tripartite.netmc.codec.LengthField; import io.github.yezhihao.netmc.codec.Delimiter;
import com.dust.tripartite.netmc.codec.MessageEncoder; import io.github.yezhihao.netmc.codec.LengthField;
import com.dust.tripartite.netmc.core.HandlerInterceptor; import io.github.yezhihao.netmc.codec.MessageDecoder;
import com.dust.tripartite.netmc.core.HandlerMapping; import io.github.yezhihao.netmc.codec.MessageEncoder;
import com.dust.tripartite.netmc.session.SessionListener; import io.github.yezhihao.netmc.core.HandlerInterceptor;
import com.dust.tripartite.netmc.codec.Delimiter; import io.github.yezhihao.netmc.core.HandlerMapping;
import com.dust.tripartite.netmc.codec.MessageDecoder; import io.github.yezhihao.netmc.session.SessionListener;
import com.dust.tripartite.netmc.session.SessionManager; import io.github.yezhihao.netmc.session.SessionManager;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
/** /**

View File

@ -1,9 +1,9 @@
package com.dust.tripartite.netmc; package io.github.yezhihao.netmc;
import com.dust.tripartite.netmc.codec.DelimiterBasedFrameDecoder; import io.github.yezhihao.netmc.codec.DelimiterBasedFrameDecoder;
import com.dust.tripartite.netmc.codec.LengthFieldAndDelimiterFrameDecoder; import io.github.yezhihao.netmc.codec.LengthFieldAndDelimiterFrameDecoder;
import com.dust.tripartite.netmc.codec.MessageDecoderWrapper; import io.github.yezhihao.netmc.codec.MessageDecoderWrapper;
import com.dust.tripartite.netmc.codec.MessageEncoderWrapper; import io.github.yezhihao.netmc.codec.MessageEncoderWrapper;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
@ -20,7 +20,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* @author yezhihao * @author yezhihao
@ -29,13 +28,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class TCPServer { public class TCPServer {
private static final Logger log = LoggerFactory.getLogger(TCPServer.class); private static final Logger log = LoggerFactory.getLogger(TCPServer.class);
private final AtomicBoolean isRunning = new AtomicBoolean(false); private volatile boolean isRunning = false;
private EventLoopGroup bossGroup = null; private EventLoopGroup bossGroup = null;
private EventLoopGroup workerGroup = null; private EventLoopGroup workerGroup = null;
private final String name; private String name;
private final NettyConfig config; private NettyConfig config;
public TCPServer(String name, NettyConfig config) { public TCPServer(String name, NettyConfig config) {
this.name = name; this.name = name;
@ -54,8 +53,8 @@ public class TCPServer {
.childOption(NioChannelOption.TCP_NODELAY, true) .childOption(NioChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<NioSocketChannel>() { .childHandler(new ChannelInitializer<NioSocketChannel>() {
private final MessageEncoderWrapper messageEncoderWrapper = new MessageEncoderWrapper(config.encoder); private MessageEncoderWrapper messageEncoderWrapper = new MessageEncoderWrapper(config.encoder);
private final MessageDecoderWrapper messageDecoderWrapper = new MessageDecoderWrapper(config.decoder); private MessageDecoderWrapper messageDecoderWrapper = new MessageDecoderWrapper(config.decoder);
@Override @Override
public void initChannel(NioSocketChannel channel) { public void initChannel(NioSocketChannel channel) {
@ -72,7 +71,7 @@ public class TCPServer {
log.warn("==={}启动成功, port={}===", name, config.port); log.warn("==={}启动成功, port={}===", name, config.port);
channelFuture.channel().closeFuture().sync(); channelFuture.channel().closeFuture().sync();
} catch (Exception e) { } catch (Exception e) {
log.warn("==={}出现异常, port={}===", e, config.port); log.warn("==={}出现异常, port={}===", e);
} finally { } finally {
stop(); stop();
} }
@ -84,27 +83,29 @@ public class TCPServer {
return new LengthFieldAndDelimiterFrameDecoder(config.maxFrameLength, config.lengthField, config.delimiter); return new LengthFieldAndDelimiterFrameDecoder(config.maxFrameLength, config.lengthField, config.delimiter);
} }
public void start() { public synchronized void start() {
if (!isRunning.compareAndSet(false, true)) { if (this.isRunning) {
log.warn("==={}已经启动, port={}===", name, config.port); log.warn("==={}已经启动, port={}===", name, config.port);
return; return;
} }
new Thread(this::startInternal).start(); this.isRunning = true;
new Thread(() -> startInternal()).start();
} }
public void stop() { public synchronized void stop() {
if (!isRunning.compareAndSet(true, false)) { if (!this.isRunning) {
log.warn("==={}已经停止, port={}===", name, config.port); log.warn("==={}已经停止, port={}===", name, config.port);
return;
} }
Future<?> future = this.bossGroup.shutdownGracefully(); this.isRunning = false;
if (!future.isSuccess()) {
Future future = this.bossGroup.shutdownGracefully();
if (!future.isSuccess())
log.warn("bossGroup 无法正常停止", future.cause()); log.warn("bossGroup 无法正常停止", future.cause());
}
future = this.workerGroup.shutdownGracefully(); future = this.workerGroup.shutdownGracefully();
if (!future.isSuccess()) { if (!future.isSuccess())
log.warn("workerGroup 无法正常停止", future.cause()); log.warn("workerGroup 无法正常停止", future.cause());
}
log.warn("==={}已经停止, port={}===", name, config.port); log.warn("==={}已经停止, port={}===", name, config.port);
} }
} }

View File

@ -1,12 +1,12 @@
package com.dust.tripartite.netmc; package io.github.yezhihao.netmc;
import com.dust.tripartite.netmc.core.HandlerInterceptor; import io.github.yezhihao.netmc.core.HandlerInterceptor;
import com.dust.tripartite.netmc.core.HandlerMapping; import io.github.yezhihao.netmc.core.HandlerMapping;
import com.dust.tripartite.netmc.core.handler.Handler; import io.github.yezhihao.netmc.core.handler.Handler;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import com.dust.tripartite.netmc.session.SessionListener; import io.github.yezhihao.netmc.session.SessionListener;
import com.dust.tripartite.netmc.session.SessionManager; import io.github.yezhihao.netmc.session.SessionManager;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -27,13 +27,13 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(TCPServerHandler.class.getSimpleName()); private static final Logger log = LoggerFactory.getLogger(TCPServerHandler.class.getSimpleName());
private final HandlerMapping handlerMapping; private HandlerMapping handlerMapping;
private final HandlerInterceptor interceptor; private HandlerInterceptor interceptor;
private final SessionManager sessionManager; private SessionManager sessionManager;
private final SessionListener sessionListener; private SessionListener sessionListener;
public TCPServerHandler(HandlerMapping handlerMapping, public TCPServerHandler(HandlerMapping handlerMapping,
HandlerInterceptor interceptor, HandlerInterceptor interceptor,

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
public class Delimiter { public class Delimiter {
protected byte[] value; protected byte[] value;

View File

@ -1,11 +1,11 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.internal.ObjectUtil; import io.netty.util.internal.ObjectUtil;
import com.dust.tripartite.netmc.util.ByteBufUtils; import io.github.yezhihao.netmc.util.ByteBufUtils;
import java.util.List; import java.util.List;
@ -40,7 +40,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
} }
} }
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) { protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
// Try all delimiters and choose the delimiter which yields the shortest frame. // Try all delimiters and choose the delimiter which yields the shortest frame.
int minFrameLength = Integer.MAX_VALUE; int minFrameLength = Integer.MAX_VALUE;
Delimiter minDelim = null; Delimiter minDelim = null;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import static io.netty.util.internal.ObjectUtil.checkPositive; import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;

View File

@ -1,12 +1,12 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.util.ByteBufUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.internal.ObjectUtil; import io.netty.util.internal.ObjectUtil;
import io.github.yezhihao.netmc.util.ByteBufUtils;
import java.util.List; import java.util.List;
@ -100,7 +100,7 @@ public class LengthFieldAndDelimiterFrameDecoder extends DelimiterBasedFrameDeco
throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less than initialBytesToStrip: " + initialBytesToStrip); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less than initialBytesToStrip: " + initialBytesToStrip);
} }
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) { protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
if (in.readableBytes() < this.lengthFieldEndOffset) { if (in.readableBytes() < this.lengthFieldEndOffset) {
return null; return null;
} else { } else {

View File

@ -1,6 +1,6 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
/** /**

View File

@ -1,6 +1,6 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
@ -16,7 +16,7 @@ import io.netty.handler.codec.DecoderException;
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter { public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter {
private final MessageDecoder decoder; private MessageDecoder decoder;
public MessageDecoderWrapper(MessageDecoder decoder) { public MessageDecoderWrapper(MessageDecoder decoder) {
this.decoder = decoder; this.decoder = decoder;

View File

@ -1,6 +1,6 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
/** /**

View File

@ -1,6 +1,6 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
@ -17,7 +17,7 @@ import io.netty.handler.codec.EncoderException;
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class MessageEncoderWrapper extends ChannelOutboundHandlerAdapter { public class MessageEncoderWrapper extends ChannelOutboundHandlerAdapter {
private final MessageEncoder encoder; private MessageEncoder encoder;
public MessageEncoderWrapper(MessageEncoder encoder) { public MessageEncoderWrapper(MessageEncoder encoder) {
this.encoder = encoder; this.encoder = encoder;

View File

@ -1,10 +1,10 @@
package com.dust.tripartite.netmc.core; package io.github.yezhihao.netmc.core;
import com.dust.tripartite.netmc.core.annotation.AsyncBatch; import io.github.yezhihao.netmc.core.annotation.AsyncBatch;
import com.dust.tripartite.netmc.core.annotation.Mapping; import io.github.yezhihao.netmc.core.annotation.Mapping;
import com.dust.tripartite.netmc.core.handler.AsyncBatchHandler; import io.github.yezhihao.netmc.core.handler.AsyncBatchHandler;
import com.dust.tripartite.netmc.core.handler.Handler; import io.github.yezhihao.netmc.core.handler.Handler;
import com.dust.tripartite.netmc.core.handler.SimpleHandler; import io.github.yezhihao.netmc.core.handler.SimpleHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
@ -17,7 +17,7 @@ import java.util.Map;
*/ */
public abstract class AbstractHandlerMapping implements HandlerMapping { public abstract class AbstractHandlerMapping implements HandlerMapping {
private final Map<Object, Handler> handlerMap = new HashMap<>(64); private final Map<Object, Handler> handlerMap = new HashMap(60);
/** /**
* Endpoint@Mapping * Endpoint@Mapping
@ -25,6 +25,8 @@ public abstract class AbstractHandlerMapping implements HandlerMapping {
protected synchronized void registerHandlers(Object bean) { protected synchronized void registerHandlers(Object bean) {
Class<?> beanClass = bean.getClass(); Class<?> beanClass = bean.getClass();
Method[] methods = beanClass.getDeclaredMethods(); Method[] methods = beanClass.getDeclaredMethods();
if (methods == null)
return;
for (Method method : methods) { for (Method method : methods) {

View File

@ -1,7 +1,7 @@
package com.dust.tripartite.netmc.core; package io.github.yezhihao.netmc.core;
import com.dust.tripartite.netmc.util.ClassUtils; import io.github.yezhihao.netmc.core.annotation.Endpoint;
import com.dust.tripartite.netmc.core.annotation.Endpoint; import io.github.yezhihao.netmc.util.ClassUtils;
import java.util.List; import java.util.List;

View File

@ -1,7 +1,7 @@
package com.dust.tripartite.netmc.core; package io.github.yezhihao.netmc.core;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
/** /**
* *
@ -9,18 +9,18 @@ import com.dust.tripartite.netmc.session.Session;
* home https://gitee.com/yezhihao/jt808-server * home https://gitee.com/yezhihao/jt808-server
*/ */
public interface HandlerInterceptor<T extends Message> { public interface HandlerInterceptor<T extends Message> {
/** @return Response 未找到对应的Handle */ /** 未找到对应的Handle */
T notSupported(T request, Session session); T notSupported(T request, Session session);
/** @return boolean 调用之前 */ /** 调用之前 */
boolean beforeHandle(T request, Session session); boolean beforeHandle(T request, Session session);
/** @return Response 调用之后返回值为void的 */ /** 调用之后返回值为void的 */
T successful(T request, Session session); T successful(T request, Session session);
/** 调用之后,有返回值的 */ /** 调用之后,有返回值的 */
void afterHandle(T request, T response, Session session); void afterHandle(T request, T response, Session session);
/** @return Response 调用之后抛出异常的 */ /** 调用之后抛出异常的 */
T exceptional(T request, Session session, Exception e); T exceptional(T request, Session session, Exception e);
} }

View File

@ -1,6 +1,6 @@
package com.dust.tripartite.netmc.core; package io.github.yezhihao.netmc.core;
import com.dust.tripartite.netmc.core.handler.Handler; import io.github.yezhihao.netmc.core.handler.Handler;
/** /**
* *

View File

@ -1,6 +1,6 @@
package com.dust.tripartite.netmc.core; package io.github.yezhihao.netmc.core;
import com.dust.tripartite.netmc.core.annotation.Endpoint; import io.github.yezhihao.netmc.core.annotation.Endpoint;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.core.annotation; package io.github.yezhihao.netmc.core.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.core.annotation; package io.github.yezhihao.netmc.core.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.core.annotation; package io.github.yezhihao.netmc.core.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;

View File

@ -1,9 +1,9 @@
package com.dust.tripartite.netmc.core.handler; package io.github.yezhihao.netmc.core.handler;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import com.dust.tripartite.netmc.util.BasicThreadFactory; import io.github.yezhihao.netmc.util.BasicThreadFactory;
import com.dust.tripartite.netmc.util.VirtualList; import io.github.yezhihao.netmc.util.VirtualList;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -23,17 +23,17 @@ public class AsyncBatchHandler extends Handler {
private static final Logger log = LoggerFactory.getLogger(AsyncBatchHandler.class.getSimpleName()); private static final Logger log = LoggerFactory.getLogger(AsyncBatchHandler.class.getSimpleName());
private final ConcurrentLinkedQueue<Message> queue; private ConcurrentLinkedQueue<Message> queue;
private final ThreadPoolExecutor executor; private ThreadPoolExecutor executor;
private final int poolSize; private int poolSize;
private final int maxElements; private int maxElements;
private final int maxWait; private int maxWait;
private final int warningLines; private int warningLines;
public AsyncBatchHandler(Object actionClass, Method actionMethod, String desc, int poolSize, int maxElements, int maxWait) { public AsyncBatchHandler(Object actionClass, Method actionMethod, String desc, int poolSize, int maxElements, int maxWait) {
super(actionClass, actionMethod, desc); super(actionClass, actionMethod, desc);
@ -50,7 +50,8 @@ public class AsyncBatchHandler extends Handler {
this.warningLines = maxElements * poolSize * 50; this.warningLines = maxElements * poolSize * 50;
this.queue = new ConcurrentLinkedQueue(); this.queue = new ConcurrentLinkedQueue();
this.executor = new ThreadPoolExecutor(this.poolSize, this.poolSize, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(400), new BasicThreadFactory.Builder().daemon(true).namingPattern(actionMethod.getName() + "-pool-%d").build()); this.executor = new ThreadPoolExecutor(this.poolSize, this.poolSize, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(400),
new BasicThreadFactory.Builder().daemon(true).namingPattern(actionMethod.getName() + "-pool-%d").build());
for (int i = 0; i < poolSize; i++) { for (int i = 0; i < poolSize; i++) {
boolean master = i == 0; boolean master = i == 0;
@ -64,7 +65,7 @@ public class AsyncBatchHandler extends Handler {
} }
} }
public <T extends Message> T invoke(T request, Session session) { public Message invoke(Message request, Session session) {
queue.offer(request); queue.offer(request);
return null; return null;
} }

View File

@ -1,7 +1,7 @@
package com.dust.tripartite.netmc.core.handler; package io.github.yezhihao.netmc.core.handler;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -34,7 +34,7 @@ public abstract class Handler {
try { try {
for (int i = 0; i < types.length; i++) { for (int i = 0; i < types.length; i++) {
Type type = types[i]; Type type = types[i];
Class<?> clazz; Class clazz;
if (type instanceof ParameterizedTypeImpl) if (type instanceof ParameterizedTypeImpl)
clazz = (Class<?>) ((ParameterizedTypeImpl) type).getActualTypeArguments()[0]; clazz = (Class<?>) ((ParameterizedTypeImpl) type).getActualTypeArguments()[0];
else else

View File

@ -1,7 +1,7 @@
package com.dust.tripartite.netmc.core.handler; package io.github.yezhihao.netmc.core.handler;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -16,7 +16,7 @@ public class SimpleHandler extends Handler {
super(actionClass, actionMethod, desc); super(actionClass, actionMethod, desc);
} }
public <T extends Message> T invoke(T request, Session session) throws Exception { public Message invoke(Message request, Session session) throws Exception {
return super.invoke(request, session); return super.invoke(request, session);
} }
} }

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.core.model; package io.github.yezhihao.netmc.core.model;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.core.model; package io.github.yezhihao.netmc.core.model;
/** /**
* @author yezhihao * @author yezhihao

View File

@ -1,7 +1,7 @@
package com.dust.tripartite.netmc.session; package io.github.yezhihao.netmc.session;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.core.model.Response; import io.github.yezhihao.netmc.core.model.Response;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.session; package io.github.yezhihao.netmc.session;
/** /**
* @author yezhihao * @author yezhihao

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.session; package io.github.yezhihao.netmc.session;
import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import java.util.AbstractCollection; import java.util.AbstractCollection;
import java.util.Collection; import java.util.Collection;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.List; import java.util.List;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Map; import java.util.Map;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import java.util.AbstractSet; import java.util.AbstractSet;
import java.util.Iterator; import java.util.Iterator;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -8,6 +8,7 @@ import java.lang.annotation.Annotation;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
@ -23,12 +24,17 @@ public class ClassUtils {
public static List<Class<?>> getClassList(String packageName, Class<? extends Annotation> annotationClass) { public static List<Class<?>> getClassList(String packageName, Class<? extends Annotation> annotationClass) {
List<Class<?>> classList = getClassList(packageName); List<Class<?>> classList = getClassList(packageName);
classList.removeIf(next -> !next.isAnnotationPresent(annotationClass)); Iterator<Class<?>> iterator = classList.iterator();
while (iterator.hasNext()) {
Class<?> next = iterator.next();
if (!next.isAnnotationPresent(annotationClass))
iterator.remove();
}
return classList; return classList;
} }
public static List<Class<?>> getClassList(String packageName) { public static List<Class<?>> getClassList(String packageName) {
List<Class<?>> classList = new LinkedList<>(); List<Class<?>> classList = new LinkedList();
String path = packageName.replace(".", "/"); String path = packageName.replace(".", "/");
try { try {
Enumeration<URL> urls = ClassUtils.getClassLoader().getResources(path); Enumeration<URL> urls = ClassUtils.getClassLoader().getResources(path);

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.util; package io.github.yezhihao.netmc.util;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;

View File

@ -1,15 +0,0 @@
package com.dust.tripartite.netmc.endpoint;
import com.dust.tripartite.netmc.core.annotation.Endpoint;
import com.dust.tripartite.netmc.core.annotation.Mapping;
import com.dust.tripartite.netmc.model.MyMessage;
import com.dust.tripartite.netmc.session.Session;
@Endpoint
public class MyEndpoint {
@Mapping(types = 1, desc = "注册")
public void register(MyMessage request, Session session) {
System.out.println(request);
}
}

View File

@ -1,10 +1,10 @@
package com.dust.tripartite.netmc; package io.github.yezhihao.netmc;
import com.dust.tripartite.netmc.codec.MyMessageDecoder; import io.github.yezhihao.netmc.core.DefaultHandlerMapping;
import com.dust.tripartite.netmc.codec.MyMessageEncoder; import io.github.yezhihao.netmc.endpoint.MyHandlerInterceptor;
import com.dust.tripartite.netmc.core.DefaultHandlerMapping; import io.github.yezhihao.netmc.codec.MyMessageDecoder;
import com.dust.tripartite.netmc.endpoint.MyHandlerInterceptor; import io.github.yezhihao.netmc.codec.MyMessageEncoder;
import com.dust.tripartite.netmc.session.SessionManager; import io.github.yezhihao.netmc.session.SessionManager;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc; package io.github.yezhihao.netmc;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;

View File

@ -1,8 +1,8 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.model.MyHeader; import io.github.yezhihao.netmc.model.MyHeader;
import com.dust.tripartite.netmc.model.MyMessage; import io.github.yezhihao.netmc.model.MyMessage;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;

View File

@ -1,8 +1,8 @@
package com.dust.tripartite.netmc.codec; package io.github.yezhihao.netmc.codec;
import com.dust.tripartite.netmc.model.MyHeader; import io.github.yezhihao.netmc.model.MyHeader;
import com.dust.tripartite.netmc.model.MyMessage; import io.github.yezhihao.netmc.model.MyMessage;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -0,0 +1,15 @@
package io.github.yezhihao.netmc.endpoint;
import io.github.yezhihao.netmc.core.annotation.Endpoint;
import io.github.yezhihao.netmc.core.annotation.Mapping;
import io.github.yezhihao.netmc.model.MyMessage;
import io.github.yezhihao.netmc.session.Session;
@Endpoint
public class MyEndpoint {
@Mapping(types = 1, desc = "注册")
public void register(MyMessage request, Session session) {
System.out.println(request);
}
}

View File

@ -1,9 +1,9 @@
package com.dust.tripartite.netmc.endpoint; package io.github.yezhihao.netmc.endpoint;
import com.dust.tripartite.netmc.core.HandlerInterceptor; import io.github.yezhihao.netmc.core.HandlerInterceptor;
import com.dust.tripartite.netmc.model.MyHeader; import io.github.yezhihao.netmc.model.MyHeader;
import com.dust.tripartite.netmc.model.MyMessage; import io.github.yezhihao.netmc.model.MyMessage;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -1,4 +1,4 @@
package com.dust.tripartite.netmc.model; package io.github.yezhihao.netmc.model;
public class MyHeader { public class MyHeader {

View File

@ -1,7 +1,7 @@
package com.dust.tripartite.netmc.model; package io.github.yezhihao.netmc.model;
import com.dust.tripartite.netmc.core.model.Message; import io.github.yezhihao.netmc.core.model.Message;
import com.dust.tripartite.netmc.session.Session; import io.github.yezhihao.netmc.session.Session;
public class MyMessage implements Message { public class MyMessage implements Message {