diff --git a/internal/signaling/handler.go b/internal/signaling/handler.go index c460f85..a77e13f 100644 --- a/internal/signaling/handler.go +++ b/internal/signaling/handler.go @@ -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. diff --git a/internal/signaling/peer.go b/internal/signaling/peer.go index bcb2eb5..92e0f8f 100644 --- a/internal/signaling/peer.go +++ b/internal/signaling/peer.go @@ -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 @@ -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] != "" { diff --git a/internal/signaling/timeout_manager.go b/internal/signaling/timeout_manager.go index 5c523f1..82760b2 100644 --- a/internal/signaling/timeout_manager.go +++ b/internal/signaling/timeout_manager.go @@ -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) }