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); }
@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 Object doFilterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest, Object message) throws Exception { if (message instanceof IoBufferEx) { IoBufferEx buf = (IoBufferEx) message; message = allocator.wrap(buf.buf(), buf.flags()); } return message; } }
@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; } };
protected WriteFuture flushNow(T session, IoSessionEx parent, IoBufferEx buf, IoFilterChain filterChain, WriteRequest request) { IoBufferAllocatorEx<?> parentAllocator = parent.getBufferAllocator(); IoBufferEx parentBuf = parentAllocator.wrap(buf.buf(), buf.flags()); return flushNowInternal(parent, parentBuf, buf, filterChain, request); }
@Override protected Object doFilterWriteWsBinary(NextFilter nextFilter, IoSession session, WriteRequest writeRequest, WsBinaryMessage wsBinary) throws Exception { IoBufferEx binaryEx = wsBinary.getBytes(); ByteBuffer binary = binaryEx.buf(); ByteBuffer encoded = base64.encode(binary); IoSessionEx sessionEx = (IoSessionEx) session; IoBufferAllocatorEx<?> allocator = sessionEx.getBufferAllocator(); IoBufferEx encodedEx = allocator.wrap(encoded, binaryEx.flags()); return new WsTextMessage(encodedEx); }
@Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { IoBufferEx encodedEx = (IoBufferEx) message; ByteBuffer encoded = encodedEx.buf(); ByteBuffer decoded = encoding.decode(encoded, new SessionDecodingState(session)); IoSessionEx sessionEx = (IoSessionEx) session; IoBufferAllocatorEx<?> allocator = sessionEx.getBufferAllocator(); IoBufferEx decodedEx = allocator.wrap(decoded, encodedEx.flags()); super.messageReceived(nextFilter, session, decodedEx); }
@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 wsTextReceived(NextFilter nextFilter, IoSession session, WsTextMessage wsText) throws Exception { IoBufferEx encodedEx = wsText.getBytes(); ByteBuffer encoded = encodedEx.buf(); ByteBuffer binary = utf8.decode(encoded); IoSessionEx sessionEx = (IoSessionEx) session; IoBufferAllocatorEx<?> allocator = sessionEx.getBufferAllocator(); IoBufferEx binaryEx = allocator.wrap(binary, encodedEx.flags()); super.wsBinaryReceived(nextFilter, session, new WsBinaryMessage(binaryEx)); }
@Override protected void wsTextReceived(NextFilter nextFilter, IoSession session, WsTextMessage wsText) throws Exception { IoBufferEx encodedEx = wsText.getBytes(); ByteBuffer encoded = encodedEx.buf(); ByteBuffer binary = base64.decode(encoded); IoSessionEx sessionEx = (IoSessionEx) session; IoBufferAllocatorEx<?> allocator = sessionEx.getBufferAllocator(); IoBufferEx binaryEx = allocator.wrap(binary, encodedEx.flags()); super.wsBinaryReceived(nextFilter, session, new WsBinaryMessage(binaryEx)); }
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)); }
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 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)); }
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 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 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)); }