Need to ensure that a message is sent and a response is received before the socket is closed
Trustin Lee (이희승)
trustin at gmail.com
Thu Sep 17 22:43:30 EDT 2009
Hi John,
On Thu, Sep 17, 2009 at 12:14 AM, John Platts <john_platts at hotmail.com> wrote:
>
> I am writing a VoIP TAPI integration program that uses Netty.
>
> I need to ensure that the following occurs before the socket channel is disconnected or closed:
> 1. If a close is requested, and the socket channel is disconnected, the channel is simply closed.
> 2. If the socket channel is already closed or disconnected, the steps below are skipped.
> 3. An unregistration request message is written out to the socket channel.
> 4. We want to do the actual close or disconnect, but only after one of the following conditions is true:
> - The writing of the message sent in step #3 is done, but is unsuccessful
You can add a listener to the ChannelFuture which is returned by
Channel.write(). The ChannelFuture has the properties that tell you
if the write attempt was successful.
> - The unregistration acknowledgment message is received and processed
You can simply close the connection when the ack message is received.
> - The unregistration acknowledgment message is not received within a certain time interval
There are many ways to achieve it:
1) Add a ReadTimeoutHandler right before sending the unregistration
request message, and close the connection on ReadTimeoutException.
You can add the ReadTimeoutHandler dynamically:
pipeline.addFirst("readTimeout", new ReadTimeoutHandler(...));
channel.write(unregistrationRequest);
2) Use a HashedWheelTimer directly to schedule a one time timeout.
(refer to the ReadTimeoutHandler source code to learn how to schedule
a timeout.)
> - The socket gets disconnected because of an timeout
> - The socket gets disconnected as a result of the other endpoint disconnecting the socket
No matter how the socket was disconnected (either normally or by
exception), you will be notified with channelDisconnected() and
channelClosed() event.
HTH
— Trustin Lee, http://gleamynode.net/
More information about the netty-users
mailing list