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.7k stars 1.04k forks source link

[Question]: About func (el *eventloop) read(c *conn) #473

Closed EddieChan1993 closed 1 year ago

EddieChan1993 commented 1 year ago

Actions I've taken before I'm here

Questions with details

Why do we still need to write the buffer into inboundBuffer after the data has been processed in the OnTraffic function?

Code snippets (optional)

c.buffer = el.buffer[:n]
    action := el.eventHandler.OnTraffic(c)
....    
    _, _ = c.inboundBuffer.Write(c.buffer)
panjf2000 commented 1 year ago

If you've already consumed all data, then c.buffer will be empty.

EddieChan1993 commented 1 year ago

If you've already consumed all data, then c.buffer will be empty.

Thanks for your reply. In what scenario would OnTraffic not consume all data in c.buffer? Could you please give me an example?"

panjf2000 commented 1 year ago

If you've already consumed all data, then c.buffer will be empty.

Thanks for your reply. In what scenario would OnTraffic not consume all data in c.buffer? Could you please give me an example?"

When there are multiple packets in the buffer and the user doesn't unpack all of them and process it, or when there is an incomplete packet at the end of the buffer.

EddieChan1993 commented 1 year ago

If you've already consumed all data, then c.buffer will be empty.

Thanks for your reply. In what scenario would OnTraffic not consume all data in c.buffer? Could you please give me an example?"

When there are multiple packets in the buffer and the user doesn't unpack all of them and process it, or when there is an incomplete packet at the end of the buffer.

3ks