/**
   * Tries to start server with given parameters.
   *
   * @param hostAddr Host on which server should be bound.
   * @param port Port on which server should be bound.
   * @param lsnr Server message listener.
   * @param parser Server message parser.
   * @param sslCtx SSL context in case if SSL is enabled.
   * @param cfg Configuration for other parameters.
   * @return {@code True} if server successfully started, {@code false} if port is used and server
   *     was unable to start.
   */
  private boolean startTcpServer(
      InetAddress hostAddr,
      int port,
      GridNioServerListener<GridClientMessage> lsnr,
      GridNioParser parser,
      @Nullable SSLContext sslCtx,
      GridConfiguration cfg) {
    try {
      GridNioFilter codec = new GridNioCodecFilter(parser, log, false);

      GridNioFilter[] filters;

      if (sslCtx != null) {
        GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, log);

        boolean auth = cfg.isRestTcpSslClientAuth();

        sslFilter.wantClientAuth(auth);

        sslFilter.needClientAuth(auth);

        filters = new GridNioFilter[] {codec, sslFilter};
      } else filters = new GridNioFilter[] {codec};

      srv =
          GridNioServer.<GridClientMessage>builder()
              .address(hostAddr)
              .port(port)
              .listener(lsnr)
              .logger(log)
              .selectorCount(cfg.getRestTcpSelectorCount())
              .gridName(ctx.gridName())
              .tcpNoDelay(cfg.isRestTcpNoDelay())
              .directBuffer(cfg.isRestTcpDirectBuffer())
              .byteOrder(ByteOrder.nativeOrder())
              .socketSendBufferSize(cfg.getRestTcpSendBufferSize())
              .socketReceiveBufferSize(cfg.getRestTcpReceiveBufferSize())
              .sendQueueLimit(cfg.getRestTcpSendQueueLimit())
              .filters(filters)
              .build();

      srv.idleTimeout(cfg.getRestIdleTimeout());

      srv.start();

      ctx.ports().registerPort(port, GridPortProtocol.TCP, getClass());

      return true;
    } catch (GridException e) {
      if (log.isDebugEnabled())
        log.debug(
            "Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage());

      return false;
    }
  }
  /**
   * Checks entry for empty value.
   *
   * @param entry Entry to check.
   * @return {@code True} if entry is empty.
   */
  private boolean empty(GridCacheEntry<K, V> entry) {
    try {
      return entry.peek(F.asList(GLOBAL)) == null;
    } catch (GridException e) {
      U.error(null, e.getMessage(), e);

      assert false : "Should never happen: " + e;

      return false;
    }
  }
  /**
   * Reconstructs object on demarshalling.
   *
   * @return Reconstructed object.
   * @throws ObjectStreamException Thrown in case of demarshalling error.
   */
  @SuppressWarnings("unchecked")
  private Object readResolve() throws ObjectStreamException {
    try {
      GridBiTuple<GridCacheContext, String> t = stash.get();

      return t.get1().dataStructures().atomicReference(t.get2(), null, false);
    } catch (GridException e) {
      throw U.withCause(new InvalidObjectException(e.getMessage()), e);
    } finally {
      stash.remove();
    }
  }