Skip to content

Commit

Permalink
Send acks before sending set active request
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 5, 2024
1 parent 94e59bb commit 96c09b4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
16 changes: 7 additions & 9 deletions libgm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/http"
"net/url"
"sync/atomic"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -75,8 +74,6 @@ type Client struct {

conversationsFetchedOnce bool

hackyDelaySetActive atomic.Bool

AuthData *AuthData
cfg *gmproto.Config

Expand Down Expand Up @@ -156,18 +153,19 @@ func (c *Client) Connect() error {
// return fmt.Errorf("failed to get web encryption key: %w", err)
//}
//c.updateWebEncryptionKey(webEncryptionKeyResponse.GetKey())
go c.doLongPoll(true)
go c.doLongPoll(true, c.postConnect)
c.sessionHandler.startAckInterval()
go c.postConnect()
return nil
}

func (c *Client) postConnect() {
// For some reason SetActiveSession fails if it's called immediately after reconnecting after a google login,
// so hackily delay it a few seconds to make it work.
if c.hackyDelaySetActive.CompareAndSwap(true, false) {
time.Sleep(3 * time.Second)
time.Sleep(2 * time.Second)
c.Logger.Debug().Msg("Sending acks before get updates request")
if c.skipCount > 0 {
c.Logger.Warn().Int("skip_count", c.skipCount).Msg("Skip count is still non-zero")
}
c.sessionHandler.sendAckRequest()
time.Sleep(1 * time.Second)
c.Logger.Debug().Msg("Sending get updates request")
err := c.SetActiveSession()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion libgm/longpoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func tryReadBody(resp io.ReadCloser) []byte {
return data
}

func (c *Client) doLongPoll(loggedIn bool) {
func (c *Client) doLongPoll(loggedIn bool, onFirstConnect func()) {
c.listenID++
listenID := c.listenID
listenReqID := uuid.NewString()
Expand Down Expand Up @@ -304,6 +304,10 @@ func (c *Client) doLongPoll(loggedIn bool) {
log.Debug().Msg("Ditto pinger is still waiting for previous ping, skipping new ping")
}
}
if onFirstConnect != nil {
go onFirstConnect()
onFirstConnect = nil
}
c.readLongPoll(&log, resp.Body)
c.longPollingConn = nil
}
Expand Down
2 changes: 1 addition & 1 deletion libgm/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Client) StartLogin() (string, error) {
return "", err
}
c.updateTachyonAuthToken(registered.GetAuthKeyData())
go c.doLongPoll(false)
go c.doLongPoll(false, nil)
qr, err := c.GenerateQRCodeData(registered.GetPairingKey())
if err != nil {
return "", fmt.Errorf("failed to generate QR code: %w", err)
Expand Down
7 changes: 5 additions & 2 deletions libgm/pair_google.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"fmt"
"io"
"math/big"
"sync"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -267,7 +268,10 @@ func (c *Client) DoGaiaPairing(ctx context.Context, emojiCallback func(string))
return fmt.Errorf("failed to parse destination UUID: %w", err)
}
c.AuthData.DestRegID = destRegUUID
go c.doLongPoll(false)
var longPollConnectWait sync.WaitGroup
longPollConnectWait.Add(1)
go c.doLongPoll(false, longPollConnectWait.Done)
longPollConnectWait.Wait()
ps := NewPairingSession()
clientInit, clientFinish, err := ps.PreparePayloads()
if err != nil {
Expand Down Expand Up @@ -300,7 +304,6 @@ func (c *Client) DoGaiaPairing(ctx context.Context, emojiCallback func(string))
c.AuthData.PairingID = ps.UUID
c.triggerEvent(&events.PairSuccessful{PhoneID: c.AuthData.Mobile.GetSourceID()})

c.hackyDelaySetActive.Store(true)
go func() {
// Sleep for a bit to let the phone save the pair data. If we reconnect too quickly,
// the phone won't recognize the session the bridge will get unpaired.
Expand Down
2 changes: 1 addition & 1 deletion libgm/session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (s *SessionHandler) queueMessageAck(messageID string) {

func (s *SessionHandler) startAckInterval() {
if s.ackTicker != nil {
s.ackTicker.Stop()
return
}
ticker := time.NewTicker(5 * time.Second)
s.ackTicker = ticker
Expand Down

0 comments on commit 96c09b4

Please sign in to comment.