-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
peer.go
153 lines (130 loc) · 3.73 KB
/
peer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package enet
// #include <enet/enet.h>
import "C"
import (
"encoding/binary"
"fmt"
"math"
"unsafe"
)
// Peer is a peer which data packets may be sent or received from
type Peer interface {
GetAddress() Address
GetConnectId() uint
Disconnect(data uint32)
DisconnectNow(data uint32)
DisconnectLater(data uint32)
SendBytes(data []byte, channel uint8, flags PacketFlags) error
SendString(str string, channel uint8, flags PacketFlags) error
SendPacket(packet Packet, channel uint8) error
// SetData sets an arbitrary value against a peer. This is useful to attach some
// application-specific data for future use, such as an identifier.
//
// http://enet.bespin.org/structENetPeer.html#a1873959810db7ac7a02da90469ee384e
//
// Note that due to the way the enet library works, if using this you are
// responsible for clearing this data when the peer is finished with.
// SetData(nil) will free underlying memory and avoid any leaks.
//
// See http://enet.bespin.org/Tutorial.html#ManageHost for an example of this
// in the underlying library.
SetData(data []byte)
// GetData returns an application-specific value that's been set
// against this peer. This returns nil if no data has been set.
//
// http://enet.bespin.org/structENetPeer.html#a1873959810db7ac7a02da90469ee384e
GetData() []byte
}
type enetPeer struct {
cPeer *C.struct__ENetPeer
}
func (peer enetPeer) GetAddress() Address {
return &enetAddress{
cAddr: peer.cPeer.address,
}
}
func (peer enetPeer) GetConnectId() uint {
return uint(peer.cPeer.connectID)
}
func (peer enetPeer) Disconnect(data uint32) {
C.enet_peer_disconnect(
peer.cPeer,
(C.enet_uint32)(data),
)
}
func (peer enetPeer) DisconnectNow(data uint32) {
C.enet_peer_disconnect_now(
peer.cPeer,
(C.enet_uint32)(data),
)
}
func (peer enetPeer) DisconnectLater(data uint32) {
C.enet_peer_disconnect_later(
peer.cPeer,
(C.enet_uint32)(data),
)
}
func (peer enetPeer) SendBytes(data []byte, channel uint8, flags PacketFlags) error {
packet, err := NewPacket(data, flags)
if err != nil {
return err
}
return peer.SendPacket(packet, channel)
}
func (peer enetPeer) SendString(str string, channel uint8, flags PacketFlags) error {
packet, err := NewPacket([]byte(str), flags)
if err != nil {
return err
}
return peer.SendPacket(packet, channel)
}
func (peer enetPeer) SendPacket(packet Packet, channel uint8) error {
C.enet_peer_send(
peer.cPeer,
(C.enet_uint8)(channel),
packet.(enetPacket).cPacket,
)
return nil
}
func (peer enetPeer) SetData(data []byte) {
if len(data) > math.MaxUint32 {
panic(fmt.Sprintf("maximum peer data length is uint32 (%d)", math.MaxUint32))
}
// Free any data that was previously stored against this peer.
existing := unsafe.Pointer(peer.cPeer.data)
if existing != nil {
C.free(existing)
}
// If nil, set this explicitly.
if data == nil {
peer.cPeer.data = nil
return
}
// First 4 bytes stores how many bytes we have. This is so we can C.GoBytes when
// retrieving which requires a byte length to read.
b := make([]byte, len(data)+4)
binary.LittleEndian.PutUint32(b, uint32(len(data)))
// Join this header + data in to a contiguous slice
copy(b[4:], data)
// And write it out to C memory, storing our pointer.
peer.cPeer.data = unsafe.Pointer(C.CBytes(b))
}
func (peer enetPeer) GetData() []byte {
ptr := unsafe.Pointer(peer.cPeer.data)
if ptr == nil {
return nil
}
// First 4 bytes are the bytes length.
header := []byte{
*(*byte)(unsafe.Add(ptr, 0)),
*(*byte)(unsafe.Add(ptr, 1)),
*(*byte)(unsafe.Add(ptr, 2)),
*(*byte)(unsafe.Add(ptr, 3)),
}
return []byte(C.GoBytes(
// Take from the start of the data.
unsafe.Add(ptr, 4),
// As many bytes as were indicated in the header.
C.int(binary.LittleEndian.Uint32(header)),
))
}