类 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
sendCommandis 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 intAUTHENTICATION_FAILURE_IDprotected AtomicIntegercurrentRequestIdprotected static CharsetDEFAULT_PAYLOAD_CHARSETprotected booleanisAutoReadprotected CharsetPAYLOAD_CHARSETprotected SocketChannelsocketChannelprotected static intTYPE_AUTHprotected static intTYPE_COMMAND
-
构造器概要
构造器 限定符 构造器 说明 protectedRconClient(SocketChannel socketChannel)
-
方法概要
修饰符和类型 方法 说明 protected voidauthenticate(String password)booleanautoRead()voidclose()CharsetgetCharset()SocketChannelgetSocketChannel()booleanisOpen()static RconClientopen(String host, int port, String password)Create an instance ofRconClient, opening a connection to the specifiedhostandport, and authenticate using the specifiedpassword.protected ByteBufferreadData(int size)ByteBufferreadResponse()protected Stringsend(int type, byte[] payload)protected Stringsend(int type, String payload)protected byte[]send_(int type, byte[] payload)protected byte[]send_(int type, byte[] payload, int from, int length)StringsendCommand(String command)Sendcommandto the server, returning any data that was returned by the server.voidsetAutoRead(boolean r)RconClientsetCharset(Charset c)protected ByteBuffertoByteBuffer(int requestId, int type, byte[] payload, int from, int length)protected ByteBuffertoByteBuffer(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 specifiedhostandport, and authenticate using the specifiedpassword. If no connection can be established,RconClientExceptionis thrown, wrapping any exception of the underlying communication channel (eg.IOException). If thepasswordis incorrect, anAuthFailureExceptionis thrown.- 参数:
host- The server's host name or IP addressport- The server RCON port number (rcon.portinserver.properties)password- The server's RCON password (rcon.passwordinserver.properties)- 返回:
- An
RconClientwith 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
Sendcommandto 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
RconClientExceptionis 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)
-
-