1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00
MiraiGo/client/statistics.go
icarus-ai 08ca3b023a
deprecate CAS in favor of CompareAndSwap (#291)
* deprecate CAS in favor of CompareAndSwap

* ci: update to go1.19

* 为 Statistics 实现 MarshalJSON 接口 (See 2d79fcc)
2022-08-24 14:22:36 +08:00

47 lines
1.4 KiB
Go

package client
import (
"bytes"
"strconv"
"sync/atomic"
)
type Statistics struct {
PacketReceived atomic.Uint64
PacketSent atomic.Uint64
PacketLost atomic.Uint64
MessageReceived atomic.Uint64
MessageSent atomic.Uint64
LastMessageTime atomic.Int64
DisconnectTimes atomic.Uint32
LostTimes atomic.Uint32
}
func (c *QQClient) GetStatistics() *Statistics {
return &c.stat
}
// MarshalJSON encodes the wrapped statistics into JSON.
func (m *Statistics) MarshalJSON() ([]byte, error) {
var w bytes.Buffer
w.Grow(256)
w.WriteString(`{"packet_received":`)
w.WriteString(strconv.FormatUint(m.PacketReceived.Load(), 10))
w.WriteString(`,"packet_sent":`)
w.WriteString(strconv.FormatUint(m.PacketSent.Load(), 10))
w.WriteString(`,"packet_lost":`)
w.WriteString(strconv.FormatUint(m.PacketLost.Load(), 10))
w.WriteString(`,"message_received":`)
w.WriteString(strconv.FormatUint(m.MessageReceived.Load(), 10))
w.WriteString(`,"message_sent":`)
w.WriteString(strconv.FormatUint(m.MessageSent.Load(), 10))
w.WriteString(`,"disconnect_times":`)
w.WriteString(strconv.FormatUint(uint64(m.DisconnectTimes.Load()), 10))
w.WriteString(`,"lost_times":`)
w.WriteString(strconv.FormatUint(uint64(m.LostTimes.Load()), 10))
w.WriteString(`,"last_message_time":`)
w.WriteString(strconv.FormatInt(m.LastMessageTime.Load(), 10))
w.WriteByte('}')
return w.Bytes(), nil
}