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
This commit is contained in:
parent
17ecd6f0c9
commit
dda3e4c2a0
@ -58,7 +58,7 @@ public class Network
|
|||||||
*/
|
*/
|
||||||
public static Server createServer( int port ) throws IOException
|
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
|
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(udpPort);
|
||||||
|
//UdpKernel fast = new UdpKernel( InetAddress.getByName("localhost"), udpPort);
|
||||||
SelectorKernel reliable = new SelectorKernel(tcpPort);
|
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()
|
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,
|
public static Client connectToServer( InetAddress address, int port, int remoteUdpPort,
|
||||||
int localUdpPort ) throws IOException
|
int localUdpPort ) throws IOException
|
||||||
{
|
{
|
||||||
InetAddress local = InetAddress.getLocalHost();
|
UdpConnector fast = new UdpConnector( localUdpPort, address, port );
|
||||||
UdpConnector fast = new UdpConnector( local, localUdpPort, address, port );
|
|
||||||
SocketConnector reliable = new SocketConnector( address, port );
|
SocketConnector reliable = new SocketConnector( address, port );
|
||||||
|
|
||||||
return new DefaultClient( reliable, fast );
|
return new DefaultClient( reliable, fast );
|
||||||
@ -132,8 +158,9 @@ public class Network
|
|||||||
|
|
||||||
protected static class NetworkClientImpl extends DefaultClient implements NetworkClient
|
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,
|
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,
|
public void connectToServer( InetAddress address, int port, int remoteUdpPort,
|
||||||
int localUdpPort ) throws IOException
|
int localUdpPort ) throws IOException
|
||||||
{
|
{
|
||||||
InetAddress local = InetAddress.getLocalHost();
|
UdpConnector fast = new UdpConnector( localUdpPort, address, port );
|
||||||
UdpConnector fast = new UdpConnector( local, localUdpPort, address, port );
|
|
||||||
SocketConnector reliable = new SocketConnector( address, port );
|
SocketConnector reliable = new SocketConnector( address, port );
|
||||||
|
|
||||||
setConnectors( reliable, fast );
|
setConnectors( reliable, fast );
|
||||||
|
@ -43,6 +43,18 @@ import java.util.Collection;
|
|||||||
*/
|
*/
|
||||||
public interface Server
|
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.
|
* Sends the specified message to all connected clients.
|
||||||
*/
|
*/
|
||||||
|
@ -57,6 +57,8 @@ public class DefaultClient implements Client
|
|||||||
|
|
||||||
private int id = -1;
|
private int id = -1;
|
||||||
private boolean isRunning = false;
|
private boolean isRunning = false;
|
||||||
|
private String gameName;
|
||||||
|
private int version;
|
||||||
private Connector reliable;
|
private Connector reliable;
|
||||||
private Connector fast;
|
private Connector fast;
|
||||||
private MessageListenerRegistry<Client> messageListeners = new MessageListenerRegistry<Client>();
|
private MessageListenerRegistry<Client> messageListeners = new MessageListenerRegistry<Client>();
|
||||||
@ -65,8 +67,10 @@ public class DefaultClient implements Client
|
|||||||
private ConnectorAdapter reliableAdapter;
|
private ConnectorAdapter reliableAdapter;
|
||||||
private ConnectorAdapter fastAdapter;
|
private ConnectorAdapter fastAdapter;
|
||||||
|
|
||||||
public DefaultClient()
|
public DefaultClient( String gameName, int version )
|
||||||
{
|
{
|
||||||
|
this.gameName = gameName;
|
||||||
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultClient( Connector reliable, Connector fast )
|
public DefaultClient( Connector reliable, Connector fast )
|
||||||
|
@ -59,6 +59,8 @@ public class DefaultServer implements Server
|
|||||||
|
|
||||||
private boolean isRunning = false;
|
private boolean isRunning = false;
|
||||||
private AtomicInteger nextId = new AtomicInteger(0);
|
private AtomicInteger nextId = new AtomicInteger(0);
|
||||||
|
private String gameName;
|
||||||
|
private int version;
|
||||||
private Kernel reliable;
|
private Kernel reliable;
|
||||||
private KernelAdapter reliableAdapter;
|
private KernelAdapter reliableAdapter;
|
||||||
private Kernel fast;
|
private Kernel fast;
|
||||||
@ -76,8 +78,10 @@ public class DefaultServer implements Server
|
|||||||
= new MessageListenerRegistry<HostedConnection>();
|
= new MessageListenerRegistry<HostedConnection>();
|
||||||
private List<ConnectionListener> connectionListeners = new CopyOnWriteArrayList<ConnectionListener>();
|
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.reliable = reliable;
|
||||||
this.fast = fast;
|
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()
|
public void start()
|
||||||
{
|
{
|
||||||
if( isRunning )
|
if( isRunning )
|
||||||
|
@ -64,30 +64,14 @@ public class UdpConnector implements Connector
|
|||||||
* Creates a new UDP connection that send datagrams to the
|
* Creates a new UDP connection that send datagrams to the
|
||||||
* specified address and port.
|
* specified address and port.
|
||||||
*/
|
*/
|
||||||
public UdpConnector( InetAddress local, int localPort,
|
public UdpConnector( int localPort, InetAddress remote, int remotePort ) throws IOException
|
||||||
InetAddress remote, int remotePort ) throws IOException
|
|
||||||
{
|
{
|
||||||
InetSocketAddress localSocketAddress = new InetSocketAddress(localPort);
|
InetSocketAddress localSocketAddress = new InetSocketAddress(localPort);
|
||||||
this.sock = new DatagramSocket( localSocketAddress );
|
this.sock = new DatagramSocket( localSocketAddress );
|
||||||
//this.sock = new DatagramSocket( localPort, local );
|
|
||||||
remoteAddress = new InetSocketAddress( remote, remotePort );
|
remoteAddress = new InetSocketAddress( remote, remotePort );
|
||||||
|
|
||||||
// Setup to receive only from the remote address
|
// Setup to receive only from the remote address
|
||||||
sock.connect( remoteAddress );
|
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);
|
connected.set(true);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user