From 0a2da83a2c0dcbbe191d627abb72eaf651a9b928 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 4 Apr 2018 15:25:02 +0200 Subject: [PATCH] Better Ping/UDP-close-hint management --- .../network/connection/ConnectionManager.java | 26 ++++++++++++- .../connection/DatagramCloseListener.java | 32 --------------- src/dorkbox/network/connection/EndPoint.java | 10 ----- .../connection/ping/PingSystemListener.java | 39 ------------------- 4 files changed, 25 insertions(+), 82 deletions(-) delete mode 100644 src/dorkbox/network/connection/DatagramCloseListener.java delete mode 100644 src/dorkbox/network/connection/ping/PingSystemListener.java diff --git a/src/dorkbox/network/connection/ConnectionManager.java b/src/dorkbox/network/connection/ConnectionManager.java index c93bdfa1..0053ced1 100644 --- a/src/dorkbox/network/connection/ConnectionManager.java +++ b/src/dorkbox/network/connection/ConnectionManager.java @@ -31,10 +31,12 @@ import dorkbox.network.connection.listenerManagement.OnConnectedManager; import dorkbox.network.connection.listenerManagement.OnDisconnectedManager; import dorkbox.network.connection.listenerManagement.OnIdleManager; import dorkbox.network.connection.listenerManagement.OnMessageReceivedManager; +import dorkbox.network.connection.ping.PingMessage; import dorkbox.util.Property; import dorkbox.util.collections.ConcurrentEntry; import dorkbox.util.generics.ClassHelper; import dorkbox.util.generics.TypeResolver; +import io.netty.bootstrap.DatagramCloseMessage; import io.netty.util.concurrent.ImmediateEventExecutor; import io.netty.util.concurrent.Promise; @@ -312,7 +314,29 @@ class ConnectionManager implements Listeners, ISessionMana @Override public final void onMessage(final ConnectionImpl connection, final Object message) { - notifyOnMessage0(connection, message, false); + // add the ping listener (internal use only!) + Class messageClass = message.getClass(); + if (messageClass == PingMessage.class) { + PingMessage ping = (PingMessage) message; + if (ping.isReply) { + connection.updatePingResponse(ping); + } + else { + // return the ping from whence it came + ping.isReply = true; + + connection.ping0(ping); + } + } + + // add the UDP "close hint" to close remote connections (internal use only!) + else if (messageClass == DatagramCloseMessage.class) { + connection.forceClose(); + } + + else { + notifyOnMessage0(connection, message, false); + } } @SuppressWarnings("Duplicates") diff --git a/src/dorkbox/network/connection/DatagramCloseListener.java b/src/dorkbox/network/connection/DatagramCloseListener.java deleted file mode 100644 index 92e80a7f..00000000 --- a/src/dorkbox/network/connection/DatagramCloseListener.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2018 dorkbox, llc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.network.connection; - -import io.netty.bootstrap.DatagramCloseMessage; - -/** - * Uses a hint to close remote UDP connections - */ -public -class DatagramCloseListener implements Listener.OnMessageReceived { - @Override - public - void received(final Connection connection, final DatagramCloseMessage message) { - if (connection instanceof ConnectionImpl) { - ((ConnectionImpl) connection).forceClose(); - } - } -} diff --git a/src/dorkbox/network/connection/EndPoint.java b/src/dorkbox/network/connection/EndPoint.java index 87305a03..e5ae0c80 100644 --- a/src/dorkbox/network/connection/EndPoint.java +++ b/src/dorkbox/network/connection/EndPoint.java @@ -31,7 +31,6 @@ import org.slf4j.LoggerFactory; import dorkbox.network.Configuration; import dorkbox.network.connection.bridge.ConnectionBridgeBase; -import dorkbox.network.connection.ping.PingSystemListener; import dorkbox.network.connection.registration.MetaChannel; import dorkbox.network.connection.wrapper.ChannelLocalWrapper; import dorkbox.network.connection.wrapper.ChannelNetworkWrapper; @@ -259,15 +258,6 @@ class EndPoint extends Shutdownable { //noinspection unchecked connectionManager = new ConnectionManager(type.getSimpleName(), connection0(null, null).getClass()); - // add the ping listener (internal use only!) - connectionManager.add(new PingSystemListener()); - - // add the UDP "close hint" to close remote connections (internal use only!) - if (config.udpPort > 0) { - connectionManager.add(new DatagramCloseListener()); - } - - if (rmiEnabled) { rmiHandler = null; localRmiHandler = new RmiObjectLocalHandler(logger); diff --git a/src/dorkbox/network/connection/ping/PingSystemListener.java b/src/dorkbox/network/connection/ping/PingSystemListener.java deleted file mode 100644 index 82d733b8..00000000 --- a/src/dorkbox/network/connection/ping/PingSystemListener.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2010 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.network.connection.ping; - -import dorkbox.network.connection.ConnectionImpl; -import dorkbox.network.connection.Listener; - -public -class PingSystemListener implements Listener.OnMessageReceived { - - public - PingSystemListener() { - } - - @Override - public void received(ConnectionImpl connection, PingMessage ping) { - if (ping.isReply) { - connection.updatePingResponse(ping); - } else { - // return the ping from whence it came - ping.isReply = true; - - connection.ping0(ping); - } - } -}