Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
huaran committed Jan 16, 2019
1 parent 824cb40 commit 82aade1
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import com.fhr.netty.heartbeat.DelimiterEncoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -41,12 +38,11 @@ protected void initChannel(Channel ch) throws Exception {
p.addLast(new LineBasedFrameDecoder(1024));
// 添加字符串解码器
p.addLast(new StringDecoder(StandardCharsets.UTF_8));
// 添加行分隔符编码器
p.addLast(new DelimiterEncoder());
// p.addLast(new StringEncoder(StandardCharsets.UTF_8));
// 添加请求处理器
p.addLast(new ClientHandler());

// 添加行分隔符编码器
p.addLast(new DelimiterEncoder());
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.fhr.netty.hearbeatv2;

import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
* @author Fan Huaran
* created on 2018/12/29
* @description
*/
public class DelimiterEncoder extends MessageToMessageEncoder<String> {

@Override
protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
// out.add(msg + "\r\n");
if (msg.length() == 0) {
return;
}

out.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg + "\r\n"), StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.fhr.netty.hearbeatv2;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import jdk.nashorn.internal.runtime.linker.Bootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,7 +13,7 @@
* created on 2018/12/29
* @description
*/
public abstract class HeartbeatHandler extends SimpleChannelInboundHandler<String> {
public class HeartbeatHandler extends SimpleChannelInboundHandler<String> {
private static final Logger logger = LoggerFactory.getLogger(HeartbeatHandler.class);

private static final String PING_MSG = "ping";
Expand Down Expand Up @@ -74,19 +72,20 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("--- {} is inactive---", ctx.channel().remoteAddress());
Channel channel = ctx.channel();
}

protected void handleReaderIdle(ChannelHandlerContext ctx) {
logger.info("---READER_IDLE---");
sendPingMsg(ctx);
}

protected void handleWriterIdle(ChannelHandlerContext ctx) {
logger.info("---WRITER_IDLE---");
sendPingMsg(ctx);
}

protected void handleAllIdle(ChannelHandlerContext ctx) {
logger.info("---ALL_IDLE---");
// sendPingMsg(ctx);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fhr.netty.hearbeatv2.client;

import com.fhr.netty.hearbeatv2.DelimiterEncoder;
import com.fhr.netty.hearbeatv2.HeartbeatHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;

/**
* @author Fan Huaran
* created on 2018/12/29
* @description
*/
public class ClientBoot {
public static void main(String[] args) throws ExecutionException, InterruptedException {
NioEventLoopGroup workerGroup = new NioEventLoopGroup(1);

Bootstrap bootstrap = new Bootstrap()
.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// 添加空闲状态处理器
p.addLast(new IdleStateHandler(0, 0, 10));
// 添加行分隔符解码器
p.addLast(new DelimiterBasedFrameDecoder(1024, true, Unpooled.copiedBuffer("&&".getBytes(StandardCharsets.UTF_8))));
// 添加字符串解码器
p.addLast(new StringDecoder(StandardCharsets.UTF_8));
// 添加行分隔符编码器
p.addLast(new DelimiterEncoder());
// p.addLast(new StringEncoder(StandardCharsets.UTF_8));
// 添加心跳处理器
p.addLast(new HeartbeatHandler());
// 添加请求处理器
}
});

ChannelFuture future = bootstrap.connect("127.0.0.1", 12345).sync();
Channel channel = future.channel();
channel.writeAndFlush("TEST");

// TODO
// future.get();
// workerGroup.shutdownGracefully();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.fhr.netty.hearbeatv2.server;

import com.fhr.netty.hearbeatv2.DelimiterEncoder;
import com.fhr.netty.hearbeatv2.HeartbeatHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.nio.charset.StandardCharsets;

/**
* @author Fan Huaran
* created on 2018/12/29
* @description
*/
public class ServerBoot {
public static void main(String[] args) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
NioEventLoopGroup workGroup = new NioEventLoopGroup(4);
try {


ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline p = socketChannel.pipeline();
// 添加空闲状态处理器
p.addLast(new IdleStateHandler(0, 0, 10));
// 添加行分隔符解码器
// p.addLast(new DelimiterBasedFrameDecoder(1024,true,Unpooled.copiedBuffer("&&".getBytes(StandardCharsets.UTF_8))));
p.addLast(new LineBasedFrameDecoder(1024));
// 添加字符串解码器
p.addLast(new StringDecoder(StandardCharsets.UTF_8));
// 添加行分隔符编码器
p.addLast(new DelimiterEncoder());
// p.addLast(new StringEncoder(StandardCharsets.UTF_8));
// 添加心跳处理器
p.addLast(new HeartbeatHandler());
}
});

Channel ch = bootstrap.bind(12345).sync().channel();
ch.closeFuture().sync();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}

}
}

0 comments on commit 82aade1

Please sign in to comment.