use of com.esotericsoftware.kryonet.Server in project ProjektGG by eskalon.
the class BaseGameServer method startBroadcastServer.
private void startBroadcastServer() {
broadcastServer = new Server();
broadcastServer.start();
broadcastServer.getKryo().register(DiscoveryResponsePacket.class);
broadcastServer.setDiscoveryHandler(new ServerDiscoveryHandler() {
@Override
public boolean onDiscoverHost(DatagramChannel datagramChannel, InetSocketAddress fromAddress) throws IOException {
DiscoveryResponsePacket packet = new DiscoveryResponsePacket(serverSetup.getPort(), serverSetup.getGameName(), players.size(), serverSetup.getMaxPlayerCount());
ByteBuffer buffer = ByteBuffer.allocate(256);
broadcastServer.getSerialization().write(null, buffer, packet);
buffer.flip();
datagramChannel.send(buffer, fromAddress);
return true;
}
});
try {
broadcastServer.bind(0, UDP_DISCOVER_PORT);
Log.info("Server", "Broadcast server started");
} catch (IOException e1) {
Log.error("Server", "Broadcast server couldn't be started: %s", e1);
}
}
use of com.esotericsoftware.kryonet.Server in project ProjektGG by eskalon.
the class BaseGameServer method start.
/**
* Sets up a server asynchronously. After it is finished the callback is
* informed.
*
* @param callback
* the callback that is informed when the server is started.
*/
public void start(ISuccessCallback callback) {
Preconditions.checkNotNull(callback, "callback cannot be null.");
Log.info("Server", "--- Server is starting ---");
this.server = new Server();
this.server.start();
// ON NEW CONNECTION & ON DICONNECTED
this.server.addListener(new ConnectionListener() {
@Override
public void connected(Connection con) {
onClientConnected(con);
}
@Override
public void disconnected(Connection con) {
onClientDisconnected(con);
}
});
TypeListener typeListener = new TypeListener();
typeListener.addTypeHandler(ClientHandshakeRequest.class, (con, msg) -> onClientHandshake(con, msg));
server.addListener(typeListener);
onCreation();
ThreadHandler.getInstance().executeRunnable(() -> {
try {
// Start the server
server.bind(serverSetup.getPort());
Log.info("Server", "Server started");
// Create & start the broadcast server
if (serverSetup.isPublic()) {
startBroadcastServer();
}
// Host successfully started
callback.onSuccess(null);
} catch (IOException | IllegalArgumentException e2) {
Log.error("Server", "Server could not be started: %s", e2);
// Something went wrong
callback.onFailure(e2);
}
});
}
Aggregations