Java ByteToMessageDecoder类内存泄漏
我对内蒂不熟悉。我必须使用静态管道(因为项目经理更喜欢它)。这有点困难,因为我必须在同一条线上处理RTP和RTSP协议 虽然它几乎可以工作,但是内存泄漏。 我猜是我的分裂班的错。 此外,我认为错误可能是近旁路方法(因为netty的开发人员为了避免不定式循环,不允许将ByteBuf保持不变,这就是我必须创建旁路方法的原因。) 如果你有任何想法,请帮助我!(提前感谢!) 这是我的密码:Java ByteToMessageDecoder类内存泄漏,java,netty,red5,Java,Netty,Red5,我对内蒂不熟悉。我必须使用静态管道(因为项目经理更喜欢它)。这有点困难,因为我必须在同一条线上处理RTP和RTSP协议 虽然它几乎可以工作,但是内存泄漏。 我猜是我的分裂班的错。 此外,我认为错误可能是近旁路方法(因为netty的开发人员为了避免不定式循环,不允许将ByteBuf保持不变,这就是我必须创建旁路方法的原因。) 如果你有任何想法,请帮助我!(提前感谢!) 这是我的密码: import io.netty.buffer.ByteBuf; import io.netty.buffer.Un
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.MessageList;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.ArrayList;
import java.util.List;
public class Splitter extends ByteToMessageDecoder {
private ByteBuf bb = Unpooled.buffer();
final RtspClientHandler rtspClientHandler;
final RtpClientHandler rtpClientHandler;
public Splitter(RtspClientHandler rtspClientHandler, RtpClientHandler rtpClientHandler) {
this.rtspClientHandler = rtspClientHandler;
this.rtpClientHandler = rtpClientHandler;
}
protected void bypass(ByteBuf in, MessageList<Object> out) {
bb.writeBytes(in);
in.discardReadBytes();
bb.retain();
out.add(bb);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageList<Object> out) throws Exception {
if (rtspClientHandler.getRTSPstate() == RtspClientHandler.RTSP_CLIENT_STATE.READY) {
if (in.getByte(0) == 0x24 && in.readableBytes() > 4) {
int lengthToRead = in.getUnsignedShort(2);
if (in.readableBytes() >= (lengthToRead + 4)) {
in.skipBytes(4);
if (in.getByte(16) == 0x67 || in.getByte(16) == 0x68) {
final byte bytes[] = new byte[lengthToRead];
in.readBytes(bytes);
in.discardReadBytes();
SPSPPSbuffer spspps = new SPSPPSbuffer();
spspps.setSPSPPS(bytes);
out.add(spspps);
} else {
final byte packetArray[] = new byte[lengthToRead];// copy packet.
in.readBytes(packetArray);
in.discardReadBytes();
out.add(packetArray);
}
}
} else {
bypass(in, out);
}
} else {
bypass(in, out);
}
}
}
导入io.netty.buffer.ByteBuf;
导入io.netty.buffer.Unpooled;
导入io.netty.channel.ChannelHandlerContext;
导入io.netty.channel.MessageList;
导入io.netty.handler.codec.ByteToMessageDecoder;
导入java.util.ArrayList;
导入java.util.List;
公共类拆分器扩展为teToMessageDecoder{
private ByteBuf bb=unmooled.buffer();
最终RtspClientHandler RtspClientHandler;
最终RtpClientHandler RtpClientHandler;
公共拆分器(RtspClientHandler RtspClientHandler、RtpClientHandler RtpClientHandler){
this.rtspClientHandler=rtspClientHandler;
this.rtpClientHandler=rtpClientHandler;
}
受保护的无效旁路(ByteBuf输入,MessageList输出){
bb.写入字节(in);
in.discardReadBytes();
bb.保留();
加上(bb);
}
@凌驾
受保护的无效解码(ChannelHandlerContext ctx、ByteBuf输入、MessageList输出)引发异常{
if(rtspClientHandler.getRTSPstate()==rtspClientHandler.RTSP_CLIENT_STATE.READY){
if(in.getByte(0)=0x24&&in.readableBytes()>4){
int lengthToRead=in.getUnsignedShort(2);
if(in.readableBytes()>=(lengthToRead+4)){
in.skipBytes(4);
if(in.getByte(16)==0x67 | | in.getByte(16)==0x68){
最终字节数[]=新字节数[lengthToRead];
in.readBytes(字节);
in.discardReadBytes();
SPSPPSbuffer spspps=新的SPSPPSbuffer();
spspps.setSPSPPS(字节);
out.add(spspps);
}否则{
最终字节packetArray[]=新字节[lengthToRead];//复制数据包。
in.readBytes(packetArray);
in.discardReadBytes();
out.add(packetArray);
}
}
}否则{
旁路(输入、输出);
}
}否则{
旁路(输入、输出);
}
}
}
看来我能解决这个问题
主要问题是:我必须使用收集器ByteBuf,其中我收集来自网络的所有字节(我必须清除输入ByteBuf),因为可能有4种情况:
- 字节数(在收集器中)小于RTP块大小
- 字节数(在收集器中)等于RTP块大小
- 字节数(在收集器中)大于RTP块大小
- 收集器ByteBuf中有多个块
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.MessageList;
import io.netty.handler.codec.ByteToMessageDecoder;
public class Splitter extends ByteToMessageDecoder {
private ByteBuf collector = Unpooled.buffer();
final RtspClientHandler rtspClientHandler;
final RtpClientHandler rtpClientHandler;
public Splitter(RtspClientHandler rtspClientHandler, RtpClientHandler rtpClientHandler) {
this.rtspClientHandler = rtspClientHandler;
this.rtpClientHandler = rtpClientHandler;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageList<Object> out) throws Exception {
collector.writeBytes(in);
in.discardReadBytes();
in.clear();
if (rtspClientHandler.getRTSPstate() != RtspClientHandler.RTSP_CLIENT_STATE.READY) {
System.out.println("RTSP communication in progress");
collector.retain();
out.add(collector);
return;
}
if (collector.readableBytes() > 0 && collector.getByte(0) != 0x24) {
System.out.println("Clearing the Unpooled.buffer() (because it does not start with 0x24)");
collector.readerIndex(collector.writerIndex());
collector.discardReadBytes();
}
System.out.println("*****New bytes arrived");
while (collector.readableBytes() > 0 && collector.getByte(0) == 0x24) {
System.out.println("Length: " + collector.readableBytes());
if (collector.readableBytes() > 4) {
int lengthToRead = collector.getUnsignedShort(2);
if (collector.readableBytes() >= (lengthToRead + 4)) {
collector.skipBytes(4);
if (collector.getByte(16) == 0x67 || collector.getByte(16) == 0x68) {
final byte bytes[] = new byte[lengthToRead];
collector.readBytes(bytes);
collector.discardReadBytes();
SPSPPSbuffer spspps = new SPSPPSbuffer();
spspps.setSPSPPS(bytes);
out.add(spspps);
} else {
final byte packetArray[] = new byte[lengthToRead];// copy packet.
collector.readBytes(packetArray);
collector.discardReadBytes();
out.add(packetArray);
}
} else {
System.out.println("Not enough length, " + (lengthToRead + 4) + " byte should be required (together with 4 bytes header)");
return;
}
} else {
System.out.println("Less than 5 bytes");
return;
}
}
}
导入io.netty.buffer.ByteBuf;
导入io.netty.buffer.Unpooled;
导入io.netty.channel.ChannelHandlerContext;
导入io.netty.channel.MessageList;
导入io.netty.handler.codec.ByteToMessageDecoder;
公共类拆分器扩展为teToMessageDecoder{
private ByteBuf collector=unmooled.buffer();
最终RtspClientHandler RtspClientHandler;
最终RtpClientHandler RtpClientHandler;
公共拆分器(RtspClientHandler RtspClientHandler、RtpClientHandler RtpClientHandler){
this.rtspClientHandler=rtspClientHandler;
this.rtpClientHandler=rtpClientHandler;
}
@凌驾
受保护的无效解码(ChannelHandlerContext ctx、ByteBuf输入、MessageList输出)引发异常{
collector.writeBytes(in);
in.discardReadBytes();
in.clear();
if(rtspClientHandler.getRTSPstate()!=rtspClientHandler.RTSP_CLIENT_STATE.READY){
System.out.println(“RTSP通信进行中”);
collector.retain();
out.add(收集器);
返回;
}
if(collector.readableBytes()>0&&collector.getByte(0)!=0x24){
System.out.println(“清除unmooled.buffer()(因为它不以0x24开头)”;
collector.readerIndex(collector.writerIndex());
collector.discardReadBytes();
}
System.out.println(“******新字节到达”);
while(collector.readableBytes()>0&&collector.getByte(0)==0x24){
System.out.println(“长度:“+collector.readableBytes());
if(collector.readableBytes()>4){
int lengthToRead=collector.getUnsignedShort(2);
if(collector.readableBytes()>=(lengthToRead+4)){
集电器。skipBytes(4);
if(collector.getByte(16)==0x67 | | collector.getByte(16)==0x68){
最终字节数[]=新字节数[lengthToRead];
collector.readBytes(字节);
collector.discardReadBytes();
SPSPPSbuffer spspps=新的SPSPPSbuffer();
spspps.setSPSPPS(字节);
out.add(spspps);
}否则{
最终字节packetArray[]=新字节[lengthToRead];//复制数据包。
collector.readBytes(packetArray);
collector.discardReadBytes();
out.add(packetArray);
}
}否则{
System.out.println(“长度不够,+(lengthToRead+4)+应该需要字节(连同4字节头)”;
返回;
}
}否则{
System.out.println(“小于5字节”);
返回;