From e7ac429f2b90e5eb75d70592183ff9d30e98ad0c Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 24 Apr 2018 01:16:25 +0900 Subject: [PATCH] ing --- .gitignore | 2 +- pom.xml | 6 ++ .../com/loafle/commons/server/Server.java | 100 ++++++++++++++++++ .../handler/codec/BinarySocketFrame.java | 2 +- .../handler/codec/CloseSocketFrame.java | 2 +- .../codec/ContinuationSocketFrame.java | 2 +- .../handler/codec/PingSocketFrame.java | 2 +- .../handler/codec/PongSocketFrame.java | 2 +- .../handler/codec/Socket13FrameDecoder.java | 2 +- .../handler/codec/Socket13FrameEncoder.java | 2 +- .../handler/codec/SocketFrame.java | 2 +- .../handler/codec/SocketFrameDecoder.java | 2 +- .../handler/codec/SocketFrameEncoder.java | 2 +- .../handler/codec/SocketProtocolHandler.java | 2 +- .../codec/SocketServerProtocolHandler.java | 2 +- .../handler/codec/TextSocketFrame.java | 2 +- .../handler/codec/Utf8FrameValidator.java | 2 +- .../handler/codec/Utf8Validator.java | 2 +- 18 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/loafle/commons/server/Server.java rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/BinarySocketFrame.java (96%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/CloseSocketFrame.java (98%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/ContinuationSocketFrame.java (97%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/PingSocketFrame.java (96%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/PongSocketFrame.java (96%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/Socket13FrameDecoder.java (99%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/Socket13FrameEncoder.java (98%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/SocketFrame.java (96%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/SocketFrameDecoder.java (84%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/SocketFrameEncoder.java (84%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/SocketProtocolHandler.java (92%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/SocketServerProtocolHandler.java (98%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/TextSocketFrame.java (97%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/Utf8FrameValidator.java (97%) rename src/main/java/com/loafle/commons/server/socket/{netty => }/handler/codec/Utf8Validator.java (97%) diff --git a/.gitignore b/.gitignore index 60af490..ebef548 100644 --- a/.gitignore +++ b/.gitignore @@ -82,7 +82,7 @@ hs_err_pid* .idea/ *.iml -/target/ +target/ .settings/ .classpath .project diff --git a/pom.xml b/pom.xml index 7665523..7592316 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ 4.1.17.Final + 5.0.5.RELEASE @@ -26,6 +27,11 @@ netty-all ${netty.version} + + org.springframework + spring-context + ${spring.version} + diff --git a/src/main/java/com/loafle/commons/server/Server.java b/src/main/java/com/loafle/commons/server/Server.java new file mode 100644 index 0000000..7431400 --- /dev/null +++ b/src/main/java/com/loafle/commons/server/Server.java @@ -0,0 +1,100 @@ +package com.loafle.commons.server; + +import java.net.SocketAddress; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.ServerChannel; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.handler.ssl.SslContext; + +/** + * Server + */ +public abstract class Server { + @Value("${server.netty.thread.count.boss}") + protected int threadCountBoss; + @Value("${server.netty.thread.count.worker}") + protected int threadCountWorker; + + @Autowired + protected SslContext sslContext; + @Autowired + protected List> channelClasses; + + @Autowired + protected List> channelOptions; + @Autowired + protected List channelHandlers; + @Autowired + protected ChannelInitializer channelInitializer; + @Autowired + protected SocketAddress address; + + public void start() throws Exception { + EventLoopGroup bossGroup = new NioEventLoopGroup(this.threadCountBoss); + EventLoopGroup workerGroup = new NioEventLoopGroup(this.threadCountWorker); + + try { + ServerBootstrap serverBootstrap = new ServerBootstrap(); + serverBootstrap.group(bossGroup, workerGroup); + + if (null != this.channelClasses) { + for (Class channelClass : this.channelClasses) { + serverBootstrap.channel(channelClass); + } + } + if (null != this.channelOptions) { + for (ChannelOptionItem optionItem : this.channelOptions) { + optionItem.setOption(serverBootstrap); + } + } + if (null != this.channelHandlers) { + for (ChannelHandler channelHandler : this.channelHandlers) { + serverBootstrap.handler(channelHandler); + } + } + serverBootstrap.childHandler(this.channelInitializer); + + this.init(); + + ChannelFuture f = serverBootstrap.bind(address).sync(); + this.onStart(); + + f.channel().closeFuture().sync(); + } catch (Exception e) { + + } finally { + this.onStop(); + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); + this.destroy(); + } + } + + protected abstract void init() throws Exception; + protected abstract void onStart() throws Exception; + protected abstract void onStop() throws Exception; + protected abstract void destroy() throws Exception; + + public static class ChannelOptionItem { + private final ChannelOption option; + private final T value; + + public ChannelOptionItem(ChannelOption option, T value) { + this.option = option; + this.value = value; + } + void setOption(ServerBootstrap serverBootstrap) { + serverBootstrap.option(option, value); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/BinarySocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/BinarySocketFrame.java similarity index 96% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/BinarySocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/BinarySocketFrame.java index e451e32..ce29759 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/BinarySocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/BinarySocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/CloseSocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/CloseSocketFrame.java similarity index 98% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/CloseSocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/CloseSocketFrame.java index b9af840..93d99e6 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/CloseSocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/CloseSocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/ContinuationSocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/ContinuationSocketFrame.java similarity index 97% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/ContinuationSocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/ContinuationSocketFrame.java index 9fabde8..ed3901a 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/ContinuationSocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/ContinuationSocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/PingSocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/PingSocketFrame.java similarity index 96% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/PingSocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/PingSocketFrame.java index 7cfe33e..e2356af 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/PingSocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/PingSocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/PongSocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/PongSocketFrame.java similarity index 96% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/PongSocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/PongSocketFrame.java index 39d5871..469d1c8 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/PongSocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/PongSocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Socket13FrameDecoder.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/Socket13FrameDecoder.java similarity index 99% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Socket13FrameDecoder.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/Socket13FrameDecoder.java index ad8f5e6..c9b6798 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Socket13FrameDecoder.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/Socket13FrameDecoder.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import static io.netty.buffer.ByteBufUtil.readBytes; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Socket13FrameEncoder.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/Socket13FrameEncoder.java similarity index 98% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Socket13FrameEncoder.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/Socket13FrameEncoder.java index 7f00f23..6c9e316 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Socket13FrameEncoder.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/Socket13FrameEncoder.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrame.java similarity index 96% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrame.java index 10149c2..207fc3b 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.DefaultByteBufHolder; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrameDecoder.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrameDecoder.java similarity index 84% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrameDecoder.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrameDecoder.java index 57a97be..658cfbc 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrameDecoder.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrameDecoder.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelPipeline; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrameEncoder.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrameEncoder.java similarity index 84% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrameEncoder.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrameEncoder.java index 880d988..031d0a7 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketFrameEncoder.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketFrameEncoder.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.channel.ChannelOutboundHandler; import io.netty.channel.ChannelPipeline; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketProtocolHandler.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketProtocolHandler.java similarity index 92% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketProtocolHandler.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/SocketProtocolHandler.java index 77a9827..8043dda 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketProtocolHandler.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketProtocolHandler.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketServerProtocolHandler.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketServerProtocolHandler.java similarity index 98% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketServerProtocolHandler.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/SocketServerProtocolHandler.java index 8dbb28f..1a49fc7 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/SocketServerProtocolHandler.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/SocketServerProtocolHandler.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/TextSocketFrame.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/TextSocketFrame.java similarity index 97% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/TextSocketFrame.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/TextSocketFrame.java index 1ea97af..a87737e 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/TextSocketFrame.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/TextSocketFrame.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Utf8FrameValidator.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/Utf8FrameValidator.java similarity index 97% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Utf8FrameValidator.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/Utf8FrameValidator.java index 247fe6d..9f59f41 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Utf8FrameValidator.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/Utf8FrameValidator.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Utf8Validator.java b/src/main/java/com/loafle/commons/server/socket/handler/codec/Utf8Validator.java similarity index 97% rename from src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Utf8Validator.java rename to src/main/java/com/loafle/commons/server/socket/handler/codec/Utf8Validator.java index 2e8c812..2cdfcaa 100644 --- a/src/main/java/com/loafle/commons/server/socket/netty/handler/codec/Utf8Validator.java +++ b/src/main/java/com/loafle/commons/server/socket/handler/codec/Utf8Validator.java @@ -1,4 +1,4 @@ -package com.loafle.commons.server.socket.netty.handler.codec; +package com.loafle.commons.server.socket.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.CorruptedFrameException;