类 RconClient
- java.lang.Object
-
- cn.mcres.gyhhy.MXLib.http.handlers.rcon.RconClient
-
- 所有已实现的接口:
Closeable
,AutoCloseable
public class RconClient extends Object implements Closeable
An RCON client to remotely control a Minecraft server. Useopen(String, int, String)
to create an instance ofRconClient
, open a connection to a Minecraft server, and authenticate. Then usesendCommand(String)
to send a command to the server. Make sure to close the connection again usingclose()
.The connection is only opened when the client is created. If for any reason the connection is closed or broken (eg. by the server shutting down or some network failure), no attempt is made to re-establish the connection. When
sendCommand
is invoked in such a case, an exception will occur.To enable RCON support in your Minecraft server, add the following to your
server.properties
(or modify existing properties if they are already present):enable-rcon=true rcon.password=<your password> rcon.port=<1-65535>
-
-
字段概要
字段 修饰符和类型 字段 说明 protected static int
AUTHENTICATION_FAILURE_ID
protected AtomicInteger
currentRequestId
protected static Charset
DEFAULT_PAYLOAD_CHARSET
protected boolean
isAutoRead
protected Charset
PAYLOAD_CHARSET
protected SocketChannel
socketChannel
protected static int
TYPE_AUTH
protected static int
TYPE_COMMAND
-
构造器概要
构造器 限定符 构造器 说明 protected
RconClient(SocketChannel socketChannel)
-
方法概要
修饰符和类型 方法 说明 protected void
authenticate(String password)
boolean
autoRead()
void
close()
Charset
getCharset()
SocketChannel
getSocketChannel()
boolean
isOpen()
static RconClient
open(String host, int port, String password)
Create an instance ofRconClient
, opening a connection to the specifiedhost
andport
, and authenticate using the specifiedpassword
.protected ByteBuffer
readData(int size)
ByteBuffer
readResponse()
protected String
send(int type, byte[] payload)
protected String
send(int type, String payload)
protected byte[]
send_(int type, byte[] payload)
protected byte[]
send_(int type, byte[] payload, int from, int length)
String
sendCommand(String command)
Sendcommand
to the server, returning any data that was returned by the server.void
setAutoRead(boolean r)
RconClient
setCharset(Charset c)
protected ByteBuffer
toByteBuffer(int requestId, int type, byte[] payload, int from, int length)
protected ByteBuffer
toByteBuffer(int requestId, int type, String payload)
-
-
-
字段详细资料
-
AUTHENTICATION_FAILURE_ID
protected static final int AUTHENTICATION_FAILURE_ID
- 另请参阅:
- 常量字段值
-
DEFAULT_PAYLOAD_CHARSET
protected static final Charset DEFAULT_PAYLOAD_CHARSET
-
TYPE_COMMAND
protected static final int TYPE_COMMAND
- 另请参阅:
- 常量字段值
-
TYPE_AUTH
protected static final int TYPE_AUTH
- 另请参阅:
- 常量字段值
-
PAYLOAD_CHARSET
protected Charset PAYLOAD_CHARSET
-
socketChannel
protected final SocketChannel socketChannel
-
currentRequestId
protected final AtomicInteger currentRequestId
-
isAutoRead
protected boolean isAutoRead
-
-
构造器详细资料
-
RconClient
protected RconClient(SocketChannel socketChannel)
-
-
方法详细资料
-
open
public static RconClient open(String host, int port, String password) throws IOException
Create an instance ofRconClient
, opening a connection to the specifiedhost
andport
, and authenticate using the specifiedpassword
. If no connection can be established,RconClientException
is thrown, wrapping any exception of the underlying communication channel (eg.IOException
). If thepassword
is incorrect, anAuthFailureException
is thrown.- 参数:
host
- The server's host name or IP addressport
- The server RCON port number (rcon.port
inserver.properties
)password
- The server's RCON password (rcon.password
inserver.properties
)- 返回:
- An
RconClient
with an established connection - 抛出:
RconClientException
- When any exception is thrown by the communication channelAuthFailureException
- When the password is wrongIOException
-
getCharset
public Charset getCharset()
-
getSocketChannel
public SocketChannel getSocketChannel()
-
setCharset
public RconClient setCharset(Charset c)
-
autoRead
public boolean autoRead()
-
setAutoRead
public void setAutoRead(boolean r)
-
sendCommand
public String sendCommand(String command) throws IOException
Sendcommand
to the server, returning any data that was returned by the server. Note that in a lot of cases when the command is delivered and executed successfully, an empty response is returned by the server, resulting in an empty String as a return value of this method. When an unknown command is sent, the server will return some text along the lines of"Unknown command. Try /help for a list of commands"
.When any communication failure occurs (eg. broken connection, server has shut down), an
RconClientException
is thrown, wrapping any exception of the underlying communication channel (eg.IOException
).- 参数:
command
- The command to send to the server- 返回:
- Response as returned by the server
- 抛出:
RconClientException
- When any exception is thrown by the communication channelIOException
-
close
public void close() throws IOException
- 指定者:
close
在接口中AutoCloseable
- 指定者:
close
在接口中Closeable
- 抛出:
IOException
-
isOpen
public boolean isOpen()
-
authenticate
protected void authenticate(String password) throws IOException
- 抛出:
IOException
-
send
protected String send(int type, String payload) throws IOException
- 抛出:
IOException
-
send
protected String send(int type, byte[] payload) throws IOException
- 抛出:
IOException
-
send_
protected byte[] send_(int type, byte[] payload) throws IOException
- 抛出:
IOException
-
send_
protected byte[] send_(int type, byte[] payload, int from, int length) throws IOException
- 抛出:
IOException
-
readResponse
public ByteBuffer readResponse() throws IOException
- 抛出:
IOException
-
readData
protected ByteBuffer readData(int size) throws IOException
- 抛出:
IOException
-
toByteBuffer
protected ByteBuffer toByteBuffer(int requestId, int type, byte[] payload, int from, int length)
-
toByteBuffer
protected ByteBuffer toByteBuffer(int requestId, int type, String payload)
-
-