Skip to content
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

Make sure messageID never be lesser than previous messageID (code 33) #134

Open
wants to merge 3 commits into
base: dev
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
8 changes: 6 additions & 2 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import (

// GenerateMessageId отдает по сути unix timestamp но ужасно специфическим образом
// TODO: нахуя нужно битовое и на -4??
func GenerateMessageId() int64 {
func GenerateMessageId(prevID int64) int64 {
const billion = 1000 * 1000 * 1000
unixnano := time.Now().UnixNano()
seconds := unixnano / billion
nanoseconds := unixnano % billion
return (seconds << 32) | (nanoseconds & -4)
newID := (seconds << 32) | (nanoseconds & -4)
if newID <= prevID {
return GenerateMessageId(prevID)
}
return newID
}

func AuthKeyHash(key []byte) []byte {
Expand Down
2 changes: 2 additions & 0 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type MTProto struct {
// идентификаторы сообщений, нужны что бы посылать и принимать сообщения.
seqNoMutex sync.Mutex
seqNo int32
lastMessageIDMutex sync.Mutex
lastMessageID int64

// айдишники DC для КОНКРЕТНОГО Приложения и клиента. Может меняться, но фиксирована для
// связки приложение+клиент
Expand Down
5 changes: 4 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ func (m *MTProto) sendPacket(request tl.Object, expectedTypes ...reflect.Type) (
return nil, errors.Wrap(err, "encoding request message")
}

m.lastMessageIDMutex.Lock()
var (
data messages.Common
msgID = utils.GenerateMessageId()
msgID = utils.GenerateMessageId(m.lastMessageID)
)
m.lastMessageIDMutex.Unlock()
m.lastMessageID = msgID

// adding types for parser if required
if len(expectedTypes) > 0 {
Expand Down