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

    try {
      return t.get1().dataStructures().sequence(t.get2(), 0L, false, false);
    } catch (GridException e) {
      throw U.withCause(new InvalidObjectException(e.getMessage()), e);
    }
  }
  /**
   * 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;
    }
  }
  /**
   * Reconstructs object on demarshalling.
   *
   * @return Reconstructed object.
   * @throws ObjectStreamException Thrown in case of demarshalling error.
   */
  @SuppressWarnings({"ConstantConditions"})
  private Object readResolve() throws ObjectStreamException {
    GridTuple2<GridCacheContext, String> t = stash.get();

    try {
      return t.get1().dataStructures().countDownLatch(t.get2(), 0, false, false);
    } catch (GridException e) {
      throw U.withCause(new InvalidObjectException(e.getMessage()), e);
    }
  }
  /**
   * 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();
    }
  }