add LengthFieldBasedFrameDecoder
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package io.github.ehlxr.did.client;
|
||||
|
||||
import io.github.ehlxr.did.common.*;
|
||||
import io.github.ehlxr.did.netty.MyProtocolBean;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
@@ -89,7 +90,10 @@ public abstract class AbstractClient implements Client {
|
||||
REPONSE_MAP.put(rqid, responseFuture);
|
||||
|
||||
logger.debug("write {} to channel", sdkProto);
|
||||
channel.writeAndFlush(sdkProto).addListener((ChannelFutureListener) channelFuture -> {
|
||||
|
||||
byte[] bytes = NettyUtil.toBytes(sdkProto);
|
||||
MyProtocolBean myProtocolBean = new MyProtocolBean((byte)0xA, (byte)0xC, bytes.length, bytes);
|
||||
channel.writeAndFlush(myProtocolBean).addListener((ChannelFutureListener) channelFuture -> {
|
||||
if (channelFuture.isSuccess()) {
|
||||
//发送成功后立即跳出
|
||||
return;
|
||||
@@ -132,7 +136,10 @@ public abstract class AbstractClient implements Client {
|
||||
|
||||
Try.of(() -> {
|
||||
logger.debug("write {} to channel", sdkProto);
|
||||
channelFuture.channel().writeAndFlush(sdkProto).addListener(channelFuture -> {
|
||||
|
||||
byte[] bytes = NettyUtil.toBytes(sdkProto);
|
||||
MyProtocolBean myProtocolBean = new MyProtocolBean((byte)0xA, (byte)0xC, bytes.length, bytes);
|
||||
channelFuture.channel().writeAndFlush(myProtocolBean).addListener(channelFuture -> {
|
||||
if (channelFuture.isSuccess()) {
|
||||
return;
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package io.github.ehlxr.did.client;
|
||||
|
||||
import io.github.ehlxr.did.client.handler.SdkClientDecoder;
|
||||
import io.github.ehlxr.did.client.handler.SdkClientEncoder;
|
||||
import io.github.ehlxr.did.netty.MyProtocolDecoder;
|
||||
import io.github.ehlxr.did.netty.MyProtocolEncoder;
|
||||
import io.github.ehlxr.did.client.handler.SdkClientHandler;
|
||||
import io.github.ehlxr.did.common.Constants;
|
||||
import io.github.ehlxr.did.common.Try;
|
||||
@@ -52,8 +52,11 @@ public class SdkClient extends AbstractClient {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
socketChannel.pipeline()
|
||||
.addLast(new SdkClientDecoder(Constants.DECODER_FRAMELENGTH)) // 如果长度不够会等待
|
||||
.addLast(new SdkClientEncoder())
|
||||
// .addLast(new SdkClientDecoder(Constants.DECODER_FRAMELENGTH)) // 如果长度不够会等待
|
||||
// .addLast(new SdkClientEncoder())
|
||||
.addLast(new MyProtocolEncoder())
|
||||
.addLast(new MyProtocolDecoder(Constants.MAX_FRAME_LENGTH, Constants.LENGTH_FIELD_OFFSET,
|
||||
Constants.LENGTH_FIELD_LENGTH, Constants.LENGTH_ADJUSTMENT, Constants.INITIAL_BYTES_TO_STRIP, false))
|
||||
.addLast(new SdkClientHandler());
|
||||
}
|
||||
});
|
||||
|
@@ -47,12 +47,12 @@ public class SdkClientDecoder extends FixedLengthFrameDecoder {
|
||||
@Override
|
||||
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
|
||||
return Try.of(() -> {
|
||||
ByteBuf decode = (ByteBuf) super.decode(ctx, in);
|
||||
ByteBuf buf = (ByteBuf) super.decode(ctx, in);
|
||||
|
||||
byte[] bytes = new byte[decode.readableBytes()];
|
||||
decode.readBytes(bytes);
|
||||
byte[] bytes = new byte[buf.readableBytes()];
|
||||
buf.readBytes(bytes);
|
||||
|
||||
decode.release();
|
||||
buf.release();
|
||||
return NettyUtil.toObject(bytes);
|
||||
}).trap(e -> logger.error("decode error", e)).get();
|
||||
}
|
||||
|
@@ -28,6 +28,8 @@ import io.github.ehlxr.did.client.Client;
|
||||
import io.github.ehlxr.did.client.ResponseFuture;
|
||||
import io.github.ehlxr.did.common.NettyUtil;
|
||||
import io.github.ehlxr.did.common.SdkProto;
|
||||
import io.github.ehlxr.did.common.Try;
|
||||
import io.github.ehlxr.did.netty.MyProtocolBean;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import org.slf4j.Logger;
|
||||
@@ -37,12 +39,17 @@ import org.slf4j.LoggerFactory;
|
||||
* @author ehlxr
|
||||
* @since 2021-01-20 14:43.
|
||||
*/
|
||||
public class SdkClientHandler extends SimpleChannelInboundHandler<SdkProto> {
|
||||
public class SdkClientHandler extends SimpleChannelInboundHandler<MyProtocolBean> {
|
||||
private final Logger logger = LoggerFactory.getLogger(SdkClientHandler.class);
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, SdkProto sdkProto) {
|
||||
logger.debug("sdk client handler receive sdkProto {}", sdkProto);
|
||||
protected void channelRead0(ChannelHandlerContext ctx, MyProtocolBean protocolBean) {
|
||||
logger.debug("sdk client handler receive protocolBean {}", protocolBean);
|
||||
|
||||
SdkProto sdkProto = Try.<MyProtocolBean, SdkProto>of(p ->
|
||||
(SdkProto) NettyUtil.toObject(p.getContent()))
|
||||
.apply(protocolBean)
|
||||
.get(SdkProto.newBuilder().build());
|
||||
|
||||
final int rqid = sdkProto.getRqid();
|
||||
final ResponseFuture responseFuture = Client.REPONSE_MAP.get(rqid);
|
||||
@@ -59,8 +66,8 @@ public class SdkClientHandler extends SimpleChannelInboundHandler<SdkProto> {
|
||||
responseFuture.putResponse(sdkProto);
|
||||
}
|
||||
} else {
|
||||
logger.warn("receive response {}, but not matched any request, address is {}",
|
||||
sdkProto, NettyUtil.parseRemoteAddr(ctx.channel()));
|
||||
logger.error("receive response {}, but not matched any request, address is {}",
|
||||
protocolBean, NettyUtil.parseRemoteAddr(ctx.channel()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -38,7 +38,7 @@ public class DidSdkTest {
|
||||
|
||||
// 测试异步请求
|
||||
final CountDownLatch countDownLatch = new CountDownLatch(NUM);
|
||||
IntStream.range(0, NUM).forEach(i ->
|
||||
IntStream.range(0, NUM).parallel().forEach(i ->
|
||||
Try.of(() -> client.invokeAsync(responseFuture -> {
|
||||
System.out.println(responseFuture.getSdkProto());
|
||||
countDownLatch.countDown();
|
||||
|
Reference in New Issue
Block a user