@Override public ByteBuffer allocate(int capacity, int flags) { boolean offset = (flags & IoBufferEx.FLAG_ZERO_COPY) != IoBufferEx.FLAG_NONE; if (offset) { ByteBuffer buf = parent.allocate(FRAME_OFFSET + capacity + FRAME_PADDING, flags); buf.position(buf.position() + FRAME_OFFSET); buf.limit(buf.position() + capacity); return buf; } else { // no offset return parent.allocate(capacity, flags); } }
public static IoBufferEx doCloseEncode(IoBufferAllocatorEx<?> allocator, int flags) { // Draft Hixie has no notion of status or reason ByteBuffer close = allocator.allocate(2, flags); int offset = close.position(); close.put(CLOSE_TYPE_BYTE); close.put(CLOSE_TERMINATOR_BYTE); close.flip(); close.position(offset); return allocator.wrap(close, flags); }
private WsTextMessage createTextMessage(IoBufferAllocatorEx<?> allocator, byte[] content) { // Use FLAG_SHARED so the same message instance can be written safely multiple times ByteBuffer payload = allocator.allocate(content.length, IoBufferEx.FLAG_SHARED); int offset = payload.position(); payload.put(content); payload.flip(); payload.position(offset); WsTextMessage result = new WsTextMessage(allocator.wrap(payload, IoBufferEx.FLAG_SHARED)); return result; }
@Override protected IoBufferEx doPingEncode(IoBufferAllocatorEx<?> allocator, int flags, WsMessage message) { WsPingMessage ping = (WsPingMessage)message; assert ping.getBytes().remaining() == 0 : "PING with payload not supported"; ByteBuffer text = allocator.allocate(EMPTY_PING_BYTES.length, flags); int offset = text.position(); text.put(EMPTY_PING_BYTES); text.flip(); text.position(offset); return allocator.wrap(text, flags); } @Override
protected IoBufferEx doPongEncode(IoBufferAllocatorEx<?> allocator, int flags, WsMessage message) { WsPongMessage ping = (WsPongMessage)message; assert ping.getBytes().remaining() == 0 : "PONG with payload not supported"; ByteBuffer text = allocator.allocate(EMPTY_PONG_BYTES.length, flags); int offset = text.position(); text.put(EMPTY_PONG_BYTES); text.flip(); text.position(offset); return allocator.wrap(text, flags); }
@Override protected IoBufferEx doPongEncode(IoBufferAllocatorEx<?> allocator, int flags, WsMessage message) { WsPongMessage ping = (WsPongMessage)message; assert ping.getBytes().remaining() == 0 : "PONG with payload not supported"; ByteBuffer text = allocator.allocate(EMPTY_PONG_BYTES.length, flags); int offset = text.position(); text.put(EMPTY_PONG_BYTES); text.flip(); text.position(offset); return allocator.wrap(text, flags); }
protected IoBufferEx doPingEncode(IoBufferAllocatorEx<?> allocator, int flags, WsMessage message) { WsPingMessage ping = (WsPingMessage)message; assert ping.getBytes().remaining() == 0 : "PING with payload not supported"; ByteBuffer text = allocator.allocate(EMPTY_PING_BYTES.length, flags); int offset = text.position(); text.put(EMPTY_PING_BYTES); text.flip(); text.position(offset); return allocator.wrap(text, flags); }
private void createOutNetBuffer(int expectedRemaining) { // SSLEngine requires us to allocate unnecessarily big buffer // even for small data. *Shrug* int capacity = Math.max( expectedRemaining, sslEngine.getSession().getPacketBufferSize()); if (outNetBuffer != null) { outNetBuffer.capacity(capacity, allocator); } else { outNetBuffer = allocator.wrap(allocator.allocate(capacity)).minimumCapacity(0); } }
@Override protected DecodingState finishDecode(byte lengthByte1, ProtocolDecoderOutput out) throws Exception { if ( lengthByte1 != (byte)0 ) { throw new ProtocolDecoderException("Ping frame length must be 0, but got: " + Integer.toHexString((lengthByte1 & 0xff))); } out.write(new WsPingMessage(allocator.wrap(allocator.allocate(0)))); return READ_FRAME_TYPE; } };
@Override protected DecodingState finishDecode(byte lengthByte1, ProtocolDecoderOutput out) throws Exception { if ( lengthByte1 != (byte)0 ) { throw new ProtocolDecoderException("Pong frame length must be 0, but got: " + Integer.toHexString((lengthByte1 & 0xff))); } out.write(new WsPongMessage(allocator.wrap(allocator.allocate(0)))); return READ_FRAME_TYPE; } };
@Override protected void filterWriteHttpResponse(NextFilter nextFilter, IoSessionEx session, WriteRequest writeRequest, HttpResponseMessage httpResponse) throws Exception { if (httpResponse.getStatus() == HttpStatus.SUCCESS_OK && "text/event-stream".equals(httpResponse.getHeader(HEADER_CONTENT_TYPE))) { IoBufferAllocatorEx<?> allocator = session.getBufferAllocator(); allocator.allocate(TO_ALLOCATE); IoBufferEx buffer = httpResponse.getContent().asBuffer(); buffer.expand(0, TO_ALLOCATE, allocator); buffer.putString(WRAPPED_HTTP, UTF_8_ENCODER); buffer.flip(); } super.filterWriteHttpResponse(nextFilter, session, writeRequest, httpResponse); }
@Override protected void doMessageReceived(HttpConnectSession createSession, Object message) throws Exception { // Handle fragmentation of response body IoBufferEx in = (IoBufferEx) message; IoBufferEx buf = CREATE_RESPONSE_KEY.get(createSession); if (buf == null) { IoBufferAllocatorEx<?> allocator = createSession.getBufferAllocator(); buf = allocator.wrap(allocator.allocate(in.remaining())).setAutoExpander(allocator); CREATE_RESPONSE_KEY.set(createSession, buf); } buf.put(in); }
@Override protected void doMessageReceived(HttpSession createSession, Object message) throws Exception { // Handle fragmentation of response body IoBufferEx in = (IoBufferEx) message; IoBufferEx buf = CREATE_RESPONSE_KEY.get(createSession); if (buf == null) { IoBufferAllocatorEx<?> allocator = createSession.getBufferAllocator(); ByteBuffer nioBuf = allocator.allocate(in.remaining()); buf = allocator.wrap(nioBuf).setAutoExpander(allocator); CREATE_RESPONSE_KEY.set(createSession, buf); } buf.put(in); }
private void doEncodeWindowAcknowledgmentSize(IoSession session, RtmpWindowAcknowledgmentSizeMessage message, ProtocolEncoderOutput out) { IoBufferEx buf = allocator.wrap(allocator.allocate(1000)); buf.setAutoExpander(allocator); buf.putInt((int) message.getWindowSize()); buf.flip(); out.write(doEncodeChunk0(message.getChunkStreamId(), buf, message)); }
private void doEncodeAmf0Data(IoSession session, RtmpDataMessage message, ProtocolEncoderOutput out) { IoBufferEx buf = allocator.wrap(allocator.allocate(1000)); buf.setAutoExpander(allocator); // positional arguments encodeString(buf, message.getName()); // optional arguments // TODO AMF0 encoding buf.flip(); out.write(doEncodeChunk0(message.getChunkStreamId(), buf, message)); }
protected void encodeRequest(IoSessionEx session, HttpRequestMessage httpRequest, ProtocolEncoderOutput out) throws Exception { ByteBuffer nioBuf = allocator.allocate(1024); IoBufferEx buf = allocator.wrap(nioBuf).setAutoExpander(allocator); encodeRequestLine(httpRequest, buf); encodeHeaders(session, httpRequest, buf); HttpContentMessage httpContent = httpRequest.getContent(); if (httpContent != null) { encodeContent(session, httpContent, buf); } buf.flip(); out.write(buf); }
private void doEncodeSetChunkSize(IoSession session, RtmpSetChunkSizeMessage message, ProtocolEncoderOutput out) { int chunkStreamId = message.getChunkStreamId(); int chunkHeaderLength = 12; int messageStreamLength = 4; IoBufferEx buf = allocator.wrap(allocator.allocate(chunkHeaderLength + messageStreamLength)); int allocatedPos = buf.position(); doEncodeChunk0Header(chunkStreamId, messageStreamLength, message, buf); buf.putInt(MAXIMUM_CHUNK_SIZE); buf.flip(); buf.position(allocatedPos); session.write(buf); }
private void storeRemainingInSession(IoBufferEx buf, IoSession session) { ByteBuffer remainingNioBuf = allocator.allocate(buf.capacity(), buf.flags()); final IoBufferEx remainingBuf = allocator.wrap(remainingNioBuf).setAutoExpander(allocator); remainingBuf.mark(); remainingBuf.order(buf.order()); remainingBuf.put(buf); session.setAttribute(BUFFER, remainingBuf); } }
private void doEncodeHandshake(IoSession session, RtmpHandshakeMessage message, ProtocolEncoderOutput out) { IoBufferEx buf = allocator.wrap(allocator.allocate(8 + NONCE_LENGTH)); buf.putInt(message.getTimestamp1()); buf.putInt(message.getTimestamp2()); buf.put(message.getNonce()); buf.flip(); out.write(buf); }
private void doEncodeSetPeerBandwidth(IoSession session, RtmpSetPeerBandwidthMessage message, ProtocolEncoderOutput out) { IoBufferEx buf = allocator.wrap(allocator.allocate(1000)); buf.setAutoExpander(allocator); buf.putInt((int) message.getWindowSize()); buf.put((byte) message.getLimitType().ordinal()); buf.flip(); out.write(doEncodeChunk0(message.getChunkStreamId(), buf, message)); }