From 44cdc3449fccd22b21fec1c9a0febea807cc2489 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Jun 2018 11:35:10 +1200 Subject: Release incoming reference counted objects --- src/org/traccar/ExtendedObjectDecoder.java | 13 ++++++++----- src/org/traccar/HemisphereHandler.java | 26 +++++++++----------------- src/org/traccar/RemoteAddressHandler.java | 13 +++++++------ 3 files changed, 24 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java index 22a9c7262..bb704c2e3 100644 --- a/src/org/traccar/ExtendedObjectDecoder.java +++ b/src/org/traccar/ExtendedObjectDecoder.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBufUtil; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.ReferenceCounted; import org.traccar.helper.DataConverter; import org.traccar.model.Position; @@ -46,11 +47,9 @@ public abstract class ExtendedObjectDecoder extends ChannelInboundHandlerAdapter public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { NetworkMessage networkMessage = (NetworkMessage) msg; Object originalMessage = networkMessage.getMessage(); - Object decodedMessage = decode(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage); - onMessageEvent(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage, decodedMessage); - if (originalMessage == decodedMessage) { - ctx.fireChannelRead(originalMessage); - } else { + try { + Object decodedMessage = decode(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage); + onMessageEvent(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage, decodedMessage); if (decodedMessage == null) { decodedMessage = handleEmptyMessage(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage); } @@ -65,6 +64,10 @@ public abstract class ExtendedObjectDecoder extends ChannelInboundHandlerAdapter ctx.fireChannelRead(decodedMessage); } } + } finally { + if (originalMessage instanceof ReferenceCounted) { + ((ReferenceCounted) originalMessage).release(); + } } } diff --git a/src/org/traccar/HemisphereHandler.java b/src/org/traccar/HemisphereHandler.java index 669ffd36e..d1bcd340b 100644 --- a/src/org/traccar/HemisphereHandler.java +++ b/src/org/traccar/HemisphereHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,14 +15,11 @@ */ package org.traccar; -import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import org.traccar.model.Position; -import java.net.SocketAddress; - @ChannelHandler.Sharable -public class HemisphereHandler extends ExtendedObjectDecoder { +public class HemisphereHandler extends BaseDataHandler { private int latitudeFactor; private int longitudeFactor; @@ -47,19 +44,14 @@ public class HemisphereHandler extends ExtendedObjectDecoder { } @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - if (msg instanceof Position) { - Position position = (Position) msg; - if (latitudeFactor != 0) { - position.setLatitude(Math.abs(position.getLatitude()) * latitudeFactor); - } - if (longitudeFactor != 0) { - position.setLongitude(Math.abs(position.getLongitude()) * longitudeFactor); - } + protected Position handlePosition(Position position) { + if (latitudeFactor != 0) { + position.setLatitude(Math.abs(position.getLatitude()) * latitudeFactor); } - - return msg; + if (longitudeFactor != 0) { + position.setLongitude(Math.abs(position.getLongitude()) * longitudeFactor); + } + return position; } } diff --git a/src/org/traccar/RemoteAddressHandler.java b/src/org/traccar/RemoteAddressHandler.java index 5555413cf..aabc5715e 100644 --- a/src/org/traccar/RemoteAddressHandler.java +++ b/src/org/traccar/RemoteAddressHandler.java @@ -15,27 +15,28 @@ */ package org.traccar; -import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import org.traccar.model.Position; import java.net.InetSocketAddress; -import java.net.SocketAddress; @ChannelHandler.Sharable -public class RemoteAddressHandler extends ExtendedObjectDecoder { +public class RemoteAddressHandler extends ChannelInboundHandlerAdapter { @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + public void channelRead(ChannelHandlerContext ctx, Object msg) { - String hostAddress = ((InetSocketAddress) remoteAddress).getAddress().getHostAddress(); + InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); + String hostAddress = remoteAddress != null ? remoteAddress.getAddress().getHostAddress() : null; if (msg instanceof Position) { Position position = (Position) msg; position.set(Position.KEY_IP, hostAddress); } - return msg; + ctx.fireChannelRead(msg); } } -- cgit v1.2.3