Skip to content

Commit

Permalink
#56 Possible fix for memory leak detected in logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
menacher committed Dec 31, 2013
1 parent 7534eca commit 2d76cbb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.nadron.client.event.Events;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.serialization.ClassResolvers;
Expand All @@ -24,7 +23,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in,
{
opcode = Events.SESSION_MESSAGE;
}
ByteBuf data = Unpooled.buffer(in.readableBytes()).writeBytes(in);
ByteBuf data = in.readBytes(in.readableBytes());
// TODO check if creating a new object is necessary each time
Object obj = new SourceDecoder().decode(ctx, data);
out.add(Events.event(obj, opcode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.nadron.client.event.Event;
import io.nadron.client.event.Events;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

Expand Down Expand Up @@ -41,7 +40,7 @@ protected Event decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception
{
opcode = Events.SESSION_MESSAGE;
}
ByteBuf data = Unpooled.buffer(in.readableBytes()).writeBytes(in);
ByteBuf data = in.readBytes(in.readableBytes());
return Events.event(new NettyMessageBuffer(data), opcode);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.nadron.event.Event;
import io.nadron.event.Events;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
Expand Down Expand Up @@ -33,14 +32,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer,
out.add(decode(ctx, buffer));
}

public Event decode(ChannelHandlerContext ctx, ByteBuf buffer){
byte opcode = buffer.readByte();
public Event decode(ChannelHandlerContext ctx, ByteBuf in){
byte opcode = in.readByte();
if (opcode == Events.NETWORK_MESSAGE)
{
opcode = Events.SESSION_MESSAGE;
}
ByteBuf data = Unpooled.buffer(buffer.readableBytes()).writeBytes(
buffer);
ByteBuf data = in.readBytes(in.readableBytes());
return Events.event(new NettyMessageBuffer(data), opcode);
}
}
16 changes: 16 additions & 0 deletions nadron/src/test/java/io/nadron/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ public void readWriteSocketAddress()
InetSocketAddress readAddress = NettyUtils.readSocketAddress(buffer);
assertEquals(socketAddress,readAddress);
}

@Test
public void readWriteEmptyString()
{
String hello = "Hello";
String world = "World!";
String empty = "";
String space = " ";
ByteBuf buffer = NettyUtils.writeStrings(hello, empty, space, world);
String[] readStrings = NettyUtils.readStrings(buffer, 4);
assertEquals(readStrings.length, 4);
assertEquals(readStrings[0], hello);
assertEquals(readStrings[1], empty);
assertEquals(readStrings[2], space);
assertEquals(readStrings[3], world);
}
}

0 comments on commit 2d76cbb

Please sign in to comment.