示例#1
0
  @Override
  protected void startService() throws Exception {
    if (port == 0) {
      throw new ConfigurationException("Port value not set");
    }
    socketAddress = new InetSocketAddress(port);
    newCodecFactory();
    addListeners();

    acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(codecFactory));

    if (timeout == -1) {
      keepAliveHandler = newKeepAliveHandler();
      if (keepAliveHandler != null) {
        KeepAliveFilter kaf =
            new KeepAliveFilter(
                keepAliveHandler.getKeepAliveMessageFactory(),
                IdleStatus.READER_IDLE,
                keepAliveHandler.getKeepAliveRequestTimeoutHandler(),
                keepAliveHandler.getRequestInterval(),
                keepAliveHandler.getRequestTimeout());

        acceptor.getFilterChain().addLast("keepalive", kaf);
      }
    } else if (timeout > 0) {
      acceptor.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, timeout);
    }
    acceptor.setBacklog(100);
    acceptor.setReuseAddress(true);
    acceptor.setDefaultLocalAddress(socketAddress);
    acceptor.setHandler(this);
    acceptor.getSessionConfig().setReadBufferSize(2048);
    preBindAcceptorHook(acceptor);
    acceptor.bind();
  }
示例#2
0
 public void connect() throws IOException {
   NioSocketAcceptor acceptor = new NioSocketAcceptor();
   SocketSessionConfig config = acceptor.getSessionConfig();
   // config.setKeepAlive(true);
   // config.setReuseAddress(true);
   // config.setTcpNoDelay(true);
   config.setMinReadBufferSize(1024 * 10); // 10k
   config.setMaxReadBufferSize(1024 * 1024 * 10); // 10M
   acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(codecFactory));
   // config.setUseReadOperation(true);
   if (filters != null && !filters.isEmpty()) {
     for (String key : filters.keySet()) acceptor.getFilterChain().addLast(key, filters.get(key));
   }
   acceptor.setHandler(handler);
   InetSocketAddress address = new InetSocketAddress(serverPort);
   acceptor.bind(address);
   logger.info("server start success,serverPort:" + address);
 }