use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class TestRemoteCall method createServer.
public static void createServer() {
serverApp = new SimpleApplication() {
@Override
public void simpleInitApp() {
}
};
serverApp.start();
try {
Server server = Network.createServer(5110);
server.start();
ObjectStore store = new ObjectStore(server);
store.exposeObject("access", new ServerAccessImpl());
} catch (IOException ex) {
ex.printStackTrace();
}
}
use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class Network method connectToServer.
/**
* Creates a Client that communicates with the specified host and and separate TCP and UDP ports
* using both reliable and fast transports.
*
* @param gameName This is the name that identifies the game. This must match
* the target server's name or this client will be turned away.
* @param version This is a game-specific verison that helps detect when out-of-date
* clients have connected to an incompatible server. This must match
* the server's version of this client will be turned away.
* @param hostPort The remote TCP port on the server to which this client should
* send reliable messages.
* @param remoteUdpPort The remote UDP port on the server to which this client should
* send 'fast'/unreliable messages. Set to -1 if 'fast' traffic should
* go over TCP. This will completely disable UDP traffic for this
* client.
*/
public static Client connectToServer(String gameName, int version, String host, int hostPort, int remoteUdpPort) throws IOException {
InetAddress remoteAddress = InetAddress.getByName(host);
UdpConnector fast = remoteUdpPort == -1 ? null : new UdpConnector(remoteAddress, remoteUdpPort);
SocketConnector reliable = new SocketConnector(remoteAddress, hostPort);
return new DefaultClient(gameName, version, reliable, fast, new TcpConnectorFactory(remoteAddress));
}
use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class Network method createServer.
/**
* 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 version 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. Set to -1 if 'fast' traffic should go over TCP. This will
* completely disable UDP traffic for this server.
*/
public static Server createServer(String gameName, int version, int tcpPort, int udpPort) throws IOException {
UdpKernel fast = udpPort == -1 ? null : new UdpKernel(udpPort);
SelectorKernel reliable = new SelectorKernel(tcpPort);
return new DefaultServer(gameName, version, reliable, fast);
}
use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class DefaultClient method start.
@Override
public void start() {
if (isRunning)
throw new IllegalStateException("Client is already started.");
// connectors that we have
for (ConnectorAdapter ca : channels) {
if (ca == null)
continue;
ca.start();
}
// Send our connection message with a generated ID until
// we get one back from the server. We'll hash time in
// millis and time in nanos.
// This is used to match the TCP and UDP endpoints up on the
// other end since they may take different routes to get there.
// Behind NAT, many game clients may be coming over the same
// IP address from the server's perspective and they may have
// their UDP ports mapped all over the place.
//
// Since currentTimeMillis() is absolute time and nano time
// is roughtly related to system start time, adding these two
// together should be plenty unique for our purposes. It wouldn't
// hurt to reconcile with IP on the server side, though.
long tempId = System.currentTimeMillis() + System.nanoTime();
// Set it true here so we can send some messages.
isRunning = true;
ClientRegistrationMessage reg;
reg = new ClientRegistrationMessage();
reg.setId(tempId);
reg.setGameName(getGameName());
reg.setVersion(getVersion());
reg.setReliable(true);
send(CH_RELIABLE, reg, false);
// Send registration messages to any other configured
// connectors
reg = new ClientRegistrationMessage();
reg.setId(tempId);
reg.setReliable(false);
for (int ch = CH_UNRELIABLE; ch < channels.size(); ch++) {
if (channels.get(ch) == null)
continue;
send(ch, reg, false);
}
}
use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class KernelAdapter method dispatch.
/**
* Note on threading for those writing their own server
* or adapter implementations. The rule that a single connection be
* processed by only one thread at a time is more about ensuring that
* the messages are delivered in the order that they are received
* than for any user-code safety. 99% of the time the user code should
* be writing for multithreaded access anyway.
*
* <p>The issue with the messages is that if a an implementation is
* using a general thread pool then it would be possible for a
* naive implementation to have one thread grab an Envelope from
* connection 1's and another grab the next Envelope. Since an Envelope
* may contain several messages, delivering the second thread's messages
* before or during the first's would be really confusing and hard
* to code for in user code.</p>
*
* <p>And that's why this note is here. DefaultServer does a rudimentary
* per-connection locking but it couldn't possibly guard against
* out of order Envelope processing.</p>
*/
protected void dispatch(Endpoint p, Message m) {
// here.
if (m instanceof ClientRegistrationMessage) {
server.registerClient(this, p, (ClientRegistrationMessage) m);
return;
}
try {
HostedConnection source = getConnection(p);
if (source == null) {
if (reliable) {
// If it's a reliable connection then it's slightly more
// concerning but this can happen all the time for a UDP endpoint.
log.log(Level.WARNING, "Recieved message from unconnected endpoint:" + p + " message:" + m);
}
return;
}
messageDispatcher.messageReceived(source, m);
} catch (Exception e) {
reportError(p, m, e);
}
}
Aggregations