/** 初始化连接类 */
  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);
    }
  }
 private NioSocketConnector setupConnector(IoHandlerAdapter handler) {
   NioSocketConnector connector = new NioSocketConnector();
   connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 300);
   connector.setConnectTimeoutCheckInterval(5);
   connector
       .getFilterChain()
       .addLast(
           "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
   connector.setHandler(handler);
   return connector;
 }
  /**
   * 根据host和port新建一个IoSession连接
   *
   * @param host 连接主机IP
   * @param port 连接端口
   * @param timeout 未收到数据超时时间/秒
   */
  @Override
  public void connect(String host, int port, int timeout) throws Exception {
    connector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, timeout);
    log.debug("超时时间:" + timeout + "秒");
    // 设置连接超时时间
    connector.setConnectTimeoutMillis(ExpressConstant.CONNECT_TIMEOUT);

    // 创建连接
    ConnectFuture future = connector.connect(new InetSocketAddress(host, port));

    // 等待连接创建完成
    future.awaitUninterruptibly();

    // 得到连接Session
    session = future.getSession();
    // 设置Session同步锁对象
    session.setAttribute(ExpressConstant.SESSION_LOCK, new LockExpress());
  }
Example #4
0
 /**
  * Title:
  *
  * <p>Desc:初始化客户端信息
  *
  * @param message
  * @throws UnsupportedEncodingException
  */
 public TongbuClient(Object message, String url, int port) {
   chain.addLast("logging", new LoggingFilter()); // 添加日志过滤器
   chain.addLast("myChin", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
   if (message instanceof String) {
     connector.setHandler(new ClientHandler((String) message));
   } else if (message instanceof CommEntity) {
     connector.setHandler(new ClientHandler((CommEntity) message));
   }
   connector.setConnectTimeoutMillis(10000);
   SocketSessionConfig cfg = connector.getSessionConfig();
   cfg.setUseReadOperation(true);
   IoSession session =
       connector.connect(new InetSocketAddress(url, port)).awaitUninterruptibly().getSession();
   ReadFuture readFuture = session.read();
   readFuture.awaitUninterruptibly();
   this.info = (CommResultInfo) readFuture.getMessage();
   session.close(true);
   session.getService().dispose();
 }