/
listen.go
41 lines (38 loc) · 1.49 KB
/
listen.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
// Package steamnet wraps Steam's peer-to-peer networking API.
//
// This package works in both clients and servers.
//
// See the Steam Networking documentation for more details.
// <https://partner.steamgames.com/doc/features/multiplayer/networking>
package steamnet
import (
"github.com/BenLubar/steamworks"
"github.com/BenLubar/steamworks/internal"
)
// Listen registers a function to handle connection requests.
//
// The function will be called from the callback thread, and will cause the
// connection to be accepted if it returns true.
//
// Multiple listeners may be registered simultaneously, and connections will be
// accepted if any listener returns true.
func Listen(accept func(steamworks.SteamID) bool) steamworks.Registration {
return internal.RegisterCallback_P2PSessionRequest(func(data *internal.P2PSessionRequest, _ bool) {
id := internal.SteamID(data.SteamIDRemote.Get())
if accept(steamworks.SteamID(id)) {
internal.SteamAPI_ISteamNetworking_AcceptP2PSessionWithUser(id)
}
}, 0)
}
// SetAllowPacketRelay allows or disallows P2P connections to fall back to
// being relayed through the Steam servers if a direct connection or
// NAT-traversal cannot be established.
//
// This only applies to connections created after setting this value, or to
// existing connections that need to automatically reconnect after this value
// is set.
//
// P2P packet relay is allowed by default.
func SetAllowPacketRelay(allow bool) {
internal.SteamAPI_ISteamNetworking_AllowP2PPacketRelay(allow)
}