Optimize PacketHandler performance

This commit is contained in:
Tnze
2021-12-24 15:40:39 +08:00
parent dccbf7ce46
commit 494a52320d
7 changed files with 191 additions and 69 deletions

View File

@ -49,25 +49,22 @@ func NewKeepAlive() (k *KeepAlive) {
}
}
func (k *KeepAlive) AddPlayer(p *Player) {
k.join <- p
p.AddHandler(PacketHandler{
// Init implement Component for KeepAlive
func (k *KeepAlive) Init(g *Game) {
g.AddHandler(&PacketHandler{
ID: packetid.ServerboundKeepAlive,
F: func(packet Packet757) error {
F: func(player *Player, packet Packet757) error {
var KeepAliveID pk.Long
if err := pk.Packet(packet).Scan(&KeepAliveID); err != nil {
return err
}
k.tick <- p
k.tick <- player
return nil
},
})
}
func (k *KeepAlive) RemovePlayer(p *Player) {
k.quit <- p
}
// Run implement Component for KeepAlive
func (k *KeepAlive) Run(ctx context.Context) {
for {
select {
@ -87,6 +84,12 @@ func (k *KeepAlive) Run(ctx context.Context) {
}
}
// AddPlayer implement Component for KeepAlive
func (k *KeepAlive) AddPlayer(player *Player) { k.join <- player }
// RemovePlayer implement Component for KeepAlive
func (k *KeepAlive) RemovePlayer(p *Player) { k.quit <- p }
func (k KeepAlive) pushPlayer(p *Player) {
k.listIndex[p.UUID] = k.pingList.PushBack(
keepAliveItem{player: p, t: time.Now()},