Skip to content

Commit

Permalink
Don't set lobby on failed reconnect. (#67)
Browse files Browse the repository at this point in the history
We still had 2 lingering lobbies, trying to get that to 0.
  • Loading branch information
koenbollen authored Oct 27, 2023
1 parent 3cae519 commit 1d8cf39
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/signaling/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
}
defer func() {
logger.Info("peer websocket closed", zap.String("peer", peer.ID), zap.String("game", peer.Game), zap.String("origin", r.Header.Get("Origin")))
conn.Close(websocket.StatusInternalError, "unexpceted closure")
conn.Close(websocket.StatusInternalError, "unexpected closure")

if !peer.closedPacketReceived {
// At this point ctx has already been cancelled, so we create a new one to use for the disconnect.
Expand Down
25 changes: 11 additions & 14 deletions internal/signaling/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Peer struct {

closedPacketReceived bool

retrievedIDCallback func(context.Context, *Peer) (bool, []string, error)
retrievedIDCallback func(context.Context, string, string, string) (bool, []string, error)

ID string
Secret string
Expand Down Expand Up @@ -205,30 +205,27 @@ func (p *Peer) HandleHelloPacket(ctx context.Context, packet HelloPacket) error
if !util.IsUUID(packet.Game) {
return fmt.Errorf("no game id supplied")
}
p.Game = packet.Game

hasReconnected := false
clientIsReconnecting := false
var reconnectingLobbies []string
if packet.ID != "" && packet.Secret != "" {
clientIsReconnecting = true
p.ID = packet.ID
p.Secret = packet.Secret
logger.Info("peer reconnecting", zap.String("game", p.Game), zap.String("peer", p.ID))
} else {
p.ID = util.GeneratePeerID(ctx)
p.Secret = util.GenerateSecret(ctx)
logger.Info("peer connecting", zap.String("game", p.Game), zap.String("peer", p.ID))
}
if clientIsReconnecting {
logger.Info("peer reconnecting", zap.String("game", packet.Game), zap.String("peer", packet.ID))
var err error
hasReconnected, reconnectingLobbies, err = p.retrievedIDCallback(ctx, p)
hasReconnected, reconnectingLobbies, err = p.retrievedIDCallback(ctx, packet.ID, packet.Secret, packet.Game)
if err != nil {
return fmt.Errorf("unable to reconnect: %w", err)
}
if !hasReconnected {
return fmt.Errorf("unable to reconnect")
}
p.Game = packet.Game
p.ID = packet.ID
p.Secret = packet.Secret
} else {
p.Game = packet.Game
p.ID = util.GeneratePeerID(ctx)
p.Secret = util.GenerateSecret(ctx)
logger.Info("peer connecting", zap.String("game", p.Game), zap.String("peer", p.ID))
}

if hasReconnected && len(reconnectingLobbies) > 0 && reconnectingLobbies[0] != "" {
Expand Down
6 changes: 3 additions & 3 deletions internal/signaling/timeout_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func (i *TimeoutManager) Disconnected(ctx context.Context, p *Peer) {
}
}

func (i *TimeoutManager) Reconnected(ctx context.Context, p *Peer) (bool, []string, error) {
func (i *TimeoutManager) Reconnected(ctx context.Context, id, secret, game string) (bool, []string, error) {
logger := logging.GetLogger(ctx)

logger.Debug("peer marked as reconnected", zap.String("id", p.ID))
return i.Store.ReconnectPeer(ctx, p.ID, p.Secret, p.Game)
logger.Debug("peer marked as reconnected", zap.String("id", id))
return i.Store.ReconnectPeer(ctx, id, secret, game)
}

0 comments on commit 1d8cf39

Please sign in to comment.