2017-11-19 30 views
0

現在、私はPacketSystemを開発しています。それを通して、私は、認証システムを作り、このようなクライアントからサーバーへの文字列として送信されていますキー作成:Nettyパケットと設定フレーム

protected void channelRead0(ChannelHandlerContext ctx, Packet p) throws Exception { 
    if(p instanceof PacketInServerAuthRequest) { 
     System.out.println("Sending WrapperAuthKey. Waiting for answer..."); 
     send(new PacketOutServerAuthAnswer(NetworkUtils.readWrapperKey())); 
    } 
} 

どういうわけか、サーバーがWrapperKey 3回を受信します。パケットIDなし2最後の回と、これらの2つの例外が発生:私は文字列が終了したときに、サーバーを伝えるためにLengthFieldPrependerとLengthFieldBasedFrameDecoderを使用する必要がありますが、私は真剣に考えていることが分かったいくつかの研究の後

SEVERE: Nov 19, 2017 9:56:35 PM io.netty.channel.DefaultChannelPipeline onUnhandledInboundException 
WARNUNG: 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: readerIndex(8) + length(4) exceeds writerIndex(9): PooledUnsafeDirectByteBuf(ridx: 8, widx: 9, cap: 1024) 
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:442) 
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) 
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) 
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) 
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) 
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:624) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:559) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:476) 
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) 
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) 
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(8) + length(4) exceeds writerIndex(9): PooledUnsafeDirectByteBuf(ridx: 8, widx: 9, cap: 1024) 
    at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1396) 
    at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:766) 
    at me.micha.flickcloud.netty.handler.PacketDecoder.decode(PacketDecoder.java:13) 
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:411) 
    ... 16 more 

SEVERE: Nov 19, 2017 9:56:35 PM io.netty.channel.DefaultChannelPipeline onUnhandledInboundException 
WARNUNG: 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: readerIndex(16) + length(4) exceeds writerIndex(17): PooledUnsafeDirectByteBuf(ridx: 16, widx: 17, cap: 1024) 
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:442) 
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) 
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) 
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) 
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) 
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:624) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:559) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:476) 
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) 
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) 
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(16) + length(4) exceeds writerIndex(17): PooledUnsafeDirectByteBuf(ridx: 16, widx: 17, cap: 1024) 
    at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1396) 
    at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:766) 
    at me.micha.flickcloud.netty.handler.PacketDecoder.decode(PacketDecoder.java:13) 
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:411) 
    ... 16 more 

をそれらを使用するのです。多分ByteBufを通して文字列を送るのは間違いです。

誰かが私の問題を解決できるなら、私はそれを感謝します。

EDIT: 私は両側(サーバー&クライアント)上のチャンネルを初期化方法は次のとおりです。

public static Channel initChannel(Channel channel) { 
    channel.pipeline() 
    .addLast(new PacketDecoder()) 
    .addLast(new PacketEncoder()); 

    return channel; 
} 
+0

フレームの境界を特定する方法があるはずです。フレーム境界を定義するNettyでは、ハンドラをパイプラインに追加できます。サーバー/クライアントのパイプラインを含むコードを追加すると簡単に指摘できます。 – theisuru

+0

コードの編集は上記のとおりです – xplays

答えて

0

ネッティーから読み込まれたデータを意味し、非同期/非ブロックのフレームワークであることに注意してくださいチャネルは断片化することができます(一般的にTCPと同様)。 ByteToMessageDecoderの実装で試してみる前に、読み込むに十分なデータがあることを確認する必要があります。あなたがIntを読み込もうとしているように見え、読み込みに4バイトも残っていません。

関連する問題