Parcourir la source

Real fix for the client UDP problems and removed my long
winded and erroneous comment now that we have point-to-point
connections again.
Also committing the other classes to which I started adding
game name and version... unfinished but needed for the other
fix.


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7038 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

PSp..om il y a 14 ans
Parent
commit
dda3e4c2a0

+ 36 - 10
engine/src/networking/com/jme3/network/Network.java

@@ -58,7 +58,7 @@ public class Network
      */
     public static Server createServer( int port ) throws IOException
     {   
-        return createServer( port, port );
+        return createServer( "Unnamed Game", 42, port, port );
     }
 
     /**
@@ -68,12 +68,29 @@ public class Network
      */
     public static Server createServer( int tcpPort, int udpPort ) throws IOException
     {
-        //InetAddress local = InetAddress.getLocalHost();
-        
+        return createServer( "Unnamed Game", 42, tcpPort, udpPort );
+    }
+
+    /**
+     *  Creates a named and versioned 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.
+     *
+     *  @param gameName This is the name that identifies the game.  Connecting clients
+     *                  must use this name or be turned away.
+     *  @param gersion  This is a game-specific verison that helps detect when out-of-date
+     *                  clients have connected to an incompatible server.
+     *  @param tcpPort  The port upon which the TCP hosting will listen for new connections.
+     *  @param udpPort  The port upon which the UDP hosting will listen for new 'fast' UDP 
+     *                  messages.
+     */
+    public static Server createServer( String gameName, int version, int tcpPort, int udpPort ) throws IOException
+    {
         UdpKernel fast = new UdpKernel(udpPort);
+        //UdpKernel fast = new UdpKernel( InetAddress.getByName("localhost"), udpPort);
         SelectorKernel reliable = new SelectorKernel(tcpPort);
  
-        return new DefaultServer( reliable, fast );       
+        return new DefaultServer( gameName, version, reliable, fast );       
     }
     
     /**
@@ -81,7 +98,17 @@ public class Network
      */
     public static NetworkClient createClient()
     {
-        return new NetworkClientImpl();
+        return createClient( "Unnamed Game", 42 );
+    }     
+
+    /**
+     *  Creates a client that can be connected at a later time.  The specified
+     *  game name and version must match the server or the client will be turned
+     *  away.
+     */
+    public static NetworkClient createClient( String gameName, int version )
+    {
+        return new NetworkClientImpl(gameName, version);
     }     
     
     /**
@@ -123,8 +150,7 @@ public class Network
     public static Client connectToServer( InetAddress address, int port, int remoteUdpPort, 
                                           int localUdpPort ) throws IOException
     {
-        InetAddress local = InetAddress.getLocalHost();
-        UdpConnector fast = new UdpConnector( local, localUdpPort, address, port ); 
+        UdpConnector fast = new UdpConnector( localUdpPort, address, port ); 
         SocketConnector reliable = new SocketConnector( address, port );        
        
         return new DefaultClient( reliable, fast );
@@ -132,8 +158,9 @@ public class Network
  
     protected static class NetworkClientImpl extends DefaultClient implements NetworkClient
     {
-        public NetworkClientImpl()
+        public NetworkClientImpl(String gameName, int version)
         {
+            super( gameName, version );
         }
         
         public void connectToServer( String host, int port, int remoteUdpPort, 
@@ -145,8 +172,7 @@ public class Network
         public void connectToServer( InetAddress address, int port, int remoteUdpPort, 
                                      int localUdpPort ) throws IOException
         {
-            InetAddress local = InetAddress.getLocalHost();
-            UdpConnector fast = new UdpConnector( local, localUdpPort, address, port ); 
+            UdpConnector fast = new UdpConnector( localUdpPort, address, port ); 
             SocketConnector reliable = new SocketConnector( address, port );        
             
             setConnectors( reliable, fast );

+ 12 - 0
engine/src/networking/com/jme3/network/Server.java

@@ -43,6 +43,18 @@ import java.util.Collection;
  */
 public interface Server
 {
+    /**
+     *  Returns the 'game name' for this server.  This should match the
+     *  'game name' set on connecting clients or they will be turned away.
+     */
+    public String getGameName();
+ 
+    /**
+     *  Returns the game-specific version of this server used for detecting
+     *  mismatched clients.
+     */   
+    public int getVersion();
+
     /**
      *  Sends the specified message to all connected clients.
      */ 

+ 5 - 1
engine/src/networking/com/jme3/network/base/DefaultClient.java

@@ -57,6 +57,8 @@ public class DefaultClient implements Client
     
     private int id = -1;
     private boolean isRunning = false;
+    private String gameName;
+    private int version;
     private Connector reliable;
     private Connector fast;
     private MessageListenerRegistry<Client> messageListeners = new MessageListenerRegistry<Client>();
@@ -65,8 +67,10 @@ public class DefaultClient implements Client
     private ConnectorAdapter reliableAdapter;    
     private ConnectorAdapter fastAdapter;    
     
-    public DefaultClient()
+    public DefaultClient( String gameName, int version )
     {
+        this.gameName = gameName;
+        this.version = version;
     }
     
     public DefaultClient( Connector reliable, Connector fast )

+ 15 - 1
engine/src/networking/com/jme3/network/base/DefaultServer.java

@@ -59,6 +59,8 @@ public class DefaultServer implements Server
     
     private boolean isRunning = false;
     private AtomicInteger nextId = new AtomicInteger(0);
+    private String gameName;
+    private int version;
     private Kernel reliable;
     private KernelAdapter reliableAdapter;
     private Kernel fast;
@@ -76,8 +78,10 @@ public class DefaultServer implements Server
                             = new MessageListenerRegistry<HostedConnection>();                        
     private List<ConnectionListener> connectionListeners = new CopyOnWriteArrayList<ConnectionListener>();
     
-    public DefaultServer( Kernel reliable, Kernel fast )
+    public DefaultServer( String gameName, int version, Kernel reliable, Kernel fast )
     {
+        this.gameName = gameName;
+        this.version = version;
         this.reliable = reliable;
         this.fast = fast;
         
@@ -89,6 +93,16 @@ public class DefaultServer implements Server
         }
     }   
 
