Added a count-down latch to keep track of the connecting
state so that if a caller creates a client then immediately starts sending message, we can block until the connection is really established. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7044 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
a14df8a68f
commit
382ca44941
@ -59,6 +59,7 @@ public class DefaultClient implements Client
|
|||||||
|
|
||||||
private int id = -1;
|
private int id = -1;
|
||||||
private boolean isRunning = false;
|
private boolean isRunning = false;
|
||||||
|
private CountDownLatch connecting = new CountDownLatch(1);
|
||||||
private String gameName;
|
private String gameName;
|
||||||
private int version;
|
private int version;
|
||||||
private Connector reliable;
|
private Connector reliable;
|
||||||
@ -142,7 +143,7 @@ public class DefaultClient implements Client
|
|||||||
reg.setGameName(getGameName());
|
reg.setGameName(getGameName());
|
||||||
reg.setVersion(getVersion());
|
reg.setVersion(getVersion());
|
||||||
reg.setReliable(true);
|
reg.setReliable(true);
|
||||||
send(reg);
|
send(reg, false);
|
||||||
}
|
}
|
||||||
if( fast != null ) {
|
if( fast != null ) {
|
||||||
// We create two different ones to prepare for someday
|
// We create two different ones to prepare for someday
|
||||||
@ -150,13 +151,25 @@ public class DefaultClient implements Client
|
|||||||
reg = new ClientRegistrationMessage();
|
reg = new ClientRegistrationMessage();
|
||||||
reg.setId(tempId);
|
reg.setId(tempId);
|
||||||
reg.setReliable(false);
|
reg.setReliable(false);
|
||||||
send(reg);
|
send(reg, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void waitForConnected()
|
||||||
|
{
|
||||||
|
if( isConnected() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
connecting.await();
|
||||||
|
} catch( InterruptedException e ) {
|
||||||
|
throw new RuntimeException( "Interrupted waiting for connect", e );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected()
|
public boolean isConnected()
|
||||||
{
|
{
|
||||||
return id != -1; // for now
|
return id != -1 && isRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId()
|
public int getId()
|
||||||
@ -175,9 +188,19 @@ public class DefaultClient implements Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void send( Message message )
|
public void send( Message message )
|
||||||
|
{
|
||||||
|
send( message, true );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void send( Message message, boolean waitForConnected )
|
||||||
{
|
{
|
||||||
checkRunning();
|
checkRunning();
|
||||||
|
|
||||||
|
if( waitForConnected ) {
|
||||||
|
// Make sure we aren't still connecting
|
||||||
|
waitForConnected();
|
||||||
|
}
|
||||||
|
|
||||||
// For now just send direclty. We allocate our
|
// For now just send direclty. We allocate our
|
||||||
// own buffer each time because this method might
|
// own buffer each time because this method might
|
||||||
// be called from multiple threads. If writing
|
// be called from multiple threads. If writing
|
||||||
@ -209,6 +232,9 @@ public class DefaultClient implements Client
|
|||||||
|
|
||||||
// Wait for the threads?
|
// Wait for the threads?
|
||||||
|
|
||||||
|
// Just in case we never fully connected
|
||||||
|
connecting.countDown();
|
||||||
|
|
||||||
fireDisconnected(null);
|
fireDisconnected(null);
|
||||||
|
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
@ -266,6 +292,7 @@ public class DefaultClient implements Client
|
|||||||
// Then we've gotten our real id
|
// Then we've gotten our real id
|
||||||
this.id = (int)((ClientRegistrationMessage)m).getId();
|
this.id = (int)((ClientRegistrationMessage)m).getId();
|
||||||
log.log( Level.INFO, "Connection established, id:{0}.", this.id );
|
log.log( Level.INFO, "Connection established, id:{0}.", this.id );
|
||||||
|
connecting.countDown();
|
||||||
fireConnected();
|
fireConnected();
|
||||||
return;
|
return;
|
||||||
} if( m instanceof DisconnectMessage ) {
|
} if( m instanceof DisconnectMessage ) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user