mirror of
https://github.com/Tiiffi/mcrcon.git
synced 2025-10-27 11:21:07 -04:00
Merge pull request #34 from HorlogeSkynet/master
Adds a `-w` option to throttle commands execution
This commit is contained in:
@ -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:
|
||||
```
|
||||
|
||||
6
mcrcon.1
6
mcrcon.1
@ -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
|
||||
|
||||
42
mcrcon.c
42
mcrcon.c
@ -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,6 +174,14 @@ int main(int argc, char *argv[])
|
||||
case 'v':
|
||||
puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon");
|
||||
exit(EXIT_SUCCESS);
|
||||
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;
|
||||
}
|
||||
++ret;
|
||||
}
|
||||
if (!rcon_command(global_rsock, argv[i]))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Check if amount of successfully sent commands
|
||||
// matches amount of requested commands
|
||||
if (ret == optind) ret = EXIT_SUCCESS;
|
||||
if (++i >= argc)
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
return ret;
|
||||
if (global_wait_seconds > 0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
Sleep(global_wait_seconds * 1000);
|
||||
#else
|
||||
sleep(global_wait_seconds);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// interactive terminal mode
|
||||
|
||||
Reference in New Issue
Block a user