panjf2000 / gnet

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
https://gnet.host
Apache License 2.0
9.71k stars 1.04k forks source link

Better to support bufio.SplitFunc to split packet automatically #418

Closed fwang2002 closed 2 years ago

fwang2002 commented 2 years ago

It's better to add another method in EventHandler interface

// data is a single packet data splitted from the recv buffer(it can be a slice from the original buffer)
OnPacket(c Conn, data []byte) (action Action)

and add an option when call gnet.Run

func WithSplitFunc(splitFunc bufio.SplitFunc) Option

If splitFunc is set, then OnPacket will be called when each single complete packet is received.

SplitFunc is convenient for both splitting packet by length field and splitting packet by delimeters.

panjf2000 commented 2 years ago

There were codecs in gnet v1, doing the exact same things you're talking about, however, those codecs are dropped in gnet v2, the reasons why I did that:

The built-in codecs have been deprecated and removed, to reduce the complexity and keep gnet simple. From a lot of feedback we've received, this feature does not bring convenience and benefits to users, thus, I decided to take it off from gnet. Cutting those codecs off makes the code on top of gnet more holistic and straightforward.

quoted from https://gnet.host/blog/announcing-gnet-v2/