/** 初始化连接类 */
  public synchronized void initConnector(IoHandler handler) {
    if (connector == null) {
      connector = new NioSocketConnector();
      // 设置缓存大小
      connector.getSessionConfig().setMaxReadBufferSize(2048);
      // 添加过滤器
      connector
          .getFilterChain()
          .addLast(
              "codec", new ProtocolCodecFilter(new ExpressCodecFactory(Charset.forName("UTF-8"))));

      // 设置日志过滤器
      LoggingFilter loggingFilter = new LoggingFilter();
      loggingFilter.setMessageReceivedLogLevel(LogLevel.DEBUG);
      loggingFilter.setMessageSentLogLevel(LogLevel.DEBUG);
      connector.getFilterChain().addLast("logger", loggingFilter);
      // 添加登录过滤器
      connector.getFilterChain().addLast("loginFilter", new TCPLoginFilter());
      // 添加登出过滤器
      connector.getFilterChain().addLast("logoutFilter", new TCPLogoutFilter());
      // 防回流过滤器
      connector.getFilterChain().addLast("backFlowFilter", new BackFlowFilter());

      // 添加业务逻辑处理器类
      connector.setHandler(handler);
    }
  }
Exemplo n.º 2
0
  public void connect(InetAddress ia, int port) {
    connector = new NioSocketConnector();
    connector
        .getFilterChain()
        .addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
    if (logger.isDebugEnabled()) {
      LoggingFilter logFilter = new LoggingFilter();
      logFilter.setMessageSentLogLevel(LogLevel.DEBUG);
      logFilter.setMessageReceivedLogLevel(LogLevel.DEBUG);
      connector.getFilterChain().addLast("logger", logFilter);
    }
    connector.setHandler(this);

    ConnectFuture future = connector.connect(new InetSocketAddress(ia, port));
    future.awaitUninterruptibly();
    if (!future.isConnected()) {
      logger.error("Connection failed");
    }
    session = future.getSession();
  }