gnet icon indicating copy to clipboard operation
gnet copied to clipboard

Is it possible to find out the number of bytes written with `conn.AsyncWrite()`?

Open gvitali opened this issue 3 years ago • 1 comments

Please fill out the following system information before opening an issue: OS: Ubuntu 20.04.5 LTS Go version: go1.19.3 linux/amd64 gnet version: 2.1.1

What is your question about gnet? conn.Write() returns the number of bytes written. Is it possible to find out the number of bytes written with conn.AsyncWrite()?

gvitali avatar Nov 18 '22 09:11 gvitali

Not supported yet, but I think it's reasonable to implement this feature.

panjf2000 avatar Nov 20 '22 04:11 panjf2000

Can I help with this ? But I reckon that since this library has been used for a long time, so change AsyncWrite(buf []byte, callback AsyncCallback) (err error) into something like AsyncWrite(buf []byte, callback AsyncCallback) (length int, err error) would break things right?

Alan-MQ avatar Jun 30 '24 14:06 Alan-MQ

AsyncWrite() writes bytes asynchronously, so I don't think that it can return the number of written bytes synchronously. Furthermore, there is a promising buffer management inside gnet that ensures all bytes will be handled properly, which means that once you call Write() or AsyncWrite(), you can assume that your data will be sent to the remote peer eventually if no errors are returned from those methods. You can deem that as a guarantee by gnet. That is, you only need to check the returned errors and callback returned errors of Write() and AsyncWrite(), and if all errors are nil, your data is guaranteed to be sent successfully.

panjf2000 avatar Jul 01 '24 00:07 panjf2000