mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-21 23:10:06 +00:00
server/peer: wrap 'defer pool.Put()' in a closure
This commit is contained in:
parent
cbe308a0bd
commit
7a26ffdcf7
@ -119,11 +119,11 @@ func (peer *Peer) Handler() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// grab buffer && read packet body
|
// grab buffer && read packet body
|
||||||
|
if err := func() error { // we wrap this in a closure so we can easily defer the buffer return to pool
|
||||||
buf := pool.Get()
|
buf := pool.Get()
|
||||||
defer pool.Put(buf)
|
defer pool.Put(buf)
|
||||||
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
|
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
|
||||||
log.Printf("[FATAL] failed to read packet body! %v", err)
|
return fmt.Errorf("failed to read packet body! %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// decrypt
|
// decrypt
|
||||||
@ -133,18 +133,19 @@ func (peer *Peer) Handler() {
|
|||||||
// create packet && read typeID
|
// create packet && read typeID
|
||||||
var typeID uint32
|
var typeID uint32
|
||||||
if err := pkt.Decode(&typeID); err != nil {
|
if err := pkt.Decode(&typeID); err != nil {
|
||||||
log.Printf("[FATAL] failed to read packet type! %v", err)
|
return fmt.Errorf("failed to read packet type! %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dispatch packet
|
// dispatch packet
|
||||||
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
|
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
|
||||||
if err := peer.handler.HandlePacket(peer, typeID, pkt); err != nil {
|
if err := peer.handler.HandlePacket(peer, typeID, pkt); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}(); err != nil {
|
||||||
log.Printf("[FATAL] %v", err)
|
log.Printf("[FATAL] %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore buffer to pool
|
|
||||||
pool.Put(buf)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user