示例#1
0
  private boolean scheduleIdleTimeout(IChannel channel) {
    final int timeout = m_conf.sessionIdleTimeoutInSeconds();
    if (timeout > 0) return channel.scheduleIdleTimeout(timeout);

    if (timeout == 0) channel.close();

    return true;
  }
示例#2
0
  @Override
  public void onChannelException(IChannel channel, Throwable t) {
    try {
      c_logger.error(
          StrUtil.join(channel, "(remoteAddr=", channel.remoteAddress(), ") got an error"), t);

      final ISessionListener<I, O> listener = m_listener;
      if (listener != null) listener.onSessionException(channel, t);
    } catch (Throwable e) {
      c_logger.error(
          StrUtil.join(channel, "(remoteAddr=", channel.remoteAddress(), ") Unexpected Error: "),
          e);
    } finally {
      channel.close();
    }
  }
示例#3
0
  @Override
  public void onChannelClosed(IChannel channel) {
    c_logger.debug("{}(remoteAddr={}): CLOSED", channel, channel.remoteAddress());

    final ConcurrentHashMap<Long, IChannel> channels = m_channels;
    if (channels != null) channels.remove(channel.id());

    final ISessionListener<I, O> listener = m_listener;
    if (listener != null) {
      try {
        listener.onSessionClosed(channel);
      } catch (Throwable t) {
        c_logger.error(
            StrUtil.join(channel, "(remoteAddr=", channel.remoteAddress(), ") Unexpected Error: "),
            t);
      }
    }
  }
示例#4
0
  @Override
  public void onChannelOpened(IChannel channel) {
    c_logger.debug("{}(remoteAddr={}): OPENED", channel, channel.remoteAddress());

    final Long id = channel.id();
    final ConcurrentHashMap<Long, IChannel> channels = m_channels;
    if (channels != null) channels.put(id, channel);

    if (m_stopped) {
      channel.close();
      return;
    }

    // failed to schedule, channel has been closed
    if (!scheduleIdleTimeout(channel)) return;

    final ISessionListener<I, O> listener = m_listener;
    if (listener != null) listener.onSessionOpened(channel);
  }
示例#5
0
  @Override
  public void onChannelIdleTimedOut(IChannel channel) {
    try {
      c_logger.debug("{}: IDLE_TIMEOUT", channel);

      final ISessionListener<I, O> listener = m_listener;
      if (listener != null) listener.onSessionIdleTimedOut(channel);
    } finally {
      channel.close();
    }
  }
示例#6
0
  @Override
  public void write(ISession session, O msg) {
    final ConcurrentHashMap<Long, IChannel> channels = m_channels;
    if (channels == null) return;

    final IChannel channel = channels.get(session.id());
    if (channel != null) {
      channel.write(msg);
      return;
    }

    final Object remoteAddr = session.remoteAddress();
    c_logger.warn(
        StrUtil.join(
            session,
            "(remoteAddr=",
            remoteAddr,
            ") failed to send(channel closed): ",
            StrUtil.getLineSeparator(),
            msg));

    if (msg instanceof AutoCloseable) {
      try {
        ((AutoCloseable) msg).close();
      } catch (Throwable t) {
        c_logger.error(
            StrUtil.join(
                session,
                "(remoteAddr=",
                remoteAddr,
                ") failed to close message: ",
                StrUtil.getLineSeparator(),
                msg),
            t);
      }
    }
  }
示例#7
0
 private void closeChannels() {
   final Collection<IChannel> channels = m_channels.values();
   for (final IChannel channel : channels) channel.close();
 }
示例#8
0
 @Override
 public void closeSession(ISession session) {
   ((IChannel) session).close();
 }