Giter Site home page Giter Site logo

flash-netty's Issues

ctx.writeAndFlush(msg) vs ctx.channel().writeAndFlush(msg)

你好;
代码中存在一个问题;
the.flash.protocol.Packet 在被传递的过程中;
使用 ctx.writeAndFlush(msg)调用链路
会走到 AbstractChannel#write(Object msg, ChannelPromise promise)

其主要代码:

...
// 依据msg的类型对其msg进行过滤
msg = filterOutboundMessage(msg);
...
//发送msg
outboundBuffer.addMessage(msg, size, promise);

而在我们代码中 msg 全部为 自定义的Packet类型,而非ByteBuf
这会导致数据无法发送;

应替换为
ctx.channel().writeAndFlush(msg)

其调用链路会走encode,将msg通过我们定义的encoder转变为ByteBuf

dependency scope setting issues

Hi,

We are developing a tool to check whether the dependencies' scopes in maven are set rightly and reasonably.

Our tools have found some issues in your project about scope setting in maven' pom.xml file.

  • in pom.xml, junit:junit scope should be test instead of compile.

Please help us confirm whether the above reports are right and explain why it should set like these.

Thanks a lot.

Spliter 代码有问题

19:49:03.127 [nioEventLoopGroup-3-3] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: index: 222, length: 4 (expected: range(0, 224))
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:480)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:279)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IndexOutOfBoundsException: index: 222, length: 4 (expected: range(0, 224))
at io.netty.buffer.AbstractByteBuf.checkRangeBounds(AbstractByteBuf.java:1390)
at io.netty.buffer.AbstractByteBuf.checkIndex0(AbstractByteBuf.java:1397)
at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1384)
at io.netty.buffer.AbstractByteBuf.getInt(AbstractByteBuf.java:433)
at com.example.demo.im.Spilter.decode(Spilter.java:21)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:332)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:510)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:449)
... 17 common frames omitted

JdkZlibDecoder 与 JdkZlibEncoder 问题

购买了您掘金和慕课网的课程,很优质的内容,很棒,非常感谢。
最近在做一个项目,使用到了zlib压缩,大致的流程是这样:

编解码规则

  • 测试报文

78 9c ca 48 cd c9 c9 d7 51 c8 4b 2d 29 a9 54 04 00 00 00 ff ff eb 90 eb 90 eb 90

  • 编码规则

字符串 -> zlib压缩 -> 添加包尾 -> 发送

  • 解码规则相反

读取一帧 -> 去包尾 -> zlib解压 -> 字符串

其中包尾定义为EB 90 EB 90 EB 90

  • 代码片段
protected void initChannel(NioSocketChannel ch) {
    log.info("init channel !");
     // 处理读数据的逻辑链
    ByteBuf delimiter = Unpooled.copiedBuffer(Constants.TAIL);
     ch.pipeline().addLast(new DelimiterBasedFrameDecoder(2048, delimiter));
    ch.pipeline().addLast(new JdkZlibDecoder());
    ch.pipeline().addLast(new StringDecoder());
    ch.pipeline().addLast(new InBoundHandler());
                        
    // 写逻辑处理
    ch.pipeline().addLast(new DelimiterEncoder());
    ch.pipeline().addLast(new JdkZlibEncoder());
    ch.pipeline().addLast(new StringEncoder());                        
}

调试中,我发现io.netty.handler.codec.compression包下的JdkZlibDecoderJdkZlibEncoder都有一个finish标记,这就导致我只能编解码一次,不清楚为何这样设计,最后我只能用了笨办法:

@Slf4j
public class InBoundHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        log.info("MyInBoundHandler: " + msg);
        ctx.pipeline().replace(JdkZlibDecoder.class, "zlibDecoder", new JdkZlibDecoder());
        String content = "hello, netty!";
        ctx.channel().writeAndFlush(content);
    }

}
@Slf4j
public class DelimiterEncoder extends MessageToByteEncoder<ByteBuf> {
    

    @Override
    protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
        log.info("writeBytes");
        System.err.println(ByteBufUtil.prettyHexDump(msg));
        out.writeBytes(msg);
        out.writeBytes(Constants.TAIL);
        ctx.pipeline().replace(JdkZlibEncoder.class, "zlibEncoder", new JdkZlibEncoder());
    }


}

我的问题是:

  1. netty这样设计的目的?
  2. 像我这样使用有没有问题?
  3. 如果需要连续使用,除了自定义编解码器,和我上面用的办法,有没有别的办法?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.