From 57dbf384a260aaa83903e76f21f38d87ec997114 Mon Sep 17 00:00:00 2001 From: Paul Speed Date: Fri, 1 May 2015 02:15:41 -0400 Subject: [PATCH] Modified the DefaultServer to send a second client info message to indicate that all of the local hosted services have been notified about the new connection. Modified DefaultClient to wait to start its services until it has seen this second message. Client services may want to send things to the server during their start() method but it's important that things like the serializer registry service has already processed its messages or any sends might fail. The client generally has the luxury of being able to register handlers/listeners/etc during initialize where as the server must do this when the connection arrives. So it seems reasonable to delay client service start() until all of the server-side hosted services have had a chance to initialize themselves. --- .../com/jme3/network/base/DefaultClient.java | 31 ++++++++++++++----- .../com/jme3/network/base/DefaultServer.java | 9 +++++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/jme3-networking/src/main/java/com/jme3/network/base/DefaultClient.java b/jme3-networking/src/main/java/com/jme3/network/base/DefaultClient.java index c0cc2e616..b297a933b 100644 --- a/jme3-networking/src/main/java/com/jme3/network/base/DefaultClient.java +++ b/jme3-networking/src/main/java/com/jme3/network/base/DefaultClient.java @@ -177,6 +177,10 @@ public class DefaultClient implements Client continue; send(ch, reg, false); } + } + + public boolean isStarted() { + return isRunning; } protected void waitForConnected() @@ -351,13 +355,16 @@ public class DefaultClient implements Client protected void fireConnected() { - // Let the services know we are finally started - services.start(); - for( ClientStateListener l : stateListeners ) { l.clientConnected( this ); } } + + protected void startServices() + { + // Let the services know we are finally started + services.start(); + } protected void fireDisconnected( DisconnectInfo info ) { @@ -416,11 +423,19 @@ public class DefaultClient implements Client // Pull off the connection management messages we're // interested in and then pass on the rest. if( m instanceof ClientRegistrationMessage ) { - // Then we've gotten our real id - this.id = (int)((ClientRegistrationMessage)m).getId(); - log.log( Level.FINE, "Connection established, id:{0}.", this.id ); - connecting.countDown(); - fireConnected(); + ClientRegistrationMessage crm = (ClientRegistrationMessage)m; + // See if it has a real ID + if( crm.getId() >= 0 ) { + // Then we've gotten our real id + this.id = (int)crm.getId(); + log.log( Level.FINE, "Connection established, id:{0}.", this.id ); + connecting.countDown(); + fireConnected(); + } else { + // Else it's a message letting us know that the + // hosted services have been started + startServices(); + } return; } else if( m instanceof ChannelInfoMessage ) { // This is an interum step in the connection process and diff --git a/jme3-networking/src/main/java/com/jme3/network/base/DefaultServer.java b/jme3-networking/src/main/java/com/jme3/network/base/DefaultServer.java index 0a9ac0ef1..97e36134e 100644 --- a/jme3-networking/src/main/java/com/jme3/network/base/DefaultServer.java +++ b/jme3-networking/src/main/java/com/jme3/network/base/DefaultServer.java @@ -412,7 +412,14 @@ public class DefaultServer implements Server // Now we can notify the listeners about the // new connection. - fireConnectionAdded( addedConnection ); + fireConnectionAdded( addedConnection ); + + // Send a second registration message with an invalid ID + // to let the connection know that it can start its services + m = new ClientRegistrationMessage(); + m.setId(-1); + m.setReliable(true); + addedConnection.send(m); } }