private long waitForNextTick() {
      long deadline = startTime + tickDuration * tick;

      for (; ; ) {
        final long currentTime = System.currentTimeMillis();
        long sleepTime = tickDuration * tick - (currentTime - startTime);

        // Check if we run on windows, as if thats the case we will need
        // to round the sleepTime as workaround for a bug that only affect
        // the JVM if it runs on windows.
        //
        // See https://github.com/netty/netty/issues/356
        if (DetectionUtil.isWindows()) {
          sleepTime = sleepTime / 10 * 10;
        }

        if (sleepTime <= 0) {
          break;
        }

        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
          if (shutdown.get()) {
            return -1;
          }
        }
      }

      // Increase the tick.
      tick++;
      return deadline;
    }
示例#2
0
    @Override
    public final void bind(final SocketAddress localAddress, final ChannelFuture future) {
      if (eventLoop().inEventLoop()) {
        if (!ensureOpen(future)) {
          return;
        }

        try {
          boolean wasActive = isActive();

          // See: https://github.com/netty/netty/issues/576
          if (!DetectionUtil.isWindows()
              && !DetectionUtil.isRoot()
              && Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST))
              && localAddress instanceof InetSocketAddress
              && !((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress()) {
            // Warn a user about the fact that a non-root user can't receive a
            // broadcast packet on *nix if the socket is bound on non-wildcard address.
            logger.warn(
                "A non-root user can't receive a broadcast packet if the socket "
                    + "is not bound to a wildcard address; binding to a non-wildcard "
                    + "address ("
                    + localAddress
                    + ") anyway as requested.");
          }

          doBind(localAddress);
          future.setSuccess();
          if (!wasActive && isActive()) {
            pipeline.fireChannelActive();
          }
        } catch (Throwable t) {
          future.setFailure(t);
          closeIfClosed();
        }
      } else {
        eventLoop()
            .execute(
                new Runnable() {
                  @Override
                  public void run() {
                    bind(localAddress, future);
                  }
                });
      }
    }
  @Override
  public void setBroadcast(boolean broadcast) {
    try {
      // See: https://github.com/netty/netty/issues/576
      if (broadcast
          && !DetectionUtil.isWindows()
          && !DetectionUtil.isRoot()
          && !socket.getLocalAddress().isAnyLocalAddress()) {
        // Warn a user about the fact that a non-root user can't receive a
        // broadcast packet on *nix if the socket is bound on non-wildcard address.
        logger.warn(
            "A non-root user can't receive a broadcast packet if the socket "
                + "is not bound to a wildcard address; setting the SO_BROADCAST flag "
                + "anyway as requested on the socket which is bound to "
                + socket.getLocalSocketAddress()
                + '.');
      }

      socket.setBroadcast(broadcast);
    } catch (SocketException e) {
      throw new ChannelException(e);
    }
  }