@@ -2,28 +2,22 @@ package gamequery
2
2
3
3
import (
4
4
"errors"
5
- "github.com/wisp-gg/gamequery/protocols"
5
+ "github.com/wisp-gg/gamequery/api"
6
+ "github.com/wisp-gg/gamequery/internal"
7
+ "github.com/wisp-gg/gamequery/internal/protocols"
6
8
"sort"
7
9
"sync"
8
10
"time"
9
11
)
10
12
11
- // Representation of a query request for a specific game server.
12
- type Request struct {
13
- Game string // The game protocol to use, can be left out for the `Detect` function.
14
- IP string // The game server's query IP
15
- Port uint16 // The game server's query port
16
- Timeout * time.Duration // Timeout for a single send/receive operation in the game's protocol.
17
- }
18
-
19
- var queryProtocols = []protocols.Protocol {
13
+ var queryProtocols = []internal.Protocol {
20
14
protocols.SourceQuery {},
21
15
protocols.MinecraftUDP {},
22
16
protocols.MinecraftTCP {},
23
17
}
24
18
25
- func findProtocols (name string ) []protocols .Protocol {
26
- found := make ([]protocols .Protocol , 0 )
19
+ func findProtocols (name string ) []internal .Protocol {
20
+ found := make ([]internal .Protocol , 0 )
27
21
for _ , protocol := range queryProtocols {
28
22
if protocol .Name () == name {
29
23
found = append (found , protocol )
@@ -43,35 +37,35 @@ type queryResult struct {
43
37
Name string
44
38
Priority uint16
45
39
Err error
46
- Response protocols .Response
40
+ Response api .Response
47
41
}
48
42
49
43
// Query the game server by detecting the protocol (trying all available protocols).
50
44
// This usually should be used as the initial query function and then use `Query` function
51
45
// with the returned protocol if the query succeeds. Otherwise each function call will take always
52
46
// <req.Timeout> duration even if the response was received earlier from one of the protocols.
53
- func Detect (req Request ) (protocols .Response , string , error ) {
47
+ func Detect (req api. Request ) (api .Response , string , error ) {
54
48
return query (req , queryProtocols )
55
49
}
56
50
57
51
// Query the game server using the protocol provided in req.Game.
58
- func Query (req Request ) (protocols .Response , error ) {
52
+ func Query (req api. Request ) (api .Response , error ) {
59
53
chosenProtocols := findProtocols (req .Game )
60
54
if len (chosenProtocols ) < 1 {
61
- return protocols .Response {}, errors .New ("could not find protocols for the game" )
55
+ return api .Response {}, errors .New ("could not find protocols for the game" )
62
56
}
63
57
64
58
response , _ , err := query (req , chosenProtocols )
65
59
return response , err
66
60
}
67
61
68
- func query (req Request , chosenProtocols []protocols .Protocol ) (protocols .Response , string , error ) {
62
+ func query (req api. Request , chosenProtocols []internal .Protocol ) (api .Response , string , error ) {
69
63
var wg sync.WaitGroup
70
64
wg .Add (len (chosenProtocols ))
71
65
72
66
queryResults := make ([]queryResult , len (chosenProtocols ))
73
67
for index , queryProtocol := range chosenProtocols {
74
- go func (queryProtocol protocols .Protocol , index int ) {
68
+ go func (queryProtocol internal .Protocol , index int ) {
75
69
defer wg .Done ()
76
70
77
71
var port = queryProtocol .DefaultPort ()
@@ -84,12 +78,12 @@ func query(req Request, chosenProtocols []protocols.Protocol) (protocols.Respons
84
78
timeout = * req .Timeout
85
79
}
86
80
87
- networkHelper := protocols .NetworkHelper {}
81
+ networkHelper := internal .NetworkHelper {}
88
82
if err := networkHelper .Initialize (queryProtocol .Network (), req .IP , port , timeout ); err != nil {
89
83
queryResults [index ] = queryResult {
90
84
Priority : queryProtocol .Priority (),
91
85
Err : err ,
92
- Response : protocols .Response {},
86
+ Response : api .Response {},
93
87
}
94
88
return
95
89
}
@@ -100,7 +94,7 @@ func query(req Request, chosenProtocols []protocols.Protocol) (protocols.Respons
100
94
queryResults [index ] = queryResult {
101
95
Priority : queryProtocol .Priority (),
102
96
Err : err ,
103
- Response : protocols .Response {},
97
+ Response : api .Response {},
104
98
}
105
99
return
106
100
}
@@ -130,5 +124,5 @@ func query(req Request, chosenProtocols []protocols.Protocol) (protocols.Respons
130
124
}
131
125
}
132
126
133
- return protocols .Response {}, "" , firstError
127
+ return api .Response {}, "" , firstError
134
128
}
0 commit comments