|
@@ -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,8 +188,18 @@ 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
|
|
@@ -208,6 +231,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);
|
|
|
|
|
|
@@ -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 ) {
|