use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class NioEndpoint method close.
public void close(boolean flushData) {
if (flushData) {
closing = true;
// Enqueue a close marker message to let the server
// know we should close
send(CLOSE_MARKER, false, true);
return;
}
try {
// Note: even though we may be disconnected from the socket.isConnected()
// standpoint, it's still safest to tell the kernel so that it can be sure
// to stop managing us gracefully.
kernel.closeEndpoint(this);
} catch (IOException e) {
throw new KernelException("Error closing endpoint for socket:" + socket, e);
}
}
use of com.jme3.network.Server 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.Server in project jmonkeyengine by jMonkeyEngine.
the class RpcHostedService method terminate.
/**
* Used internally to remove the message delegator from the
* server.
*/
@Override
public void terminate(HostedServiceManager serviceManager) {
Server server = serviceManager.getServer();
server.removeMessageListener(delegator, delegator.getMessageTypes());
}
use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.
the class DefaultServer method addChannel.
@Override
public int addChannel(int port) {
if (isRunning)
throw new IllegalStateException("Channels cannot be added once server is started.");
// Check for consistency with the channels list
if (channels.size() - CH_FIRST != alternatePorts.size())
throw new IllegalStateException("Channel and port lists do not match.");
try {
int result = alternatePorts.size();
alternatePorts.add(port);
Kernel kernel = kernelFactory.createKernel(result, port);
channels.add(new KernelAdapter(this, kernel, dispatcher, true));
return result;
} catch (IOException e) {
throw new RuntimeException("Error adding channel for port:" + port, e);
}
}
use of com.jme3.network.Server 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);
}
}
Aggregations