public void finish() throws CorruptedFrameException { checking = false; codep = 0; if (state != UTF8_ACCEPT) { state = UTF8_ACCEPT; throw new CorruptedFrameException("bytes are not UTF-8"); } }
@Override public void channelClosed( ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { cleanup(ctx, e); }
private static int toFrameLength(long l) throws TooLongFrameException { if (l > Integer.MAX_VALUE) { throw new TooLongFrameException("Length:" + l); } else { return (int) l; } }
public LengthFieldBasedFrameDecoder createLengthBasedFrameDecoder() { return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2); }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); // Add the text line codec combination first, DelimiterBasedFrameDecoder frameDecoder = new DelimiterBasedFrameDecoder( maxCommandLength, Delimiters.lineDelimiter()); pipeline.addLast("framer", frameDecoder); pipeline.addLast("decoder", new WordSplitter()); pipeline.addLast("encoder", new StringEncoder()); // and then business logic. pipeline.addLast("handler", this); return pipeline; }
@Override public void applyProtocol(PlayerSession playerSession) { ChannelPipeline pipeline = NettyUtils .getPipeLineOfConnection(playerSession); // Upstream handlers or encoders (i.e towards server) are added to // pipeline now. pipeline.addLast("framer", new DelimiterBasedFrameDecoder(frameSize, Delimiters.nulDelimiter())); pipeline.addLast("stringDecoder", stringDecoder); // Downstream handlers (i.e towards client) are added to pipeline now. pipeline.addLast("nulEncoder", nulEncoder); pipeline.addLast("stringEncoder", stringEncoder); }
@Override public void beforeAdd(ChannelHandlerContext ctx) throws Exception { super.beforeAdd(ctx); this.ctx = ctx; }
@Override protected ChannelBuffer internalBuffer() { return super.internalBuffer(); }
private void failIfNecessary(ChannelHandlerContext ctx, boolean firstDetectionOfTooLongFrame) { if (bytesToDiscard == 0) { // Reset to the initial state and tell the handlers that // the frame was too large. long tooLongFrameLength = this.tooLongFrameLength; this.tooLongFrameLength = 0; discardingTooLongFrame = false; if (!failFast || failFast && firstDetectionOfTooLongFrame) { fail(ctx, tooLongFrameLength); } } else { // Keep discarding and notify handlers if necessary. if (failFast && firstDetectionOfTooLongFrame) { fail(ctx, tooLongFrameLength); } } }
/** * Decodes the received data so far into a frame when the channel is * disconnected. * * @param ctx the context of this handler * @param channel the current channel * @param buffer the cumulative buffer of received packets so far. * Note that the buffer might be empty, which means you * should not make an assumption that the buffer contains * at least one byte in your decoder implementation. * * @return the decoded frame if a full frame was received and decoded. * {@code null} if there's not enough data in the buffer to decode a frame. */ protected Object decodeLast( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { return decode(ctx, channel, buffer); }
private void fail(final ChannelHandlerContext ctx, int length) { fail(ctx, String.valueOf(length)); }
@Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (buffer.readableBytes() < frameLength) { return null; } else { ChannelBuffer frame = extractFrame(buffer, buffer.readerIndex(), frameLength); buffer.skipBytes(frameLength); return frame; } }
public ChannelHandler createLengthBasedFrameDecoder() { return new LengthFieldBasedFrameDecoder(frameSize, 0, 2, 0, 2); }
@Override public void applyProtocol(PlayerSession playerSession) { ChannelPipeline pipeline = NettyUtils .getPipeLineOfConnection(playerSession); // Upstream handlers or encoders (i.e towards server) are added to // pipeline now. pipeline.addLast("framer", new DelimiterBasedFrameDecoder(maxFrameSize, Delimiters.nulDelimiter())); pipeline.addLast("base64Decoder", base64Decoder); pipeline.addLast("amf3ToJavaObjectDecoder", amf3ToJavaObjectDecoder); // Downstream handlers - Filter for data which flows from server to // client. Note that the last handler added is actually the first // handler for outgoing data. pipeline.addLast("nulEncoder", nulEncoder); pipeline.addLast("base64Encoder", base64Encoder); pipeline.addLast("javaObjectToAMF3Encoder", javaObjectToAMF3Encoder); }
private void write(int b) throws CorruptedFrameException { byte type = TYPES[b & 0xFF]; codep = state != UTF8_ACCEPT ? b & 0x3f | codep << 6 : 0xff >> type & b; state = STATES[state + type]; if (state == UTF8_REJECT) { throw new CorruptedFrameException("bytes are not UTF-8"); } }
@Override public void channelDisconnected( ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { cleanup(ctx, e); }
@Override public void beforeAdd(ChannelHandlerContext ctx) throws Exception { super.beforeAdd(ctx); this.ctx = ctx; }
/** * Returns the actual number of readable bytes in the internal cumulative * buffer of this decoder. You usually do not need to rely on this value * to write a decoder. Use it only when you muse use it at your own risk. * This method is a shortcut to {@link #internalBuffer() internalBuffer().readableBytes()}. */ protected int actualReadableBytes() { return internalBuffer().readableBytes(); }
public ChannelHandler createLengthBasedFrameDecoder() { return new LengthFieldBasedFrameDecoder(frameSize, 0, 2, 0, 2); }
private void protocolViolation(Channel channel, String reason) throws CorruptedFrameException { protocolViolation(channel, new CorruptedFrameException(reason)); }