+    public String getGameName()
+    {
+        return gameName;
+    }
+
+    public int getVersion()
+    {
+        return version;
+    }
+
     public void start()
     {
         if( isRunning )

+ 1 - 17
engine/src/networking/com/jme3/network/kernel/udp/UdpConnector.java

@@ -64,30 +64,14 @@ public class UdpConnector implements Connector
      *  Creates a new UDP connection that send datagrams to the
      *  specified address and port.
      */
-    public UdpConnector( InetAddress local, int localPort, 
-                         InetAddress remote, int remotePort ) throws IOException
+    public UdpConnector( int localPort, InetAddress remote, int remotePort ) throws IOException
     {
         InetSocketAddress localSocketAddress = new InetSocketAddress(localPort);
         this.sock = new DatagramSocket( localSocketAddress );
-        //this.sock = new DatagramSocket( localPort, local );
         remoteAddress = new InetSocketAddress( remote, remotePort );
         
         // Setup to receive only from the remote address
         sock.connect( remoteAddress );
-        //
-        // The above is a really nice idea since it means that we
-        // wouldn't get random datagram packets from anything that
-        // happened to send garbage to our UDP port.  The problem is
-        // when connecting to a server at "localhost" because "localhost"
-        // will/should always resolve to 127.0.0.1... but the server
-        // doesn't send packets from 127.0.0.1 because it's listening
-        // on the real interface.  And that doesn't match and this end
-        // rejects them.
-        //
-        // This means at some point the read() code will need to validate
-        // that the packets came from the real server before parsing them.
-        // Otherwise we likely throw random deserialization errors and kill
-        // the client.  Which may or may not warrant extra code below.  <shrug>
  
         connected.set(true);
     }