2020-12-28 16:33:17 -05:00
|
|
|
package d2netpacket //nolint:dupl // ServerClosed and Ping just happen to be very similar packets
|
2020-06-22 20:31:42 -04:00
|
|
|
|
|
|
|
import (
|
2020-08-09 20:32:47 -04:00
|
|
|
"encoding/json"
|
2020-06-22 20:31:42 -04:00
|
|
|
"time"
|
2020-06-29 17:01:26 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
2020-06-22 20:31:42 -04:00
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// PingPacket contains the time at which it was sent. It is sent by the
|
|
|
|
// server and instructs the client to respond with a Pong packet.
|
2020-06-22 20:31:42 -04:00
|
|
|
type PingPacket struct {
|
|
|
|
TS time.Time `json:"ts"`
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreatePingPacket returns a NetPacket which declares a GenerateMapPacket
|
|
|
|
// with the the current time.
|
2020-11-23 08:18:30 -05:00
|
|
|
func CreatePingPacket() (NetPacket, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
ping := PingPacket{
|
|
|
|
TS: time.Now(),
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(ping)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.Ping}, err
|
2020-09-23 13:30:54 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
|
2020-06-22 20:31:42 -04:00
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.Ping,
|
2020-08-09 20:32:47 -04:00
|
|
|
PacketData: b,
|
2020-11-23 08:18:30 -05:00
|
|
|
}, nil
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|
2020-12-28 16:33:17 -05:00
|
|
|
|
|
|
|
// UnmarshalPing unmarshals the given data to a PingPacket struct
|
|
|
|
func UnmarshalPing(packet []byte) (PingPacket, error) {
|
|
|
|
var p PingPacket
|
|
|
|
if err := json.Unmarshal(packet, &p); err != nil {
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|