@Override public void messageReceived(IoSession session, Object message) throws Exception { if (message != null && message instanceof RemotingCommand) { remoting.processMessageReceived(new MinaChannel(session), (RemotingCommand) message); } } }
@Override public void messageReceived(IoSession session, Object message) throws Exception { if (message != null && message instanceof RemotingCommand) { remoting.processMessageReceived(new MinaChannel(session), (RemotingCommand) message); } } }
@Override public Channel getChannel() { return new MinaChannel(connectFuture.getSession()); }
@Override public Channel getChannel() { return new MinaChannel(connectFuture.getSession()); }
@Override public void sessionCreated(IoSession session) throws Exception { final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(new MinaChannel(session)); LOGGER.info("{} : sessionCreated {}", sideType, remoteAddress); super.sessionCreated(session); }
@Override public void sessionCreated(IoSession session) throws Exception { final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(new MinaChannel(session)); LOGGER.info("{} : sessionCreated {}", sideType, remoteAddress); super.sessionCreated(session); }
@Override public void sessionClosed(IoSession session) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.info("{}: sessionClosed, the channel[{}]", sideType, remoteAddress); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.CLOSE, remoteAddress, channel)); } }
@Override public void sessionOpened(IoSession session) throws Exception { Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.info("{}: sessionOpened, the channel[{}]", sideType, remoteAddress); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.CONNECT, remoteAddress, channel)); } }
@Override public void sessionOpened(IoSession session) throws Exception { Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.info("{}: sessionOpened, the channel[{}]", sideType, remoteAddress); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.CONNECT, remoteAddress, channel)); } }
@Override public void sessionClosed(IoSession session) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.info("{}: sessionClosed, the channel[{}]", sideType, remoteAddress); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.CLOSE, remoteAddress, channel)); } }
@Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.warn("{}: exceptionCaught {}. ", sideType, remoteAddress, cause); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.EXCEPTION, remoteAddress, channel)); } RemotingHelper.closeChannel(channel); }
@Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.warn("{}: exceptionCaught {}. ", sideType, remoteAddress, cause); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.EXCEPTION, remoteAddress, channel)); } RemotingHelper.closeChannel(channel); }
@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { while (in.remaining() > 4) { // 前4位是长度 byte[] lengthBytes = new byte[4]; in.mark(); //标记当前位置,以便reset in.get(lengthBytes); //读取前4字节 int length = ByteBuffer.wrap(lengthBytes).getInt(); // 数据不够,返回false,需要继续读取 if (length == 0 || length > in.remaining()) { in.reset(); return false; } // 够了,开始解码 byte[] bytes = new byte[length]; in.get(bytes, 0, length); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { RemotingCommand remotingCommand = codec.decode(byteBuffer); out.write(remotingCommand); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("decode exception, {}", RemotingHelper.parseChannelRemoteAddr(channel), e); RemotingHelper.closeChannel(channel); } } return false; }
@Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { while (in.remaining() > 4) { // 前4位是长度 byte[] lengthBytes = new byte[4]; in.mark(); //标记当前位置,以便reset in.get(lengthBytes); //读取前4字节 int length = ByteBuffer.wrap(lengthBytes).getInt(); // 数据不够,返回false,需要继续读取 if (length == 0 || length > in.remaining()) { in.reset(); return false; } // 够了,开始解码 byte[] bytes = new byte[length]; in.get(bytes, 0, length); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); try { RemotingCommand remotingCommand = codec.decode(byteBuffer); out.write(remotingCommand); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("decode exception, {}", RemotingHelper.parseChannelRemoteAddr(channel), e); RemotingHelper.closeChannel(channel); } } return false; }
@Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); if (IdleStatus.BOTH_IDLE == status) { LOGGER.info("{}: IDLE [{}]", sideType, remoteAddress); RemotingHelper.closeChannel(channel); } if (remoting.getChannelEventListener() != null) { RemotingEventType remotingEventType = null; if (IdleStatus.BOTH_IDLE == status) { remotingEventType = RemotingEventType.ALL_IDLE; } else if (IdleStatus.READER_IDLE == status) { remotingEventType = RemotingEventType.READER_IDLE; } else if (IdleStatus.WRITER_IDLE == status) { remotingEventType = RemotingEventType.WRITER_IDLE; } remoting.putRemotingEvent(new RemotingEvent(remotingEventType, remoteAddress, channel)); } }
@Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); if (IdleStatus.BOTH_IDLE == status) { LOGGER.info("{}: IDLE [{}]", sideType, remoteAddress); RemotingHelper.closeChannel(channel); } if (remoting.getChannelEventListener() != null) { RemotingEventType remotingEventType = null; if (IdleStatus.BOTH_IDLE == status) { remotingEventType = RemotingEventType.ALL_IDLE; } else if (IdleStatus.READER_IDLE == status) { remotingEventType = RemotingEventType.READER_IDLE; } else if (IdleStatus.WRITER_IDLE == status) { remotingEventType = RemotingEventType.WRITER_IDLE; } remoting.putRemotingEvent(new RemotingEvent(remotingEventType, remoteAddress, channel)); } }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { LOGGER.error("Message is null"); return; } if (!(message instanceof RemotingCommand)) { LOGGER.error("{} is not instanceof RemotingCommand", message); return; } RemotingCommand remotingCommand = (RemotingCommand) message; try { ByteBuffer byteBuffer = codec.encode(remotingCommand); IoBuffer ioBuffer = bufferAllocator.wrap(byteBuffer); out.write(ioBuffer); out.flush(); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("encode exception, addr={}, remotingCommand={}", RemotingHelper.parseChannelRemoteAddr(channel), remotingCommand.toString(), e); RemotingHelper.closeChannel(channel); } } };
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { LOGGER.error("Message is null"); return; } if (!(message instanceof RemotingCommand)) { LOGGER.error("{} is not instanceof RemotingCommand", message); return; } RemotingCommand remotingCommand = (RemotingCommand) message; try { ByteBuffer byteBuffer = codec.encode(remotingCommand); IoBuffer ioBuffer = bufferAllocator.wrap(byteBuffer); out.write(ioBuffer); out.flush(); } catch (Exception e) { Channel channel = new MinaChannel(session); LOGGER.error("encode exception, addr={}, remotingCommand={}", RemotingHelper.parseChannelRemoteAddr(channel), remotingCommand.toString(), e); RemotingHelper.closeChannel(channel); } } };
@Override public void sessionCreated(IoSession session) throws Exception { final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(new MinaChannel(session)); LOGGER.info("{} : sessionCreated {}", sideType, remoteAddress); super.sessionCreated(session); }
@Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { com.github.ltsopensource.remoting.Channel channel = new MinaChannel(session); final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel); LOGGER.warn("{}: exceptionCaught {}. ", sideType, remoteAddress, cause); if (remoting.getChannelEventListener() != null) { remoting.putRemotingEvent(new RemotingEvent(RemotingEventType.EXCEPTION, remoteAddress, channel)); } RemotingHelper.closeChannel(channel); }