use of com.jme3.network.Client in project jmonkeyengine by jMonkeyEngine.
the class DefaultServer method registerClient.
protected void registerClient(KernelAdapter ka, Endpoint p, ClientRegistrationMessage m) {
Connection addedConnection = null;
// important enough I won't take chances
synchronized (this) {
// Grab the random ID that the client created when creating
// its two registration messages
long tempId = m.getId();
// See if we already have one
Connection c = connecting.remove(tempId);
if (c == null) {
c = new Connection(channels.size());
log.log(Level.FINE, "Registering client for endpoint, pass 1:{0}.", p);
} else {
log.log(Level.FINE, "Refining client registration for endpoint:{0}.", p);
}
// Fill in what we now know
int channel = getChannel(ka);
c.setChannel(channel, p);
log.log(Level.FINE, "Setting up channel:{0}", channel);
// and we will send the connection information
if (channel == CH_RELIABLE) {
// over the reliable connection at this point.
if (!getGameName().equals(m.getGameName()) || getVersion() != m.getVersion()) {
log.log(Level.FINE, "Kicking client due to name/version mismatch:{0}.", c);
// Need to kick them off... I may regret doing this from within
// the sync block but the alternative is more code
c.close("Server client mismatch, server:" + getGameName() + " v" + getVersion() + " client:" + m.getGameName() + " v" + m.getVersion());
return;
}
// Else send the extra channel information to the client
if (!alternatePorts.isEmpty()) {
ChannelInfoMessage cim = new ChannelInfoMessage(m.getId(), alternatePorts);
c.send(cim);
}
}
if (c.isComplete()) {
// Then we are fully connected
if (connections.put(c.getId(), c) == null) {
for (Endpoint cp : c.channels) {
if (cp == null)
continue;
endpointConnections.put(cp, c);
}
addedConnection = c;
}
} else {
// Need to keep getting channels so we'll keep it in
// the map
connecting.put(tempId, c);
}
}
// over synchronizing which is the path to deadlocks
if (addedConnection != null) {
log.log(Level.FINE, "Client registered:{0}.", addedConnection);
// Send the ID back to the client letting it know it's
// fully connected.
m = new ClientRegistrationMessage();
m.setId(addedConnection.getId());
m.setReliable(true);
addedConnection.send(m);
// Now we can notify the listeners about the
// new connection.
fireConnectionAdded(addedConnection);
// Send a second registration message with an invalid ID
// to let the connection know that it can start its services
m = new ClientRegistrationMessage();
m.setId(-1);
m.setReliable(true);
addedConnection.send(m);
}
}
use of com.jme3.network.Client in project jmonkeyengine by jMonkeyEngine.
the class RpcHostedService method onInitialize.
/**
* Used internally to setup the message delegator that will
* handle HostedConnection specific messages and forward them
* to that connection's RpcConnection.
*/
@Override
protected void onInitialize(HostedServiceManager serviceManager) {
Server server = serviceManager.getServer();
// A general listener for forwarding the messages
// to the client-specific handler
this.delegator = new SessionDataDelegator(RpcConnection.class, ATTRIBUTE_NAME, true);
server.addMessageListener(delegator, delegator.getMessageTypes());
if (log.isLoggable(Level.FINEST)) {
log.log(Level.FINEST, "Registered delegator for message types:{0}", Arrays.asList(delegator.getMessageTypes()));
}
}
use of com.jme3.network.Client in project jmonkeyengine by jMonkeyEngine.
the class TestRemoteCall method main.
public static void main(String[] args) throws IOException, InterruptedException {
Serializer.registerClass(Savable.class, new SavableSerializer());
createServer();
Client client = Network.connectToServer("localhost", 5110);
client.start();
ObjectStore store = new ObjectStore(client);
ServerAccess access = store.getExposedObject("access", ServerAccess.class, true);
boolean result = access.attachChild("Models/Oto/Oto.mesh.xml");
System.out.println(result);
}
use of com.jme3.network.Client 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.Client 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);
}
}
Aggregations