org.jboss.netty.channel.Channels类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.4k)|赞(0)|评价(0)|浏览(242)

本文整理了Java中org.jboss.netty.channel.Channels类的一些代码示例,展示了Channels类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Channels类的具体详情如下:
包路径:org.jboss.netty.channel.Channels
类名称:Channels

Channels介绍

[英]A helper class which provides various convenience methods related with Channel, ChannelHandler, and ChannelPipeline.

Factory methods

It is always recommended to use the factory methods provided by Channels rather than calling the constructor of the implementation types.

  • #pipeline()
  • #pipeline(ChannelPipeline)
  • #pipelineFactory(ChannelPipeline)
  • #succeededFuture(Channel)
  • #failedFuture(Channel,Throwable)

Upstream and downstream event generation

Various event generation methods are provided to simplify the generation of upstream events and downstream events. It is always recommended to use the event generation methods provided by Channels rather than calling ChannelHandlerContext#sendUpstream(ChannelEvent) or ChannelHandlerContext#sendDownstream(ChannelEvent) by yourself.
[中]一个帮助器类,提供与通道、通道处理程序和通道管道相关的各种方便方法。
####工厂方法
始终建议使用通道提供的工厂方法,而不是调用实现类型的构造函数。
*#管道()
*#管道(渠道管道)
*#管道工厂(管道)
*#成功的未来(频道)
*#失败的未来(频道,可丢弃)
####上游和下游事件生成
提供了各种事件生成方法,以简化上游事件和下游事件的生成。始终建议使用通道提供的事件生成方法,而不是自己调用ChannelHandlerContext#sendUpstream(ChannelEvent)或ChannelHandlerContext#sendUpstream(ChannelEvent)。

代码示例

代码示例来源:origin: menacher/java-game-server

static ChannelPipeline init()
{
  ChannelPipeline pipeline = Channels.pipeline();
  pipeline.addLast("eventDecoder", EVENT_DECODER);
  pipeline.addLast("eventEncoder", EVENT_ENCODER);
  pipeline.addLast("UDPUpstreamHandler",UDP_UPSTREAM_HANDLER);
  return pipeline;
}

代码示例来源:origin: io.netty/netty

private List<MessageEvent> consolidatedWrite(final List<MessageEvent> pendingWrites) {
  final int size = pendingWrites.size();
  if (size == 1) {
    ctx.sendDownstream(pendingWrites.remove(0));
    return pendingWrites;
    data[i] = (ChannelBuffer) pendingWrites.get(i).getMessage();
  ChannelFuture future = Channels.future(ctx.getChannel());
  future.addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future)
        throws Exception {
  Channels.write(ctx, future, composite);
  return null;

代码示例来源:origin: io.netty/netty

public void handleUpstream(
    ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
  if (!(evt instanceof MessageEvent)) {
    ctx.sendUpstream(evt);
    return;
  }
  MessageEvent e = (MessageEvent) evt;
  Object originalMessage = e.getMessage();
  Object decodedMessage = decode(ctx, e.getChannel(), originalMessage);
  if (originalMessage == decodedMessage) {
    ctx.sendUpstream(evt);
  } else if (decodedMessage != null) {
    fireMessageReceived(ctx, decodedMessage, e.getRemoteAddress());
  }
}

代码示例来源:origin: io.netty/netty

private synchronized ChannelFuture sendGoAwayFrame(
    ChannelHandlerContext ctx, Channel channel, SocketAddress remoteAddress, SpdySessionStatus status) {
  if (!sentGoAwayFrame) {
    sentGoAwayFrame = true;
    SpdyGoAwayFrame spdyGoAwayFrame = new DefaultSpdyGoAwayFrame(lastGoodStreamId, status);
    ChannelFuture future = Channels.future(channel);
    Channels.write(ctx, future, spdyGoAwayFrame, remoteAddress);
    return future;
  }
  return Channels.succeededFuture(channel);
}

代码示例来源:origin: io.netty/netty

public void operationComplete(ChannelFuture future) throws Exception {
    if (!future.isSuccess()) {
      Throwable cause = future.getCause();
      hsFuture.setFailure(cause);
      fireExceptionCaught(ctx, cause);
      if (closeOnSslException) {
        Channels.close(ctx, future(channel));
      }
    }
  }
});

代码示例来源:origin: io.netty/netty

public void readHeaderBlockEnd() {
  Object frame = null;
  try {
    spdyHeaderBlockDecoder.endHeaderBlock(spdyHeadersFrame);
    frame = spdyHeadersFrame;
    spdyHeadersFrame = null;
  } catch (Exception e) {
    Channels.fireExceptionCaught(ctx, e);
  }
  if (frame != null) {
    Channels.fireMessageReceived(ctx, frame);
  }
}

代码示例来源:origin: io.netty/netty

