mirror of
https://github.com/amineo/t2_server_query_elixir.git
synced 2026-01-19 18:14:44 +00:00
refactor(spec): add type specs
This commit is contained in:
parent
0c785ccb39
commit
a500fea095
|
|
@ -70,6 +70,7 @@ defmodule T2ServerQuery do
|
|||
}}
|
||||
|
||||
"""
|
||||
@spec query(String.t(), integer(), integer()) :: {atom(), %T2ServerQuery.QueryResult{}}
|
||||
def query(server_ip, port \\ 28_000, timeout \\ 3_500) do
|
||||
Logger.info "query: #{server_ip}"
|
||||
case is_valid_ip?(server_ip) do
|
||||
|
|
@ -78,9 +79,8 @@ defmodule T2ServerQuery do
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
@spec handle_query(String.t(), integer(), integer()) :: {atom(), %T2ServerQuery.QueryResult{}}
|
||||
defp handle_query(server_ip, port, timeout) do
|
||||
|
||||
{:ok, socket} = :gen_udp.open(0, [:binary, {:active, false}])
|
||||
|
||||
# Convert a string ip from "127.0.0.1" into {127, 0, 0, 1}
|
||||
|
|
@ -106,7 +106,7 @@ defmodule T2ServerQuery do
|
|||
PacketParser.init(hex_info_packet, hex_status_packet)
|
||||
end
|
||||
|
||||
|
||||
@spec is_valid_ip?(any()) :: boolean()
|
||||
defp is_valid_ip?(nil), do: false
|
||||
defp is_valid_ip?(server_ip) do
|
||||
case Regex.match?(~r/^([1-2]?[0-9]{1,2}\.){3}([1-2]?[0-9]{1,2})$/, server_ip) do
|
||||
|
|
@ -116,6 +116,7 @@ defmodule T2ServerQuery do
|
|||
end
|
||||
|
||||
|
||||
@spec handle_udp_response(tuple(), String.t(), integer()) :: tuple() | String.t()
|
||||
defp handle_udp_response({:ok, {_ip, port, packet}}, _server_ip, port) do
|
||||
packet
|
||||
|> Base.encode16
|
||||
|
|
|
|||
|
|
@ -43,9 +43,9 @@ defmodule T2ServerQuery.PacketParser do
|
|||
|
||||
Refer to `T2ServerQuery.QueryResult` for what a typical struct would look like.
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
alias T2ServerQuery.QueryResult
|
||||
|
||||
@doc """
|
||||
|
|
@ -53,6 +53,7 @@ defmodule T2ServerQuery.PacketParser do
|
|||
Normally you wouldn't need to run this function manually since it's called in a pipeline from the main `T2ServerQuery.query`
|
||||
|
||||
"""
|
||||
@spec init({:error, String.t()}, any()) :: {:error, map()}
|
||||
def init({:error, host}, _) do
|
||||
results = %QueryResult{}
|
||||
|
||||
|
|
@ -65,6 +66,7 @@ defmodule T2ServerQuery.PacketParser do
|
|||
}
|
||||
end
|
||||
|
||||
@spec init(binary(), binary()) :: {:ok, %QueryResult{}}
|
||||
def init(info_packet, status_packet) when is_binary(info_packet) and is_binary(status_packet) do
|
||||
|
||||
info_results = info_packet
|
||||
|
|
@ -81,6 +83,7 @@ defmodule T2ServerQuery.PacketParser do
|
|||
pack_results({:ok, status_results, info_results})
|
||||
end
|
||||
|
||||
@spec pack_results({:ok, map(), map()}) :: {:ok, %QueryResult{}}
|
||||
defp pack_results({:ok, status_results, info_results}) do
|
||||
results = %QueryResult{}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue