mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-01-12 04:17:25 -05:00
Merge pull request #946 from gucio321/d2networking-logger2
d2networking logger
This commit is contained in:
commit
19e3053c0a
@ -326,8 +326,13 @@ func (v *Game) OnPlayerMove(targetX, targetY float64) {
|
||||
worldPosition := v.localPlayer.Position.World()
|
||||
|
||||
playerID, worldX, worldY := v.gameClient.PlayerID, worldPosition.X(), worldPosition.Y()
|
||||
createMovePlayerPacket := d2netpacket.CreateMovePlayerPacket(playerID, worldX, worldY, targetX, targetY)
|
||||
err := v.gameClient.SendPacketToServer(createMovePlayerPacket)
|
||||
|
||||
createMovePlayerPacket, err := d2netpacket.CreateMovePlayerPacket(playerID, worldX, worldY, targetX, targetY)
|
||||
if err != nil {
|
||||
v.Errorf("MovePlayerPacket: %v", err)
|
||||
}
|
||||
|
||||
err = v.gameClient.SendPacketToServer(createMovePlayerPacket)
|
||||
|
||||
if err != nil {
|
||||
v.Errorf(moveErrStr, v.gameClient.PlayerID, targetX, targetY)
|
||||
@ -337,7 +342,13 @@ func (v *Game) OnPlayerMove(targetX, targetY float64) {
|
||||
// OnPlayerSave instructs the server to save our player data
|
||||
func (v *Game) OnPlayerSave() error {
|
||||
playerState := v.gameClient.Players[v.gameClient.PlayerID]
|
||||
err := v.gameClient.SendPacketToServer(d2netpacket.CreateSavePlayerPacket(playerState))
|
||||
|
||||
sp, err := d2netpacket.CreateSavePlayerPacket(playerState)
|
||||
if err != nil {
|
||||
return fmt.Errorf("SavePlayerPacket: %v", err)
|
||||
}
|
||||
|
||||
err = v.gameClient.SendPacketToServer(sp)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -348,7 +359,12 @@ func (v *Game) OnPlayerSave() error {
|
||||
|
||||
// OnPlayerCast sends the casting skill action to the server
|
||||
func (v *Game) OnPlayerCast(skillID int, targetX, targetY float64) {
|
||||
err := v.gameClient.SendPacketToServer(d2netpacket.CreateCastPacket(v.gameClient.PlayerID, skillID, targetX, targetY))
|
||||
cp, err := d2netpacket.CreateCastPacket(v.gameClient.PlayerID, skillID, targetX, targetY)
|
||||
if err != nil {
|
||||
v.Errorf("CastPacket: %v", err)
|
||||
}
|
||||
|
||||
err = v.gameClient.SendPacketToServer(cp)
|
||||
if err != nil {
|
||||
v.Errorf(castErrStr, v.gameClient.PlayerID, skillID, targetX, targetY)
|
||||
}
|
||||
@ -367,9 +383,12 @@ func (v *Game) debugSpawnItemAtPlayer(codes ...string) {
|
||||
}
|
||||
|
||||
func (v *Game) debugSpawnItemAtLocation(x, y int, codes ...string) {
|
||||
packet := d2netpacket.CreateSpawnItemPacket(x, y, codes...)
|
||||
packet, err := d2netpacket.CreateSpawnItemPacket(x, y, codes...)
|
||||
if err != nil {
|
||||
v.Errorf("SpawnItemPacket: %v", err)
|
||||
}
|
||||
|
||||
err := v.gameClient.SendPacketToServer(packet)
|
||||
err = v.gameClient.SendPacketToServer(packet)
|
||||
if err != nil {
|
||||
v.Errorf(spawnItemErrStr, x, y, codes)
|
||||
}
|
||||
|
@ -87,7 +87,12 @@ func (l *LocalClientConnection) Open(_, saveFilePath string) error {
|
||||
|
||||
// Close disconnects from the server and destroys it.
|
||||
func (l *LocalClientConnection) Close() error {
|
||||
err := l.SendPacketToServer(d2netpacket.CreateServerClosedPacket())
|
||||
sc, err := d2netpacket.CreateServerClosedPacket()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = l.SendPacketToServer(sc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -77,7 +77,12 @@ func (r *RemoteClientConnection) Open(connectionString, saveFilePath string) err
|
||||
r.Infof("Connected to server at %s", r.tcpConnection.RemoteAddr().String())
|
||||
|
||||
gameState := r.heroState.LoadHeroState(saveFilePath)
|
||||
packet := d2netpacket.CreatePlayerConnectionRequestPacket(r.GetUniqueID(), gameState)
|
||||
|
||||
packet, err := d2netpacket.CreatePlayerConnectionRequestPacket(r.GetUniqueID(), gameState)
|
||||
if err != nil {
|
||||
r.Errorf("PlayerConnectionRequestPacket: %v", err)
|
||||
}
|
||||
|
||||
err = r.SendPacketToServer(packet)
|
||||
|
||||
if err != nil {
|
||||
@ -92,7 +97,13 @@ func (r *RemoteClientConnection) Open(connectionString, saveFilePath string) err
|
||||
// RemoteClientConnection.active to false.
|
||||
func (r *RemoteClientConnection) Close() error {
|
||||
r.active = false
|
||||
err := r.SendPacketToServer(d2netpacket.CreatePlayerDisconnectRequestPacket(r.GetUniqueID()))
|
||||
|
||||
pd, err := d2netpacket.CreatePlayerDisconnectRequestPacket(r.GetUniqueID())
|
||||
if err != nil {
|
||||
return fmt.Errorf("PlayerDisconnectRequestPacket: %v", err)
|
||||
}
|
||||
|
||||
err = r.SendPacketToServer(pd)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -148,12 +159,12 @@ func (r *RemoteClientConnection) serverListener() {
|
||||
|
||||
p, err := r.decodeToPacket(packet.PacketType, string(packet.PacketData))
|
||||
if err != nil {
|
||||
r.Error(fmt.Sprintln(packet.PacketType, err))
|
||||
r.Errorf("%v %v", packet.PacketType, err)
|
||||
}
|
||||
|
||||
err = r.clientListener.OnPacketReceived(p)
|
||||
if err != nil {
|
||||
r.Error(fmt.Sprintln(packet.PacketType, err))
|
||||
r.Errorf("%v %v", packet.PacketType, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -171,8 +182,10 @@ func (r *RemoteClientConnection) bytesToJSON(buffer []byte) (string, d2netpacket
|
||||
|
||||
// decodeToPacket unmarshals the JSON string into the correct struct
|
||||
// and returns a NetPacket declaring that struct.
|
||||
// nolint:gocyclo // switch statement on packet type makes sense, no need to change
|
||||
func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType, data string) (d2netpacket.NetPacket, error) {
|
||||
// nolint:gocyclo,funlen // switch statement on packet type makes sense, no need to change
|
||||
func (r *RemoteClientConnection) decodeToPacket(
|
||||
t d2netpackettype.NetPacketType,
|
||||
data string) (d2netpacket.NetPacket, error) {
|
||||
var np = d2netpacket.NetPacket{}
|
||||
|
||||
var err error
|
||||
@ -184,7 +197,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
case d2netpackettype.MovePlayer:
|
||||
var p d2netpacket.MovePlayerPacket
|
||||
@ -192,7 +210,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
case d2netpackettype.UpdateServerInfo:
|
||||
var p d2netpacket.UpdateServerInfoPacket
|
||||
@ -200,7 +223,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
case d2netpackettype.AddPlayer:
|
||||
var p d2netpacket.AddPlayerPacket
|
||||
@ -208,7 +236,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
case d2netpackettype.CastSkill:
|
||||
var p d2netpacket.CastPacket
|
||||
@ -216,7 +249,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
case d2netpackettype.Ping:
|
||||
var p d2netpacket.PingPacket
|
||||
@ -224,7 +262,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
case d2netpackettype.PlayerDisconnectionNotification:
|
||||
var p d2netpacket.PlayerDisconnectRequestPacket
|
||||
@ -232,7 +275,12 @@ func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType,
|
||||
break
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
||||
mp, marshalErr := d2netpacket.MarshalPacket(p)
|
||||
if marshalErr != nil {
|
||||
r.Errorf("MarshalPacket: %v", marshalErr)
|
||||
}
|
||||
|
||||
np = d2netpacket.NetPacket{PacketType: t, PacketData: mp}
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("RemoteClientConnection: unrecognized packet type: %v", t)
|
||||
|
@ -432,8 +432,12 @@ func (g *GameClient) playCastOverlay(overlayRecord *d2records.OverlayRecord, x,
|
||||
}
|
||||
|
||||
func (g *GameClient) handlePingPacket() error {
|
||||
pongPacket := d2netpacket.CreatePongPacket(g.PlayerID)
|
||||
err := g.clientConnection.SendPacketToServer(pongPacket)
|
||||
pongPacket, err := d2netpacket.CreatePongPacket(g.PlayerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = g.clientConnection.SendPacketToServer(pongPacket)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -2,7 +2,6 @@ package d2netpackettype
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
)
|
||||
|
||||
// NetPacketType is an enum referring to all packet types in package
|
||||
@ -57,11 +56,11 @@ func (n NetPacketType) String() string {
|
||||
}
|
||||
|
||||
// MarshalPacket marshals the packet to a byte slice
|
||||
func (n NetPacketType) MarshalPacket() []byte {
|
||||
func (n NetPacketType) MarshalPacket() ([]byte, error) {
|
||||
p, err := json.Marshal(n)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
)
|
||||
@ -17,14 +16,14 @@ type NetPacket struct {
|
||||
}
|
||||
|
||||
// InspectPacketType determines the packet type from the given data
|
||||
func InspectPacketType(b []byte) d2netpackettype.NetPacketType {
|
||||
func InspectPacketType(b []byte) (d2netpackettype.NetPacketType, error) {
|
||||
var packet NetPacket
|
||||
|
||||
if err := json.Unmarshal(b, &packet); err != nil {
|
||||
log.Println(err)
|
||||
return d2netpackettype.UnknownPacketType, err
|
||||
}
|
||||
|
||||
return packet.PacketType
|
||||
return packet.PacketType, nil
|
||||
}
|
||||
|
||||
// UnmarshalNetPacket unmarshals the byte slice into a NetPacket struct
|
||||
@ -38,11 +37,11 @@ func UnmarshalNetPacket(packet []byte) (NetPacket, error) {
|
||||
}
|
||||
|
||||
// MarshalPacket is a quick helper function to Marshal very anything UNSAFELY, meaning the error is not checked before sending.
|
||||
func MarshalPacket(packet interface{}) []byte {
|
||||
func MarshalPacket(packet interface{}) ([]byte, error) {
|
||||
b, err := json.Marshal(packet)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b
|
||||
return b, nil
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
||||
@ -37,7 +36,7 @@ func CreateAddPlayerPacket(
|
||||
skills map[int]*d2hero.HeroSkill,
|
||||
equipment d2inventory.CharacterEquipment,
|
||||
leftSkill, rightSkill int,
|
||||
gold int) NetPacket {
|
||||
gold int) (NetPacket, error) {
|
||||
addPlayerPacket := AddPlayerPacket{
|
||||
ID: id,
|
||||
Name: name,
|
||||
@ -54,13 +53,13 @@ func CreateAddPlayerPacket(
|
||||
|
||||
b, err := json.Marshal(addPlayerPacket)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.AddPlayer}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.AddPlayer,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalAddPlayer unmarshals the packet data into an AddPlayerPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
@ -17,20 +16,20 @@ type GenerateMapPacket struct {
|
||||
|
||||
// CreateGenerateMapPacket returns a NetPacket which declares a
|
||||
// GenerateMapPacket with the given regionType.
|
||||
func CreateGenerateMapPacket(regionType d2enum.RegionIdType) NetPacket {
|
||||
func CreateGenerateMapPacket(regionType d2enum.RegionIdType) (NetPacket, error) {
|
||||
generateMapPacket := GenerateMapPacket{
|
||||
RegionType: regionType,
|
||||
}
|
||||
|
||||
b, err := json.Marshal(generateMapPacket)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.GenerateMap}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.GenerateMap,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalGenerateMap unmarshals the given packet data into a GenerateMapPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
)
|
||||
@ -16,7 +15,7 @@ type SpawnItemPacket struct {
|
||||
|
||||
// CreateSpawnItemPacket returns a NetPacket which declares a
|
||||
// SpawnItemPacket with the data in given parameters.
|
||||
func CreateSpawnItemPacket(x, y int, codes ...string) NetPacket {
|
||||
func CreateSpawnItemPacket(x, y int, codes ...string) (NetPacket, error) {
|
||||
spawnItemPacket := SpawnItemPacket{
|
||||
X: x,
|
||||
Y: y,
|
||||
@ -25,13 +24,13 @@ func CreateSpawnItemPacket(x, y int, codes ...string) NetPacket {
|
||||
|
||||
b, err := json.Marshal(spawnItemPacket)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.SpawnItem}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.SpawnItem,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalSpawnItem unmarshals the given data to a SpawnItemPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
)
|
||||
@ -20,7 +19,7 @@ type MovePlayerPacket struct {
|
||||
|
||||
// CreateMovePlayerPacket returns a NetPacket which declares a MovePlayerPacket
|
||||
// with the given ID and movement command.
|
||||
func CreateMovePlayerPacket(playerID string, startX, startY, destX, destY float64) NetPacket {
|
||||
func CreateMovePlayerPacket(playerID string, startX, startY, destX, destY float64) (NetPacket, error) {
|
||||
movePlayerPacket := MovePlayerPacket{
|
||||
PlayerID: playerID,
|
||||
StartX: startX,
|
||||
@ -31,13 +30,13 @@ func CreateMovePlayerPacket(playerID string, startX, startY, destX, destY float6
|
||||
|
||||
b, err := json.Marshal(movePlayerPacket)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.MovePlayer}, nil
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.MovePlayer,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalMovePlayer unmarshals the given data to a MovePlayerPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
@ -16,18 +15,18 @@ type PingPacket struct {
|
||||
|
||||
// CreatePingPacket returns a NetPacket which declares a GenerateMapPacket
|
||||
// with the the current time.
|
||||
func CreatePingPacket() NetPacket {
|
||||
func CreatePingPacket() (NetPacket, error) {
|
||||
ping := PingPacket{
|
||||
TS: time.Now(),
|
||||
}
|
||||
|
||||
b, err := json.Marshal(ping)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.Ping}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.Ping,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
)
|
||||
@ -20,7 +19,7 @@ type CastPacket struct {
|
||||
|
||||
// CreateCastPacket returns a NetPacket which declares a CastPacket with the
|
||||
// given skill command.
|
||||
func CreateCastPacket(entityID string, skillID int, targetX, targetY float64) NetPacket {
|
||||
func CreateCastPacket(entityID string, skillID int, targetX, targetY float64) (NetPacket, error) {
|
||||
castPacket := CastPacket{
|
||||
SourceEntityID: entityID,
|
||||
SkillID: skillID,
|
||||
@ -31,13 +30,13 @@ func CreateCastPacket(entityID string, skillID int, targetX, targetY float64) Ne
|
||||
|
||||
b, err := json.Marshal(castPacket)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.CastSkill}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.CastSkill,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalCast unmarshals the given data to a CastPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
||||
|
||||
@ -18,7 +17,7 @@ type PlayerConnectionRequestPacket struct {
|
||||
|
||||
// CreatePlayerConnectionRequestPacket returns a NetPacket which defines a
|
||||
// PlayerConnectionRequestPacket with the given ID and game state.
|
||||
func CreatePlayerConnectionRequestPacket(id string, playerState *d2hero.HeroState) NetPacket {
|
||||
func CreatePlayerConnectionRequestPacket(id string, playerState *d2hero.HeroState) (NetPacket, error) {
|
||||
playerConnectionRequest := PlayerConnectionRequestPacket{
|
||||
ID: id,
|
||||
PlayerState: playerState,
|
||||
@ -26,13 +25,13 @@ func CreatePlayerConnectionRequestPacket(id string, playerState *d2hero.HeroStat
|
||||
|
||||
b, err := json.Marshal(playerConnectionRequest)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.PlayerConnectionRequest}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.PlayerConnectionRequest,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalPlayerConnectionRequest unmarshals the given data to a
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
||||
|
||||
@ -18,20 +17,20 @@ type PlayerDisconnectRequestPacket struct {
|
||||
|
||||
// CreatePlayerDisconnectRequestPacket returns a NetPacket which defines a
|
||||
// PlayerDisconnectRequestPacket with the given ID.
|
||||
func CreatePlayerDisconnectRequestPacket(id string) NetPacket {
|
||||
func CreatePlayerDisconnectRequestPacket(id string) (NetPacket, error) {
|
||||
playerDisconnectRequest := PlayerDisconnectRequestPacket{
|
||||
ID: id,
|
||||
}
|
||||
|
||||
b, err := json.Marshal(playerDisconnectRequest)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.PlayerDisconnectionNotification}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.PlayerDisconnectionNotification,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalPlayerDisconnectionRequest unmarshals the given data to a
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
@ -17,7 +16,7 @@ type PongPacket struct {
|
||||
|
||||
// CreatePongPacket returns a NetPacket which declares a PongPacket with
|
||||
// the current time and given ID.
|
||||
func CreatePongPacket(id string) NetPacket {
|
||||
func CreatePongPacket(id string) (NetPacket, error) {
|
||||
pong := PongPacket{
|
||||
ID: id,
|
||||
TS: time.Now(),
|
||||
@ -25,13 +24,13 @@ func CreatePongPacket(id string) NetPacket {
|
||||
|
||||
b, err := json.Marshal(pong)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.Pong}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.Pong,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalPong unmarshals the given data to a PongPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapentity"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
@ -15,20 +14,20 @@ type SavePlayerPacket struct {
|
||||
}
|
||||
|
||||
// CreateSavePlayerPacket sends a packet which instructs the server to save the Player
|
||||
func CreateSavePlayerPacket(playerState *d2mapentity.Player) NetPacket {
|
||||
func CreateSavePlayerPacket(playerState *d2mapentity.Player) (NetPacket, error) {
|
||||
savePlayerData := SavePlayerPacket{
|
||||
Player: playerState,
|
||||
}
|
||||
|
||||
b, err := json.Marshal(savePlayerData)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.SavePlayer}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.SavePlayer,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalSavePlayer unmarshalls the given data to a SavePlayerPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
@ -16,20 +15,20 @@ type ServerClosedPacket struct {
|
||||
|
||||
// CreateServerClosedPacket returns a NetPacket which declares a
|
||||
// ServerClosedPacket with the current time.
|
||||
func CreateServerClosedPacket() NetPacket {
|
||||
func CreateServerClosedPacket() (NetPacket, error) {
|
||||
serverClosed := ServerClosedPacket{
|
||||
TS: time.Now(),
|
||||
}
|
||||
|
||||
b, err := json.Marshal(serverClosed)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.ServerClosed}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.ServerClosed,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalServerClosed unmarshals the given data to a ServerClosedPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
)
|
||||
@ -12,18 +11,18 @@ import (
|
||||
type ServerFullPacket struct{}
|
||||
|
||||
// CreateServerFullPacket returns a NetPacket which declares a ServerFullPacket.
|
||||
func CreateServerFullPacket() NetPacket {
|
||||
func CreateServerFullPacket() (NetPacket, error) {
|
||||
serverClosed := ServerFullPacket{}
|
||||
|
||||
b, err := json.Marshal(serverClosed)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.ServerFull}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.ServerFull,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalServerFull unmarshalls the given data to a ServerFullPacket struct
|
||||
|
@ -2,7 +2,6 @@ package d2netpacket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
||||
)
|
||||
@ -16,7 +15,7 @@ type UpdateServerInfoPacket struct {
|
||||
|
||||
// CreateUpdateServerInfoPacket returns a NetPacket which declares an
|
||||
// UpdateServerInfoPacket with the given player ID and map seed.
|
||||
func CreateUpdateServerInfoPacket(seed int64, playerID string) NetPacket {
|
||||
func CreateUpdateServerInfoPacket(seed int64, playerID string) (NetPacket, error) {
|
||||
updateServerInfo := UpdateServerInfoPacket{
|
||||
Seed: seed,
|
||||
PlayerID: playerID,
|
||||
@ -24,13 +23,13 @@ func CreateUpdateServerInfoPacket(seed int64, playerID string) NetPacket {
|
||||
|
||||
b, err := json.Marshal(updateServerInfo)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return NetPacket{PacketType: d2netpackettype.UpdateServerInfo}, err
|
||||
}
|
||||
|
||||
return NetPacket{
|
||||
PacketType: d2netpackettype.UpdateServerInfo,
|
||||
PacketData: b,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UnmarshalUpdateServerInfo unmarshals the data to a UpdateServerInfoPacket struct
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
||||
@ -14,8 +13,12 @@ import (
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
||||
)
|
||||
|
||||
const logPrefix = "UDP Connection"
|
||||
|
||||
// UDPClientConnection is the implementation of the
|
||||
// d2server.ClientConnection interface to represent remote client from the
|
||||
// server perspective.
|
||||
@ -24,17 +27,23 @@ type UDPClientConnection struct {
|
||||
address *net.UDPAddr // IP address of the associated RemoteClientConnection
|
||||
udpConnection *net.UDPConn // Server's UDP Connection
|
||||
playerState *d2hero.HeroState // Client's game state
|
||||
|
||||
*d2util.Logger
|
||||
}
|
||||
|
||||
// CreateUDPClientConnection constructs a new UDPClientConnection and
|
||||
// returns a pointer to it.
|
||||
func CreateUDPClientConnection(udpConnection *net.UDPConn, id string, address *net.UDPAddr) *UDPClientConnection {
|
||||
func CreateUDPClientConnection(udpConnection *net.UDPConn, id string, l d2util.LogLevel, address *net.UDPAddr) *UDPClientConnection {
|
||||
result := &UDPClientConnection{
|
||||
id: id,
|
||||
address: address,
|
||||
udpConnection: udpConnection,
|
||||
}
|
||||
|
||||
result.Logger = d2util.NewLogger()
|
||||
result.Logger.SetPrefix(logPrefix)
|
||||
result.Logger.SetLevel(l)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@ -63,7 +72,7 @@ func (u *UDPClientConnection) SendPacketToClient(packet d2netpacket.NetPacket) e
|
||||
|
||||
writer, err := gzip.NewWriterLevel(&buff, gzip.BestCompression)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
u.Error(err.Error())
|
||||
}
|
||||
|
||||
if written, writeErr := writer.Write(data); writeErr != nil {
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
@ -112,7 +111,7 @@ func NewGameServer(asset *d2asset.AssetManager,
|
||||
gameServer.scriptEngine.AddFunction("getMapEngines", func(call otto.FunctionCall) otto.Value {
|
||||
val, err := gameServer.scriptEngine.ToValue(gameServer.mapEngines)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
gameServer.Error(err.Error())
|
||||
}
|
||||
return val
|
||||
})
|
||||
@ -174,7 +173,12 @@ func (g *GameServer) packetManager() {
|
||||
case <-g.ctx.Done():
|
||||
return
|
||||
case p := <-g.packetManagerChan:
|
||||
switch d2netpacket.InspectPacketType(p) {
|
||||
ipt, err := d2netpacket.InspectPacketType(p)
|
||||
if err != nil {
|
||||
g.Errorf("InspectPacketType: %v", err)
|
||||
}
|
||||
|
||||
switch ipt {
|
||||
case d2netpackettype.PlayerConnectionRequest:
|
||||
player, err := d2netpacket.UnmarshalNetPacket(p)
|
||||
if err != nil {
|
||||
@ -256,10 +260,20 @@ func (g *GameServer) handleConnection(conn net.Conn) {
|
||||
if err := g.registerConnection(packet.PacketData, conn); err != nil {
|
||||
switch err {
|
||||
case errServerFull: // Server is currently full and not accepting new connections.
|
||||
_, errServerFullPacket := conn.Write(d2netpacket.MarshalPacket(d2netpacket.CreateServerFullPacket()))
|
||||
g.Error(fmt.Sprintln(errServerFullPacket))
|
||||
sf, serverFullErr := d2netpacket.CreateServerFullPacket()
|
||||
if serverFullErr != nil {
|
||||
g.Errorf("ServerFullPacket: %v", serverFullErr)
|
||||
}
|
||||
|
||||
msf, marshalServerFullErr := d2netpacket.MarshalPacket(sf)
|
||||
if marshalServerFullErr != nil {
|
||||
g.Errorf("MarshalPacket: %v", marshalServerFullErr)
|
||||
}
|
||||
|
||||
_, errServerFullPacket := conn.Write(msf)
|
||||
g.Warningf("%v", errServerFullPacket)
|
||||
case errPlayerAlreadyExists: // Player is already registered and did not disconnection correctly.
|
||||
g.Error(fmt.Sprintln(err))
|
||||
g.Errorf("%v", err)
|
||||
}
|
||||
|
||||
return
|
||||
@ -347,12 +361,22 @@ func (g *GameServer) OnClientConnected(client ClientConnection) {
|
||||
}
|
||||
|
||||
func (g *GameServer) handleClientConnection(client ClientConnection, x, y float64) {
|
||||
err := client.SendPacketToClient(d2netpacket.CreateUpdateServerInfoPacket(g.seed, client.GetUniqueID()))
|
||||
usi, err := d2netpacket.CreateUpdateServerInfoPacket(g.seed, client.GetUniqueID())
|
||||
if err != nil {
|
||||
g.Errorf("UpdateServerInfoPacket: %v", err)
|
||||
}
|
||||
|
||||
err = client.SendPacketToClient(usi)
|
||||
if err != nil {
|
||||
g.Errorf("GameServer: error sending UpdateServerInfoPacket to client %s: %s", client.GetUniqueID(), err)
|
||||
}
|
||||
|
||||
err = client.SendPacketToClient(d2netpacket.CreateGenerateMapPacket(d2enum.RegionAct1Town))
|
||||
gmp, err := d2netpacket.CreateGenerateMapPacket(d2enum.RegionAct1Town)
|
||||
if err != nil {
|
||||
g.Errorf("GenerateMapPacket: %v", err)
|
||||
}
|
||||
|
||||
err = client.SendPacketToClient(gmp)
|
||||
if err != nil {
|
||||
g.Errorf("GameServer: error sending GenerateMapPacket to client %s: %s", client.GetUniqueID(), err)
|
||||
}
|
||||
@ -365,7 +389,7 @@ func (g *GameServer) handleClientConnection(client ClientConnection, x, y float6
|
||||
|
||||
d2hero.HydrateSkills(playerState.Skills, g.asset)
|
||||
|
||||
createPlayerPacket := d2netpacket.CreateAddPlayerPacket(
|
||||
createPlayerPacket, err := d2netpacket.CreateAddPlayerPacket(
|
||||
client.GetUniqueID(),
|
||||
playerState.HeroName,
|
||||
playerX,
|
||||
@ -378,6 +402,9 @@ func (g *GameServer) handleClientConnection(client ClientConnection, x, y float6
|
||||
playerState.RightSkill,
|
||||
playerState.Gold,
|
||||
)
|
||||
if err != nil {
|
||||
g.Errorf("AddPlayerPacket: %v", err)
|
||||
}
|
||||
|
||||
for _, connection := range g.connections {
|
||||
err := connection.SendPacketToClient(createPlayerPacket)
|
||||
@ -392,22 +419,26 @@ func (g *GameServer) handleClientConnection(client ClientConnection, x, y float6
|
||||
conPlayerState := connection.GetPlayerState()
|
||||
playerX := int(conPlayerState.X*subtilesPerTile) + middleOfTileOffset
|
||||
playerY := int(conPlayerState.Y*subtilesPerTile) + middleOfTileOffset
|
||||
err = client.SendPacketToClient(
|
||||
d2netpacket.CreateAddPlayerPacket(
|
||||
connection.GetUniqueID(),
|
||||
conPlayerState.HeroName,
|
||||
playerX,
|
||||
playerY,
|
||||
conPlayerState.HeroType,
|
||||
conPlayerState.Stats,
|
||||
conPlayerState.Skills,
|
||||
conPlayerState.Equipment,
|
||||
conPlayerState.LeftSkill,
|
||||
conPlayerState.RightSkill,
|
||||
conPlayerState.Gold,
|
||||
),
|
||||
app, err := d2netpacket.CreateAddPlayerPacket(
|
||||
connection.GetUniqueID(),
|
||||
conPlayerState.HeroName,
|
||||
playerX,
|
||||
playerY,
|
||||
conPlayerState.HeroType,
|
||||
conPlayerState.Stats,
|
||||
conPlayerState.Skills,
|
||||
conPlayerState.Equipment,
|
||||
conPlayerState.LeftSkill,
|
||||
conPlayerState.RightSkill,
|
||||
conPlayerState.Gold,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
g.Errorf("AddPlayerPacket: %v", err)
|
||||
}
|
||||
|
||||
err = client.SendPacketToClient(app)
|
||||
|
||||
if err != nil {
|
||||
g.Errorf("GameServer: error sending CreateAddPlayerPacket to client %s: %s", connection.GetUniqueID(), err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user