List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertGoAwayFrame(ByteBuf frame, int lastStreamId, int errorCode) { byte type = 0x07; byte flags = 0x00; assertEquals(8, assertFrameHeader(frame, type, flags, 0)); assertEquals(lastStreamId, frame.readInt()); assertEquals(errorCode, frame.readInt()); assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertWindowUpdateFrame(ByteBuf frame, int streamId, int windowSizeIncrement) { byte type = 0x08; byte flags = 0x00; assertEquals(4, assertFrameHeader(frame, type, flags, streamId)); assertEquals(windowSizeIncrement, frame.readInt()); assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpHeaderBlockDecoder.java
License:Apache License
public void decode(ByteBuf headerBlock, final HttpHeaderBlockFrame frame) throws IOException { HeaderListener headerListener = NULL_HEADER_LISTENER; if (frame != null) { headerListener = new HeaderListenerImpl(frame.headers()); }//from w ww. j ava 2 s.c o m if (cumulation == null) { decoder.decode(new ByteBufInputStream(headerBlock), headerListener); if (headerBlock.isReadable()) { cumulation = Unpooled.buffer(headerBlock.readableBytes()); cumulation.writeBytes(headerBlock); } } else { cumulation.writeBytes(headerBlock); decoder.decode(new ByteBufInputStream(cumulation), headerListener); if (cumulation.isReadable()) { cumulation.discardReadBytes(); } else { cumulation.release(); cumulation = null; } } }
From source file:com.twitter.http2.HttpStreamDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { if (msg instanceof HttpHeadersFrame) { HttpHeadersFrame httpHeadersFrame = (HttpHeadersFrame) msg; int streamId = httpHeadersFrame.getStreamId(); StreamedHttpMessage message = messageMap.get(streamId); if (message == null) { if (httpHeadersFrame.headers().contains(":status")) { // If a client receives a reply with a truncated header block, // reply with a RST_STREAM frame with error code INTERNAL_ERROR. if (httpHeadersFrame.isTruncated()) { HttpRstStreamFrame httpRstStreamFrame = new DefaultHttpRstStreamFrame(streamId, HttpErrorCode.INTERNAL_ERROR); out.add(httpRstStreamFrame); return; }//from w ww . j a v a 2 s .co m try { StreamedHttpResponse response = createHttpResponse(httpHeadersFrame); if (httpHeadersFrame.isLast()) { HttpHeaders.setContentLength(response, 0); response.getContent().close(); } else { // Response body will follow in a series of Data Frames if (!HttpHeaders.isContentLengthSet(response)) { HttpHeaders.setTransferEncodingChunked(response); } messageMap.put(streamId, response); } out.add(response); } catch (Exception e) { // If a client receives a SYN_REPLY without valid getStatus and version headers // the client must reply with a RST_STREAM frame indicating a PROTOCOL_ERROR HttpRstStreamFrame httpRstStreamFrame = new DefaultHttpRstStreamFrame(streamId, HttpErrorCode.PROTOCOL_ERROR); ctx.writeAndFlush(httpRstStreamFrame); out.add(httpRstStreamFrame); } } else { // If a client sends a request with a truncated header block, the server must // reply with a HTTP 431 REQUEST HEADER FIELDS TOO LARGE reply. if (httpHeadersFrame.isTruncated()) { httpHeadersFrame = new DefaultHttpHeadersFrame(streamId); httpHeadersFrame.setLast(true); httpHeadersFrame.headers().set(":status", HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE.code()); ctx.writeAndFlush(httpHeadersFrame); return; } try { message = createHttpRequest(httpHeadersFrame); if (httpHeadersFrame.isLast()) { message.setDecoderResult(DecoderResult.SUCCESS); message.getContent().close(); } else { // Request body will follow in a series of Data Frames messageMap.put(streamId, message); } out.add(message); } catch (Exception e) { // If a client sends a SYN_STREAM without all of the method, url (host and path), // scheme, and version headers the server must reply with a HTTP 400 BAD REQUEST reply. // Also sends HTTP 400 BAD REQUEST reply if header name/value pairs are invalid httpHeadersFrame = new DefaultHttpHeadersFrame(streamId); httpHeadersFrame.setLast(true); httpHeadersFrame.headers().set(":status", HttpResponseStatus.BAD_REQUEST.code()); ctx.writeAndFlush(httpHeadersFrame); } } } else { LastHttpContent trailer = trailerMap.remove(streamId); if (trailer == null) { trailer = new DefaultLastHttpContent(); } // Ignore trailers in a truncated HEADERS frame. if (!httpHeadersFrame.isTruncated()) { for (Map.Entry<String, String> e : httpHeadersFrame.headers()) { trailer.trailingHeaders().add(e.getKey(), e.getValue()); } } if (httpHeadersFrame.isLast()) { messageMap.remove(streamId); message.addContent(trailer); } else { trailerMap.put(streamId, trailer); } } } else if (msg instanceof HttpDataFrame) { HttpDataFrame httpDataFrame = (HttpDataFrame) msg; int streamId = httpDataFrame.getStreamId(); StreamedHttpMessage message = messageMap.get(streamId); // If message is not in map discard Data Frame. if (message == null) { return; } ByteBuf content = httpDataFrame.content(); if (content.isReadable()) { content.retain(); message.addContent(new DefaultHttpContent(content)); } if (httpDataFrame.isLast()) { messageMap.remove(streamId); message.addContent(LastHttpContent.EMPTY_LAST_CONTENT); message.setDecoderResult(DecoderResult.SUCCESS); } } else if (msg instanceof HttpRstStreamFrame) { HttpRstStreamFrame httpRstStreamFrame = (HttpRstStreamFrame) msg; int streamId = httpRstStreamFrame.getStreamId(); StreamedHttpMessage message = messageMap.remove(streamId); if (message != null) { message.getContent().close(); message.setDecoderResult(DecoderResult.failure(CANCELLATION_EXCEPTION)); } } else { // HttpGoAwayFrame out.add(msg); } }
From source file:com.vethrfolnir.game.network.mu.MuCyperDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable()) { MuClient client = ctx.channel().attr(MuClient.ClientKey).get(); _log.warn("Client[" + client + "] sent an empty packet!"); return; //XXX: is it critical? }/*w w w . j a v a2 s .c o m*/ if (in.readableBytes() < 3) { return; // come back later } in.markReaderIndex(); int opcode = in.readUnsignedByte(); int lengthAt = 0; switch (opcode) { case 0xc1: case 0xc3: lengthAt = 1; break; case 0xc2: case 0xc4: lengthAt = 2; break; } //in.markReaderIndex(); int rez = lengthAt > 1 ? in.readShort() : in.readUnsignedByte(); in.resetReaderIndex(); //System.out.println("1 Size[point="+(lengthAt > 1 ? "Short" : "Unsigned byte")+"]: "+rez+" opcode "+Integer.toHexString(opcode & 0xFF)); if (in.readableBytes() < rez) { in.resetReaderIndex(); return; } int header = in.getUnsignedByte(0); if (header == 0xC1 || header == 0xC2) { ByteBuf buff = ctx.alloc().heapBuffer(rez); in.readBytes(buff); out.add(DecodeXor32(buff)); } else { out.add(DecodePacket(in)); } }
From source file:com.vethrfolnir.network.ReadPacket.java
License:Open Source License
/** * This is only for LS <-> GS Communication, do not use it for clients! Unless overriden and managed * @param buff/*from ww w .j a v a 2s .co m*/ * @return */ protected String readS(ByteBuf buff) { try { ArrayList<Character> ins = new ArrayList<>(); char in; while (buff.isReadable() && (in = buff.readChar()) != '\000') { ins.add(in); } char[] arr = new char[ins.size()]; for (int i = 0; i < arr.length; i++) { arr[i] = ins.get(i); } String str = new String(arr); return str; } catch (Exception e) { log.warn("Failed reading string!", e); } return null; }
From source file:com.vmware.dcp.common.http.netty.NettyHttpClientRequestHandler.java
License:Open Source License
private void decodeRequestBody(ChannelHandlerContext ctx, Operation request, ByteBuf content) { if (!content.isReadable()) { // skip body decode, request had no body request.setContentLength(0);/*from w ww .j a v a 2 s. com*/ submitRequest(ctx, request); return; } request.nestCompletion((o, e) -> { if (e != null) { request.setStatusCode(Operation.STATUS_CODE_BAD_REQUEST); request.setBody(ServiceErrorResponse.create(e, request.getStatusCode())); sendResponse(ctx, request); return; } submitRequest(ctx, request); }); Utils.decodeBody(request, content.nioBuffer()); }
From source file:com.vmware.dcp.common.http.netty.NettyHttpServerResponseHandler.java
License:Open Source License
private void decodeResponseBody(Operation request, ByteBuf content) { if (!content.isReadable()) { if (checkResponseForError(request)) { return; }//from w w w . j a va2 s .co m // skip body decode, request had no body request.setContentLength(0).setContentType(Operation.MEDIA_TYPE_APPLICATION_JSON); request.complete(); return; } request.nestCompletion((o, e) -> { if (e != null) { request.fail(e); return; } completeRequest(request); }); Utils.decodeBody(request, content.nioBuffer()); }
From source file:com.vmware.xenon.common.http.netty.NettyHttpClientRequestHandler.java
License:Open Source License
private void decodeRequestBody(ChannelHandlerContext ctx, Operation request, ByteBuf content, Integer streamId, URI callbackUri) {//from w w w .ja va 2s.c o m if (!content.isReadable()) { // skip body decode, request had no body request.setContentLength(0); submitRequest(ctx, request, streamId, callbackUri); return; } request.nestCompletion((o, e) -> { if (e != null) { request.setStatusCode(Operation.STATUS_CODE_BAD_REQUEST); request.setBody(ServiceErrorResponse.create(e, request.getStatusCode())); sendResponse(ctx, request, streamId); return; } submitRequest(ctx, request, streamId, callbackUri); }); Utils.decodeBody(request, content.nioBuffer()); }
From source file:com.vmware.xenon.common.http.netty.NettyHttpServerResponseHandler.java
License:Open Source License
private void decodeResponseBody(Operation request, ByteBuf content) { if (!content.isReadable()) { if (checkResponseForError(request)) { return; }//from w ww .j av a 2 s . co m // skip body decode, request had no body request.setContentLength(0).complete(); return; } request.nestCompletion((o, e) -> { if (e != null) { request.fail(e); return; } if (checkResponseForError(request)) { return; } completeRequest(request); }); Utils.decodeBody(request, content.nioBuffer()); }