Archived
0

Bridge: при потере соединения - восстанавливаем связь

This commit is contained in:
2017-05-12 23:12:36 +03:00
parent 8d31c2142d
commit affa8c62c2
3 changed files with 45 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys'
version = '0.5-SNAPSHOT'
version = '0.5.1-SNAPSHOT'
repositories {
maven { url 'https://hub.spigotmc.org/nexus/content/groups/public/' }

View File

@@ -28,19 +28,7 @@ public class BridgePlugin extends JavaPlugin {
INSTANCE = this;
saveDefaultConfig();
client = new Client();
ses = Executors.newScheduledThreadPool(2);
sesFuture = ses.scheduleAtFixedRate(() -> {
getLogger().info(String.format("Connect(%d) to ASys...", ++tryConnect));
client.connect(getConfig().getString("host"), getConfig().getInt("port"));
if (client.isConnected()) {
sesFuture.cancel(false);
sesFuture = null;
} else {
getLogger().warning(
String.format("Connection(%d) fail. Try reconnect...", tryConnect));
}
}, 0L, 5L, TimeUnit.SECONDS);
startReconnect();
}
@Override
@@ -65,12 +53,47 @@ public class BridgePlugin extends JavaPlugin {
return true;
}
public void startReconnect() {
client = new Client();
ses = Executors.newScheduledThreadPool(2);
sesFuture = ses.scheduleAtFixedRate(() -> {
getLogger().info(String.format("Connect(%d) to ASys...", ++tryConnect));
client.connect(getConfig().getString("host"), getConfig().getInt("port"));
if (client.isConnected()) {
stopReconnect();
} else {
getLogger().warning(
String.format("Connection(%d) fail. Try reconnect...", tryConnect));
}
}, 0L, 5L, TimeUnit.SECONDS);
}
public void stopReconnect() {
if (sesFuture != null) {
sesFuture.cancel(false);
sesFuture = null;
tryConnect = 0;
}
}
public void startPing(Channel channel) {
sesPingFuture = ses.scheduleAtFixedRate(() -> channel.writeAndFlush(new CS_Ping(
System.currentTimeMillis(),
20.0D, //FIXME
BridgePlugin.INSTANCE.getServer().getOnlinePlayers().size()
)), 0L, 5L, TimeUnit.SECONDS);
sesPingFuture = ses.scheduleAtFixedRate(() -> {
channel.write(new CS_Ping(
System.currentTimeMillis(),
20.0D, //FIXME
getServer().getOnlinePlayers().size()
));
if (channel.isWritable()) {
channel.flush();
} else {
getLogger().warning("Lost connection!");
channel.close();
stopPing();
getLogger().warning("Try reconnect...");
startReconnect();
}
}, 0L, 5L, TimeUnit.SECONDS);
}
public void stopPing() {

View File

@@ -53,8 +53,10 @@ public class ClientPacketHandler extends ChannelInboundHandlerAdapter implements
@Override
public void channelInactive(ChannelHandlerContext context) throws Exception {
if (BridgePlugin.INSTANCE != null) {
BridgePlugin.INSTANCE.getLogger().info("channelInactive");
BridgePlugin.INSTANCE.getLogger().warning("Lost connection!");
BridgePlugin.INSTANCE.stopPing();
BridgePlugin.INSTANCE.getLogger().warning("Try reconnect...");
BridgePlugin.INSTANCE.startReconnect();
}
context.channel().attr(KNOWN_PACKETS).remove();
context.channel().attr(KNOWN_HANDLERS).remove();