Skip to content

Commit

Permalink
fix crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrs4s committed Jul 28, 2020
1 parent fe4df30 commit 91aa75e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MiraiGo
qq-android协议的golang实现 移植于Mirai
qq-android协议的golang实现 移植于mirai

# 警告
本项目目前还处于功能填充阶段,将来API和项目结构可能会有调整,目前不推荐使用。
Expand Down
26 changes: 15 additions & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type QQClient struct {
Conn net.Conn

decoders map[string]func(*QQClient, uint16, []byte) (interface{}, error)
handlers map[uint16]func(interface{}, error)
handlers sync.Map

syncCookie []byte
pubAccountCookie []byte
Expand Down Expand Up @@ -121,7 +121,6 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient {
"MultiMsg.ApplyUp": decodeMultiApplyUpResponse,
"MultiMsg.ApplyDown": decodeMultiApplyDownResponse,
},
handlers: map[uint16]func(interface{}, error){},
sigInfo: &loginSigInfo{},
requestPacketRequestId: 1921334513,
groupSeq: 22911,
Expand Down Expand Up @@ -629,14 +628,19 @@ func (c *QQClient) sendAndWait(seq uint16, pkt []byte) (interface{}, error) {
}
ch := make(chan T)
defer close(ch)
c.handlers[seq] = func(i interface{}, err error) {
c.handlers.Store(seq, func(i interface{}, err error) {
ch <- T{
Response: i,
Error: err,
}
})
select {
case rsp := <-ch:
return rsp.Response, rsp.Error
case <-time.After(time.Second * 15):
c.handlers.Delete(seq)
return nil, errors.New("time out")
}
rsp := <-ch
return rsp.Response, rsp.Error
}

func (c *QQClient) loop() {
Expand Down Expand Up @@ -686,19 +690,19 @@ func (c *QQClient) loop() {
}()
decoder, ok := c.decoders[pkt.CommandName]
if !ok {
if f, ok := c.handlers[pkt.SequenceId]; ok {
delete(c.handlers, pkt.SequenceId)
f(nil, nil)
if f, ok := c.handlers.Load(pkt.SequenceId); ok {
c.handlers.Delete(pkt.SequenceId)
f.(func(i interface{}, err error))(nil, nil)
}
return
}
rsp, err := decoder(c, pkt.SequenceId, payload)
if err != nil {
log.Println("decode", pkt.CommandName, "error:", err)
}
if f, ok := c.handlers[pkt.SequenceId]; ok {
delete(c.handlers, pkt.SequenceId)
f(rsp, err)
if f, ok := c.handlers.Load(pkt.SequenceId); ok {
c.handlers.Delete(pkt.SequenceId)
f.(func(i interface{}, err error))(rsp, err)
}
}()
}
Expand Down
2 changes: 1 addition & 1 deletion message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func ToProtoElems(elems []IMessageElement, generalFlags bool) (r []*msg.Elem) {
for _, elem := range elems {
switch elem.(type) {
case *ServiceElement:
d, _ := hex.DecodeString("08 09 78 00 C8 01 00 F0 01 00 F8 01 00 90 02 00 C8 02 00 98 03 00 A0 03 20 B0 03 00 C0 03 00 D0 03 00 E8 03 00 8A 04 02 08 03 90 04 80 80 80 10 B8 04 00 C0 04 00")
d, _ := hex.DecodeString("08097800C80100F00100F80100900200C80200980300A00320B00300C00300D00300E803008A04020803900480808010B80400C00400")
r = append(r, &msg.Elem{
GeneralFlags: &msg.GeneralFlags{
PbReserve: d,
Expand Down

0 comments on commit 91aa75e

Please sign in to comment.