Search in sources :

Example 11 with ImmediatePooledByteBuffer

use of io.undertow.util.ImmediatePooledByteBuffer in project undertow by undertow-io.

the class WebSocket07FrameSinkChannel method createFrameHeader.

@Override
protected SendFrameHeader createFrameHeader() {
    byte b0 = 0;
    //if writes are shutdown this is the final fragment
    if (isFinalFrameQueued()) {
        // set FIN
        b0 |= 1 << 7;
    }
    /*
            Known extensions (i.e. compression) should not modify RSV bit on continuation bit.
         */
    byte opCode = opCode();
    int rsv = opCode == WebSocket07Channel.OPCODE_CONT ? 0 : getRsv();
    b0 |= (rsv & 7) << 4;
    b0 |= opCode & 0xf;
    final ByteBuffer header = ByteBuffer.allocate(14);
    byte maskKey = 0;
    if (masker != null) {
        maskKey |= 1 << 7;
    }
    long payloadSize = getBuffer().remaining();
    if (payloadSize > 125 && opCode == WebSocket07Channel.OPCODE_PING) {
        throw WebSocketMessages.MESSAGES.invalidPayloadLengthForPing(payloadSize);
    }
    if (payloadSize <= 125) {
        header.put(b0);
        header.put((byte) ((payloadSize | maskKey) & 0xFF));
    } else if (payloadSize <= 0xFFFF) {
        header.put(b0);
        header.put((byte) ((126 | maskKey) & 0xFF));
        header.put((byte) (payloadSize >>> 8 & 0xFF));
        header.put((byte) (payloadSize & 0xFF));
    } else {
        header.put(b0);
        header.put((byte) ((127 | maskKey) & 0xFF));
        header.putLong(payloadSize);
    }
    if (masker != null) {
        //generate a new key for this frame
        int maskingKey = random.nextInt();
        header.put((byte) ((maskingKey >> 24) & 0xFF));
        header.put((byte) ((maskingKey >> 16) & 0xFF));
        header.put((byte) ((maskingKey >> 8) & 0xFF));
        header.put((byte) ((maskingKey & 0xFF)));
        masker.setMaskingKey(maskingKey);
        //do any required masking
        ByteBuffer buf = getBuffer();
        masker.beforeWrite(buf, buf.position(), buf.remaining());
    }
    header.flip();
    return new SendFrameHeader(0, new ImmediatePooledByteBuffer(header));
}
Also used : ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Example 12 with ImmediatePooledByteBuffer

use of io.undertow.util.ImmediatePooledByteBuffer in project undertow by undertow-io.

the class PerMessageDeflateFunction method toArrayBacked.

private PooledByteBuffer toArrayBacked(ByteBuffer buffer, ByteBufferPool pool) {
    if (pool.getBufferSize() < buffer.remaining()) {
        return new ImmediatePooledByteBuffer(ByteBuffer.wrap(Buffers.take(buffer)));
    }
    PooledByteBuffer newBuf = pool.getArrayBackedPool().allocate();
    newBuf.getBuffer().put(buffer);
    newBuf.getBuffer().flip();
    return newBuf;
}
Also used : ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Aggregations

ImmediatePooledByteBuffer (io.undertow.util.ImmediatePooledByteBuffer)12 ByteBuffer (java.nio.ByteBuffer)10 SendFrameHeader (io.undertow.server.protocol.framed.SendFrameHeader)7 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)6 HeaderMap (io.undertow.util.HeaderMap)3 HeaderValues (io.undertow.util.HeaderValues)2 HttpString (io.undertow.util.HttpString)2 AjpUtils.putString (io.undertow.protocols.ajp.AjpUtils.putString)1 Session (io.undertow.server.session.Session)1 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)1 BufferOverflowException (java.nio.BufferOverflowException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 HttpSession (javax.servlet.http.HttpSession)1