Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/gateway/cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"github.com/walkline/ToCloud9/apps/gateway/service"
"github.com/walkline/ToCloud9/apps/gateway/session"
"github.com/walkline/ToCloud9/apps/gateway/sockets/gamesocket"
pbAH "github.com/walkline/ToCloud9/gen/auctionhouse/pb"
pbChar "github.com/walkline/ToCloud9/gen/characters/pb"
pbChat "github.com/walkline/ToCloud9/gen/chat/pb"
pbGroup "github.com/walkline/ToCloud9/gen/group/pb"
pbGuild "github.com/walkline/ToCloud9/gen/guilds/pb"
pbAH "github.com/walkline/ToCloud9/gen/auctionhouse/pb"
pbMail "github.com/walkline/ToCloud9/gen/mail/pb"
pbMM "github.com/walkline/ToCloud9/gen/matchmaking/pb"
pbServ "github.com/walkline/ToCloud9/gen/servers-registry/pb"
Expand Down Expand Up @@ -179,6 +179,7 @@ func main() {
GameServerGRPCConnMgr: gameserverconn.DefaultGameServerGRPCConnMgr,
PacketProcessTimeout: time.Second * time.Duration(conf.PacketProcessTimeoutSecs),
ShowGameserverConnChangeToClient: conf.ShowGameserverConnChangeToClient,
SeamlessLayerSwitch: conf.SeamlessLayerSwitch,
})
go func() {
healthandmetrics.ActiveConnectionsMetrics.Inc()
Expand Down
4 changes: 4 additions & 0 deletions apps/gateway/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Config struct {

// ShowGameserverConnChangeToClient when enabled sends chat system message to the player with information about connection change.
ShowGameserverConnChangeToClient bool `yaml:"showGameserverConnChangeToClient" env:"SHOW_GAMESERVER_CONN_CHANGE_TO_CLIENT" env-default:"true"`

// SeamlessLayerSwitch enables native visibility transitions during same-map
// layer handoffs instead of sending SMSG_NEW_WORLD.
SeamlessLayerSwitch bool `yaml:"seamlessLayerSwitch" env:"SEAMLESS_LAYER_SWITCH" env-default:"false"`
}

func (c Config) PortInt() (p int) {
Expand Down
17 changes: 17 additions & 0 deletions apps/gateway/packet/tc9_redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package packet

// The original TC9 redirect exchange is unversioned:
//
// request: empty
// response: uint8 status
//
// Version 2 adds option negotiation while retaining status as the first
// response byte, so a new gateway can detect an older core and fall back:
//
// request: uint8 version, uint8 requestedOptions
// response: uint8 status, uint8 version, uint8 acceptedOptions
const (
TC9RedirectVersionedRequest uint8 = 2
TC9RedirectOptionSeamless uint8 = 1 << 0
TC9RedirectSupportedOptions = TC9RedirectOptionSeamless
)
78 changes: 63 additions & 15 deletions apps/gateway/session/layering.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,23 @@ func (s *GameSession) layerPlayerRedirect(ctx context.Context, characterGUID uin
}

oldSocket := s.worldSocket
oldSocket.Send(packet.NewWriterWithSize(packet.TC9CMsgPrepareForRedirect, 0))
if err := waitForWorldServerRedirect(ctx, oldSocket); err != nil {
prepareRedirect := packet.NewWriterWithSize(packet.TC9CMsgPrepareForRedirect, 0)
if s.seamlessLayerSwitch {
prepareRedirect = packet.NewWriterWithSize(packet.TC9CMsgPrepareForRedirect, 2).
Uint8(packet.TC9RedirectVersionedRequest).
Uint8(packet.TC9RedirectOptionSeamless)
}
oldSocket.Send(prepareRedirect)
var onSourcePacket func(*packet.Packet)
if s.seamlessLayerSwitch {
onSourcePacket = s.forwardLayerTransitionPacket
}
acceptedOptions, err := waitForWorldServerRedirect(ctx, oldSocket, onSourcePacket)
if err != nil {
return fmt.Errorf("prepare layer redirect for account %d: %w", s.accountID, err)
}
seamlessRedirect := s.seamlessLayerSwitch &&
acceptedOptions&packet.TC9RedirectOptionSeamless != 0

oldSocket.Close()
s.worldSocket = nil
Expand All @@ -79,13 +92,15 @@ func (s *GameSession) layerPlayerRedirect(ctx context.Context, characterGUID uin
return fmt.Errorf("connect to layer gameserver %s: %w", address, err)
}

newWorld := packet.NewWriterWithSize(packet.SMsgNewWorld, 0)
newWorld.Uint32(s.character.Map)
newWorld.Float32(s.character.PositionX)
newWorld.Float32(s.character.PositionY)
newWorld.Float32(s.character.PositionZ)
newWorld.Float32(s.character.PositionO)
s.gameSocket.Send(newWorld)
if !seamlessRedirect {
newWorld := packet.NewWriterWithSize(packet.SMsgNewWorld, 0)
newWorld.Uint32(s.character.Map)
newWorld.Float32(s.character.PositionX)
newWorld.Float32(s.character.PositionY)
newWorld.Float32(s.character.PositionZ)
newWorld.Float32(s.character.PositionO)
s.gameSocket.Send(newWorld)
}

firstPacket, err := waitForFirstWorldPacket(ctx, newSocket)
if err != nil {
Expand All @@ -106,23 +121,56 @@ func (s *GameSession) layerPlayerRedirect(ctx context.Context, characterGUID uin
return nil
}

func waitForWorldServerRedirect(ctx context.Context, socket sockets.Socket) error {
// forwardLayerTransitionPacket keeps the client's current world stream alive
// while the source core prepares a seamless redirect. In particular, an
// AzerothCore phase change emits the native out-of-range updates that make the
// old layer fade out. The gateway deliberately treats these packets as opaque:
// it neither parses nor retains any visible-world state.
func (s *GameSession) forwardLayerTransitionPacket(p *packet.Packet) {
if p == nil || s.gameSocket == nil || OpcodeBlacklist[p.Opcode] {
return
}
s.gameSocket.WriteChannel() <- p
}

func waitForWorldServerRedirect(ctx context.Context, socket sockets.Socket, onPacket func(*packet.Packet)) (uint8, error) {
confirmationContext, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

for {
select {
case <-confirmationContext.Done():
return confirmationContext.Err()
return 0, confirmationContext.Err()
case response, open := <-socket.ReadChannel():
if !open {
return nil
return 0, nil
}
if response.Opcode == packet.TC9SMsgReadyForRedirect {
if response.Reader().Uint8() != 0 {
return fmt.Errorf("worldserver rejected redirect")
reader := response.Reader()
status := reader.Uint8()
if reader.Error() != nil {
return 0, fmt.Errorf("worldserver returned malformed redirect acknowledgement")
}
if status != 0 {
return 0, fmt.Errorf("worldserver rejected redirect")
}
return nil

// A one-byte success is the legacy acknowledgement. The redirect is
// still valid, but the gateway must use SMSG_NEW_WORLD because the
// source core did not explicitly accept the seamless option.
if reader.Left() == 0 {
return 0, nil
}

version := reader.Uint8()
acceptedOptions := reader.Uint8()
if reader.Error() != nil || reader.Left() != 0 || version != packet.TC9RedirectVersionedRequest {
return 0, nil
}
return acceptedOptions & packet.TC9RedirectSupportedOptions, nil
}
if onPacket != nil {
onPacket(response)
}
}
}
Expand Down
Loading
Loading