public final class SourceQueryClient extends NettySocketClient<SourceQueryRequest,SourceQueryResponse<?>>
A query client for retreiving information on Source servers. Based on the Valve Source Server Query Protocol
Example:try (SourceQueryClient client = new SourceQueryClient()) { CompletableFuture<Source> resultFuture = client.getInfo(new InetSocketAddress("192.168.1.10", 27015); if (resultFuture.isDone()) { SourceQueryInfoResponse infoResponse = resultFuture.getNow(null); assert infoResponse != null; System.out.printf("INFO: %s\n", infoResponse.getResult()); } else { //we have not yet received a response, register a callback once we do resultFuture.whenComplete((response, error) -> { if (error != null) { error.printStackTrace(System.err); return; } assert response != null; System.out.printf("INFO: %s\n", response); }); } }
try (SourceQueryClient client = new SourceQueryClient()) { SourceQueryInfoResponse infoResponse = client.getInfo(new InetSocketAddress("192.168.1.10", 27015); assert infoResponse != null; System.out.printf("INFO: %s\n", infoResponse.getResult()); } catch (Exception error) { error.printStackTrace(System.err); }
Enable rate-limiting (disabled by default)
SourceQueryOptions options = SourceQueryOptions.builder().option(SourceQueryOptions.FAILSAFE_RATELIMIT_ENABLED, true) .option(SourceQueryOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.SMOOTH) .build(); //pass options to client's constructor try (SourceQueryClient client = new SourceQueryClient(options)) { InetSocketAddress address = new InetSocketAddress("192.168.1.10", 27015); //execute query 100 times for (int i=0; i<100; i++) { CompletableFuture<Source> resultFuture = client.getInfo(address); if (resultFuture.isDone()) { SourceQueryInfoResponse infoResponse = resultFuture.getNow(null); assert infoResponse != null; System.out.printf("INFO: %s\n", infoResponse.getResult()); } else { //we have not yet received a response, register a callback once we do resultFuture.whenComplete((response, error) -> { if (error != null) { error.printStackTrace(System.err); return; } assert response != null; System.out.printf("INFO: %s\n", response); }); } } }
SourceQueryOptions
Constructor and Description |
---|
SourceQueryClient()
Create a new
SourceQueryClient instance using the pre-defined configuration Options for this client |
SourceQueryClient(SourceQueryOptions options)
Create a new
SourceQueryClient instance using the provided SourceQueryOptions |
close, equals, getExecutor, getMessenger, hashCode
getOptions, id, send, send
public SourceQueryClient()
SourceQueryClient
instance using the pre-defined configuration Options
for this clientpublic SourceQueryClient(SourceQueryOptions options)
SourceQueryClient
instance using the provided SourceQueryOptions
options
- A SourceQueryOptions
object containing all the user-defined configuration values to be used by the client.Options
,
OptionBuilder
public CompletableFuture<SourceQueryInfoResponse> getInfo(InetSocketAddress address)
Retrieves information about the Source server. A challengenumber will automatically be obtained if required by the server.
address
- The InetSocketAddress
containing the ip address and port number information of the target serverCompletableFuture
that is notified once a response has been received from the server. If successful, the CompletableFuture
returns a value of SourceQueryInfoResponse
which provides additional details on the server.getInfo(InetSocketAddress, Integer)
public CompletableFuture<SourceQueryInfoResponse> getInfo(InetSocketAddress address, Integer challenge)
Retrieves information about the Source server using the provided challenge number (optional)
address
- The InetSocketAddress
containing the IP address and port number information of the target serverchallenge
- (optional) A 32-bit signed integer anti-spoofing challenge. Set to null
to let the library obtain one automatically. Passing a non-null integer will implicitly disable auto-update. This means that the library will not automatically send a new challenge request if the server requires a new one. Instead, it will throw a SourceChallengeException
where a valid challenge number can be obtained via SourceChallengeException.getChallenge()
. This is similar to calling getInfo(InetSocketAddress)
.CompletableFuture
that is notified once a response has been received from the server. If successful, the CompletableFuture
returns a value of SourceQueryInfoResponse
which provides additional details on the serverSourceChallengeException
- If auto-update is disabled (challenge argument is null
) and the current challenge number has been invalidated.public CompletableFuture<SourceQueryRulesResponse> getRules(InetSocketAddress address)
Retrieve rules from the Source server
address
- The InetSocketAddress
containing the IP address and port number information of the target server.CompletableFuture
that is notified once a response has been received from the server. If successful, the CompletableFuture
returns a value of SourceQueryRulesResponse
which provides additional details on the response.getRules(InetSocketAddress, Integer)
,
getChallenge(InetSocketAddress, SourceChallengeType)
public CompletableFuture<SourceQueryRulesResponse> getRules(InetSocketAddress address, Integer challenge)
Retrieve rules from the Source server
Note: This method requires a valid challenge number which can be obtained via getChallenge(InetSocketAddress, SourceChallengeType)
address
- The InetSocketAddress
containing the IP address and port number information of the target serverchallenge
- (optional) A 32-bit signed integer anti-spoofing challenge. Set to null
to let the library obtain one automatically. Passing a non-null integer will implicitly disable auto-update. This means that the library will not automatically send a new challenge request if the server requires a new one. Instead, it will throw a SourceChallengeException
where a valid challenge number can be obtained via SourceChallengeException.getChallenge()
. This is similar to calling getRules(InetSocketAddress)
.CompletableFuture
that is notified once a response has been received from the server. If successful, the future returns a value of SourceQueryRulesResponse
which provides additional details on the response.SourceChallengeException
- If auto-update is disabled (challenge argument is null
) and the current challenge number has been invalidated.getRules(InetSocketAddress, Integer)
public CompletableFuture<SourceQueryPlayerResponse> getPlayers(InetSocketAddress address)
Retrieve a list of active players in the server.
address
- The InetSocketAddress
containing the IP address and port number information of the target serverCompletableFuture
that contains a List
of SourcePlayer
currently residing on
the servergetPlayers(InetSocketAddress, Integer)
public CompletableFuture<SourceQueryPlayerResponse> getPlayers(InetSocketAddress address, Integer challenge)
Retrieve a list of active players in the server. You need to obtain a valid challenge number from the server
first via getChallenge(InetSocketAddress, SourceChallengeType)
address
- The InetSocketAddress
containing the IP address and port number information of the target serverchallenge
- (optional) A 32-bit signed integer anti-spoofing challenge. Set to null
to let the library obtain one automatically. Passing a non-null integer will implicitly disable auto-update. This means that the library will not automatically send a new challenge request if the server requires a new one. Instead, it will throw a SourceChallengeException
where a valid challenge number can be obtained via SourceChallengeException.getChallenge()
. This is similar to calling getPlayers(InetSocketAddress)
.CompletableFuture
that contains a List
of SourcePlayer
currently residing on
the serverSourceChallengeException
- If auto-update is disabled (challenge argument is null
) and the current challenge number has been invalidated.getPlayers(InetSocketAddress)
,
getChallenge(InetSocketAddress, SourceChallengeType)
public CompletableFuture<SourceQueryChallengeResponse> getChallenge(InetSocketAddress address, SourceChallengeType type)
Obtains a 4-byte (32-bit) anti-spoofing integer from the server. This is used for queries (such as PLAYERS, RULES or INFO) that requires a challenge number.
address
- The InetSocketAddress
containing the IP address and port number information of the target servertype
- The SourceChallengeType
enumeration which identifies the type of server challenge.CompletableFuture
returning a value of Integer
representing the server challenge numberprotected NettyMessenger<SourceQueryRequest,SourceQueryResponse<?>> createMessenger(Options options)
Messenger
.createMessenger
in class NettySocketClient<SourceQueryRequest,SourceQueryResponse<?>>
options
- a Options
objectMessenger
objectCopyright © 2016–2024. All rights reserved.