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
|
|
|
// ServerClosedPacket contains the current time. It is sent by the server
|
|
|
|
// to inform a client that the server has shut down.
|
2020-06-22 20:31:42 -04:00
|
|
|
type ServerClosedPacket struct {
|
|
|
|
TS time.Time `json:"ts"`
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreateServerClosedPacket returns a NetPacket which declares a
|
|
|
|
// ServerClosedPacket with the current time.
|
2020-11-23 08:18:30 -05:00
|
|
|
func CreateServerClosedPacket() (NetPacket, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
serverClosed := ServerClosedPacket{
|
|
|
|
TS: time.Now(),
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(serverClosed)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.ServerClosed}, 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.ServerClosed,
|
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
|
|
|
// UnmarshalServerClosed unmarshals the given data to a ServerClosedPacket struct
|
2020-08-09 20:32:47 -04:00
|
|
|
func UnmarshalServerClosed(packet []byte) (ServerClosedPacket, error) {
|
|
|
|
var resp ServerClosedPacket
|
|
|
|
|
|
|
|
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
|
|
|
}
|