AndrewSchmidt
AndrewSchmidt

Reputation: 81

how to confirm that netty has tried to read the socket

I'd like to confirm from netty that a read event has occured before I close a connection. It doesn't matter if it actually read in bytes, or an error occured.

From digging through netty code, I don't believe the following guarantees order of operations:

channel.read();
channel.close();

Since a read event isn't a "task" in netty's event loop, there is a possibility that the close task will execute before the read event.

There is no "Read future" that can be used. So my question is: what is the best way to guarantee a read event occurs before closing a channel?"

If auto-read is used: If netty is currently in it's task loop writing data over the wire, and we call channel.close() will a read operation occur or will the close happen first?

Upvotes: 4

Views: 668

Answers (1)

trustin
trustin

Reputation: 12351

In Netty, read() and flush() are special operations which do not return a ChannelFuture. It's because the outcome of the read() operation is channelRead(), channelReadComplete(), or exceptionCaught(). flush() operation doesn't have a ChannelFuture because it always is accompanied by write(), and you get the future there.

Therefore, to see if the data was actually read from the socket, you can see if your handler's channelRead() was invoked.

On the other hand, by default, Netty has ChannelOption.AUTO_READ turned on, so you don't need to call read() at all. It just reads as soon as the connection becomes active.

Upvotes: 2

Related Questions