1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

fix: data race in netLoop.

This commit is contained in:
wdvxdr 2021-07-24 20:13:18 +08:00
parent 4e350e6b2a
commit cd9830b977
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
4 changed files with 21 additions and 24 deletions

View File

@ -262,7 +262,6 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient {
cli.TCP.PlannedDisconnect(cli.plannedDisconnect)
cli.TCP.UnexpectedDisconnect(cli.unexpectedDisconnect)
rand.Read(cli.RandomKey)
go cli.netLoop()
return cli
}
@ -451,23 +450,6 @@ func (c *QQClient) init(tokenLogin bool) error {
}
seq, pkt := c.buildGetMessageRequestPacket(msg.SyncFlag_START, time.Now().Unix())
_, _ = c.sendAndWait(seq, pkt, requestParams{"used_reg_proxy": true, "init": true})
c.once.Do(func() {
c.OnGroupMessage(func(_ *QQClient, _ *message.GroupMessage) {
atomic.AddUint64(&c.stat.MessageReceived, 1)
atomic.StoreInt64(&c.stat.LastMessageTime, time.Now().Unix())
})
c.OnPrivateMessage(func(_ *QQClient, _ *message.PrivateMessage) {
atomic.AddUint64(&c.stat.MessageReceived, 1)
atomic.StoreInt64(&c.stat.LastMessageTime, time.Now().Unix())
})
c.OnTempMessage(func(_ *QQClient, _ *TempMessageEvent) {
atomic.AddUint64(&c.stat.MessageReceived, 1)
atomic.StoreInt64(&c.stat.LastMessageTime, time.Now().Unix())
})
c.onGroupMessageReceipt("internal", func(_ *QQClient, _ *groupMessageReceiptEvent) {
atomic.AddUint64(&c.stat.MessageSent, 1)
})
})
return nil
}

View File

@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/MiraiGo/protocol/packets"
"github.com/Mrs4s/MiraiGo/utils"
)
@ -27,6 +28,24 @@ func (c *QQClient) connect() error {
c.Error("connect server error: %v", err)
return err
}
c.once.Do(func() {
c.OnGroupMessage(func(_ *QQClient, _ *message.GroupMessage) {
atomic.AddUint64(&c.stat.MessageReceived, 1)
atomic.StoreInt64(&c.stat.LastMessageTime, time.Now().Unix())
})
c.OnPrivateMessage(func(_ *QQClient, _ *message.PrivateMessage) {
atomic.AddUint64(&c.stat.MessageReceived, 1)
atomic.StoreInt64(&c.stat.LastMessageTime, time.Now().Unix())
})
c.OnTempMessage(func(_ *QQClient, _ *TempMessageEvent) {
atomic.AddUint64(&c.stat.MessageReceived, 1)
atomic.StoreInt64(&c.stat.LastMessageTime, time.Now().Unix())
})
c.onGroupMessageReceipt("internal", func(_ *QQClient, _ *groupMessageReceiptEvent) {
atomic.AddUint64(&c.stat.MessageSent, 1)
})
go c.netLoop()
})
c.retryTimes = 0
c.ConnectTime = time.Now()
return nil

View File

@ -1,6 +1,7 @@
package utils
import (
"encoding/binary"
"io"
"net"
"time"
@ -74,7 +75,7 @@ func (t *TCPListener) ReadInt32() (int32, error) {
if err != nil {
return 0, err
}
return (int32(b[0]) << 24) | (int32(b[1]) << 16) | (int32(b[2]) << 8) | int32(b[3]), nil
return int32(binary.BigEndian.Uint32(b)), nil
}
func (t *TCPListener) Close() {

View File

@ -5,14 +5,9 @@ import (
"reflect"
"strconv"
"strings"
"time"
"unsafe"
)
func init() {
rand.Seed(time.Now().Unix())
}
func RandomString(len int) string {
return RandomStringRange(len, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
}