From f15dcb5fc10a4888a0571bac0863b9a8fd8aef46 Mon Sep 17 00:00:00 2001
From: Boom <654612@qq.com>
Date: Thu, 20 Jul 2023 20:53:21 +0800
Subject: [PATCH] :+1: init
---
.gitignore | 40 ++++++++++
pom.xml | 72 +++++++++++++++++
src/main/java/com/zsw/App.java | 24 ++++++
src/main/java/com/zsw/starter/Client.java | 64 +++++++++++++++
src/main/java/com/zsw/starter/Server.java | 54 +++++++++++++
.../zsw/starter/handler/ClientHandler.java | 33 ++++++++
.../zsw/starter/handler/ServerHandler.java | 62 +++++++++++++++
src/main/resources/logback.xml | 79 +++++++++++++++++++
src/test/java/com/zsw/AppTest.java | 38 +++++++++
9 files changed, 466 insertions(+)
create mode 100644 .gitignore
create mode 100644 pom.xml
create mode 100644 src/main/java/com/zsw/App.java
create mode 100644 src/main/java/com/zsw/starter/Client.java
create mode 100644 src/main/java/com/zsw/starter/Server.java
create mode 100644 src/main/java/com/zsw/starter/handler/ClientHandler.java
create mode 100644 src/main/java/com/zsw/starter/handler/ServerHandler.java
create mode 100644 src/main/resources/logback.xml
create mode 100644 src/test/java/com/zsw/AppTest.java
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cfa977e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,40 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
+logs
+.idea
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..99b05ba
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,72 @@
+
+ 4.0.0
+
+ com.zsw
+ netty01
+ 1.0-SNAPSHOT
+ jar
+
+ netty01
+ http://maven.apache.org
+
+
+ UTF-8
+ 1.8
+ 1.8
+ UTF-8
+
+
+
+
+ io.netty
+ netty-all
+ 4.1.63.Final
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+ 2.7.10
+ compile
+
+
+
+
+ com.google.guava
+ guava
+ 31.1-jre
+
+
+
+
+ org.projectlombok
+ lombok
+ true
+ 1.18.28
+
+
+
+
+ cn.hutool
+ hutool-all
+ 5.8.16
+
+
+
+
+ com.google.code.gson
+ gson
+ 2.9.0
+
+
+
+
+ junit
+ junit
+ 3.8.1
+ test
+
+
+
+
diff --git a/src/main/java/com/zsw/App.java b/src/main/java/com/zsw/App.java
new file mode 100644
index 0000000..182dcdd
--- /dev/null
+++ b/src/main/java/com/zsw/App.java
@@ -0,0 +1,24 @@
+package com.zsw;
+
+import com.zsw.starter.Client;
+import com.zsw.starter.Server;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Hello world!
+ *
+ */
+@Slf4j
+public class App
+{
+ public static void main( String[] args )
+ {
+ log.info("hello,world. {}",1);
+ System.out.println( "Hello World!" );
+ Server server = new Server();
+ server.run();
+
+ Client client = new Client();
+ client.run();
+ }
+}
diff --git a/src/main/java/com/zsw/starter/Client.java b/src/main/java/com/zsw/starter/Client.java
new file mode 100644
index 0000000..2797913
--- /dev/null
+++ b/src/main/java/com/zsw/starter/Client.java
@@ -0,0 +1,64 @@
+package com.zsw.starter;
+
+import cn.hutool.core.thread.ThreadUtil;
+import com.zsw.starter.handler.ClientHandler;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.json.JsonObjectDecoder;
+import io.netty.handler.codec.string.StringEncoder;
+import io.netty.handler.timeout.IdleStateHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+public class Client {
+
+ private void start(int i) throws InterruptedException {
+ NioEventLoopGroup group = new NioEventLoopGroup();
+ Bootstrap bootstrap = new Bootstrap();
+ bootstrap.group(group)
+ .channel(NioSocketChannel.class)
+ .option(ChannelOption.TCP_NODELAY, true)
+ .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
+ .handler(new ChannelInitializer() {
+ protected void initChannel(SocketChannel ch) {
+ // 注册名称
+ ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(60, 60, 60, TimeUnit.SECONDS));
+ ch.pipeline().addLast(new JsonObjectDecoder());
+ ch.pipeline().addLast(new StringEncoder());
+ ch.pipeline().addLast(new ClientHandler(i));
+ }
+ });
+ ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8899);
+ channelFuture.sync();
+ channelFuture.channel().closeFuture().sync();
+ }
+
+ public void run(){
+ for (int i = 0; i < 10; i++) {
+ // try {
+ // Thread.sleep(200);
+ // } catch (InterruptedException e) {
+ // throw new RuntimeException(e);
+ // }
+ int finalI = i;
+ ThreadUtil.execAsync(()->{
+ log.info("client:{} ready", finalI);
+ try {
+ this.start(finalI);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ }
+
+
+ }
+}
diff --git a/src/main/java/com/zsw/starter/Server.java b/src/main/java/com/zsw/starter/Server.java
new file mode 100644
index 0000000..24e8856
--- /dev/null
+++ b/src/main/java/com/zsw/starter/Server.java
@@ -0,0 +1,54 @@
+package com.zsw.starter;
+
+import com.zsw.starter.handler.ServerHandler;
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioChannelOption;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.codec.json.JsonObjectDecoder;
+import io.netty.handler.codec.string.StringEncoder;
+import io.netty.handler.timeout.IdleStateHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+public class Server {
+
+ private void start() throws InterruptedException {
+ NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
+ //new 一个工作线程组
+ NioEventLoopGroup workGroup = new NioEventLoopGroup(200);
+ ServerBootstrap bootstrap = new ServerBootstrap()
+ .group(bossGroup, workGroup)
+ .channel(NioServerSocketChannel.class)
+ .childHandler(new ChannelInitializer() {
+ @Override
+ protected void initChannel(SocketChannel socketChannel) throws Exception {
+ ChannelPipeline pipline = socketChannel.pipeline();
+ pipline.addLast("heart",new IdleStateHandler(5,5,10, TimeUnit.SECONDS));
+ // pipline.addFirst(new JsonObjectDecoder());
+ pipline.addLast(new StringEncoder());
+ pipline.addLast(new ServerHandler());
+ }
+ })
+ .localAddress(8899)
+ .option(ChannelOption.SO_BACKLOG, 1024)
+ .childOption(NioChannelOption.SO_KEEPALIVE, true);
+ ChannelFuture future = bootstrap.bind().sync();
+ log.info("打印服务启动,端口:{} ",8899);
+ }
+
+ public void run(){
+ try {
+ this.start();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/main/java/com/zsw/starter/handler/ClientHandler.java b/src/main/java/com/zsw/starter/handler/ClientHandler.java
new file mode 100644
index 0000000..ac6ec21
--- /dev/null
+++ b/src/main/java/com/zsw/starter/handler/ClientHandler.java
@@ -0,0 +1,33 @@
+package com.zsw.starter.handler;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.nio.charset.StandardCharsets;
+
+
+@Slf4j
+public class ClientHandler extends SimpleChannelInboundHandler {
+ int i ;
+ public ClientHandler(int i) {
+ this.i = i;
+ }
+
+ @Override
+ protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
+ String channelId = channelHandlerContext.channel().id().toString();
+ byte[] bytes = new byte[byteBuf.readableBytes()];
+ byteBuf.getBytes(byteBuf.readerIndex(), bytes);
+ String info = new String(bytes, StandardCharsets.UTF_8);
+ log.info("info:{}",info);
+ }
+
+ @Override
+ public void channelActive(ChannelHandlerContext ctx) throws Exception {
+ super.channelActive(ctx);
+ log.info("客户端连接:{}",ctx.channel().id());
+ ctx.writeAndFlush("ctx-"+i);
+ }
+}
diff --git a/src/main/java/com/zsw/starter/handler/ServerHandler.java b/src/main/java/com/zsw/starter/handler/ServerHandler.java
new file mode 100644
index 0000000..e049ff4
--- /dev/null
+++ b/src/main/java/com/zsw/starter/handler/ServerHandler.java
@@ -0,0 +1,62 @@
+package com.zsw.starter.handler;
+
+import cn.hutool.core.util.StrUtil;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelId;
+import io.netty.channel.SimpleChannelInboundHandler;
+import lombok.Synchronized;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+
+
+@Slf4j
+public class ServerHandler extends SimpleChannelInboundHandler {
+ protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
+
+ String channelId = channelHandlerContext.channel().id().toString();
+ byte[] bytes = new byte[byteBuf.readableBytes()];
+ byteBuf.getBytes(byteBuf.readerIndex(), bytes);
+ String info = new String(bytes, StandardCharsets.UTF_8);
+ log.info("channelHandlerContext:{}",channelHandlerContext.channel().id());
+ test(channelHandlerContext.channel().id());
+
+ // Thread.sleep(5000L);
+ Socket socket = new Socket("192.168.10.188", 9100);
+ OutputStream socketOut = socket.getOutputStream();
+ OutputStreamWriter writer = new OutputStreamWriter(socketOut, "GBK");
+ socketOut.write(27);
+ socketOut.write(27);
+ String str = StrUtil.format("ctx:{},text:{}\n",channelHandlerContext.channel().id(),info);
+ writer.write(str);
+ writer.flush();
+
+ writer.write(27);
+ writer.write(100);
+ writer.write(4);
+ writer.write(10);
+ writer.flush();
+
+ writer.write(0x1D);
+ writer.write("V");
+ writer.write(48);
+ writer.write(0);
+ writer.flush();
+
+ log.info("停止5秒后完成");
+ socket.close();
+ writer.close();
+ socketOut.close();
+ }
+
+
+ private synchronized void test(ChannelId id){
+ log.info("测试同步开始...{}",id);
+ }
+
+
+}
diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml
new file mode 100644
index 0000000..799bfe5
--- /dev/null
+++ b/src/main/resources/logback.xml
@@ -0,0 +1,79 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ${CONSOLE_LOG_PATTERN}
+
+
+
+
+
+ ${log.path}/info.log
+
+ ${log.path}/%d{yyyy-MM, aux}/info.%d{yyyy-MM-dd}.%i.log.gz
+ 50MB
+ 30
+
+
+ ${CONSOLE_LOG_PATTERN_NO_COLOR}
+ UTF-8
+
+
+
+
+
+ ${log.path}/error.log
+
+ ${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz
+ 50MB
+ 30
+
+
+ ${CONSOLE_LOG_PATTERN_NO_COLOR}
+ UTF-8
+
+
+ ERROR
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/java/com/zsw/AppTest.java b/src/test/java/com/zsw/AppTest.java
new file mode 100644
index 0000000..bd74e1e
--- /dev/null
+++ b/src/test/java/com/zsw/AppTest.java
@@ -0,0 +1,38 @@
+package com.zsw;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite( AppTest.class );
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue( true );
+ }
+}