2020-06-22 20:31:42 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
|
|
|
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
|
|
|
// PongPacket contains the time at which it was sent and the ID of the
|
|
|
|
// client. It is sent by the client in response to a Pong packet.
|
2020-06-22 20:31:42 -04:00
|
|
|
type PongPacket struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
TS time.Time `json:"ts"`
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreatePongPacket returns a NetPacket which declares a PongPacket with
|
|
|
|
// the current time and given ID.
|
2020-11-23 08:18:30 -05:00
|
|
|
func CreatePongPacket(id string) (NetPacket, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
pong := PongPacket{
|
|
|
|
ID: id,
|
|
|
|
TS: time.Now(),
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(pong)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.Pong}, 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.Pong,
|
2020-08-09 20:32:47 -04:00
|
|
|
PacketData: b,
|
2020-11-23 08:18:30 -05:00
|
|
|
}, nil
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:41:21 -04:00
|
|
|
// UnmarshalPong unmarshals the given data to a PongPacket struct
|
2020-08-09 20:32:47 -04:00
|
|
|
func UnmarshalPong(packet []byte) (PongPacket, error) {
|
|
|
|
var resp PongPacket
|
|
|
|
|
|
|
|
if err := json.Unmarshal(packet, &resp); err != nil {
|
|
|
|
return resp, err
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return resp, nil
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|