Merge pull request #34 from HorlogeSkynet/master

Adds a `-w` option to throttle commands execution
This commit is contained in:
Tiiffi
2019-12-16 17:43:12 +02:00
committed by GitHub
3 changed files with 33 additions and 18 deletions

View File

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

View File

@ -31,6 +31,8 @@ Output raw packets
Print usage
.IP -v
Output version information
.IP -w
Wait for specified duration (seconds) between each command
.PP
Commands with spaces must be enclosed in quotes.
.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"
.RE
.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
\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
.SH BUGS
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_print_colors = 1;
static int global_connection_alive = 1;
static int global_wait_seconds = 0;
static int global_rsock;
#ifdef _WIN32
@ -154,7 +155,7 @@ int main(int argc, char *argv[])
// default getopt error handler enabled
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)
{
@ -173,7 +174,15 @@ int main(int argc, char *argv[])
case 'v':
puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon");
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 '?': usage(); break;
/*
@ -247,7 +256,8 @@ void usage(void)
" -c\t\tDisable colors\n"
" -r\t\tOutput raw packets\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"
" MCRCON_HOST\n"
" MCRCON_PORT\n"
@ -640,23 +650,25 @@ int rcon_command(int sock, char *command)
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 (!ok) {
ret = EXIT_FAILURE;
break;
if (!rcon_command(global_rsock, argv[i]))
return EXIT_FAILURE;
if (++i >= argc)
return EXIT_SUCCESS;
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
// matches amount of requested commands
if (ret == optind) ret = EXIT_SUCCESS;
return ret;
}
// interactive terminal mode