2020-12-15 21:13:50 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-01-08 15:54:54 +00:00
|
|
|
"git.0cd.xyz/michael/mcstatus/mcstatuspb"
|
2020-12-15 21:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetStatus gets minecraft server status
|
2021-01-08 15:54:54 +00:00
|
|
|
func (client *Client) GetStatus() (*mcstatuspb.Response, error) {
|
2022-06-22 03:05:10 +00:00
|
|
|
if err := client.write(); err != nil {
|
|
|
|
return nil, err
|
2020-12-15 21:13:50 +00:00
|
|
|
}
|
2022-06-22 03:05:10 +00:00
|
|
|
resp, err := client.read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
2020-12-15 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PingServer pings Minecraft server
|
2021-01-08 15:54:54 +00:00
|
|
|
func (client *Client) PingServer() (time.Duration, error) {
|
2020-12-15 21:13:50 +00:00
|
|
|
ping := make([]byte, 1)
|
|
|
|
start := time.Now()
|
2022-06-22 03:05:10 +00:00
|
|
|
if _, err := client.Conn.Write([]byte{0x01, 0x00}); err != nil {
|
2021-01-08 15:54:54 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2022-06-22 03:05:10 +00:00
|
|
|
client.Conn.Read(ping[:])
|
|
|
|
diff := time.Since(start)
|
2021-01-08 15:54:54 +00:00
|
|
|
return diff, nil
|
2020-12-15 21:13:50 +00:00
|
|
|
}
|