From 5d2fa50de245ad12668ff4ee27b55168f214498a Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 3 Apr 2018 14:38:52 +0200 Subject: [PATCH] Moved UDP close hint to it's own class and into the EndPoint (similar to how Ping works) --- .../network/connection/ConnectionImpl.java | 15 +++------ src/dorkbox/network/connection/EndPoint.java | 7 +++++ .../connection/RegistrationWrapper.java | 2 -- .../bootstrap/DatagramCloseListener.java | 31 +++++++++++++++++++ .../netty/bootstrap/DatagramCloseMessage.java | 2 +- 5 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/io/netty/bootstrap/DatagramCloseListener.java diff --git a/src/dorkbox/network/connection/ConnectionImpl.java b/src/dorkbox/network/connection/ConnectionImpl.java index 5f74ddce..61117ef6 100644 --- a/src/dorkbox/network/connection/ConnectionImpl.java +++ b/src/dorkbox/network/connection/ConnectionImpl.java @@ -32,7 +32,6 @@ import org.bouncycastle.crypto.params.ParametersWithIV; import org.slf4j.Logger; import dorkbox.network.Client; -import dorkbox.network.connection.Listener.OnMessageReceived; import dorkbox.network.connection.bridge.ConnectionBridge; import dorkbox.network.connection.idle.IdleBridge; import dorkbox.network.connection.idle.IdleSender; @@ -58,7 +57,6 @@ import dorkbox.network.serialization.CryptoSerializationManager; import dorkbox.util.collections.LockFreeHashMap; import dorkbox.util.collections.LockFreeIntMap; import dorkbox.util.generics.ClassHelper; -import io.netty.bootstrap.DatagramCloseMessage; import io.netty.bootstrap.DatagramSessionChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler.Sharable; @@ -201,15 +199,6 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements CryptoConne if (channelWrapper.udp() != null) { count++; - - // we received a hint to close this channel from the remote end. - add(new OnMessageReceived() { - @Override - public - void received(final Connection connection, final DatagramCloseMessage message) { - connection.close(); - } - }); } // when closing this connection, HOW MANY endpoints need to be closed? @@ -570,6 +559,10 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements CryptoConne super.userEventTriggered(context, event); } + /** + * @param context can be NULL when running deferred messages from registration process. + * @param message the received message + */ @Override public void channelRead(ChannelHandlerContext context, Object message) throws Exception { diff --git a/src/dorkbox/network/connection/EndPoint.java b/src/dorkbox/network/connection/EndPoint.java index 49ba8075..d39df5e2 100644 --- a/src/dorkbox/network/connection/EndPoint.java +++ b/src/dorkbox/network/connection/EndPoint.java @@ -46,6 +46,7 @@ import dorkbox.util.Property; import dorkbox.util.crypto.CryptoECC; import dorkbox.util.entropy.Entropy; import dorkbox.util.exceptions.SecurityException; +import io.netty.bootstrap.DatagramCloseListener; import io.netty.channel.local.LocalAddress; import io.netty.util.NetUtil; @@ -261,6 +262,12 @@ class EndPoint extends Shutdownable { // 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/RegistrationWrapper.java b/src/dorkbox/network/connection/RegistrationWrapper.java index 4569e580..086dfc2a 100644 --- a/src/dorkbox/network/connection/RegistrationWrapper.java +++ b/src/dorkbox/network/connection/RegistrationWrapper.java @@ -267,8 +267,6 @@ class RegistrationWrapper { } /** - * For UDP, this map "exists forever" because we have to look up each session on inbound coms - *

* The session ID '0' is reserved to mean "no session ID yet" */ public diff --git a/src/io/netty/bootstrap/DatagramCloseListener.java b/src/io/netty/bootstrap/DatagramCloseListener.java new file mode 100644 index 00000000..060a2de3 --- /dev/null +++ b/src/io/netty/bootstrap/DatagramCloseListener.java @@ -0,0 +1,31 @@ +/* + * 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 io.netty.bootstrap; + +import dorkbox.network.connection.Connection; +import dorkbox.network.connection.Listener; + +/** + * Uses a hint to close remote UDP connections + */ +public +class DatagramCloseListener implements Listener.OnMessageReceived { + @Override + public + void received(final Connection connection, final DatagramCloseMessage message) { + connection.close(); + } +} diff --git a/src/io/netty/bootstrap/DatagramCloseMessage.java b/src/io/netty/bootstrap/DatagramCloseMessage.java index b656f639..24583aa6 100644 --- a/src/io/netty/bootstrap/DatagramCloseMessage.java +++ b/src/io/netty/bootstrap/DatagramCloseMessage.java @@ -16,7 +16,7 @@ package io.netty.bootstrap; /** - * + * Used as a hint to close remote UDP connections */ public class DatagramCloseMessage {}