2016-05-29 3 views
1

J'ai créé un décodeur pour traiter les octets envoyés par le client. Voici ceReplayingDecoder déclenche une exception lors du décodage

import java.util.List; 

import io.netty.buffer.ByteBuf; 
import io.netty.channel.ChannelHandlerContext; 
import io.netty.handler.codec.ReplayingDecoder; 

public class MessageDecoder extends ReplayingDecoder<DecoderState> { 

    private int length; 

    public MessageDecoder() 
    { 
     super(DecoderState.READ_LENGTH); 
    } 

    @Override 
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception{ 
     System.out.println(buf.readableBytes()); 
     switch(state()){ 
      case READ_LENGTH: 
       length=buf.readInt(); 
       System.out.println("length is: "+length); 
       checkpoint(DecoderState.READ_CONTENT); 
      case READ_CONTENT: 
       ByteBuf frame = buf.readBytes(length); 
       checkpoint(DecoderState.READ_LENGTH); 
       out.add(frame); 
       break; 
      default: 
       throw new Error("Shouldn't reach here"); 
     } 
    } 
} 

Et il jette l'erreur suivante lorsque le client envoie les octets

io.netty.handler.codec.DecoderException: java.lang.IllegalArgumentException: minimumReadableBytes: -603652096 (prévu :> = 0) à io.netty.handler.codec.ReplayingDecoder.callDecode (ReplayingDecoder.java:431) à io.netty.handler.codec.ByteToMessageDecoder.channelRead (ByteToMessageDecoder.java:245) à io.netty .channel.AbstractChannelHandlerContext.invokeChannelRead (AbstractChannelHandlerContext.java:292) à io.netty.channel.AbstractChannelHandlerContext.fireChannelRead (AbstractChannelHandlerContext.java:278) à io.netty.channel.DefaultChannelPipeline.fireChannelRead (DefaultChannelPipeline.java:962) à io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe. lire (AbstractNioByteChannel.java:131) à io.netty.channel.nio.NioEventLoop.processSelectedKey (NioEventLoop.java:528) à io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized (NioEventLoop.java:485) à io.netty.channel.nio.NioEventLoop.processSelectedKeys (NioEventLoop.java:399) à io.netty.channel.nio.NioEventLoop.run (NioEventLoop.java:371) à io.netty.util.concurrent.SingleThreadEventExecutor $ 2 .run (SingleThreadEventExecutor.java:112) à io.netty.util.concurrent.DefaultThreadFactory $ DefaultRunnableDecorator.run (DefaultThreadFactory.java:137) à java.lang.Thread.run (Source inconnue)

Ce code est de la documentation officielle http://netty.io/4.0/api/io/netty/handler/codec/ReplayingDecoder.html donc je vraiment don Je ne comprends pas pourquoi cela ne fonctionne pas

+0

btw quand j'envoie 4 octets il me dit que j'ai envoyé 2147483647 octets – user2686299

Répondre

1

Probablement l'homologue distant écrit l'int comme non signé. Peut-être que vous voulez utiliser readUnsignedInt()?

+0

mais il a le type «long», et c'est 8 octets n'est-ce pas? le pair distant écrit 4 octets – user2686299

+0

sans voir exactement ce que l'homologue distant est difficile à dire. Ce que je peux vous dire, c'est que readInt() renvoie un nombre négatif ici. –

+0

mon homologue distant envoie juste une valeur entière, 4 octets http://pastebin.com/NGmqK5PH mais Netty dit que 2147483647 octets sont disponibles lorsque j'utilise System.out.println (buf.readableBytes()); – user2686299