Skip to content

Add version to hello packet #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions internal/signaling/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ func (p *Peer) HandleHelloPacket(ctx context.Context, packet HelloPacket) error
hasReconnected := false
var reconnectingLobbies []string
if packet.ID != "" && packet.Secret != "" {
logger.Info("peer reconnecting", zap.String("game", packet.Game), zap.String("peer", packet.ID))
logger.Info("peer reconnecting", zap.String("game", packet.Game), zap.String("peer", packet.ID), zap.String("version", packet.Version))
var err error
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 {
logger.Info("peer failed reconnecting", zap.String("game", p.Game), zap.String("peer", p.ID))
logger.Info("peer failed reconnecting", zap.String("game", p.Game), zap.String("peer", p.ID), zap.String("version", packet.Version))

err := fmt.Errorf("failed to reconnect, missing pid or invalid secret")
err = util.ErrorWithCode(err, "reconnect-failed")
Expand All @@ -213,7 +213,7 @@ func (p *Peer) HandleHelloPacket(ctx context.Context, packet HelloPacket) error
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))
logger.Info("peer connecting", zap.String("game", p.Game), zap.String("peer", p.ID), zap.String("version", packet.Version))

if err := p.store.CreatePeer(ctx, p.ID, p.Secret, p.Game); err != nil {
return fmt.Errorf("unable to create peer: %w", err)
Expand All @@ -231,11 +231,11 @@ func (p *Peer) HandleHelloPacket(ctx context.Context, packet HelloPacket) error

if hasReconnected {
for _, lobbyID := range reconnectingLobbies {
logger.Info("peer rejoining lobby", zap.String("game", p.Game), zap.String("peer", p.ID), zap.String("lobby", p.Lobby))
logger.Info("peer rejoining lobby", zap.String("game", p.Game), zap.String("peer", p.ID), zap.String("lobby", p.Lobby), zap.String("version", packet.Version))
p.Lobby = lobbyID
p.store.Subscribe(ctx, p.ForwardMessage, p.Game, p.Lobby, p.ID)

go metrics.Record(ctx, "client", "reconnected", p.Game, p.ID, p.Lobby)
go metrics.Record(ctx, "client", "reconnected", p.Game, p.ID, p.Lobby, "version", packet.Version)

// We just reconnected, and we might be the only peer in the lobby.
// So do an election to make sure we then become the leader.
Expand Down Expand Up @@ -264,7 +264,7 @@ func (p *Peer) HandleHelloPacket(ctx context.Context, packet HelloPacket) error
}
}
} else {
go metrics.Record(ctx, "client", "connected", p.Game, p.ID, p.Lobby)
go metrics.Record(ctx, "client", "connected", p.Game, p.ID, p.Lobby, "version", packet.Version)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions internal/signaling/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type PingPacket struct {
type HelloPacket struct {
Type string `json:"type"`

Game string `json:"game"`
ID string `json:"id"`
Secret string `json:"secret"`
Game string `json:"game"`
ID string `json:"id"`
Secret string `json:"secret"`
Version string `json:"version,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why omitempty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ask ChatGPT 🙈

I already fixed some mistakes it made, didn't notice this once. AI is not smart enough yet to understand it's a struct for incoming data only and omitempty doesn't do anything.

}

type WelcomePacket struct {
Expand Down
4 changes: 3 additions & 1 deletion lib/signaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter } from 'eventemitter3'
import Network from './network'
import Peer from './peer'
import { LobbyListEntry, SignalingPacketTypes } from './types'
import { version } from '../package.json'

interface SignalingListeners {
credentials: (data: SignalingPacketTypes) => void | Promise<void>
Expand Down Expand Up @@ -50,7 +51,8 @@ export default class Signaling extends EventEmitter<SignalingListeners> {
type: 'hello',
game: this.network.gameID,
id: this.receivedID,
secret: this.receivedSecret
secret: this.receivedSecret,
version
})
}
const onError = (e: Event): void => {
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface HelloPacket extends Base {
game: string
id?: string
secret?: string
version?: string
}

export interface WelcomePacket extends Base {
Expand Down