public static void newHttpServerBootstrap(
      String ip, int port, ChannelInitializer<SocketChannel> channelInitializer) {
    // Configure the server.
    int numCPUs = Runtime.getRuntime().availableProcessors();
    EventLoopGroup bossGroup = new NioEventLoopGroup(numCPUs);
    EventLoopGroup workerGroup = new NioEventLoopGroup(numCPUs);
    try {
      // public service processor
      ServerBootstrap publicServerBootstrap = new ServerBootstrap();
      publicServerBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);
      publicServerBootstrap
          .childOption(ChannelOption.TCP_NODELAY, false)
          .childOption(ChannelOption.SO_KEEPALIVE, false)
          .childHandler(channelInitializer);

      // bind to public access host info
      Channel ch1;
      if ("*".equals(ip)) {
        ch1 = publicServerBootstrap.bind(port).sync().channel();
      } else {
        ch1 = publicServerBootstrap.bind(ip, port).sync().channel();
      }
      System.out.println(String.format("Started OK HttpServer at %s:%d", ip, port));
      ch1.config().setConnectTimeoutMillis(1800);
      ch1.closeFuture().sync();
      System.out.println("Shutdown...");
    } catch (Throwable e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      bossGroup.shutdownGracefully();
      workerGroup.shutdownGracefully();
    }
  }
    @Override
    public void dispose() {
      if (ch.eventLoop().inEventLoop()) {
        dereference();
      } else {
        ch.eventLoop().execute(this::dereference);
      }

      ch.config().setAutoRead(false);
    }
  private void a(final Packet var1, final GenericFutureListener[] var2) {
    final EnumProtocol var3 = EnumProtocol.a(var1);
    final EnumProtocol var4 = (EnumProtocol) k.attr(c).get();
    if (var4 != var3) {
      g.debug("Disabled auto read");
      k.config().setAutoRead(false);
    }

    if (k.eventLoop().inEventLoop()) {
      if (var3 != var4) {
        this.a(var3);
      }

      ChannelFuture var5 = k.writeAndFlush(var1);
      if (var2 != null) {
        var5.addListeners(var2);
      }

      var5.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    } else {
      k.eventLoop()
          .execute(
              new Runnable() {
                @Override
                public void run() {
                  if (var3 != var4) {
                    class_ek.this.a(var3);
                  }

                  ChannelFuture var1x = k.writeAndFlush(var1);
                  if (var2 != null) {
                    var1x.addListeners(var2);
                  }

                  var1x.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
                }
              });
    }
  }
 public void a(EnumProtocol var1) {
   k.attr(c).set(var1);
   k.config().setAutoRead(true);
   g.debug("Enabled auto read");
 }
 public void k() {
   k.config().setAutoRead(false);
 }
  protected void createSession(
      Long sessionId,
      Channel channel,
      SmppSessionConfiguration config,
      BaseBindResp preparedBindResponse)
      throws SmppProcessingException {
    // NOTE: exactly one PDU (bind request) was read from the channel, we
    // now need to delegate permitting this bind request by calling a method
    // further upstream.  Only after the server-side is completely ready to
    // start processing requests from this session, do we want to actually
    // return the bind response and start reading further requests -- we'll
    // initially block reading from the channel first -- this will be turned
    // back on via the "serverReady()" method call on the session object

    // make sure the channel is not being read/processed (until we flag we're ready later on)
    channel.config().setAutoRead(false);

    // auto negotiate the interface version in use based on the requested interface version
    byte interfaceVersion = this.autoNegotiateInterfaceVersion(config.getInterfaceVersion());

    // create a new server session associated with this server
    DefaultSmppSession session =
        new DefaultSmppSession(
            SmppSession.Type.SERVER,
            config,
            channel,
            this,
            sessionId,
            preparedBindResponse,
            interfaceVersion,
            monitorExecutor);

    // replace name of thread used for renaming
    SmppSessionThreadRenamer threadRenamer =
        (SmppSessionThreadRenamer)
            channel.pipeline().get(SmppChannelConstants.PIPELINE_SESSION_THREAD_RENAMER_NAME);
    threadRenamer.setThreadName(config.getName());

    // add a logging handler after the thread renamer
    SmppSessionLogger loggingHandler =
        new SmppSessionLogger(
            DefaultSmppSession.class.getCanonicalName(), config.getLoggingOptions());
    channel
        .pipeline()
        .addAfter(
            SmppChannelConstants.PIPELINE_SESSION_THREAD_RENAMER_NAME,
            SmppChannelConstants.PIPELINE_SESSION_LOGGER_NAME,
            loggingHandler);

    // decoder in pipeline is ok (keep it)

    // create a new wrapper around a session to pass the pdu up the chain
    channel.pipeline().remove(SmppChannelConstants.PIPELINE_SESSION_WRAPPER_NAME);
    channel
        .pipeline()
        .addLast(
            SmppChannelConstants.PIPELINE_SESSION_WRAPPER_NAME, new SmppSessionWrapper(session));

    // check if the # of channels exceeds maxConnections
    if (this.channels.size() > this.configuration.getMaxConnectionSize()) {
      logger.warn(
          "The current connection size [{}] exceeds the configured max connection size [{}]",
          this.channels.size(),
          this.configuration.getMaxConnectionSize());
    }

    // session created, now pass it upstream
    counters.incrementSessionCreatedAndGet();
    incrementSessionSizeCounters(session);
    this.serverHandler.sessionCreated(sessionId, session, preparedBindResponse);

    // register this session as an mbean
    if (configuration.isJmxEnabled()) {
      session.registerMBean(
          configuration.getJmxDomain()
              + ":type="
              + configuration.getName()
              + "Sessions,name="
              + sessionId);
    }
  }
示例#7
0
 /** Resumes the underlying packet reader. */
 private void resume() {
   mChannel.config().setAutoRead(true);
   mChannel.read();
 }
示例#8
0
 /** Pauses the underlying packet reader. */
 private void pause() {
   mChannel.config().setAutoRead(false);
 }
    boolean drain() {
      if (wip++ != 0) {
        return false;
      }

      int missed = 1;

      for (; ; ) {
        final Queue<Object> q = queue;
        final Subscriber<? super Object> a = actual;

        if (a == null) {
          return false;
        }

        long r = requested;
        long e = 0L;

        while (e != r) {
          if (isCancelled()) {
            return false;
          }

          boolean d = done;
          Object v = q != null ? q.poll() : null;
          boolean empty = v == null;

          if (d && empty) {
            cancelResource();
            if (q != null) {
              q.clear();
            }
            Throwable ex = error;
            if (ex != null) {
              a.onError(ex);
            } else {
              a.onComplete();
            }
            return false;
          }

          if (empty) {
            ch.read();
            break;
          }

          try {
            a.onNext(v);
          } finally {
            ReferenceCountUtil.release(v);
            ch.read();
          }

          e++;
        }

        if (e == r) {
          if (isCancelled()) {
            return false;
          }

          if (done && (q == null || q.isEmpty())) {
            cancelResource();
            if (q != null) {
              q.clear();
            }
            Throwable ex = error;
            if (ex != null) {
              a.onError(ex);
            } else {
              a.onComplete();
            }
            return false;
          }
        }

        if (e != 0L) {
          if (r != Long.MAX_VALUE) {
            if ((requested -= e) > 0L) {
              ch.read();
            }
          }
        }

        missed = (wip = wip - missed);
        if (missed == 0) {
          if (r == Long.MAX_VALUE) {
            ch.config().setAutoRead(true);
            ch.read();
            return true;
          }
          return false;
        }
      }
    }