TCP 대응답 기능 추가
This commit is contained in:
@@ -1,73 +1,84 @@
|
||||
//package com.eactive.httpmockserver.socket;
|
||||
//
|
||||
//import java.nio.ByteOrder;
|
||||
//
|
||||
//import javax.annotation.PreDestroy;
|
||||
//
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import io.netty.bootstrap.ServerBootstrap;
|
||||
//import io.netty.channel.Channel;
|
||||
//import io.netty.channel.ChannelFuture;
|
||||
//import io.netty.channel.ChannelInitializer;
|
||||
//import io.netty.channel.ChannelOption;
|
||||
//import io.netty.channel.ChannelPipeline;
|
||||
//import io.netty.channel.EventLoopGroup;
|
||||
//import io.netty.channel.nio.NioEventLoopGroup;
|
||||
//import io.netty.channel.socket.SocketChannel;
|
||||
//import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
//import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
||||
//import io.netty.handler.codec.LengthFieldPrepender;
|
||||
//import io.netty.handler.codec.string.StringDecoder;
|
||||
//import io.netty.util.CharsetUtil;
|
||||
//
|
||||
//@Component
|
||||
//public class EchoSocketServer {
|
||||
// private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
//
|
||||
// private Channel channel;
|
||||
//
|
||||
// public void start() {
|
||||
// EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
// EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
// try {
|
||||
// ServerBootstrap server = new ServerBootstrap();
|
||||
// // @formatter:off
|
||||
// server.group(bossGroup, workerGroup)
|
||||
// .channel(NioServerSocketChannel.class)
|
||||
// .childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
// @Override
|
||||
// protected void initChannel(SocketChannel ch) throws Exception {
|
||||
// ChannelPipeline pipeline = ch.pipeline();
|
||||
// //pipeline.addLast(new LengthFieldBasedFrameDecoder (ByteOrder.LITTLE_ENDIAN,Integer.MAX_VALUE,0,8,0,8,true));
|
||||
// //pipeline.addLast(new LengthFieldPrepender(8));
|
||||
// //pipeline.addLast(new LengthFieldBasedFrameDecoder (Integer.MAX_VALUE,0,8,0,8));
|
||||
// //pipeline.addLast(new LengthFieldBasedFrameDecoder (Integer.MAX_VALUE,0,2,0,2));
|
||||
// //pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
|
||||
// pipeline.addLast(new EchoSocketServerHandler());
|
||||
// }
|
||||
// })
|
||||
// .option(ChannelOption.SO_BACKLOG, 128)
|
||||
// .childOption(ChannelOption.SO_KEEPALIVE, false);
|
||||
// // @formatter:on
|
||||
//
|
||||
// ChannelFuture channelFuture = server.bind(28181).sync();
|
||||
// channel = channelFuture.channel().closeFuture().sync().channel();
|
||||
// } catch (Exception e) {
|
||||
// logger.error(e.getMessage());
|
||||
// } finally {
|
||||
// bossGroup.shutdownGracefully();
|
||||
// workerGroup.shutdownGracefully();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @PreDestroy
|
||||
// public void stop() {
|
||||
// if (channel != null) {
|
||||
// channel.close();
|
||||
// channel.parent().closeFuture();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
package com.eactive.httpmockserver.socket;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.persistence.NoResultException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
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.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
|
||||
@Component
|
||||
public class EchoSocketServer {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private BytesMessageSpec spec;
|
||||
|
||||
EventLoopGroup bossGroup;
|
||||
EventLoopGroup workerGroup;
|
||||
ChannelFuture channelFuture;
|
||||
|
||||
public EchoSocketServer() {
|
||||
}
|
||||
|
||||
public EchoSocketServer(BytesMessageSpec spec) {
|
||||
this.spec = spec;
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
if (spec == null) {
|
||||
throw new NoResultException();
|
||||
}
|
||||
|
||||
int port = (spec.getPort() == null ? 38181 : spec.getPort().intValue());
|
||||
bossGroup = new NioEventLoopGroup();
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
try {
|
||||
ServerBootstrap server = new ServerBootstrap();
|
||||
// @formatter:off
|
||||
server.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
pipeline.addLast(new LengthHeaderFrameDecoder(spec));
|
||||
pipeline.addLast(new EchoSocketServerHandler(spec));
|
||||
}
|
||||
})
|
||||
.option(ChannelOption.SO_BACKLOG, 128)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, false);
|
||||
// @formatter:on
|
||||
|
||||
channelFuture = server.bind(port).sync();
|
||||
channelFuture.channel().closeFuture().sync().channel();
|
||||
logger.info(port + " port bounded");
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
bossGroup.shutdownGracefully();
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
try {
|
||||
bossGroup.shutdownGracefully().sync();
|
||||
workerGroup.shutdownGracefully().sync();
|
||||
channelFuture.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user