From d1c8626419b32b2f1b6390d18e58fc5823b33c44 Mon Sep 17 00:00:00 2001 From: "PSp..om" Date: Thu, 17 Mar 2011 23:34:04 +0000 Subject: [PATCH] Better address binding. Deprecated the methods on the old Message class that are truly dangerous in the new version since they always return null. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7016 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../networking/com/jme3/network/Network.java | 16 +++++++++++--- .../network/kernel/tcp/SelectorKernel.java | 22 ++++++++++--------- .../jme3/network/kernel/udp/UdpKernel.java | 21 ++++++++++-------- .../com/jme3/network/message/Message.java | 4 ++++ 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/engine/src/networking/com/jme3/network/Network.java b/engine/src/networking/com/jme3/network/Network.java index 1cf48e876..906aec92b 100644 --- a/engine/src/networking/com/jme3/network/Network.java +++ b/engine/src/networking/com/jme3/network/Network.java @@ -57,11 +57,21 @@ public class Network * will be used for both TCP and UDP communication. */ public static Server createServer( int port ) throws IOException + { + return createServer( port, port ); + } + + /** + * Creates a Server that will utilize both reliable and fast + * transports to communicate with clients. The specified port + * will be used for both TCP and UDP communication. + */ + public static Server createServer( int tcpPort, int udpPort ) throws IOException { - InetAddress local = InetAddress.getLocalHost(); + //InetAddress local = InetAddress.getLocalHost(); - UdpKernel fast = new UdpKernel(local, port); - SelectorKernel reliable = new SelectorKernel(local,port); + UdpKernel fast = new UdpKernel(udpPort); + SelectorKernel reliable = new SelectorKernel(tcpPort); return new DefaultServer( reliable, fast ); } 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 be36add77..ba90d60fd 100644 --- a/engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java +++ b/engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java @@ -54,21 +54,24 @@ import com.jme3.network.kernel.*; */ public class SelectorKernel extends AbstractKernel { - private InetAddress host; - private int port; + private InetSocketAddress address; private SelectorThread thread; private Map endpoints = new ConcurrentHashMap(); public SelectorKernel( InetAddress host, int port ) { - this.host = host; - this.port = port; + this( new InetSocketAddress(host, port) ); } public SelectorKernel( int port ) throws IOException { - this( InetAddress.getLocalHost(), port ); + this( new InetSocketAddress(port) ); + } + + public SelectorKernel( InetSocketAddress address ) + { + this.address = address; } protected SelectorThread createSelectorThread() @@ -87,7 +90,7 @@ public class SelectorKernel extends AbstractKernel thread.connect(); thread.start(); } catch( IOException e ) { - throw new KernelException( "Error hosting:" + host + " port:" + port, e ); + throw new KernelException( "Error hosting:" + address, e ); } } @@ -100,7 +103,7 @@ public class SelectorKernel extends AbstractKernel thread.close(); thread = null; } catch( IOException e ) { - throw new KernelException( "Error closing host connection:" + host + " port:" + port, e ); + throw new KernelException( "Error closing host connection:" + address, e ); } } @@ -205,7 +208,7 @@ public class SelectorKernel extends AbstractKernel public SelectorThread() { - setName( "Selector@" + host + ":" + port ); + setName( "Selector@" + address ); setDaemon(true); } @@ -219,8 +222,7 @@ public class SelectorKernel extends AbstractKernel serverChannel.configureBlocking(false); // Bind the server socket to the specified address and port - InetSocketAddress isa = new InetSocketAddress(host, port); - serverChannel.socket().bind(isa); + serverChannel.socket().bind(address); // Register the server socket channel, indicating an interest in // accepting new connections 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 a16a2a40e..a80f139ef 100644 --- a/engine/src/networking/com/jme3/network/kernel/udp/UdpKernel.java +++ b/engine/src/networking/com/jme3/network/kernel/udp/UdpKernel.java @@ -49,8 +49,7 @@ import com.jme3.network.kernel.*; */ public class UdpKernel extends AbstractKernel { - private InetAddress host; - private int port; + private InetSocketAddress address; private HostThread thread; // The nature of UDP means that even through a firewall, @@ -60,13 +59,17 @@ public class UdpKernel extends AbstractKernel public UdpKernel( InetAddress host, int port ) { - this.host = host; - this.port = port; + this( new InetSocketAddress(host, port) ); } public UdpKernel( int port ) throws IOException { - this( InetAddress.getLocalHost(), port ); + this( new InetSocketAddress(port) ); + } + + public UdpKernel( InetSocketAddress address ) + { + this.address = address; } protected HostThread createHostThread() @@ -85,7 +88,7 @@ public class UdpKernel extends AbstractKernel thread.connect(); thread.start(); } catch( IOException e ) { - throw new KernelException( "Error hosting:" + host + " port:" + port, e ); + throw new KernelException( "Error hosting:" + address, e ); } } @@ -98,7 +101,7 @@ public class UdpKernel extends AbstractKernel thread.close(); thread = null; } catch( IOException e ) { - throw new KernelException( "Error closing host connection:" + host + " port:" + port, e ); + throw new KernelException( "Error closing host connection:" + address, e ); } } @@ -170,7 +173,7 @@ public class UdpKernel extends AbstractKernel public HostThread() { - setName( "UDP Host@" + host + ":" + port ); + setName( "UDP Host@" + address ); setDaemon(true); } @@ -181,7 +184,7 @@ public class UdpKernel extends AbstractKernel public void connect() throws IOException { - socket = new DatagramSocket( port, host ); + socket = new DatagramSocket( address ); } public void close() throws IOException, InterruptedException diff --git a/engine/src/networking/com/jme3/network/message/Message.java b/engine/src/networking/com/jme3/network/message/Message.java index e7dbf68df..b48989079 100644 --- a/engine/src/networking/com/jme3/network/message/Message.java +++ b/engine/src/networking/com/jme3/network/message/Message.java @@ -65,18 +65,22 @@ public class Message implements com.jme3.network.Message { return this; } + @Deprecated public Client getClient() { return connector; } + @Deprecated public void setClient(Client connector) { this.connector = connector; } + @Deprecated public Connection getConnection() { return connection; } + @Deprecated public void setConnection(Connection connection) { this.connection = connection; }