From 1f1c2ac15b99f11e41082b529ef35f9f3f7da469 Mon Sep 17 00:00:00 2001 From: "PSp..om" Date: Sat, 16 Apr 2011 01:54:51 +0000 Subject: [PATCH] Tightened up the DefaultServer connection close code to make sure that the connection closed change event only goes out once. Added some additional logging to the endpoint closings. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7244 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/network/base/DefaultServer.java | 36 ++++++++++++------- .../network/kernel/tcp/SelectorKernel.java | 2 ++ .../jme3/network/kernel/udp/UdpEndpoint.java | 2 +- .../jme3/network/kernel/udp/UdpKernel.java | 2 ++ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/engine/src/networking/com/jme3/network/base/DefaultServer.java b/engine/src/networking/com/jme3/network/base/DefaultServer.java index 077bfafb4..e5b88c297 100644 --- a/engine/src/networking/com/jme3/network/base/DefaultServer.java +++ b/engine/src/networking/com/jme3/network/base/DefaultServer.java @@ -358,18 +358,8 @@ public class DefaultServer implements Server if( removed != null ) { log.log( Level.INFO, "Client closed:{0}.", removed ); - - // Make sure both endpoints are closed. Note: reliable - // should always already be closed through all paths that I - // can conceive... but it doesn't hurt to be sure. - if( removed.reliable != null && removed.reliable.isConnected() ) { - removed.reliable.close(); - } - if( removed.fast != null && removed.fast.isConnected() ) { - removed.fast.close(); - } - - fireConnectionRemoved( removed ); + + removed.closeConnection(); } } @@ -378,6 +368,7 @@ public class DefaultServer implements Server private int id; private Endpoint reliable; private Endpoint fast; + private boolean closed; private Map sessionData = new ConcurrentHashMap(); @@ -410,6 +401,25 @@ public class DefaultServer implements Server fast.send( buffer ); } } + + protected void closeConnection() + { + if( closed ) + return; + closed = true; + + // Make sure both endpoints are closed. Note: reliable + // should always already be closed through all paths that I + // can conceive... but it doesn't hurt to be sure. + if( reliable != null && reliable.isConnected() ) { + reliable.close(); + } + if( fast != null && fast.isConnected() ) { + fast.close(); + } + + fireConnectionRemoved( this ); + } public void close( String reason ) { @@ -422,6 +432,8 @@ public class DefaultServer implements Server // Just close the reliable endpoint // fast will be cleaned up as a side-effect + // when closeConnection() is called by the + // connectionClosed() endpoint callback. if( reliable != null ) { // Close with flush so we make sure our // message gets out diff --git a/engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java b/engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java index 3a41ea74d..392f9b243 100644 --- a/engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java +++ b/engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java @@ -181,6 +181,8 @@ public class SelectorKernel extends AbstractKernel */ protected void closeEndpoint( NioEndpoint p ) throws IOException { + log.log( Level.INFO, "Closing endpoint:{0}.", p ); + thread.cancel(p); } diff --git a/engine/src/networking/com/jme3/network/kernel/udp/UdpEndpoint.java b/engine/src/networking/com/jme3/network/kernel/udp/UdpEndpoint.java index acb681cb4..872a7cb92 100644 --- a/engine/src/networking/com/jme3/network/kernel/udp/UdpEndpoint.java +++ b/engine/src/networking/com/jme3/network/kernel/udp/UdpEndpoint.java @@ -49,7 +49,7 @@ import com.jme3.network.kernel.*; */ public class UdpEndpoint implements Endpoint { - private long id; + private long id; private SocketAddress address; private DatagramSocket socket; private UdpKernel kernel; diff --git a/engine/src/networking/com/jme3/network/kernel/udp/UdpKernel.java b/engine/src/networking/com/jme3/network/kernel/udp/UdpKernel.java index 6a1669243..728d4c171 100644 --- a/engine/src/networking/com/jme3/network/kernel/udp/UdpKernel.java +++ b/engine/src/networking/com/jme3/network/kernel/udp/UdpKernel.java @@ -152,6 +152,8 @@ public class UdpKernel extends AbstractKernel */ protected void closeEndpoint( UdpEndpoint p ) throws IOException { + log.log( Level.INFO, "Closing endpoint:{0}.", p ); + // Just book-keeping to do here. socketEndpoints.remove( p.getRemoteAddress() );