From a2a39bd0d56998a20e1df79c521df64443dad270 Mon Sep 17 00:00:00 2001 From: "PSp..om" Date: Fri, 18 Oct 2013 03:08:29 +0000 Subject: [PATCH] Added a connected check to DefaultClient's channel-based send method. Even though it delegates to a method that will wait for being connected, send(channel, msg) can't properly do its channel verification until the connection is fully setup. So now it waits. I also went ahead and added constants for the normal default (previouslly hidden) channels that are used during normal send(msg). Useful for code that wants to deal with channels genericly but still wants to be able to use the default channels. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10837 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/network/MessageConnection.java | 26 +++++++++++++++++++ .../com/jme3/network/base/DefaultClient.java | 14 ++++++++-- .../com/jme3/network/base/DefaultServer.java | 6 +++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/engine/src/networking/com/jme3/network/MessageConnection.java b/engine/src/networking/com/jme3/network/MessageConnection.java index ee444a7e9..6c88636dc 100644 --- a/engine/src/networking/com/jme3/network/MessageConnection.java +++ b/engine/src/networking/com/jme3/network/MessageConnection.java @@ -41,6 +41,32 @@ package com.jme3.network; */ public interface MessageConnection { + /** + * Indicates the default reliable channel that is used + * when calling the channel-less send() with a reliable + * message. This channel number can be used in the send(channel, msg) + * version of send. + * + *

Normally, callers should just call the regular non-channel + * send message but these channel numbers are useful for extensions + * that allow the user to specify a channel and want to still + * support the default channels.

+ */ + public static final int CHANNEL_DEFAULT_RELIABLE = -2; + + /** + * Indicates the default unreliable channel that is used + * when calling the channel-less send() with a reliable=false + * message. This channel number can be used in the send(channel, msg) + * version of send. + * + *

Normally, callers should just call the regular non-channel + * send message but these channel numbers are useful for extensions + * that allow the user to specify a channel and want to still + * support the default channels.

+ */ + public static final int CHANNEL_DEFAULT_UNRELIABLE = -1; + /** * Sends a message to the other end of the connection. */ diff --git a/engine/src/networking/com/jme3/network/base/DefaultClient.java b/engine/src/networking/com/jme3/network/base/DefaultClient.java index ed3a8c200..54e8fd7f9 100644 --- a/engine/src/networking/com/jme3/network/base/DefaultClient.java +++ b/engine/src/networking/com/jme3/network/base/DefaultClient.java @@ -212,9 +212,19 @@ public class DefaultClient implements Client public void send( int channel, Message message ) { - if( channel < 0 || channel + CH_FIRST >= channels.size() ) + if( channel >= 0 ) { + // Make sure we aren't still connecting. Channels + // won't be valid until we are fully connected since + // we receive the channel list from the server. + // The default channels don't require the connection + // to be fully up before sending. + waitForConnected(); + } + + if( channel < CHANNEL_DEFAULT_RELIABLE || channel + CH_FIRST >= channels.size() ) { throw new IllegalArgumentException( "Channel is undefined:" + channel ); - send( channel + CH_FIRST, message, true ); + } + send(channel + CH_FIRST, message, true); } protected void send( int channel, Message message, boolean waitForConnected ) diff --git a/engine/src/networking/com/jme3/network/base/DefaultServer.java b/engine/src/networking/com/jme3/network/base/DefaultServer.java index 2f43a351e..3816fbace 100644 --- a/engine/src/networking/com/jme3/network/base/DefaultServer.java +++ b/engine/src/networking/com/jme3/network/base/DefaultServer.java @@ -143,8 +143,10 @@ public class DefaultServer implements Server protected void checkChannel( int channel ) { - if( channel < 0 || channel >= alternatePorts.size() ) - throw new IllegalArgumentException( "Channel is undefined:" + channel ); + if( channel < MessageConnection.CHANNEL_DEFAULT_RELIABLE + || channel >= alternatePorts.size() ) { + throw new IllegalArgumentException( "Channel is undefined:" + channel ); + } } public void start()