while (channel.isWritable()) {
  if (currentEvent == null) {
    currentEvent = queue.poll();
  if (currentEvent.getFuture().isDone()) {
        this.currentEvent = null;
        currentEvent.getFuture().setFailure(t);
        if (fireNow) {
          fireExceptionCaught(ctx, t);
        } else {
          fireExceptionCaughtLater(ctx, t);
          writeFuture.addListener(new ChannelFutureListener() {
          writeFuture = future(channel);
        write(
      ctx.sendDownstream(currentEvent);
  if (!channel.isConnected()) {
    discard(ctx, fireNow);
    return;

代码示例来源:origin: io.netty/netty

try {
      for (;;) {
        MessageEvent e = writeBuffer.poll();
        if (e == null) {
          break;
        fireMessageReceived(pairedChannel, e.getMessage());
        e.getFuture().setSuccess();
        fireWriteComplete(this, 1);
  cause = new NotYetConnectedException();
} else {
  cause = new ClosedChannelException();
  MessageEvent e = writeBuffer.poll();
  if (e == null) {
    break;
  e.getFuture().setFailure(cause);
  fireExceptionCaught(this, cause);

代码示例来源:origin: io.netty/netty

throws Exception {
Object msg = e.getMessage();
if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().getCode() == 100) {
  ctx.sendDownstream(e);
} else  if (msg instanceof HttpMessage) {
  HttpMessage m = (HttpMessage) msg;
  String acceptEncoding = acceptEncodingQueue.poll();
  if (acceptEncoding == null) {
    throw new IllegalStateException("cannot send more responses than requests");
    !HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) {
    ctx.sendDownstream(e);
  } else {
    ctx.sendDownstream(e);
        Channels.write(
            ctx, Channels.succeededFuture(e.getChannel()),
            new DefaultHttpChunk(lastProduct), e.getRemoteAddress());

代码示例来源:origin: io.netty/netty

final ChannelPipeline pipeline = Channels.pipeline();
final ServletOutputStream out = res.getOutputStream();
final OutboundConnectionHandler handler = new OutboundConnectionHandler(out);
pipeline.addLast("handler", handler);
  future = channel.connect(remoteAddress).awaitUninterruptibly();
  if (!future.isSuccess()) {
    tries++;
    try {
if (!future.isSuccess()) {
  if (logger.isWarnEnabled()) {
    Throwable cause = future.getCause();
  while (channel.isConnected()) {
    ChannelBuffer buffer;
    try {
      break;
    lastWriteFuture = channel.write(buffer);

代码示例来源:origin: itisaid/Doris

private void writeCommand(ChannelHandlerContext ctx, MessageEvent evt, Command<?> command) throws Exception {
  Object encodedMessage = encode(ctx, evt.getChannel(), command);
  if (command == encodedMessage) {
    ctx.sendDownstream(evt);
  } else if (encodedMessage != null) {
    try {
      boolean closeChannel = false;
      ChannelFuture feture = evt.getFuture();
      write(ctx, feture, encodedMessage, evt.getRemoteAddress());
      if (feture.await(60000)) {
        if (feture.isSuccess()) {
          writeFailureCount = 0;
          commandQueue.offer(command);
          if (logger.isDebugEnabled()) {
            logger.debug("Send command:" + command + " to " + ctx.getChannel().getRemoteAddress());
        Channel channel = ctx.getChannel();
        if (channel.isOpen()) {
          logger.error("Couldn't sent command to:" + channel + "; close channel;");
          Channels.close(channel).awaitUninterruptibly(1000);

代码示例来源:origin: io.netty/netty

public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent evt)
    throws Exception {
  if (!(evt instanceof MessageEvent)) {
    ctx.sendDownstream(evt);
    return;
  Object msg = e.getMessage();
    currentStreamId = spdySynStreamFrame.getStreamId();
    ChannelFuture future = getMessageFuture(ctx, e, currentStreamId, httpRequest);
    Channels.write(ctx, future, spdySynStreamFrame, e.getRemoteAddress());
      currentStreamId = spdySynStreamFrame.getStreamId();
      ChannelFuture future = getMessageFuture(ctx, e, currentStreamId, httpResponse);
      Channels.write(ctx, future, spdySynStreamFrame, e.getRemoteAddress());
    } else {
      SpdySynReplyFrame spdySynReplyFrame = createSynReplyFrame(httpResponse);
      currentStreamId = spdySynReplyFrame.getStreamId();
      ChannelFuture future = getMessageFuture(ctx, e, currentStreamId, httpResponse);
      Channels.write(ctx, future, spdySynReplyFrame, e.getRemoteAddress());
  } else {
    ctx.sendDownstream(evt);

代码示例来源:origin: io.netty/netty

private ChannelFuture finishEncode(final ChannelHandlerContext ctx, final ChannelEvent evt) {
  if (!finished.compareAndSet(false, true)) {
    if (evt != null) {
      ctx.sendDownstream(evt);
    return Channels.succeededFuture(ctx.getChannel());
        future = Channels.failedFuture(
            ctx.getChannel(),
            ZlibUtil.exception(z, "compression failure", resultCode));
        footer = null;
      } else if (z.next_out_index != 0) {
        future = Channels.future(ctx.getChannel());
        footer =
          ctx.getChannel().getConfig().getBufferFactory().getBuffer(
              out, 0, z.next_out_index);
      } else {
        future = Channels.future(ctx.getChannel());
        footer = ChannelBuffers.EMPTY_BUFFER;
    Channels.write(ctx, future, footer);
    future.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) throws Exception {
        ctx.sendDownstream(evt);

代码示例来源:origin: okleine/nCoAP

private void sendRequest() {
    ChannelFuture future = Channels.future(getContext().getChannel());
    Channels.write(getContext(), future, coapMessage, this.remoteSocket);
    future.addListener(new ChannelFutureListener() {
      @Override
      public void operationComplete(ChannelFuture future) throws Exception {
        if (!future.isSuccess()) {
          ClientCallback callback = removeCallback(remoteSocket, coapMessage.getToken());
          log.error("Could not write CoAP Request!", future.getCause());
          if (callback != null) {
            callback.processMiscellaneousError("Message could not be sent (Reason: \"" +
                future.getCause().getMessage() + ")\"");
          }
        }
      }
    });
  }
}

代码示例来源:origin: io.netty/netty

ctx.sendDownstream(evt);
  return;
Object msg = e.getMessage();
    e.getFuture().setFailure(PROTOCOL_EXCEPTION);
    return;
      ChannelFuture writeFuture = Channels.future(e.getChannel());
      final SocketAddress remoteAddress = e.getRemoteAddress();
      final ChannelHandlerContext context = ctx;
      e.getFuture().addListener(new ChannelFutureListener() {
        public void operationComplete(ChannelFuture future) throws Exception {
          if (!future.isSuccess()) {
      Channels.write(ctx, writeFuture, partialDataFrame, remoteAddress);
      return;
    } else {
      final SocketAddress remoteAddress = e.getRemoteAddress();
      final ChannelHandlerContext context = ctx;
      e.getFuture().addListener(new ChannelFutureListener() {
        public void operationComplete(ChannelFuture future) throws Exception {
          if (!future.isSuccess()) {
ctx.sendDownstream(evt);

代码示例来源:origin: apache/incubator-druid

final ChannelPipeline pipeline = connectFuture.getChannel().getPipeline();
pipeline.addFirst("ssl", sslHandler);
final ChannelFuture handshakeFuture = Channels.future(connectFuture.getChannel());
pipeline.addLast("connectionErrorHandler", new SimpleChannelUpstreamHandler()
connectFuture.addListener(
  new ChannelFutureListener()

代码示例来源:origin: com.lambdaworks/lettuce

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
  Command<?, ?, ?> cmd = (Command<?, ?, ?>) e.getMessage();
  Channel channel = ctx.getChannel();
  ChannelBuffer buf = ChannelBuffers.dynamicBuffer(channel.getConfig().getBufferFactory());
  cmd.encode(buf);
  Channels.write(ctx, e.getFuture(), buf);
}

代码示例来源:origin: io.netty/netty

SpdyDataFrame spdyDataFrame = (SpdyDataFrame) e.getMessage();
int dataFrameSize = spdyDataFrame.getData().readableBytes();
int writeStreamId = spdyDataFrame.getStreamId();
  final SocketAddress remoteAddress = e.getRemoteAddress();
  final ChannelHandlerContext context = ctx;
  e.getFuture().addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future) throws Exception {
      if (!future.isSuccess()) {
  Channels.write(ctx, e.getFuture(), spdyDataFrame, e.getRemoteAddress());
} else {
  partialDataFrame.setData(spdyDataFrame.getData().readSlice(newWindowSize));
  ChannelFuture writeFuture = Channels.future(e.getChannel());
  final SocketAddress remoteAddress = e.getRemoteAddress();
  final ChannelHandlerContext context = ctx;
  e.getFuture().addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future) throws Exception {
      if (!future.isSuccess()) {
  Channels.write(ctx, writeFuture, partialDataFrame, remoteAddress);

代码示例来源:origin: io.netty/netty

throws Exception {
Object msg = e.getMessage();
HttpMessage currentMessage = this.currentMessage;
    write(ctx, succeededFuture(ctx.getChannel()), CONTINUE.duplicate());
    ctx.sendUpstream(e);
    fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
  ctx.sendUpstream(e);

代码示例来源:origin: cgbystrom/sockjs-netty

@Override
  public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
      if (headerSent.compareAndSet(false, true)) {
        HttpResponse response = createResponse(CONTENT_TYPE_JAVASCRIPT);
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), Channels.future(e.getChannel()), response, e.getRemoteAddress()));

        // IE requires 2KB prefix:
        // http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhttprequest-and-xdomainrequest.aspx
        DefaultHttpChunk message = new DefaultHttpChunk(Frame.encode(Frame.preludeFrame(), true));
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), Channels.future(e.getChannel()), message, e.getRemoteAddress()));
      }
      final Frame frame = (Frame) e.getMessage();
      ChannelBuffer content = Frame.encode(frame, true);
      
      if (frame instanceof Frame.CloseFrame) {
        e.getFuture().addListener(ChannelFutureListener.CLOSE);
      }

      ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(content), e.getRemoteAddress()));
      logResponseSize(e.getChannel(), content);
    } else {
      super.writeRequested(ctx, e);
    }
  }
}

相关文章