@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { while (in.remaining() > 4) { // 前4位是长度 byte[] lengthBytes = new byte[4]; in.mark(); //标记当前位置,以便reset in.get(lengthBytes); //读取前4字节 int length = ByteBuffer.wrap(lengthBytes).getInt(); // 数据不够,返回false,需要继续读取 if (length == 0 || length > in.remaining()) { in.reset(); return false; } // 够了,开始解码 byte[] bytes = new byte[length]; in.get(bytes, 0, length); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { RemotingCommand remotingCommand = codec.decode(byteBuffer); out.write(remotingCommand); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("decode exception, {}", RemotingHelper.parseChannelRemoteAddr(channel), e); RemotingHelper.closeChannel(channel); } } return false; }
@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { while (in.remaining() > 4) { // 前4位是长度 byte[] lengthBytes = new byte[4]; in.mark(); //标记当前位置,以便reset in.get(lengthBytes); //读取前4字节 int length = ByteBuffer.wrap(lengthBytes).getInt(); // 数据不够,返回false,需要继续读取 if (length == 0 || length > in.remaining()) { in.reset(); return false; } // 够了,开始解码 byte[] bytes = new byte[length]; in.get(bytes, 0, length); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { RemotingCommand remotingCommand = codec.decode(byteBuffer); out.write(remotingCommand); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("decode exception, {}", RemotingHelper.parseChannelRemoteAddr(channel), e); RemotingHelper.closeChannel(channel); } } return false; }
/** * Generates response for non-versioned connections, such as those before FP9. * * @param input * incoming RTMP bytes * @return outgoing handshake */ private IoBuffer generateUnversionedHandshake(byte[] input) { log.debug("Using old style (un-versioned) handshake"); IoBuffer output = IoBuffer.allocate((Constants.HANDSHAKE_SIZE * 2) + 1); // 3073 // non-encrypted output.put(RTMPConnection.RTMP_NON_ENCRYPTED); // set server uptime in seconds output.putInt((int) Red5.getUpTime() / 1000); //0x01 output.position(Constants.HANDSHAKE_SIZE + 1); output.put(input); output.flip(); // fill S1 with handshake data (nearly all 0's) output.mark(); output.position(1); output.get(s1); output.reset(); return output; }
buf.mark(); int msgPos = savedBuf.position(); // carry on from previous buffer boolean seenNewline = false;
/** * {@inheritDoc} */ @Override public IoBuffer mark() { buf.mark(); return this; }
/** * {@inheritDoc} */ @Override public IoBuffer mark() { buf.mark(); return this; }
@Override public IoBuffer mark() { buf.mark(); return this; }
/** * {@inheritDoc} */ public synchronized WriteRequest poll(IoSession session) { WriteRequest request = q.poll(); // call mark() from the I/O thread to match // corresponding call to reset() from the I/O thread // KG-8210: now done here instead of in AbstractPollingIoProcessor.flushNow or DefaultIoFilterChain$HeadFilter // .filterWrite to make sure reset will work in both flushNow and clearWriteRequestQueue. if (request != null) { Object message = request.getMessage(); if (message instanceof IoBuffer) { ((IoBuffer) message).mark(); } } return request; }
@Override public void writeExternal(ObjectOutput out) throws IOException { super.writeExternal(out); out.writeObject(frameType); if (data != null) { if (data.hasArray()) { out.writeObject(data.array()); } else { byte[] array = new byte[data.remaining()]; data.mark(); data.get(array); data.reset(); out.writeObject(array); } } else { out.writeObject(null); } }
@Override public void writeExternal(ObjectOutput out) throws IOException { super.writeExternal(out); if (data != null) { if (data.hasArray()) { out.writeObject(data.array()); } else { byte[] array = new byte[data.remaining()]; data.mark(); data.get(array); data.reset(); out.writeObject(array); } } else { out.writeObject(null); } }
private Object getMessageCopy(Object message) { Object messageCopy = message; if (message instanceof IoBuffer) { IoBuffer rb = (IoBuffer) message; rb.mark(); IoBuffer wb = IoBuffer.allocate(rb.remaining()); wb.put(rb); wb.flip(); rb.reset(); messageCopy = wb; } return messageCopy; }
public void setData(IoBuffer data) { if (data != null && data.limit() > 0) { data.mark(); codecId = ((data.get(0) & 0xff) & ITag.MASK_SOUND_FORMAT) >> 4; if (codecId == AudioCodec.AAC.getId()) { config = (data.get() == 0); } data.reset(); } this.data = data; }
public void setData(IoBuffer data) { if (data != null && data.limit() > 0) { data.mark(); codecId = ((data.get(0) & 0xff) & ITag.MASK_SOUND_FORMAT) >> 4; if (codecId == AudioCodec.AAC.getId()) { config = (data.get() == 0); } data.reset(); } this.data = data; }
private Object getMessageCopy(Object message) { Object messageCopy = message; if (message instanceof IoBuffer) { IoBuffer rb = (IoBuffer) message; rb.mark(); IoBuffer wb = IoBuffer.allocate(rb.remaining()); wb.put(rb); wb.flip(); rb.reset(); messageCopy = wb; } return messageCopy; }
private Object getMessageCopy(Object message) { Object messageCopy = message; if (message instanceof IoBuffer) { IoBuffer rb = (IoBuffer) message; rb.mark(); IoBuffer wb = IoBuffer.allocate(rb.remaining()); wb.put(rb); wb.flip(); rb.reset(); messageCopy = wb; } return messageCopy; }
/** * Create aggregate data event with given data buffer. * * @param data * aggregate data * @param copy * true to use a copy of the data or false to use reference */ public Aggregate(IoBuffer data, boolean copy) { super(Type.STREAM_DATA); if (copy) { byte[] array = new byte[data.limit()]; data.mark(); data.get(array); data.reset(); setData(array); } else { setData(data); } }
/** * Create aggregate data event with given data buffer. * * @param data * aggregate data * @param copy * true to use a copy of the data or false to use reference */ public Aggregate(IoBuffer data, boolean copy) { super(Type.STREAM_DATA); if (copy) { byte[] array = new byte[data.remaining()]; data.mark(); data.get(array); data.reset(); setData(array); } else { setData(data); } }
@Override protected boolean doDecode(IoSession session, IoBuffer msg, ProtocolDecoderOutput out) throws Exception { /** * 消息已经解析 * 谷歌浏览器一次请求存在多次收到消息,还额外请求了/favicon.ico路径 */ if (session.containsAttribute(HTTP_REQUEST)) { return false; } msg.mark(); HttpRequestImpl rq = parseHttpRequestHead(msg.buf(), msg); if (rq != null) { out.write(rq); session.setAttribute(HTTP_REQUEST, rq); // LOG.info("解析成功"); return true; } msg.reset(); return false; }
@Override protected boolean doDecode(IoSession session, IoBuffer ib, ProtocolDecoderOutput out) throws Exception { if (ib.remaining() < 4) { return false; } ib.mark(); int length = ib.getInt(); if (length < 1 || length > maxReadSize) { int id = ib.getInt(); ib.clear(); log.warn("消息解析异常:长度{},id {}, 大于长度 maxReadSize {}", length, id, maxReadSize); session.close(true); return false; } if (ib.remaining() < length) { ib.reset(); return false; } decodeBytes(length, ib, out); return true; }
@Override public boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { if (in.limit() <= 0 || in.remaining() < Constants.HEADER_SIZE) { return false; } in.mark(); int dataLength = in.getInt(); if (in.remaining() < dataLength) { //logger.warn("[JobX]serializer error!body length < {}", dataLength); in.reset(); return false; } byte[] data = new byte[dataLength]; in.get(data); Object obj = serializer.deserialize(data,type); out.write(obj); return true; } }