/
error.go
70 lines (59 loc) · 2.6 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package steamnet
import (
"github.com/BenLubar/steamworks"
"github.com/BenLubar/steamworks/internal"
)
// RegisterErrorCallback registers a function to be called when packets can't
// get through to the specified user.
//
// All queued packets unsent at this point will be dropped, further attempts to
// send will retry making the connection (but will be dropped if we fail again).
func RegisterErrorCallback(f func(steamworks.SteamID, error)) steamworks.Registration {
return internal.RegisterCallback_P2PSessionConnectFail(func(data *internal.P2PSessionConnectFail, _ bool) {
f(steamworks.SteamID(data.SteamIDRemote.Get()), toError(internal.EP2PSessionError(data.EP2PSessionError)))
}, 0)
}
// Error represents a connection error in the Steam P2P API.
type Error internal.EP2PSessionError
var (
// ErrNotRunningApp is returned if the target user is not running the same
// game.
ErrNotRunningApp error = Error(internal.EP2PSessionError_NotRunningApp)
// ErrNoRightsToApp is returned if the local user doesn't own the app that
// is running.
ErrNoRightsToApp error = Error(internal.EP2PSessionError_NoRightsToApp)
// ErrDestinationNotLoggedIn is returned if the target user isn't connected
// to Steam.
ErrDestinationNotLoggedIn error = Error(internal.EP2PSessionError_DestinationNotLoggedIn)
// ErrTimeout is returned if the connection timed out because the target
// user didn't respond.
ErrTimeout error = Error(internal.EP2PSessionError_Timeout)
)
func (err Error) Error() string {
if err == 0 || int(err) >= len(p2pErrors) {
return "steamnet: unknown error"
}
return p2pErrors[err]
}
// Timeout returns true iff this error was caused by a timeout.
func (err Error) Timeout() bool {
return err == ErrTimeout
}
// Temporary returns true iff this error might go away after a retry with no
// other local actions.
func (err Error) Temporary() bool {
return err == ErrNotRunningApp || err == ErrDestinationNotLoggedIn || err == ErrTimeout
}
var p2pErrors = [...]string{
internal.EP2PSessionError_None: "",
internal.EP2PSessionError_NotRunningApp: "steamnet: the target user is not running the same game",
internal.EP2PSessionError_NoRightsToApp: "steamnet: the local user doesn't own the app that is running",
internal.EP2PSessionError_DestinationNotLoggedIn: "steamnet: the target user isn't connected to Steam",
internal.EP2PSessionError_Timeout: "steamnet: the connection timed out because the target user didn't respond",
}
func toError(code internal.EP2PSessionError) error {
if code == internal.EP2PSessionError_None {
return nil
}
return Error(code)
}