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

client: shrink event handler slice

This commit is contained in:
wdvxdr 2022-03-02 21:41:46 +08:00
parent 7699c32258
commit 348e317a34
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
5 changed files with 40 additions and 36 deletions

View File

@ -24,7 +24,7 @@ func SelectWriter() *Writer {
// PutWriter 将 Writer 放回池中 // PutWriter 将 Writer 放回池中
func PutWriter(w *Writer) { func PutWriter(w *Writer) {
// See https://golang.org/issue/23199 // See https://golang.org/issue/23199
const maxSize = 1 << 16 const maxSize = 32 * 1024
if (*bytes.Buffer)(w).Cap() < maxSize { // 对于大Buffer直接丢弃 if (*bytes.Buffer)(w).Cap() < maxSize { // 对于大Buffer直接丢弃
w.Reset() w.Reset()
bufferPool.Put(w) bufferPool.Put(w)

View File

@ -49,7 +49,6 @@ type QQClient struct {
// protocol public field // protocol public field
SequenceId atomic.Int32 SequenceId atomic.Int32
SessionId []byte SessionId []byte
RandomKey []byte
TCP *network.TCPListener // todo: combine other protocol state into one struct TCP *network.TCPListener // todo: combine other protocol state into one struct
ConnectTime time.Time ConnectTime time.Time
@ -269,7 +268,6 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient {
} }
cli.TCP.PlannedDisconnect(cli.plannedDisconnect) cli.TCP.PlannedDisconnect(cli.plannedDisconnect)
cli.TCP.UnexpectedDisconnect(cli.unexpectedDisconnect) cli.TCP.UnexpectedDisconnect(cli.unexpectedDisconnect)
rand.Read(cli.RandomKey)
return cli return cli
} }

View File

@ -45,11 +45,11 @@ func decodeLoginResponse(c *QQClient, _ *network.IncomingPacketInfo, payload []b
} }
if t == 0 { // login success if t == 0 { // login success
// if t150, ok := m[0x150]; ok { // if t150, ok := m[0x150]; ok {
// c.t150 = t150 // c.t150 = t150
// }
// if t161, ok := m[0x161]; ok {
// c.decodeT161(t161)
// } // }
if t161, ok := m[0x161]; ok {
c.decodeT161(t161)
}
if m.Exists(0x403) { if m.Exists(0x403) {
c.sig.RandSeed = m[0x403] c.sig.RandSeed = m[0x403]
} }

View File

@ -20,7 +20,11 @@ type EventHandle[T any] struct {
func (handle *EventHandle[T]) Subscribe(handler func(client *QQClient, event T)) { func (handle *EventHandle[T]) Subscribe(handler func(client *QQClient, event T)) {
eventMu.Lock() eventMu.Lock()
defer eventMu.Unlock() defer eventMu.Unlock()
handle.handlers = append(handle.handlers, handler) // shrink the slice
newHandlers := make([]func(client *QQClient, event T), len(handle.handlers)+1)
copy(newHandlers, handle.handlers)
newHandlers[len(handle.handlers)] = handler
handle.handlers = newHandlers
} }
func (handle *EventHandle[T]) dispatch(client *QQClient, event T) { func (handle *EventHandle[T]) dispatch(client *QQClient, event T) {

View File

@ -12,16 +12,16 @@ import (
// --- tlv decoders for qq client --- // --- tlv decoders for qq client ---
/*
func (c *QQClient) decodeT161(data []byte) { func (c *QQClient) decodeT161(data []byte) {
/* reader := binary.NewReader(data)
reader := binary.NewReader(data) reader.ReadBytes(2)
reader.ReadBytes(2) t := reader.ReadTlvMap(2)
t := reader.ReadTlvMap(2) if t172, ok := t[0x172]; ok {
if t172, ok := t[0x172]; ok { c.rollbackSig = t172
c.rollbackSig = t172 }
}
*/
} }
*/
func (c *QQClient) decodeT119(data, ek []byte) { func (c *QQClient) decodeT119(data, ek []byte) {
tea := binary.NewTeaCipher(ek) tea := binary.NewTeaCipher(ek)
@ -63,32 +63,32 @@ func (c *QQClient) decodeT119(data, ek []byte) {
pt4TokenMap map[string][]byte pt4TokenMap map[string][]byte
) )
if _, ok := m[0x125]; ok {
// openId, openKey = readT125(t125)
}
if t186, ok := m[0x186]; ok {
c.decodeT186(t186)
}
if t11a, ok := m[0x11a]; ok { if t11a, ok := m[0x11a]; ok {
nick, age, gender = readT11A(t11a) nick, age, gender = readT11A(t11a)
} }
if _, ok := m[0x199]; ok { /*
// openId, payToken = readT199(t199) if _, ok := m[0x125]; ok {
} openId, openKey = readT125(t125)
if _, ok := m[0x200]; ok { }
// pf, pfkey = readT200(t200) if t186, ok := m[0x186]; ok {
} c.decodeT186(t186)
}
if _, ok := m[0x199]; ok {
openId, payToken = readT199(t199)
}
if _, ok := m[0x200]; ok {
pf, pfkey = readT200(t200)
}
if _, ok := m[0x531]; ok {
a1, noPicSig = readT531(t531)
}
if _, ok := m[0x138]; ok {
readT138(t138) // chg time
}
*/
if t512, ok := m[0x512]; ok { if t512, ok := m[0x512]; ok {
psKeyMap, pt4TokenMap = readT512(t512) psKeyMap, pt4TokenMap = readT512(t512)
} }
if _, ok := m[0x531]; ok {
// a1, noPicSig = readT531(t531)
}
if _, ok := m[0x138]; ok {
// readT138(t138) // chg time
}
c.oicq.WtSessionTicketKey = utils.Select(m[0x134], c.oicq.WtSessionTicketKey) c.oicq.WtSessionTicketKey = utils.Select(m[0x134], c.oicq.WtSessionTicketKey)
// we don't use `c.sigInfo = &auth.SigInfo{...}` here, // we don't use `c.sigInfo = &auth.SigInfo{...}` here,
@ -160,9 +160,11 @@ func (c *QQClient) decodeT113(data []byte) {
fmt.Println("got t113 uin:", uin) fmt.Println("got t113 uin:", uin)
} }
/*
func (c *QQClient) decodeT186(data []byte) { func (c *QQClient) decodeT186(data []byte) {
// c.pwdFlag = data[1] == 1 // c.pwdFlag = data[1] == 1
} }
*/
// --- tlv readers --- // --- tlv readers ---