connect - can't detect server-side offline in netty -


my question this: "specifically interested in case other end of channel dies unexpectedly (eg process gets killed). seems netty not reliably fire channel closed/disconnect event, , not. not throw socketexception connection reset."

but software need keep connected @ possible conditon,unless device shutdown or net wire break.

i connect tcp device netty. device tcp server-side,tcp client-side write netty. use netty tcp-client receive continus data device. connect long connect ,never colsed on usual. now,how detect device offline unexcept conditions,like shutdown poweroff button or net wire has broken. heartbeat of netty can not work .

cpline.addlast("idlestatehandler", new idlestatehandler(new hashedwheeltimer(), 5, 0, 0)); cpline.addlast("heartbeathandler", new heartbeathandler());. 

,

public void init()     {         factory = new nioclientsocketchannelfactory(mainframe.thread_pool,                 mainframe.thread_pool);         bootstrap = new clientbootstrap(factory);         bootstrap.setpipelinefactory(new channelpipelinefactory()         {             public channelpipeline getpipeline() throws exception             {                 channelpipeline cpline = channels.pipeline();                 cpline.addlast("idlestatehandler", new idlestatehandler(new hashedwheeltimer(), 5, 0, 0));                 cpline.addlast("heartbeathandler", new heartbeathandler());                 (channelhandler handler : handlerlist)                 {                     cpline.addlast(handler.tostring(), handler);                 }                 return cpline;             }         });          bootstrap.setoption("child.tcpnodelay", true);         bootstrap.setoption("child.keepalive", true);         bootstrap.setoption("writebufferhighwatermark", 100*1024*1024);         connect();         log.debug("tcpclient 初始化完成!");     } 

i still can't detect device-side offline. when idlestatus ,i can't close channel in heartbeathandler.because readidlestatus not means device has shutdown,it has no data trasfer.

this heartbeathandler:

public class heartbeathandler extends idlestateawarechannelhandler {      logger log=logmanager.getlogger(heartbeathandler.class);     @override     public void channelidle(channelhandlercontext ctx, idlestateevent e) {         if (e.getstate() == idlestate.reader_idle) {             log.info("reader idle, closing channel");             //e.getchannel().close();             e.getchannel().write("heartbeat-reader_idle");         }         else if (e.getstate() == idlestate.writer_idle) {             log.info("writer idle, sending heartbeat");             e.getchannel().write("heartbeat-writer_idle");         }         else if (e.getstate() == idlestate.all_idle) {             log.info("all idle, sending heartbeat");             e.getchannel().write("heartbeat-all_idle");         }     } } 

you need implement kind of heartbeat in protocol layer. can make use of idlestatehandler this.


Comments