Adds a -w option to throttle commands execution

This commit is contained in:
Samuel FORESTIER
2019-12-14 17:46:08 +01:00
parent fed62d9e14
commit a16d875626
3 changed files with 33 additions and 16 deletions

View File

@ -43,6 +43,7 @@ Option:
-r Output raw packets -r Output raw packets
-h Print usage -h Print usage
-v Version information -v Version information
-w Wait for specified duration (seconds) between each command
``` ```
Server address, port and password can be set using following environment variables: Server address, port and password can be set using following environment variables:
``` ```

View File

@ -31,6 +31,8 @@ Output raw packets
Print usage Print usage
.IP -v .IP -v
Output version information Output version information
.IP -w
Wait for specified duration (seconds) between each command
.PP .PP
Commands with spaces must be enclosed in quotes. Commands with spaces must be enclosed in quotes.
.br .br
@ -56,9 +58,9 @@ Send "weather clear" command to server using custom port 1337
\fBmcrcon\fR -H my.minecraft.server -P 1337 -p password "weather clear" \fBmcrcon\fR -H my.minecraft.server -P 1337 -p password "weather clear"
.RE .RE
.PP .PP
Send three commands to server ("say", "save-all" and "stop") Send three commands to server ("say", "save-all" and "stop"), and wait 2 seconds between them
.RS .RS
\fBmcrcon\fR -H my.minecraft.server -p password "say Server is restarting!" save-all stop \fBmcrcon\fR -H my.minecraft.server -p password -w 2 "say Server is restarting!" save-all stop
.RE .RE
.SH BUGS .SH BUGS
Bugs can be reported to \fBtiiffi+mcrcon at gmail\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR Bugs can be reported to \fBtiiffi+mcrcon at gmail\fR or \fBhttps://github.com/Tiiffi/mcrcon/issues/\fR

View File

@ -110,6 +110,7 @@ static int global_raw_output = 0;
static int global_silent_mode = 0; static int global_silent_mode = 0;
static int global_print_colors = 1; static int global_print_colors = 1;
static int global_connection_alive = 1; static int global_connection_alive = 1;
static int global_wait_seconds = 0;
static int global_rsock; static int global_rsock;
#ifdef _WIN32 #ifdef _WIN32
@ -154,7 +155,7 @@ int main(int argc, char *argv[])
// default getopt error handler enabled // default getopt error handler enabled
opterr = 1; opterr = 1;
while ((opt = getopt(argc, argv, "vrtcshH:p:P:i")) != -1) while ((opt = getopt(argc, argv, "vrtcshw:H:p:P:i")) != -1)
{ {
switch (opt) switch (opt)
{ {
@ -173,7 +174,15 @@ int main(int argc, char *argv[])
case 'v': case 'v':
puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon"); puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; case 'W':
case 'w':
global_wait_seconds = strtol(optarg, NULL, 10);
if (errno != 0)
{
fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
break;
case 'h': case 'h':
case '?': usage(); break; case '?': usage(); break;
/* /*
@ -247,7 +256,8 @@ void usage(void)
" -c\t\tDisable colors\n" " -c\t\tDisable colors\n"
" -r\t\tOutput raw packets\n" " -r\t\tOutput raw packets\n"
" -h\t\tPrint usage\n" " -h\t\tPrint usage\n"
" -v\t\tVersion information\n\n" " -v\t\tVersion information\n"
" -w\t\tWait for specified duration (seconds) between each command\n\n"
"Server address, port and password can be set with following environment variables:\n" "Server address, port and password can be set with following environment variables:\n"
" MCRCON_HOST\n" " MCRCON_HOST\n"
" MCRCON_PORT\n" " MCRCON_PORT\n"
@ -640,23 +650,27 @@ int rcon_command(int sock, char *command)
int run_commands(int argc, char *argv[]) int run_commands(int argc, char *argv[])
{ {
int i, ok = 1, ret = EXIT_SUCCESS; int i = optind;
for (i = optind; i < argc && ok; i++) for (;;)
{ {
ok = rcon_command(global_rsock, argv[i]); if (!rcon_command(global_rsock, argv[i]))
if (!ok) { return EXIT_FAILURE;
ret = EXIT_FAILURE;
if (++i >= argc)
break; break;
if (global_wait_seconds > 0)
{
#ifdef _WIN32
Sleep(global_wait_seconds * 1000);
#else
sleep(global_wait_seconds);
#endif
} }
++ret;
} }
// Check if amount of successfully sent commands return EXIT_SUCCESS;
// matches amount of requested commands
if (ret == optind) ret = EXIT_SUCCESS;
return ret;
} }
// interactive terminal mode // interactive terminal mode