Can be just inlined, not attached:
Code: Select all
########################################################
# This plugin is licensed under the GNU GPL #
# Copyright 2012 by Thrice #
# Contact : - dthrice <AT> *NOSPAM* G*MAIL <DOT> COM #
# #
# ---------------------------------------------------- #
# ---------------------------------------------------- #
# koreping.pl #
# Pings server and enables you to dc / relog on #
# high ping #
########################################################
package koreping;
use strict;
use Plugins;
use Log qw(message);
use Globals;
use Misc qw(relog);
use Utils;
use lib $Plugins::current_plugin_folder;
use koreping::External qw(ping);
Plugins::register("koreping", "koreping", \&on_unload, \&on_reload);
my $hook = Plugins::addHook('mainLoop_pre', \&pingcheck, undef);
my $ID = Commands::register(
["ping", "pings server", \&cmdPing],
);
sub on_unload {
Plugins::delHook("mainLoop_pre", $hook);
}
sub on_reload {
message "koreping, ";
Plugins::delHook("mainLoop_pre", $hook);
}
my %time;
sub pingcheck {
$time{timeout} = $config{ping_timeout};
if ($net->getState() == Network::IN_GAME && timeOut(\%time)) {
my $maxping = $config{ping_max};
my $host = $config{ping_host};
my $ping_quit = $config{ping_quit};
my $ping_relog = $config{ping_relog};
my $ping_relog_time = $config{ping_relog_time};
my $ping = ping(hostname => $host, count => 1, size => 1024, timeout => 3);
my @pinged = split / /, $ping;
if ($pinged[-1] >= $maxping) {
if ($ping_quit) {
message "Quitting due to high ping: $pinged[-1]\n";
$quit = 1;
}
elsif ($ping_relog) {
message "Relogging due to high ping: $pinged[-1]\n";
relog ($ping_relog_time);
}
# else { message "I would dc now\n"; }
}
$time{time} = time;
}
}
sub cmdPing {
my $host = $config{ping_host};
my $ping = ping(hostname => $host, count => 1, size => 1024, timeout => 3);
my @pinged = split / /, $ping;
message "Ping: $pinged[-1]\n";
}
1;
However, if ping() call blocks for a while in main loop, it may lead to disconnect or slow reaction to ingame events.