1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-19 10:56:07 -05:00
OpenDiablo2/d2networking/d2netpacket/packet_server_closed.go
Ian Ling 3936e01afb Networking bugfixes and cleanup
Make sure connections close properly, without weird error messages
Remove player map entity when a player disconnects from a multiplayer game
Close server properly when host disconnects, handle ServerClose on remote clients
Don't mix JSON decoders and raw TCP writes
Actually handle incoming packets from remote clients
General code cleanup for simplicity and consistency
2020-12-28 13:33:17 -08:00

44 lines
1.1 KiB
Go

package d2netpacket //nolint:dupl // ServerClosed and Ping just happen to be very similar packets
import (
"encoding/json"
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
)
// ServerClosedPacket contains the current time. It is sent by the server
// to inform a client that the server has shut down.
type ServerClosedPacket struct {
TS time.Time `json:"ts"`
}
// CreateServerClosedPacket returns a NetPacket which declares a
// ServerClosedPacket with the current time.
func CreateServerClosedPacket() (NetPacket, error) {
serverClosed := ServerClosedPacket{
TS: time.Now(),
}
b, err := json.Marshal(serverClosed)
if err != nil {
return NetPacket{PacketType: d2netpackettype.ServerClosed}, err
}
return NetPacket{
PacketType: d2netpackettype.ServerClosed,
PacketData: b,
}, nil
}
// UnmarshalServerClosed unmarshals the given data to a ServerClosedPacket struct
func UnmarshalServerClosed(packet []byte) (ServerClosedPacket, error) {
var resp ServerClosedPacket
if err := json.Unmarshal(packet, &resp); err != nil {
return resp, err
}
return resp, nil
}