-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrespond.go
50 lines (40 loc) · 1.7 KB
/
respond.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
// Package globals implements variables and functions used by all protocol packages
package globals
import (
"github.com/PretendoNetwork/nex-go/v2"
"github.com/PretendoNetwork/nex-go/v2/constants"
)
// Respond sends the client a given RMC message
func Respond(packet nex.PacketInterface, message *nex.RMCMessage) {
sender := packet.Sender()
var responsePacket nex.PacketInterface
switch packet := packet.(type) {
case nex.PRUDPPacketInterface:
var prudpPacket nex.PRUDPPacketInterface
endpoint := sender.(*nex.PRUDPConnection).Endpoint()
server := endpoint.(*nex.PRUDPEndPoint).Server
if packet.Version() == 1 {
prudpPacket, _ = nex.NewPRUDPPacketV1(server, sender.(*nex.PRUDPConnection), nil)
} else {
prudpPacket, _ = nex.NewPRUDPPacketV0(server, sender.(*nex.PRUDPConnection), nil)
}
prudpPacket.SetType(constants.DataPacket)
if packet.HasFlag(constants.PacketFlagReliable) {
prudpPacket.AddFlag(constants.PacketFlagReliable)
}
prudpPacket.AddFlag(constants.PacketFlagNeedsAck)
prudpPacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
prudpPacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
prudpPacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
prudpPacket.SetDestinationVirtualPortStreamID(packet.SourceVirtualPortStreamID())
prudpPacket.SetSubstreamID(packet.SubstreamID())
responsePacket = prudpPacket
responsePacket.SetPayload(message.Bytes())
case *nex.HPPPacket:
// * We reuse the same packet from input and replace
// * the RMC message so that it can be delivered back
responsePacket = packet
responsePacket.SetRMCMessage(message)
}
sender.Endpoint().Send(responsePacket)
}