1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-25 00:25:23 +00:00
OpenDiablo2/d2networking/d2netpacket/net_packet.go
2020-09-12 16:25:09 -04:00

44 lines
1.1 KiB
Go

package d2netpacket
import (
"encoding/json"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
)
// NetPacket is used to wrap and send all packet types under d2netpacket.
// When decoding a packet: First the PacketType byte is read, then the
// PacketData is unmarshalled to a struct of the type associated with
// PacketType.
type NetPacket struct {
PacketType d2netpackettype.NetPacketType `json:"packetType"`
PacketData json.RawMessage `json:"packetData"`
}
func InspectPacketType(b []byte) d2netpackettype.NetPacketType {
var packet NetPacket
if err := json.Unmarshal(b, &packet); err != nil {
log.Println(err)
}
return packet.PacketType
}
func UnmarshalNetPacket(packet []byte) (NetPacket, error) {
var p NetPacket
if err := json.Unmarshal(packet, &p); err != nil {
return p, err
}
return p, nil
}
// MarshalPacket is a quick helper function to Marshal very anything UNSAFELY, meaning the error is not checked before sending.
func MarshalPacket(packet interface{}) []byte {
b, _ := json.Marshal(packet)
return b
}