-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathclient_instance.go
68 lines (59 loc) · 1.56 KB
/
client_instance.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
package netcode
import (
"fmt"
"log"
"net"
)
type ClientInstance struct {
clientId uint64
clientIndex int
serverConn *NetcodeConn
confirmed bool
connected bool
encryptionIndex int
sequence uint64
lastSendTime float64
lastRecvTime float64
userData []byte
protocolId uint64
replayProtection *ReplayProtection
address *net.UDPAddr
packetQueue *PacketQueue
packetData []byte
}
func NewClientInstance() *ClientInstance {
c := &ClientInstance{}
c.userData = make([]byte, USER_DATA_BYTES)
c.packetQueue = NewPacketQueue(PACKET_QUEUE_SIZE)
c.packetData = make([]byte, MAX_PACKET_BYTES)
c.replayProtection = NewReplayProtection()
return c
}
func (c *ClientInstance) Clear() {
c.replayProtection.Reset()
c.connected = false
c.confirmed = false
c.clientId = 0
c.sequence = 0
c.lastSendTime = 0.0
c.lastRecvTime = 0.0
c.address = nil
c.clientIndex = -1
c.encryptionIndex = -1
c.packetQueue.Clear()
c.userData = make([]byte, USER_DATA_BYTES)
c.packetData = make([]byte, MAX_PACKET_BYTES)
}
func (c *ClientInstance) SendPacket(packet Packet, writePacketKey []byte, serverTime float64) error {
var bytesWritten int
var err error
if bytesWritten, err = packet.Write(c.packetData, c.protocolId, c.sequence, writePacketKey); err != nil {
return fmt.Errorf("error: unable to write packet: %s", err)
}
if _, err := c.serverConn.WriteTo(c.packetData[:bytesWritten], c.address); err != nil {
log.Printf("error writing to client: %s\n", err)
}
c.sequence++
c.lastSendTime = serverTime
return nil
}