List of usage examples for io.netty.buffer ByteBuf getInt
public abstract int getInt(int index);
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundErrorResponse.java
License:Open Source License
public InboundErrorResponse(ByteBuf buffer) { super(buffer); int len = buffer.getInt(4); error = buffer.getInt(8);/*w w w.j a va2 s .com*/ errorMessage = buffer.toString(12, len - 4, US_ASCII); }
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundHandshakeResponse.java
License:Open Source License
public InboundHandshakeResponse(ByteBuf buffer) throws XrootdException { super(buffer); int len = buffer.getInt(4); if (len < 8) { throw new XrootdException(kXR_NotAuthorized, "bad handshake"); }/*from w w w.ja v a 2s . c om*/ pval = buffer.getInt(8); flag = buffer.getInt(12); }
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundLoginResponse.java
License:Open Source License
public InboundLoginResponse(ByteBuf buffer) throws XrootdException { super(buffer); if (buffer.readableBytes() > 8) { int slen = buffer.getInt(4) - SESSION_ID_SIZE; byte[] session = new byte[SESSION_ID_SIZE]; buffer.getBytes(8, session);// w ww. j ava2 s .c o m sessionId = new XrootdSessionIdentifier(session); if (slen > 0) { protocols = new ArrayList<>(); String sec = buffer.toString(24, slen, US_ASCII); for (String description : Splitter.on('&').trimResults().omitEmptyStrings().split(sec)) { if (!description.startsWith(PROTOCOL_PREFIX)) { throw new XrootdException(kXR_error, "Malformed 'sec': " + sec); } protocols.add(new SecurityInfo(description.substring(PROTOCOL_PREFIX.length()))); } } else { protocols = Collections.EMPTY_LIST; } } else { sessionId = null; protocols = Collections.EMPTY_LIST; } protocolMap = protocols.stream().collect(Collectors.toMap((p) -> p.getProtocol(), (p) -> p)); }
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundOpenReadOnlyResponse.java
License:Open Source License
public InboundOpenReadOnlyResponse(ByteBuf buffer) { super(buffer); resplen = buffer.getInt(4); fhandle = buffer.getInt(8);/*from www . j a v a 2 s . c o m*/ cpsize = buffer.getInt(12); cptype = buffer.getInt(16); int len = resplen - 12; if (len > 0) { info = buffer.toString(20, len, StandardCharsets.US_ASCII); } else { info = null; } }
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundProtocolResponse.java
License:Open Source License
public InboundProtocolResponse(ByteBuf buffer) throws XrootdException { super(buffer); int len = buffer.getInt(4); byte secopt = (byte) 0; int seclvl = kXR_secNone; Map<Integer, Integer> overrides = new HashMap<>(); if (len >= 14) { secopt = buffer.getByte(19);/*from w ww . j a v a 2 s . c om*/ seclvl = buffer.getByte(20); int secvsz = buffer.getByte(21); int index = 22; for (int i = 0; i < secvsz; ++i) { overrides.put((int) buffer.getByte(index++), (int) buffer.getByte(index++)); } } signingPolicy = new SigningPolicy(seclvl, secopt, overrides); }
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundReadResponse.java
License:Open Source License
public InboundReadResponse(ByteBuf buffer) { super(buffer); dlen = buffer.getInt(4); data = buffer.alloc().ioBuffer(dlen); buffer.getBytes(8, data); }
From source file:org.dcache.xrootd.tpc.protocol.messages.InboundRedirectResponse.java
License:Open Source License
public InboundRedirectResponse(ByteBuf buffer, int requestId) throws ParseException { super(buffer); this.requestId = requestId; int len = buffer.getInt(4); port = buffer.getInt(8);/* ww w . j a v a 2 s . c om*/ parseRedirectString(buffer.toString(12, len - 4, StandardCharsets.US_ASCII)); wsec = 0; msec = 0; }
From source file:org.elasticsearch.hadoop.transport.netty4.Netty4MessageChannelHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Transports.assertTransportThread();//from w w w.j a v a 2s.co m if (!(msg instanceof ByteBuf)) { ctx.fireChannelRead(msg); return; } final ByteBuf buffer = (ByteBuf) msg; final int remainingMessageSize = buffer.getInt(buffer.readerIndex() - TcpHeader.MESSAGE_LENGTH_SIZE); final int expectedReaderIndex = buffer.readerIndex() + remainingMessageSize; InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); try { // netty always copies a buffer, either in NioWorker in its read handler, where it copies to a fresh // buffer, or in the cumulation buffer, which is cleaned each time so it could be bigger than the actual size BytesReference reference = Netty4Utils.toBytesReference(buffer, remainingMessageSize); transport.messageReceived(reference, ctx.channel(), profileName, remoteAddress, remainingMessageSize); } finally { // Set the expected position of the buffer, no matter what happened buffer.readerIndex(expectedReaderIndex); } }
From source file:org.elasticsearch.transport.netty4.ESLoggingHandler.java
License:Apache License
private String format(final ChannelHandlerContext ctx, final String eventName, final ByteBuf arg) throws IOException { final int readableBytes = arg.readableBytes(); if (readableBytes == 0) { return super.format(ctx, eventName, arg); } else if (readableBytes >= 2) { final StringBuilder sb = new StringBuilder(); sb.append(ctx.channel().toString()); final int offset = arg.readerIndex(); // this might be an ES message, check the header if (arg.getByte(offset) == (byte) 'E' && arg.getByte(offset + 1) == (byte) 'S') { if (readableBytes == TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE) { final int length = arg.getInt(offset + MESSAGE_LENGTH_OFFSET); if (length == TcpTransport.PING_DATA_SIZE) { sb.append(" [ping]").append(' ').append(eventName).append(": ").append(readableBytes) .append('B'); return sb.toString(); }//from ww w .ja v a2 s.com } else if (readableBytes >= TcpHeader.HEADER_SIZE) { // we are going to try to decode this as an ES message final int length = arg.getInt(offset + MESSAGE_LENGTH_OFFSET); final long requestId = arg.getLong(offset + REQUEST_ID_OFFSET); final byte status = arg.getByte(offset + STATUS_OFFSET); final boolean isRequest = TransportStatus.isRequest(status); final String type = isRequest ? "request" : "response"; final String version = Version.fromId(arg.getInt(offset + VERSION_ID_OFFSET)).toString(); sb.append(" [length: ").append(length); sb.append(", request id: ").append(requestId); sb.append(", type: ").append(type); sb.append(", version: ").append(version); if (isRequest) { // it looks like an ES request, try to decode the action final int remaining = readableBytes - ACTION_OFFSET; final ByteBuf slice = arg.slice(offset + ACTION_OFFSET, remaining); // the stream might be compressed try (StreamInput in = in(status, slice, remaining)) { // the first bytes in the message is the context headers try (ThreadContext context = new ThreadContext(Settings.EMPTY)) { context.readHeaders(in); } // now we can decode the action name sb.append(", action: ").append(in.readString()); } } sb.append(']'); sb.append(' ').append(eventName).append(": ").append(readableBytes).append('B'); return sb.toString(); } } } // we could not decode this as an ES message, use the default formatting return super.format(ctx, eventName, arg); }
From source file:org.jdiameter.client.impl.transport.tcp.netty.DiameterMessageDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() >= 4) { int first = in.getInt(in.readerIndex()); byte version = (byte) (first >> 24); if (version != 1) { return; }/*from w ww. j a v a 2 s . c o m*/ int messageLength = (first & 0xFFFFFF); if (in.readableBytes() < messageLength) { return; } byte[] bytes = new byte[messageLength]; in.readBytes(bytes); try { out.add(this.parser.createMessage(bytes)); } catch (AvpDataException e) { this.parentConnection.onAvpDataException(e); } } }